Search Unity

converting standard customer shaders to work in HDRP

Discussion in 'Shaders' started by twn9009, Jul 2, 2019.

  1. twn9009

    twn9009

    Joined:
    May 2, 2019
    Posts:
    58
    hi all, i've just imported everything i need to get the VFX graph working in unity but forgot i had a custom shader on my terrain, i've had a look around and it seems nearly impossible to convert them easily, how can i recreate this shader on the HDRP, i am very inexperienced when it comes to shader code and i've only just installed the shader graph so have yet to play around.

    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. }
    75.