Search Unity

Merging 2 Shaders kinda broke mine...

Discussion in 'Shaders' started by Zero_Xue, May 8, 2018.

  1. Zero_Xue

    Zero_Xue

    Joined:
    Apr 18, 2012
    Posts:
    116
    Hi ive got all of about 2hrs of experiance coding shaders but ive been working on Procedural terrain, and its all working, i pick up a shader and messed around with it and got it working, then i tried to add Triplaner from http://www.martinpalko.com/triplanar-mapping/#Implementation - Unity but well am getting errors but i dun have a clue what they mean and google aint much help atm

    The goal is to have 4 textures that blend based on height while being Triplaner

    Code (CSharp):
    1.            
    2.     Shader "WorldBlendSharder"
    3.     {
    4.         Properties
    5.         {
    6.            _BlendSharpness ("Blending" ,Range(0,1)) = 0.1
    7.            [Space(30)]
    8.            _MainTex ("Low Texture", 2D) = "white" {}
    9.            
    10.                 _Layer1 ("Mid Texture", 2D) = "white" {}
    11.                 _Layer1Height ("Mid Texture Start", Range(0, 1)) = 0
    12.             [Space(20)]
    13.                 _Layer2 ("High Texture", 2D) = "white" {}
    14.                 _Layer2Height ("High Texture Start", Range(0, 1)) = 1
    15.             [Space(20)]
    16.                 _Layer3 ("Peak Texture", 2D) = "white" {}
    17.                 _Layer3Height("Layer 3 Height", Range(0, 1)) = 1
    18.         }
    19.         SubShader
    20.         {
    21.             Tags { "RenderType" = "Opaque" }
    22.             LOD 200
    23.      
    24.             CGPROGRAM
    25.             #pragma surface surf Standard
    26.             #pragma target 3.0
    27.      
    28.             float _Metalness = 0.1;
    29.             float _Smoothness = 0.1;
    30.            int _MaxHeight;
    31.            float _BlendSharpness;
    32.  
    33.             UNITY_DECLARE_TEX2D(_MainTex);
    34.             float _BaseHeight = 0;
    35.      
    36.             UNITY_DECLARE_TEX2D(_Layer1);
    37.            float _Layer1Height = 0;
    38.             float4 _Layer1HeightAct;
    39.            //
    40.      
    41.             UNITY_DECLARE_TEX2D(_Layer2);
    42.             float _Layer2Height = 1;
    43.            float4 _Layer2HeightAct;
    44.            //= lerp(0,_MaxHeight,_Layer2Height);
    45.      
    46.             UNITY_DECLARE_TEX2D(_Layer3);
    47.            float _Layer3Height = 1;
    48.             float4 _Layer3HeightAct;
    49.            //= lerp(0,_MaxHeight,_Layer3Height);
    50.      
    51.             struct Input
    52.             {
    53.                 float2 uv_MainTex;
    54.                 float2 uv_Layer1;
    55.                 float2 uv_Layer2;
    56.                 float2 uv_Layer3;
    57.                 float3 worldPos;
    58.                float3 worldNormal;
    59.             };
    60.  
    61.             struct blendingData
    62.             {
    63.                 float height;
    64.                 float4 result;
    65.             };
    66.      
    67.             blendingData BlendLayer(float4 layer, float layerHeight, blendingData bd)
    68.             {
    69.                 bd.height = max(0, bd.height - layerHeight);
    70.                 float t = min(1, bd.height * _BlendSharpness);
    71.                 bd.result = lerp(bd.result, layer, t);
    72.                 return bd;
    73.             }
    74.      
    75. void surf (Input i, inout SurfaceOutputStandard o)
    76.             {
    77.                          
    78.                 blendingData bdata;
    79.                     bdata.height = i.worldPos.y;
    80.                     _Layer1HeightAct = lerp(0,_MaxHeight,_Layer1Height / 10);
    81.                     _Layer2HeightAct = lerp(0,_MaxHeight,_Layer2Height / 10);
    82.                     _Layer3HeightAct = lerp(0,_MaxHeight,_Layer3Height / 10);
    83.                     bdata.result = UNITY_SAMPLE_TEX2D(_MainTex, i.uv_MainTex);
    84.                     float4 layer1 = UNITY_SAMPLE_TEX2D(_Layer1, i.uv_Layer1);
    85.                     float4 layer2 = UNITY_SAMPLE_TEX2D(_Layer2, i.uv_Layer2);
    86.                     float4 layer3 = UNITY_SAMPLE_TEX2D(_Layer3, i.uv_Layer3);
    87.    
    88.                     bdata = BlendLayer(layer1, _Layer1HeightAct, bdata);
    89.                     bdata = BlendLayer(layer2, _Layer2HeightAct, bdata);
    90.                     bdata = BlendLayer(layer3, _Layer3HeightAct, bdata);
    91.    
    92.             // Find our UVs for each axis based on world position of the fragment.
    93.             half2 yUV = i.worldPos.xz / 1;
    94.             half2 xUV = i.worldPos.zy / 1;
    95.             half2 zUV = i.worldPos.xy / 1;
    96.             // Now do texture samples from our diffuse map with each of the 3 UV set's we've just made.
    97.             half3 yDiff = UNITY_SAMPLE_TEX2D (bdata.result, yUV);
    98.             half3 xDiff = UNITY_SAMPLE_TEX2D (bdata.result, xUV);
    99.             half3 zDiff = UNITY_SAMPLE_TEX2D (bdata.result, zUV);
    100.             // Get the absolute value of the world normal.
    101.             // Put the blend weights to the power of BlendSharpness, the higher the value,
    102.             // the sharper the transition between the planar maps will be.
    103.             half3 blendWeights = pow (abs(i.worldNormal), 1);
    104.             // Divide our blend mask by the sum of it's components, this will make x+y+z=1
    105.             blendWeights = blendWeights / (blendWeights.x + blendWeights.y + blendWeights.z);
    106.             // Finally, blend together all three samples based on the blend mask.
    107.             ///o.Albedo = xDiff * blendWeights.x + yDiff * blendWeights.y + zDiff * blendWeights.z;
    108.  
    109.  
    110.                 o.Albedo = bdata.result;
    111.                 o.Metallic = _Metalness;
    112.                 o.Smoothness = _Smoothness;
    113.             }
    114.             ENDCG
    115.         }
    116.         FallBack "Diffuse"
    117.     }
    118.  
    119.  
    --* = Unable to find Compatable overloaded Function (tex2d(float4,half2)
    --! = Implicit cast from float4 to float

    It works fine if remove all the triplaner stuff, like i said i got no idea when it comes to shaders.... so any help would be amazing
     
    Last edited: May 8, 2018