Shader with changeable base colour, and fadeable alpha texture on top

I’m trying to figure out a shader that will allow me to choose a colour as the base colour of a material and draw a fade-able texture on top of this base, the texture will have an alpha channel so the base colour will show through the alpha parts. Most things I find online refer to fading the object in and out which I’m not looking to do.

Here’s what Ive got so far from kludging scripts together. It’s a mess as I’m well out of my comfort zone…

Shader "Custom/VertexLitAlphaColoring" {
    Properties {
        _Blend ("Blend", Range (0, 1) ) = 0.0
        _Color1 ("Color 1", Color) = (1, 1, 1, 1)
        _AlphaColor ("Alpha Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }

    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 80
           
        Pass {
            Tags { "LightMode" = "Vertex" }
               
            // Setup Basic
            Material {
                Diffuse (1,1,1,1)
                Ambient (1,1,1,1)
            }
            Lighting On

            // Lerp between AlphaColor and the basic vertex lighting color
            SetTexture [_MainTex] {
                constantColor [_AlphaColor]
                    combine previous lerp(texture) constant DOUBLE, previous lerp(texture) constant
            }
            // Multiply in texture
                    SetTexture [_Color1] {
                        ConstantColor (0,0,0, [_Blend])
                        combine texture Lerp(constant) previous
                    }
        }
           
    }
}

I made a quick surface shader and added a lerp to the albedo between a base colour and the texture colour based on the texture alpha, is that what you are looking for? Hopefully that will point you in the right direction regardless. First steps in writing shaders are painful

3185749–243112–BaseColour.shader (1.42 KB)

1 Like

Thanks for taking the time @FlaxenFlash - much appreciated!

o.Albedo = lerp(_BaseColor, c.rgb, c.a);

I take it this line is doing what you describe above?

I think I’ve figured out how to fade the texture in… Adding a float input called _Blend and multiplying the following line by it. Is this the most efficient way to go about things?

fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color * _Blend;

Here’s the amended shader in case anyone stumbles on this.

Shader "Custom/BaseColour" {
    Properties {
        _Blend ("Blend", Range (0, 1) ) = 0.0
        _BaseColor ("Color", Color) = (1,1,1,1)
        _Color ("Tint", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        LOD 200
       
        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        half _Blend;
        fixed4 _Color, _BaseColor;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_CBUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_CBUFFER_END

        void surf (Input IN, inout SurfaceOutputStandard o) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color * _Blend;
            o.Albedo = lerp(_BaseColor, c.rgb, c.a);
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = 1;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

Yeah, that is the line doing all the work. The other other thing I changed was setting o.Alpha to 1 and adding the _BaseColor.

I forgot that you wanted to be able to fade it in and out. I feel like that might result in slightly strange blending behaviour but if that works fine then go for it. I probably would have tried:
o.Albedo = lerp(_BaseColor, c.rgb, c.a * _Blend);

The difference is subtle, your one is multiplying the colour as well as the alpha by the blend so it gets darker as well as fading. It probably isn’t too noticable here but it might be if your base colour and texture colour were both bright