Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to convert height based shader to URP?

Discussion in 'Shaders' started by JalexRas, Jun 27, 2023.

  1. JalexRas

    JalexRas

    Joined:
    Mar 30, 2020
    Posts:
    6
    Can anyone help me transition this built in shader to URP. This shader takes multiple layers of color and texture and applies them based on their height in the world. Its variables are set in another script using material.SetFloatArray, material.SetColorArray, etc. What is the new version of these methods?


    Code (CSharp):
    1. Shader "Custom/Terrain" {
    2.     Properties {
    3.         testTexture("Texture", 2D) = "white"{}
    4.         testScale("Scale", Float) = 1
    5.  
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" }
    9.         LOD 200
    10.        
    11.         CGPROGRAM
    12.         // Physically based Standard lighting model, and enable shadows on all light types
    13.         #pragma surface surf Standard fullforwardshadows
    14.  
    15.         // Use shader model 3.0 target, to get nicer looking lighting
    16.         #pragma target 3.0
    17.  
    18.         const static int maxLayerCount = 8;
    19.         const static float epsilon = 1E-4;
    20.  
    21.         int layerCount;
    22.         float3 baseColours[maxLayerCount];
    23.         float baseStartHeights[maxLayerCount];
    24.         float baseBlends[maxLayerCount];
    25.         float baseColourStrength[maxLayerCount];
    26.         float baseTextureScales[maxLayerCount];
    27.  
    28.         float minHeight;
    29.         float maxHeight;
    30.  
    31.         sampler2D testTexture;
    32.         float testScale;
    33.  
    34.         UNITY_DECLARE_TEX2DARRAY(baseTextures);
    35.  
    36.         struct Input {
    37.             float3 worldPos;
    38.             float3 worldNormal;
    39.         };
    40.  
    41.         float inverseLerp(float a, float b, float value) {
    42.             return saturate((value-a)/(b-a));
    43.         }
    44.  
    45.         float3 triplanar(float3 worldPos, float scale, float3 blendAxes, int textureIndex) {
    46.             float3 scaledWorldPos = worldPos / scale;
    47.             float3 xProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures, float3(scaledWorldPos.y, scaledWorldPos.z, textureIndex)) * blendAxes.x;
    48.             float3 yProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures, float3(scaledWorldPos.x, scaledWorldPos.z, textureIndex)) * blendAxes.y;
    49.             float3 zProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures, float3(scaledWorldPos.x, scaledWorldPos.y, textureIndex)) * blendAxes.z;
    50.             return xProjection + yProjection + zProjection;
    51.         }
    52.  
    53.         void surf (Input IN, inout SurfaceOutputStandard o) {
    54.             float heightPercent = inverseLerp(minHeight,maxHeight, IN.worldPos.y);
    55.             float3 blendAxes = abs(IN.worldNormal);
    56.             blendAxes /= blendAxes.x + blendAxes.y + blendAxes.z;
    57.  
    58.             for (int i = 0; i < layerCount; i ++) {
    59.                 float drawStrength = inverseLerp(-baseBlends[i]/2 - epsilon, baseBlends[i]/2, heightPercent - baseStartHeights[i]);
    60.  
    61.                 float3 baseColour = baseColours[i] * baseColourStrength[i];
    62.                 float3 textureColour = triplanar(IN.worldPos, baseTextureScales[i], blendAxes, i) * (1-baseColourStrength[i]);
    63.  
    64.                 o.Albedo = o.Albedo * (1-drawStrength) + (baseColour+textureColour) * drawStrength;
    65.             }
    66.  
    67.        
    68.         }
    69.  
    70.  
    71.         ENDCG
    72.     }
    73.     FallBack "Diffuse"
    74. }