Search Unity

NormalSplatMap

Discussion in 'Shaders' started by phedon, Dec 12, 2014.

  1. phedon

    phedon

    Joined:
    Dec 7, 2013
    Posts:
    10
    I am new to shaders and im trying to make a splat map shader that it take also normals.I made the shader with no problems except that there is a lot of difference when i put in into an object from when i put it into a terrain.In terrain seem completely wrong.Does anyone have any idea what may happening?I think maybe is the tilling,or the size of the textures. Sorry if there is an other thread for that,but making shaders is a little complicated,and there are very few clear tutorials.

    I am posting you the scenes and the shader code

    Code (CSharp):
    1. SubShader {
    2.      
    3.      
    4.         CGPROGRAM
    5.         #pragma surface surf Lambert
    6.  
    7.         float4 _MainTint;
    8.      
    9.         float4 _ColorA;
    10.         float4 _ColorB;
    11.         sampler2D _RTexture;
    12.         sampler2D _GTexture;
    13.         sampler2D _BTexture;
    14.         sampler2D _BlendTex;
    15.         sampler2D _ATexture;
    16.      
    17.         sampler2D _NormalTexR;
    18.         sampler2D _NormalTexG;
    19.         sampler2D _NormalTexB;
    20.         sampler2D _NormalTexA;
    21.      
    22.         float _AlphaSlider;
    23.      
    24.      
    25.         struct Input {
    26.      
    27.             float2 uv_RTexture;
    28.             float2 uv_GTexture;
    29.             float2 uv_BTexture;
    30.             float2 uv_ATexture;
    31.             float2 uv_BlendTex;
    32.          
    33.             float2 uv_NormalTexR;
    34.             float2 uv_NormalTexG;
    35.             float2 uv_NormalTexB;
    36.             float2 uv_NormalTexA;
    37.          
    38.             float _AlphaSlider;
    39.          
    40.          
    41.         };
    42.  
    43.         void surf (Input IN, inout SurfaceOutput o)
    44.         {
    45.      
    46.             float4 blendData = tex2D (_BlendTex, IN.uv_BlendTex);
    47.          
    48.             float4 rTexData = tex2D(_RTexture, IN.uv_RTexture);
    49.             float4 gTexData = tex2D(_GTexture, IN.uv_GTexture);
    50.             float4 bTexData = tex2D(_BTexture, IN.uv_BTexture);
    51.             float4 aTexData = tex2D(_ATexture, IN.uv_ATexture);
    52.          
    53.             float4 finalColor;
    54.          
    55.             finalColor = lerp(rTexData, gTexData, blendData.g);
    56.             finalColor = lerp(finalColor, bTexData, blendData.b);
    57.             finalColor = lerp(finalColor, aTexData, blendData.a);
    58.             finalColor.a = _AlphaSlider;
    59.          
    60.             float4 terrainLayers = lerp(_ColorA, _ColorB, blendData.r);
    61.          
    62.             finalColor *= terrainLayers;
    63.          
    64.             finalColor = saturate(finalColor);
    65.          
    66.             float3 normalMap0 = UnpackNormal (tex2D(_NormalTexR, IN.uv_RTexture)).r;
    67.             float3 normalMap01 = UnpackNormal (tex2D(_NormalTexG, IN.uv_GTexture)).g;
    68.             float3 normalMap02 = UnpackNormal (tex2D(_NormalTexB, IN.uv_BTexture)).b;
    69.             float3 normalMap03 = UnpackNormal (tex2D(_NormalTexA, IN.uv_ATexture));
    70.          
    71.             float3 finalNormals = normalMap0 * finalColor.r;
    72.                    finalNormals += normalMap01 * finalColor.g;
    73.                    finalNormals += normalMap02 * finalColor.b;
    74.                    finalNormals += normalMap03 * finalColor.a;
    75.          
    76.             //finalNormals = normalize(finalNormals);
    77.             o.Normal = finalNormals.rgb;
    78.             o.Albedo = finalColor.rgb * _MainTint.rgb ;
    79.             o.Alpha = finalColor.a;
    80.         }
    81.         ENDCG
    82.     }
    83.     FallBack "Diffuse"
    84. }
    85.  
    TerrainSplatNormal.jpg CapsuleSplatNormal.jpg
     
    Last edited: Dec 12, 2014