Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Overlaying a texture over another one but with 2 materials

Discussion in 'Shaders' started by _BlenMiner_, Jan 21, 2016.

  1. _BlenMiner_

    _BlenMiner_

    Joined:
    Jun 10, 2015
    Posts:
    65
    So I got a map that used UV2 for lightmaps. I tried doing something like this http://i.imgur.com/xNPKM9l.png where one of the shader has something like:

    "o.Albedo = c.rgb;"

    and the other shader something like:

    "o.Albedo *= c.rgb;"

    But when accessing o.Albedo it appears to always be a black color and not the previous set one..

    I am pretty new to shaders if u didn't notice, sarcasm
     
  2. shmomo

    shmomo

    Joined:
    Oct 11, 2011
    Posts:
    127
    Code (CSharp):
    1.  
    2.  
    3. Shader "Custom/Diffuse Layer"
    4. {
    5.     Properties
    6.     {
    7.         _Color ("Color", color) = (1.0, 1.0, 1.0, 1.0)
    8.         _Tex1("Tex1", 2D) = "white" {}
    9.         _Tex2("Tex2", 2D) = "white" {}
    10.         _Mix ("Mix", range(0.0, 1.0)) = 0.5
    11.     }
    12.  
    13.     SubShader
    14.     {
    15.         Tags { "RenderType"="Opaque" "LightMode" = "ForwardBase" }
    16.  
    17.         Pass
    18.         {
    19.             CGPROGRAM
    20.  
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.  
    24.             #include "UnityCG.cginc"
    25.  
    26.             uniform float4 _Color;
    27.             uniform sampler2D _Tex1;
    28.             uniform sampler2D _Tex2;
    29.             uniform float4 _LightColor0;
    30.             uniform float _Mix;
    31.  
    32.             struct vertIn
    33.             {
    34.                 float4 pos : POSITION;
    35.                 float4 texCoord : TEXCOORD0;
    36.                 float4 col : COLOR;
    37.                 float3 norm : NORMAL;
    38.             };
    39.  
    40.             struct vertOut
    41.             {
    42.               float4 pos : SV_POSITION;
    43.               float4 texCoord : TEXCOORD0;
    44.               float4 col : COLOR;
    45.             };
    46.  
    47.             vertOut vert(vertIn input)
    48.             {
    49.               vertOut output;
    50.  
    51.               output.pos = mul(UNITY_MATRIX_MVP, input.pos);
    52.  
    53.               output.texCoord = input.texCoord;
    54.  
    55.               float4 norm = float4(input.norm, 0.0);
    56.               float3 surfaceNormal = normalize(mul(norm, _World2Object));
    57.               float3 lightDirection = normalize(_WorldSpaceLightPos0);
    58.               float3 NdotL = max(0.0, dot(surfaceNormal, lightDirection));
    59.               float3 dotProduct = NdotL * _LightColor0 * _Color;
    60.               float3 ambientLight = UNITY_LIGHTMODEL_AMBIENT * _Color;
    61.               float4 col = float4(dotProduct + ambientLight, 1.0);
    62.               output.col = col;
    63.  
    64.               return output;
    65.             }
    66.  
    67.             struct fragOutput
    68.             {
    69.                 float4 col : COLOR;
    70.             };
    71.  
    72.             fragOutput frag(vertOut input)
    73.             {
    74.                 fragOutput output;
    75.  
    76.                 float4 col1 = tex2D(_Tex1, input.texCoord);
    77.                 float4 col2 = tex2D(_Tex2, input.texCoord);
    78.  
    79.                 output.col = input.col;
    80.  
    81.                 output.col *= lerp(col1, col2, _Mix);
    82.  
    83.                 return output;
    84.             }
    85.             ENDCG
    86.         }
    87.     }
    88. }
     
  3. _BlenMiner_

    _BlenMiner_

    Joined:
    Jun 10, 2015
    Posts:
    65
    Well that wont really work since both materials are supposed to work with different submeshes
     
  4. Flailer

    Flailer

    Joined:
    Apr 1, 2014
    Posts:
    66
    Then use one submesh and an alpha texture?
     
  5. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,864
    In your overlay materials shader, use a multiplicative blend mode.
    Read the shaderlab refference in the manual on how to do so.