Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Writing shader dependent on data stored in mesh variables.

Discussion in 'Shaders' started by davidokelly, Apr 16, 2021.

  1. davidokelly

    davidokelly

    Joined:
    Apr 1, 2021
    Posts:
    1
    Hi there, if any of you have seen Sebastian Lague's procedural generation series on youtube, I have been adapting his script to use biomes. The biome map is generated as an array of integer's which correspond to the index of that biome in a biome settings matrix.

    The issue I have at present is that I am not sure how to create different texture blending methods dependent on the current biome, as the biome map is generated on a chunk by chunk basis.

    Sebastian's custom shader blends several layers of custom textures (at designed heights) and tints together to create a height dependent texture across the entire terrain, but I would like to have this height dependent texturing customised separately for each biome.

    I am very new to shader language so any help is appreciated.

    Sebastian's shader code:
    Code (CSharp):
    1. Shader "Custom/BiomeTerrain" {
    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.         ENDCG
    68.     }
    69.     FallBack "Diffuse"
    70. }
     
    Last edited: Apr 16, 2021
unityunity