Search Unity

Terrain multi UV Mixing with new unity 5 standard shader.

Discussion in 'Shaders' started by zRedCode, Mar 7, 2015.

  1. zRedCode

    zRedCode

    Joined:
    Nov 11, 2013
    Posts:
    131
    I founded this and i tryed to impleemnt it in the new unity 5 standard terrain shader. I looked around in the built-in shaders finding the TerrainSplatmapCommon.cginc, where i changed it as the topic said, so my actual TerrainSplatmapAdvanced.cginc (renamed it) is like:
    Code (CSharp):
    1. #ifndef TERRAIN_SPLATMAP_ADVANCED_CGINC_INCLUDED
    2. #define TERRAIN_SPLATMAP_ADVANCED_CGINC_INCLUDED
    3.  
    4. struct Input
    5. {
    6.     float2 uv_Splat0 : TEXCOORD0;
    7.     float2 uv_Splat1 : TEXCOORD1;
    8.     float2 uv_Splat2 : TEXCOORD2;
    9.     float2 uv_Splat3 : TEXCOORD3;
    10.     float2 tc_Control : TEXCOORD4;    // Not prefixing '_Contorl' with 'uv' allows a tighter packing of interpolators, which is necessary to support directional lightmap.
    11.     UNITY_FOG_COORDS(5)
    12. };
    13.  
    14. sampler2D _Control;
    15. float4 _Control_ST;
    16. sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    17.  
    18. #ifdef _TERRAIN_NORMAL_MAP
    19.     sampler2D _Normal0, _Normal1, _Normal2, _Normal3;
    20. #endif
    21.  
    22. void SplatmapVert(inout appdata_full v, out Input data)
    23. {
    24.     UNITY_INITIALIZE_OUTPUT(Input, data);
    25.     data.tc_Control = TRANSFORM_TEX(v.texcoord, _Control);    // Need to manually transform uv here, as we choose not to use 'uv' prefix for this texcoord.
    26.     float4 pos = mul (UNITY_MATRIX_MVP, v.vertex);
    27.     UNITY_TRANSFER_FOG(data, pos);
    28.    
    29. #ifdef _TERRAIN_NORMAL_MAP
    30.     v.tangent.xyz = cross(v.normal, float3(0,0,1));
    31.     v.tangent.w = -1;
    32. #endif
    33. }
    34.  
    35. void SplatmapMix(Input IN, out half4 splat_control, out half weight, out fixed4 mixedDiffuse, inout fixed3 mixedNormal)
    36. {
    37.     splat_control = tex2D(_Control, IN.tc_Control);
    38.     weight = dot(splat_control, half4(1,1,1,1));
    39.  
    40.     #ifndef UNITY_PASS_DEFERRED
    41.         // Normalize weights before lighting and restore weights in applyWeights function so that the overal
    42.         // lighting result can be correctly weighted.
    43.         // In G-Buffer pass we don't need to do it if Additive blending is enabled.
    44.         // TODO: Normal blending in G-buffer pass...
    45.         splat_control /= (weight + 1e-3f); // avoid NaNs in splat_control
    46.     #endif
    47.  
    48.     #if !defined(SHADER_API_MOBILE) && defined(TERRAIN_SPLAT_ADDPASS)
    49.         clip(weight - 0.0039 /*1/255*/);
    50.     #endif
    51.  
    52.     mixedDiffuse = 0.0f;
    53.     mixedDiffuse += splat_control.r * tex2D(_Splat0, IN.uv_Splat0) * tex2D(_Splat0, IN.uv_Splat0 * -0.25) * 4;
    54.     mixedDiffuse += splat_control.g * tex2D(_Splat1, IN.uv_Splat1) * tex2D(_Splat1, IN.uv_Splat1 * -0.25) * 4;
    55.     mixedDiffuse += splat_control.b * tex2D(_Splat2, IN.uv_Splat2) * tex2D(_Splat2, IN.uv_Splat2 * -0.25) * 4;
    56.     mixedDiffuse += splat_control.a * tex2D(_Splat3, IN.uv_Splat3) * tex2D(_Splat3, IN.uv_Splat3 * -0.25) * 4;
    57.  
    58.     #ifdef _TERRAIN_NORMAL_MAP
    59.         fixed4 nrm = 0.0f;
    60.         nrm += splat_control.r * tex2D(_Normal0, IN.uv_Splat0);
    61.         nrm += splat_control.g * tex2D(_Normal1, IN.uv_Splat1);
    62.         nrm += splat_control.b * tex2D(_Normal2, IN.uv_Splat2);
    63.         nrm += splat_control.a * tex2D(_Normal3, IN.uv_Splat3);
    64.         mixedNormal = UnpackNormal(nrm);
    65.     #endif
    66. }
    67.  
    68. void SplatmapApplyWeight(inout fixed4 color, fixed weight)
    69. {
    70.     color.rgb *= weight;
    71.     color.a = 1.0f;
    72. }
    73.  
    74. void SplatmapApplyFog(inout fixed4 color, Input IN)
    75. {
    76.     #ifdef TERRAIN_SPLAT_ADDPASS
    77.         UNITY_APPLY_FOG_COLOR(IN.fogCoord, color, fixed4(0,0,0,0));
    78.     #else
    79.         UNITY_APPLY_FOG(IN.fogCoord, color);
    80.     #endif
    81. }
    82.  
    83. #endif
    84.  
    then I have imported it in my project, changed the references to the first-pass.shader, add-pass and base-pass (renamed this to base-pass-advanced) but then my terrain turned out without multi UV mixing and with smoothness fixed at 1. Someone can tell me what im doing wrong?
     
  2. ZeddZedd

    ZeddZedd

    Joined:
    Feb 16, 2015
    Posts:
    3
    I managed to get this working, see my reply in the thread you linked. Instead of copying the entire file and including that, i just copied the SplatmapMix-function and put it directly in my shader file.
     
    SergeyTroshkov likes this.