Search Unity

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

Discussion in 'Shaders' started by wxxhrt, Aug 16, 2017.

  1. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    139
    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..

    Code (CSharp):
    1. Shader "Custom/VertexLitAlphaColoring" {
    2.     Properties {
    3.         _Blend ("Blend", Range (0, 1) ) = 0.0
    4.         _Color1 ("Color 1", Color) = (1, 1, 1, 1)
    5.         _AlphaColor ("Alpha Color", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 80
    12.            
    13.         Pass {
    14.             Tags { "LightMode" = "Vertex" }
    15.                
    16.             // Setup Basic
    17.             Material {
    18.                 Diffuse (1,1,1,1)
    19.                 Ambient (1,1,1,1)
    20.             }
    21.             Lighting On
    22.  
    23.             // Lerp between AlphaColor and the basic vertex lighting color
    24.             SetTexture [_MainTex] {
    25.                 constantColor [_AlphaColor]
    26.                     combine previous lerp(texture) constant DOUBLE, previous lerp(texture) constant
    27.             }
    28.             // Multiply in texture
    29.                     SetTexture [_Color1] {
    30.                         ConstantColor (0,0,0, [_Blend])
    31.                         combine texture Lerp(constant) previous
    32.                     }
    33.         }
    34.            
    35.     }
    36. }
     

    Attached Files:

  2. FlaxenFlash

    FlaxenFlash

    Joined:
    Oct 15, 2015
    Posts:
    27
    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
     

    Attached Files:

  3. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    139
    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.


    Code (CSharp):
    1. Shader "Custom/BaseColour" {
    2.     Properties {
    3.         _Blend ("Blend", Range (0, 1) ) = 0.0
    4.         _BaseColor ("Color", Color) = (1,1,1,1)
    5.         _Color ("Tint", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 200
    13.        
    14.         CGPROGRAM
    15.         // Physically based Standard lighting model, and enable shadows on all light types
    16.         #pragma surface surf Standard fullforwardshadows
    17.  
    18.         // Use shader model 3.0 target, to get nicer looking lighting
    19.         #pragma target 3.0
    20.  
    21.         sampler2D _MainTex;
    22.  
    23.         struct Input {
    24.             float2 uv_MainTex;
    25.         };
    26.  
    27.         half _Glossiness;
    28.         half _Metallic;
    29.         half _Blend;
    30.         fixed4 _Color, _BaseColor;
    31.  
    32.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    33.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    34.         // #pragma instancing_options assumeuniformscaling
    35.         UNITY_INSTANCING_CBUFFER_START(Props)
    36.             // put more per-instance properties here
    37.         UNITY_INSTANCING_CBUFFER_END
    38.  
    39.         void surf (Input IN, inout SurfaceOutputStandard o) {
    40.             // Albedo comes from a texture tinted by color
    41.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color * _Blend;
    42.             o.Albedo = lerp(_BaseColor, c.rgb, c.a);
    43.             // Metallic and smoothness come from slider variables
    44.             o.Metallic = _Metallic;
    45.             o.Smoothness = _Glossiness;
    46.             o.Alpha = 1;
    47.         }
    48.         ENDCG
    49.     }
    50.     FallBack "Diffuse"
    51. }
    52.  
     
  4. FlaxenFlash

    FlaxenFlash

    Joined:
    Oct 15, 2015
    Posts:
    27
    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