简体   繁体   English

Unity Shader RGBA 颜色遮罩

[英]Unity Shader RGBA Color mask

I am working on a shader for Unity in which I want to change the color based on an mask image.我正在为 Unity 开发一个着色器,我想在其中根据蒙版图像更改颜色。 In this mask image the RGB channels stand for a color that can be choosen in the shader.在此蒙版图像中,RGB 通道代表可以在着色器中选择的颜色。 The idea behind the shader is that it is easy to change the look of an object without having to change the texture by hand.着色器背后的想法是,无需手动更改纹理即可轻松更改对象的外观。

Shader "Custom/MultiColor" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _MaskTex ("Mask area (RGB)", 2D) = "black" {}
        _ColorR ("Red Color", Color) = (1,1,1,1)
        _ColorG ("Green Color", Color) = (1,1,1,1)
        _ColorB ("Blue Color", Color) = (1,1,1,1)
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Lambert

        sampler2D _MainTex;
        sampler2D _MaskTex;
        half4 _ColorR;
        half4 _ColorG;
        half4 _ColorB;
        half4 _MaskMult;

        struct Input {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o) {
            half4 main = tex2D (_MainTex, IN.uv_MainTex);
            half4 mask = tex2D (_MaskTex, IN.uv_MainTex);

            half3 cr = main.rgb * _ColorR;
            half3 cg = main.rgb * _ColorG;
            half3 cb = main.rgb * _ColorB;

            half r = mask.r;
            half g = mask.g;
            half b = mask.b;

            half minv = min(r + g + b, 1);

            half3 cf = lerp(lerp(cr, cg, g*(r+g)), cb, b*(r+g+b));
            half3 c = lerp(main.rgb, cf, minv);

            o.Albedo = c.rgb;
            o.Alpha = main.a;
        }
        ENDCG
    } 
    FallBack "Diffuse"
}

The problem with the shader is the blending between the masked color based on the green and blue channel.着色器的问题是基于绿色和蓝色通道的蒙版颜色之间的混合。 Between colors defined in the color supposted to be from the red region is visible.在假定来自红色区域的颜色中定义的颜色之间是可见的。 Below a sample is visable.下面的样本是可见的。在此处输入图像描述

The red color is create by the red mask, green by the green mask and desert yellow by the blue region.红色由红色蒙版创建,绿色由绿色蒙版创建,沙漠黄色由蓝色区域创建。 I do not know why this happens or how to solve this problem.我不知道为什么会发生这种情况或如何解决这个问题。

Best guess: anti-aliasing or image compression.最佳猜测:抗锯齿图像压缩。 Aliasing (on the brush your using) will cause an overlap in the color channels, causing them to mix.锯齿(在您使用的画笔上)将导致颜色通道重叠,导致它们混合。 Compression usually works by averaging each pixel's color info based on the colors around it (jpeg is especially notorious for this).压缩通常通过根据每个像素周围的颜色平均每个像素的颜色信息来工作(jpeg 在这方面尤其臭名昭著)。

Troubleshoot by using a straight pixel based brush (no aliasing, no rounded edges) in Photoshop (or whatever image suite you're using) and/or try changing the colors through your shader and see how they mix- doing either should give you a better idea of what's going on under the hood.通过在 Photoshop(或您正在使用的任何图像套件)中使用基于直像素的画笔(无锯齿,无圆边)和/或尝试通过着色器更改颜色并查看它们如何混合 - 进行故障排除更好地了解引擎盖下发生的事情。 This combined with an lossless/uncompressed image-type, such as .tga should help, though they may use more memory.这与无损/未压缩的图像类型相结合,例如 .tga 应该会有所帮助,尽管它们可能会使用更多内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM