Search Unity

Convert shader to URP, or just explain how materials work

Discussion in 'Universal Render Pipeline' started by CarefreeCoding, May 2, 2022.

  1. CarefreeCoding

    CarefreeCoding

    Joined:
    Mar 19, 2022
    Posts:
    4
    Hello!

    I am very very new to unity. I have followed a few examples and made a shader that works in srp as intended.

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

    Or my intent is simply the following.

    I have a mesh that has height from 0 to 100. I just want height 0 to 30 to have one material. Height from 20 to 70 to have another material. And height from 60 to 100 to have different one. I want the material from 20 to 30 and from 60 to 70 to actually be blended together.

    The shader above achieves that, however if there is some other way to apply material to a mesh in URP then I would gladly do that!

    I understand that what I wrote might sound stupid and make no sense, but to explain it simply, I generate a random mesh, and want lower part of mesh to have water material, mid part of mesh to have sand material and top part of mesh to have grass material. And unfortunately I am at a loss on how to do that.

    If I am an idiot please do tell me and I will try to update the question or will delete it.