Search Unity

Question How to make normal maps in a terrain shader?

Discussion in 'Shaders' started by LxWore, Sep 21, 2022.

  1. LxWore

    LxWore

    Joined:
    Sep 18, 2022
    Posts:
    1
    There are 4 textures in the shader that fill the terrain with certain colors, there are "R Channel", "G Channel", "B Channel", "A Channel". I want to make sure that each texture has its own normal, for example: R Channel Bump, B Channel Bump, and so on. How do I do this?
    Code (CSharp):
    1. Shader "X-Ray Plugin/Terrain/Simple terrain"
    2. {
    3.     Properties
    4.     {
    5.         _SplatMap01("Splat Map", 2D) = "black" {}
    6.         _ColorMap01("Color Map", 2D) = "white" {}
    7.         _TintColor01("Tint Color R", Color) = (1,1,1,1)
    8.         _Albedo01("R Channel", 2D) = "white" {}
    9.         _TintColor02("Tint Color G", Color) = (1,1,1,1)
    10.         _Albedo02("G Channel", 2D) = "white" {}
    11.         _TintColor03("Tint Color B", Color) = (1,1,1,1)
    12.         _Albedo03("B Channel", 2D) = "white" {}
    13.         _TintColor04("Tint Color A", Color) = (1,1,1,1)
    14.         _Albedo04("A Channel", 2D) = "white" {}
    15.         _Detail("Detail", 2D) = "white" {}
    16.         _LightMap("Lightmap", 2D) = "white" {}
    17.         _AoMap("Ambient Occlusion", 2D) = "white" {}
    18.         _Intensity("Blend", Range(0, 1)) = 0
    19.     }
    20.  
    21.         SubShader
    22.     {
    23.         CGPROGRAM
    24.         #pragma surface surf Lambert vertex:vert
    25.         #pragma target 3.0
    26.         #include "UnityCG.cginc"
    27.  
    28.         struct Input
    29.         {
    30.             float2 myUV;
    31.             float2 uv2_LightMap;
    32.         };
    33.  
    34.  
    35.         sampler2D _SplatMap01;
    36.         float4 _SplatMap01_ST;
    37.         sampler2D _ColorMap01;
    38.         float4 _ColorMap01_ST;
    39.         sampler2D _Albedo01, _Albedo02, _Albedo03, _Albedo04, _Detail, _Density, _LightMap, _AoMap;
    40.         float4 _Albedo01_ST, _Albedo02_ST, _Albedo03_ST, _Albedo04_ST, _Detail_ST, _LightMap_ST, _AoMap_ST;
    41.         fixed3 _TintColor01, _TintColor02, _TintColor03, _TintColor04;
    42.         float _Intensity;
    43.  
    44.         void vert(inout appdata_full v, out Input o)
    45.         {
    46.             UNITY_INITIALIZE_OUTPUT(Input, o);
    47.             o.myUV = v.texcoord;
    48.         }
    49.  
    50.         void surf(Input IN, inout SurfaceOutput o)
    51.         {
    52.             fixed4 splatMap01 = tex2D(_SplatMap01, TRANSFORM_TEX(IN.myUV, _SplatMap01)).rgba;
    53.             fixed4 colorMap01 = tex2D(_ColorMap01, TRANSFORM_TEX(IN.myUV, _ColorMap01)).rgba;
    54.             fixed4 lightmap01 = tex2D(_LightMap, TRANSFORM_TEX(IN.myUV, _LightMap)).rgba;
    55.             fixed4 ambient01 = tex2D(_AoMap, TRANSFORM_TEX(IN.myUV, _LightMap)).rgba;
    56.             fixed3 albedo01 = tex2D(_Albedo01, TRANSFORM_TEX(IN.myUV, _Albedo01)).rgb;
    57.             fixed3 albedo02 = tex2D(_Albedo02, TRANSFORM_TEX(IN.myUV, _Albedo02)).rgb;
    58.             fixed3 albedo03 = tex2D(_Albedo03, TRANSFORM_TEX(IN.myUV, _Albedo03)).rgb;
    59.             fixed3 albedo04 = tex2D(_Albedo04, TRANSFORM_TEX(IN.myUV, _Albedo04)).rgb;
    60.  
    61.             //fixed4 colormap = lerp(colorMap01, lightmap01, _Intensity);
    62.             /*fixed4 colormap = colorMap01 * lightmap01.a;*/
    63.             fixed4 colormap = (colorMap01 * lightmap01.a / _Intensity) + (colorMap01 * ambient01.a / _Intensity);
    64.  
    65.             o.Albedo = colormap.rgb*(splatMap01.r * albedo01.rgb * _TintColor01.rgb
    66.                 + splatMap01.g * albedo02.rgb * _TintColor02.rgb
    67.                 + splatMap01.b * albedo03.rgb * _TintColor03.rgb
    68.                 + splatMap01.a * albedo04.rgb * _TintColor04.rgb);
    69.  
    70.             o.Alpha = colorMap01.a;
    71.  
    72.         }
    73.  
    74.         ENDCG
    75.     }
    76.  
    77.         FallBack "Diffuse"
    78. }
    79.