Search Unity

Blend touching triangle textures

Discussion in 'Shaders' started by SpikeViper, Apr 3, 2017.

  1. SpikeViper

    SpikeViper

    Joined:
    Nov 6, 2016
    Posts:
    2
    Hello! I made a project where a voxel terrain system uses a specific vertex color to determine the texture used for that voxel. This works fine - but has very sharp edges and looks quite bad. I was wondering if there's any way to make the edges blend together.



    Thanks! Current shader code below:

    Code (CSharp):
    1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    2.  
    3. Shader"Triplanar" {
    4.   Properties {
    5.     _ArrTex("TextureArr", 2DArray) = "" {}
    6.     scale("Scale", Float) = 1
    7.     _brightness("Texture Brightness", Float) = 1
    8.     _SpecColor("Specular Material Color", Color) = (1, 1, 1, 1)
    9.     _Shininess("Shininess", Float) = 10
    10.   }
    11.   SubShader {
    12.     Pass {
    13.       Tags { "LightMode" = "ForwardBase" }
    14.  
    15. CGPROGRAM
    16. #pragma multi_compile_fwdbase
    17. #pragma vertex vert
    18. #pragma fragment frag
    19. #include"UnityCG.cginc"
    20. #include"AutoLight.cginc"
    21.  
    22. struct v2f
    23. {
    24.     float3 worldPos : TEXCOORD0;
    25.     half3 worldNormal : TEXCOORD1;
    26.     float4 pos : SV_POSITION;
    27.     float4 color : COLOR;
    28.         LIGHTING_COORDS(2, 3)
    29. };
    30.  
    31. v2f vert(float4 vertex : POSITION, float3 normal : NORMAL, float4 color : COLOR)
    32. {
    33.     v2f o;
    34.     o.color = color;
    35.     o.pos = UnityObjectToClipPos(vertex);
    36.     o.worldPos = mul(unity_ObjectToWorld, vertex).xyz;
    37.     o.worldNormal = UnityObjectToWorldNormal(normal);
    38.     TRANSFER_VERTEX_TO_FRAGMENT(o);
    39.     return o;
    40. }
    41.  
    42. float scale;
    43. float _brightness;
    44. float4 _SpecColor;
    45. float _Shininess;
    46.  
    47. UNITY_DECLARE_TEX2DARRAY(_ArrTex);
    48.  
    49. fixed4 frag(v2f i) : SV_Target
    50. {
    51.  
    52.         //////////////////////////////////   TRIPLANAR   ////////////////////////////////
    53.  
    54.     half texid = i.color.x * 255;
    55.     half3 norm = i.worldNormal;
    56.         // in wNorm is the world-space normal of the fragment
    57.     vector blending = abs(float4(norm.x, norm.y, norm.z, 0));
    58.     blending = normalize(max(blending, 0.00001)); // Force weights to sum to 1.0
    59.     float b = (blending.x + blending.y + blending.z);
    60.     blending /= vector(b, b, b, 1);
    61.  
    62.     float4 xaxis = UNITY_SAMPLE_TEX2DARRAY(_ArrTex, float3(i.worldPos.y, i.worldPos.z, texid) * scale);
    63.     float4 yaxis = UNITY_SAMPLE_TEX2DARRAY(_ArrTex, float3(i.worldPos.x, i.worldPos.z, texid) * scale);
    64.     float4 zaxis = UNITY_SAMPLE_TEX2DARRAY(_ArrTex, float3(i.worldPos.x, i.worldPos.y, texid) * scale);
    65.  
    66.         // in float scale
    67.     float4 tex = xaxis * blending.x + yaxis * blending.y + zaxis * blending.z;
    68.  
    69.     tex = tex * _brightness;
    70.  
    71.         //////////////////////////////      LIGHTING     ////////////////////////////////
    72.  
    73.     float attenuation = LIGHT_ATTENUATION(i);
    74.     tex = tex * attenuation;
    75.  
    76.         ////////////////////////////////////////////////////////////////////////////////////
    77.  
    78.     return tex;
    79.  
    80. }
    81.       ENDCG
    82.     }
    83.  
    84.     Pass {
    85.  
    86. Blend one one
    87.  
    88.       Tags { "LightMode" = "ForwardAdd" }
    89.  
    90. CGPROGRAM
    91. #pragma vertex vert
    92. #pragma fragment frag
    93. #include"UnityCG.cginc"
    94. #pragma multi_compile_fwdadd_fullshadows
    95. #include"AutoLight.cginc"
    96.  
    97. struct v2f
    98. {
    99.     float3 worldPos : TEXCOORD0;
    100.     half3 worldNormal : TEXCOORD1;
    101.     float4 pos : SV_POSITION;
    102.     float4 color : COLOR;
    103.         LIGHTING_COORDS(2, 3)
    104. };
    105.  
    106. v2f vert(appdata_base v, float4 vertex : POSITION, float3 normal : NORMAL, float4 color : COLOR)
    107. {
    108.     v2f o;
    109.     o.color = color;
    110.     o.pos = UnityObjectToClipPos(vertex);
    111.     o.worldPos = mul(unity_ObjectToWorld, vertex).xyz;
    112.     o.worldNormal = UnityObjectToWorldNormal(normal);
    113.     TRANSFER_VERTEX_TO_FRAGMENT(o);
    114.     return o;
    115. }
    116.  
    117. float scale;
    118. float _brightness;
    119. float4 _SpecColor;
    120. float _Shininess;
    121.  
    122.       UNITY_DECLARE_TEX2DARRAY(_ArrTex);
    123.  
    124. fixed4 frag(v2f i) : SV_Target
    125. {
    126.  
    127.         //////////////////////////////////   TRIPLANAR   ////////////////////////////////
    128.  
    129.     half texid = i.color.x * 255;
    130.     half3 norm = i.worldNormal;
    131.         // in wNorm is the world-space normal of the fragment
    132.     vector blending = abs(float4(norm.x, norm.y, norm.z, 0));
    133.     blending = normalize(max(blending, 0.00001)); // Force weights to sum to 1.0
    134.     float b = (blending.x + blending.y + blending.z);
    135.     blending /= vector(b, b, b, 1);
    136.  
    137.     float4 xaxis = UNITY_SAMPLE_TEX2DARRAY(_ArrTex, float3(i.worldPos.y, i.worldPos.z, texid) * scale);
    138.     float4 yaxis = UNITY_SAMPLE_TEX2DARRAY(_ArrTex, float3(i.worldPos.x, i.worldPos.z, texid) * scale);
    139.     float4 zaxis = UNITY_SAMPLE_TEX2DARRAY(_ArrTex, float3(i.worldPos.x, i.worldPos.y, texid) * scale);
    140.  
    141.         // in float scale
    142.     float4 tex = xaxis * blending.x + yaxis * blending.y + zaxis * blending.z;
    143.  
    144.     tex = tex * _brightness;
    145.  
    146.         //////////////////////////////      LIGHTING     ////////////////////////////////
    147.  
    148.     float attenuation = LIGHT_ATTENUATION(i);
    149.     tex = tex * attenuation;
    150.  
    151.         ////////////////////////////////////////////////////////////////////////////////////
    152.  
    153.     return tex;
    154.  
    155. }
    156.  
    157.       ENDCG
    158.  
    159.     }
    160.  
    161.   }
    162.  
    163. Fallback"VertexLit"
    164.  
    165. }
     
  2. SpikeViper

    SpikeViper

    Joined:
    Nov 6, 2016
    Posts:
    2