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. Dismiss Notice

Question changing the default shader

Discussion in 'Shaders' started by Zimaell, Sep 5, 2023.

  1. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    341
    I need to use all the advantages of the Lit terrain shader, everything suits me except for the stretched texture with a large difference in the height of the vertices, so I want to make changes to this standard shader, make a triplanar, that is, copy everything except one file and make changes to it, if not I'm wrong, this is TerrainLitPasses.hlsl , there, if I'm not mistaken, you need to change only two methods NormalMapMix and SplatmapMix...
    Am I on the right track? or am i missing something?
    does anyone have any advice or tips on this?
    (URP, Shader Terrain Lit)
     
  2. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    73
    you could copy the shader and make an alternate version of the hlsl file and feed that in where nescessary.
     
    Zimaell likes this.
  3. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    341
    I did just that, I made an alternative copy, now it remains to understand the most important thing - do I think correctly that it is necessary to change the NormalMapMix and SplatmapMix methods?
    Am I on the right track to make the texture triplanar?
     
  4. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    73
  5. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    341
    Code (CSharp):
    1.  
    2. void NormalMapMix(float4 uvSplat01, float4 uvSplat23, inout half4 splatControl, inout half3 mixedNormal)
    3. {
    4.     #if defined(_NORMALMAP)
    5.         half3 nrm = half(0.0);
    6.         nrm += splatControl.r * UnpackNormalScale(SAMPLE_TEXTURE2D(_Normal0, sampler_Normal0, uvSplat01.xy), _NormalScale0);
    7.         nrm += splatControl.g * UnpackNormalScale(SAMPLE_TEXTURE2D(_Normal1, sampler_Normal0, uvSplat01.zw), _NormalScale1);
    8.         nrm += splatControl.b * UnpackNormalScale(SAMPLE_TEXTURE2D(_Normal2, sampler_Normal0, uvSplat23.xy), _NormalScale2);
    9.         nrm += splatControl.a * UnpackNormalScale(SAMPLE_TEXTURE2D(_Normal3, sampler_Normal0, uvSplat23.zw), _NormalScale3);
    10.  
    11.         // avoid risk of NaN when normalizing.
    12.         #if HAS_HALF
    13.             nrm.z += half(0.01);
    14.         #else
    15.             nrm.z += 1e-5f;
    16.         #endif
    17.  
    18.         mixedNormal = normalize(nrm.xyz);
    19.     #endif
    20. }
    21.  
    22.  
    23. void SplatmapMix(float4 uvMainAndLM, float4 uvSplat01, float4 uvSplat23, inout half4 splatControl, out half weight, out half4 mixedDiffuse,
    24.     out half4 defaultSmoothness, inout half3 mixedNormal, in float3 Position){
    25.     half4 diffAlbedo[4];
    26.  
    27.     diffAlbedo[0] = SAMPLE_TEXTURE2D(_Splat0, sampler_Splat0, uvSplat01.xy);
    28.     diffAlbedo[1] = SAMPLE_TEXTURE2D(_Splat1, sampler_Splat0, uvSplat01.zw);
    29.     diffAlbedo[2] = SAMPLE_TEXTURE2D(_Splat2, sampler_Splat0, uvSplat23.xy);
    30.     diffAlbedo[3] = SAMPLE_TEXTURE2D(_Splat3, sampler_Splat0, uvSplat23.zw);
    31.  
    32.     defaultSmoothness = half4(diffAlbedo[0].a, diffAlbedo[1].a, diffAlbedo[2].a, diffAlbedo[3].a);
    33.     defaultSmoothness *= half4(_Smoothness0, _Smoothness1, _Smoothness2, _Smoothness3);
    34.  
    35.     weight = dot(splatControl, 1.0h);
    36.  
    37.     #ifdef TERRAIN_SPLAT_ADDPASS
    38.         clip(weight <= 0.005h ? -1.0h : 1.0h);
    39.     #endif
    40.  
    41.     #ifndef _TERRAIN_BASEMAP_GEN
    42.         splatControl /= (weight + HALF_MIN);
    43.     #endif
    44.  
    45.     mixedDiffuse = 0.0h;
    46.     mixedDiffuse += diffAlbedo[0] * half4(_DiffuseRemapScale0.rgb * splatControl.rrr, 1.0h);
    47.     mixedDiffuse += diffAlbedo[1] * half4(_DiffuseRemapScale1.rgb * splatControl.ggg, 1.0h);
    48.     mixedDiffuse += diffAlbedo[2] * half4(_DiffuseRemapScale2.rgb * splatControl.bbb, 1.0h);
    49.     mixedDiffuse += diffAlbedo[3] * half4(_DiffuseRemapScale3.rgb * splatControl.aaa, 1.0h);
    50.  
    51.     NormalMapMix(uvSplat01, uvSplat23, splatControl, mixedNormal);
    52.     }
    I just can’t figure out how to apply it correctly here, I mean triplanar, can you tell me what to do?