Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Texture Array Shader Problem

Discussion in 'Shaders' started by phray123, Oct 21, 2020.

  1. phray123

    phray123

    Joined:
    Sep 1, 2020
    Posts:
    1
    Hello, I am new to unity and currently working on a project for procedural world generation following a tutorial from Sebastian Lague but deviating of course. Instead of determining my textures on height I want to determine it by a given index.

    I am writing the surface shader code to place textures on my procedurally generated terrain mesh, I'm having trouble with the transition between different biomes. I am passing the texture index on every vertice in my mesh. the shader will then interpolate the value of texture index which results in the shader looping through all values between the 2 different biomes in the texture array which I don't want to happen. Picture of what is happening: https://imgur.com/a/3tNcajD

    Shader code:
    Code (CSharp):
    1. Shader "Custom/Blend"
    2. {
    3.     Properties {
    4.         _BaseTextures ("Terrain Textures", 2DArray) = "" {}
    5.     }
    6.  
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 600
    11.  
    12.         CGPROGRAM
    13.         // Physically based Standard lighting model, and enable shadows on all light types
    14.         #pragma surface surf Standard fullforwardshadows
    15.  
    16.         // Use shader model 3.0 target, to get nicer looking lighting
    17.         #pragma target 4.0
    18.         const static int maxLayerCount = 10;
    19.         const static float epsilon = 1E-4;
    20.  
    21.  
    22.         float3 baseColours[maxLayerCount];
    23.         float baseStartHeights[maxLayerCount];
    24.         float baseBlends[maxLayerCount];
    25.         float baseColourStrength[maxLayerCount];
    26.         float baseTextureScales[maxLayerCount];
    27.  
    28.         UNITY_DECLARE_TEX2DARRAY(_BaseTextures);
    29.  
    30.         struct Input
    31.         {
    32.             float2 uv_BaseTextures;
    33.             float3 worldPos;
    34.             float3 worldNormal;
    35.         };
    36.  
    37.         float3 triplanar(float3 worldPos, float scale, float3 blendAxes, float index) {
    38.             float3 scaledWorldPos = worldPos / scale;
    39.  
    40.             float3 xProjection = UNITY_SAMPLE_TEX2DARRAY(_BaseTextures, float3(scaledWorldPos.y, scaledWorldPos.z, index)) * blendAxes.x;
    41.             float3 yProjection = UNITY_SAMPLE_TEX2DARRAY(_BaseTextures, float3(scaledWorldPos.x, scaledWorldPos.z, index)) * blendAxes.y;
    42.             float3 zProjection = UNITY_SAMPLE_TEX2DARRAY(_BaseTextures, float3(scaledWorldPos.x, scaledWorldPos.y, index)) * blendAxes.z;
    43.             return xProjection + yProjection + zProjection;
    44.         }
    45.  
    46.  
    47.         void surf (Input IN, inout SurfaceOutputStandard o)
    48.         {
    49.             int i = IN.uv_BaseTextures.y;
    50.             float3 blendAxes = abs(IN.worldNormal);
    51.  
    52.             float drawStrength = 1;
    53.             float3 baseColour = baseColours[i] * baseColourStrength[i];
    54.             float3 textureColour = triplanar(IN.worldPos, baseTextureScales[i], blendAxes, i) * (1 - baseColourStrength[i]);
    55.  
    56.             o.Albedo = o.Albedo * (1 - drawStrength) + (baseColour + textureColour) * drawStrength;
    57.         }
    58.  
    59.         ENDCG
    60.     }
    61.     FallBack "Diffuse"
    62. }
     
    nghiattran likes this.
  2. nghiattran

    nghiattran

    Joined:
    Aug 27, 2016
    Posts:
    3