Search Unity

Help with shader, Loop blending???

Discussion in 'Shaders' started by Zero_Xue, Jun 17, 2019.

  1. Zero_Xue

    Zero_Xue

    Joined:
    Apr 18, 2012
    Posts:
    116
    First of all sorry about the title...

    am trying to make a Biome shader that pulls textures from an array and applies them (this bit works).

    am writing the biome values to the BiomeMap.r but clamped down between 0-1 and using that value to lerp between the biomes, how ever all the shader is doing is drawing both Biome Textures on top of each other in the 1st Biome and darking the second biome instead of jsut drawing Biome 1 Textires in Biome and transistioning into to second biome with the correct Textures......

    Ive added both the Shader code and pics of whats happening below


    Incorrect Blending





    Both Biome Ground Textures draw in Biome 1



    Code (CSharp):
    1.         Shader "Testing"
    2. {
    3.     Properties
    4.     {
    5.         Blending("Blending",Float) = 0.0 // 0.3
    6.         BiomesCount("Biomes Loaded", Float) = 0
    7.         BiomeMap("BiomeMap",2D) = "white"{}
    8.         Test("Testing Value",Float) = 0
    9.     }
    10.  
    11.     SubShader
    12.     {
    13.         Tags {
    14.             "Queue" = "Transparent"
    15.             "RenderType" = "Opaque" }
    16.         LOD 200
    17.         CGPROGRAM
    18.         #pragma surface surf Standard fullforwardshadows
    19.         #pragma target 5.0
    20.         #pragma enable_d3d11_debug_symbols
    21.         #include "UnityCG.cginc"
    22.  
    23.         float BiomesCount;
    24.  
    25.         float Blending;
    26.  
    27.         float HeightStarts[4];
    28.  
    29.         float Test;
    30.  
    31.         sampler2D BiomeMap;
    32.         float2 BiomeMap_UV;
    33.  
    34.         UNITY_DECLARE_TEX2DARRAY(Textures);
    35.  
    36.         struct Input
    37.         {
    38.             float3 worldPos;
    39.             float2 BiomeMap_UV;
    40.             float3 worldNormal;
    41.         };
    42.  
    43.         float inverselerp(float a, float b, float value)
    44.         {
    45.             return saturate((value-a)/(b-a));
    46.         }
    47.  
    48.         float ExtendedLerp(float min, float max, float value)
    49.         {
    50.             float x = (value - min) / (max - min);
    51.             if (x < 0)
    52.             {
    53.                 return 0;
    54.             }
    55.             if (x > 1)
    56.             {
    57.                 float a = ((value - min) / (max - min) - 2) * -1;
    58.                 if (a < 0)
    59.                 {
    60.                     a = 0;
    61.                 }
    62.                 return a;
    63.             }
    64.             return x;
    65.         }
    66.  
    67.         float3 Triplanner(float3 worldPos, float scale, float3 blendaxis, int Index)
    68.         {
    69.             float3 ScaledWorldPos = worldPos / scale;
    70.             float3 xP = UNITY_SAMPLE_TEX2DARRAY(Textures, float3(ScaledWorldPos.y,ScaledWorldPos.z,Index)) * blendaxis.x;
    71.             float3 yP = UNITY_SAMPLE_TEX2DARRAY(Textures, float3(ScaledWorldPos.x,ScaledWorldPos.z,Index)) * blendaxis.y;
    72.             float3 zP = UNITY_SAMPLE_TEX2DARRAY(Textures, float3(ScaledWorldPos.x,ScaledWorldPos.y,Index)) * blendaxis.z;
    73.             return xP + yP + zP;
    74.         }
    75.  
    76.         void surf (Input IN, inout SurfaceOutputStandard o)
    77.         {
    78.             float heightPerc = inverselerp(0,256,IN.worldPos.y);
    79.             float3 BlendAxis = abs(IN.worldNormal);
    80.             BlendAxis /= BlendAxis.x + BlendAxis.y + BlendAxis.z;
    81.  
    82.             float BiomeMarkers = saturate(1 / BiomesCount);
    83.             int IndexMarker = 0;
    84.             float3 Map = float3(0, 0, 0);
    85.             for (float i = 0; i < BiomesCount; i++)
    86.             {
    87.                 float CurrentMarker = BiomeMarkers + (BiomeMarkers * i);
    88.                 float BiomeBlend = ExtendedLerp(0, CurrentMarker, tex2D(BiomeMap, IN.BiomeMap_UV).r);
    89.                 float Blend1 = inverselerp(-Blending / 2, Blending / 2, heightPerc - HeightStarts[0]);
    90.                 float3 Tri1 = Triplanner(IN.worldPos, 1, BlendAxis, IndexMarker);
    91.                 Map = (Map * (1 - Blend1) + Tri1 * Blend1);
    92.  
    93.                 float Blend2 = inverselerp(-Blending / 2, Blending / 2, heightPerc - HeightStarts[1]);
    94.                 float3 Tri2 = Triplanner(IN.worldPos, 1, BlendAxis, IndexMarker + 1);
    95.                 Map = (Map * (1 - Blend2) + Tri2 * Blend2);
    96.  
    97.                 float Blend3 = inverselerp(-Blending / 2, Blending / 2, heightPerc - HeightStarts[2]);
    98.                 float3 Tri3 = Triplanner(IN.worldPos, 1, BlendAxis, IndexMarker + 2);
    99.                 Map = (Map * (1 - Blend3) + Tri3 * Blend3);
    100.  
    101.                 float Blend4 = inverselerp(-Blending / 2, Blending / 2, heightPerc - HeightStarts[3]);
    102.                 float3 Tri4 = Triplanner(IN.worldPos, 1, BlendAxis, IndexMarker + 3);
    103.                 Map = (Map * (1 - Blend4) + Tri4 * Blend4);
    104.                 IndexMarker += 4;
    105.                 Map = Map * BiomeBlend;
    106.                 o.Albedo += Map;
    107.             }
    108.         }
    109.         ENDCG
    110.     }
    111.     FallBack "Diffuse"
    112. }