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

Z Sorting issue with billboard grass

Discussion in 'Shaders' started by Mirks, Jun 26, 2020.

  1. Mirks

    Mirks

    Joined:
    Dec 16, 2016
    Posts:
    3
    I created a gameobject with multiple billboard meshes which uses a transparent shader. The problem is that some of the billboards bleed through. Is there something that can be done in the shader to fix this issue? I tried playing around with Z-testing but with no success. Below is my shader code and a screenshot showing the problem.



    Code (CSharp):
    1. Shader "Custom/WindGrass" {
    2.  
    3.     Properties{
    4.         // Surface shader parameters
    5.         _Color("Color", Color) = (1,1,1,1)
    6.         _MainTex("Albedo (RGB)", 2D) = "white" {}
    7.         _Glossiness("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic("Metallic", Range(0,1)) = 0.0
    9.         [NoScaleOffset] _BumpMap("Normal Map", 2D) = "bump" {}
    10.         [Enum(Flip,0,Invert,1)] _BumpFlipMode("Normal Flip Mode", Float) = 0
    11.  
    12.         // Wind effect parameteres
    13.         _WindFrecuency("Wind Frecuency",Range(0.001,100)) = 1
    14.         _WindStrength("Wind Strength", Range(0, 2)) = 0.3
    15.         _WindGustDistance("Distance between gusts",Range(0.001,50)) = .25
    16.         _WindDirection("Wind Direction", vector) = (1,0, 1,0)
    17.  
    18.     }
    19.         SubShader{
    20.         Tags { "Queue" = "Transparent"
    21.         "RenderType" = "TransparentCutout"
    22.         }
    23.         Cull off
    24.         LOD 200
    25.  
    26.         CGPROGRAM
    27.  
    28.         #pragma surface surf Standard vertex:vert alpha:fade
    29.             #pragma target 3.0
    30.  
    31.             sampler2D _MainTex;
    32.             sampler2D _BumpMap;
    33.  
    34.             struct Input {
    35.             float2 uv_MainTex;
    36.             float2 uv2_MainTex;
    37.             };
    38.  
    39.             half _Glossiness;
    40.             half _Metallic;
    41.             fixed4 _Color;
    42.             bool _BumpFlipMode;
    43.  
    44.             half _WindFrecuency;
    45.             half _WindGustDistance;
    46.             half _WindStrength;
    47.             float3 _WindDirection;
    48.  
    49.             void vert(inout appdata_full v)
    50.             {
    51.                 float4 localSpaceVertex = v.vertex;
    52.                 float4 worldSpaceVertex = mul(unity_ObjectToWorld, localSpaceVertex);
    53.  
    54.                 float height = ((localSpaceVertex.y / 2) + .5);
    55.  
    56.                 worldSpaceVertex.x += sin(_Time.x * _WindFrecuency + worldSpaceVertex.x * _WindGustDistance) * height * _WindStrength * _WindDirection.x;
    57.                 worldSpaceVertex.z += sin(_Time.x * _WindFrecuency + worldSpaceVertex.z * _WindGustDistance) * height * _WindStrength * _WindDirection.z;
    58.    
    59.             v.vertex = mul(unity_WorldToObject, worldSpaceVertex);
    60.  
    61.             }
    62.  
    63.             void surf(Input IN, inout SurfaceOutputStandard o) {
    64.                 fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    65.                 o.Albedo = c.rgb;
    66.                 o.Metallic = _Metallic;
    67.                 o.Smoothness = _Glossiness;
    68.                 o.Alpha = c.a;
    69.                 o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
    70.                 float2 uv2 = IN.uv2_MainTex;
    71.             }
    72.             ENDCG
    73.  
    74.         }
    75.             FallBack "Diffuse"
    76. }
     
    Last edited: Jun 27, 2020
  2. Mirks

    Mirks

    Joined:
    Dec 16, 2016
    Posts:
    3
    Well, i figured it out by making a proper cutout shader using clip:

    Code (CSharp):
    1. Shader "Custom/WindGrass" {
    2.     Properties{
    3.         // Surface shader parameters
    4.         _Color("Color", Color) = (1,1,1,1)
    5.         _MainTex("Albedo (RGB)", 2D) = "white" {}
    6.         _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
    7.         _Glossiness("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic("Metallic", Range(0,1)) = 0.0
    9.         [NoScaleOffset] _BumpMap("Normal Map", 2D) = "bump" {}
    10.         [Enum(Flip,0,Invert,1)] _BumpFlipMode("Normal Flip Mode", Float) = 0
    11.         // Wind effect parameteres
    12.         _WindFrecuency("Wind Frecuency",Range(0.001,100)) = 1
    13.         _WindStrength("Wind Strength", Range(0, 2)) = 0.3
    14.         _WindGustDistance("Distance between gusts",Range(0.001,50)) = .25
    15.         _WindDirection("Wind Direction", vector) = (1,0, 1,0)
    16.     }
    17.         SubShader{
    18.         Tags { "Queue" = "Transparent"
    19.         "RenderType" = "TransparentCutout"
    20.         }
    21.         Cull off
    22.         LOD 200
    23.         CGPROGRAM
    24.         #pragma surface surf Standard vertex:vert
    25.             #pragma target 3.0
    26.             sampler2D _MainTex;
    27.             sampler2D _BumpMap;
    28.             struct Input {
    29.             float2 uv_MainTex;
    30.             float2 uv2_MainTex;
    31.             };
    32.             half _Glossiness;
    33.             half _Metallic;
    34.             fixed4 _Color;
    35.             bool _BumpFlipMode;
    36.             half _WindFrecuency;
    37.             half _WindGustDistance;
    38.             half _WindStrength;
    39.             float3 _WindDirection;
    40.  
    41.             fixed _Cutoff;
    42.             void vert(inout appdata_full v)
    43.             {
    44.                 float4 localSpaceVertex = v.vertex;
    45.                 float4 worldSpaceVertex = mul(unity_ObjectToWorld, localSpaceVertex);
    46.                 float height = ((localSpaceVertex.y / 2) + .5);
    47.                 worldSpaceVertex.x += sin(_Time.x * _WindFrecuency + worldSpaceVertex.x * _WindGustDistance) * height * _WindStrength * _WindDirection.x;
    48.                 worldSpaceVertex.z += sin(_Time.x * _WindFrecuency + worldSpaceVertex.z * _WindGustDistance) * height * _WindStrength * _WindDirection.z;
    49.  
    50.             v.vertex = mul(unity_WorldToObject, worldSpaceVertex);
    51.             }
    52.             void surf(Input IN, inout SurfaceOutputStandard o) {
    53.                 fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    54.                 o.Albedo = c.rgb;
    55.                 clip(c.a - _Cutoff);
    56.                 o.Metallic = _Metallic;
    57.                 o.Smoothness = _Glossiness;
    58.                 o.Alpha = c.a;
    59.                 o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
    60.                 float2 uv2 = IN.uv2_MainTex;
    61.             }
    62.             ENDCG
    63.         }
    64.             FallBack "Diffuse"
    65. }
     
  3. fmaribo

    fmaribo

    Joined:
    Jan 24, 2023
    Posts:
    1
    For anyone still wondering how to fix this in URP, Use this shader:
    Universal Render Pipeline/Nature/SpeedTree8_PBRLit
    this shader even comes with Subsurface scattering, color tint and hue variations. and much more