Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Vertex shader displacement does not write to depth properly

Discussion in 'Shaders' started by Aeonvirtual, Jan 29, 2020.

  1. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    33
    Hey All,
    I have made a vertex shader for a waving flag. It works fine, but it writes an undeformed version of the flag to the depth texture which is why some post effects and other shader effects don't work properly.

    I have read some posts about this and tried adding the 'addshadow' pragma and made sure my rendertype is set to opaque but wasn't able to get it to write proper values to the depth texture.

    I Hope someone can help with this.

    Here is my shader:
    Code (CSharp):
    1. /*
    2. @file Flag Shader
    3.  
    4. This shader is used for waving flags.
    5.  
    6. In order for the flag to work it needs to:
    7. 1) Be a flat, subdivided plane with between 4 and 10 subdivisions
    8. 2) Have its pivot in the middle of the left edge
    9. 3) Have UVs that span from (0,0) in the lower left to (1,1) in the top right
    10. 4) Have its local axis like so:
    11.     x = depth (perpendicular to plane)
    12.     y = along the length of the flag
    13.     z = along the height
    14.     in other words, your flag lies in the yz plane
    15.  
    16. 4) Be sure to apply scale in blender so the object scale is (1,1,1)
    17.  
    18. Parameters:
    19. _Wave = The size of the waves
    20. _Speed = How fast the flag flutters
    21. _Amount = How strongly the flag gets distorted
    22. _Droop = How much the flag sags from purely horizontal
    23. */
    24.  
    25. Shader "Flag"
    26. {
    27.     Properties
    28.     {
    29.         _Color ("Color", Color) = (1,1,1,1)
    30.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    31.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    32.         _Metallic ("Metallic", Range(0,1)) = 0.0
    33.        
    34.         _Wave("Wave", float) = 0.0
    35.         _Speed("Speed", float) = 0.0
    36.         _Amount("Amount", float) = 0.0
    37.         _Droop("Droop", float) = 0.0
    38.  
    39.         _Seed("Seed", float) = 0.0
    40.     }
    41.     SubShader
    42.     {
    43.         Tags {
    44.                  "RenderType" = "Opaque"
    45.                  "Queue" = "Geometry"
    46.                  "LightMode" = "ForwardBase"
    47.              }
    48.         LOD 200
    49.        
    50.         Cull Off
    51.  
    52.         CGPROGRAM
    53.         // Physically based Standard lighting model, and enable shadows on all light types
    54.         #pragma surface surf Standard fullforwardshadows addshadow vertex:vert
    55.  
    56.         // Use shader model 3.0 target, to get nicer looking lighting
    57.         #pragma target 3.0
    58.  
    59.         sampler2D _MainTex;
    60.  
    61.         half _Glossiness;
    62.         half _Metallic;
    63.         half _Wave, _Speed, _Amount, _Droop, _Seed;
    64.         fixed4 _Color;
    65.  
    66.         struct Input
    67.         {
    68.             float2 uv_MainTex;
    69.         };
    70.  
    71.         void vert(inout appdata_full v) {
    72.             float2 uv = v.texcoord.xy;
    73.             float t = _Time.y;
    74.             float4 p = v.vertex;    // x = depth, y=length, z=height
    75.             float4 P = mul(unity_ObjectToWorld, p);
    76.             float dist = p.y;
    77.            
    78.             float offs = sin(_Seed+uv.x*_Wave - t * _Speed);
    79.             offs += sin(_Seed+uv.y*_Wave - t * _Speed*2.2)*.4;
    80.             offs *= uv.x*_Amount;
    81.             p.x += offs;
    82.             p.y *= (p.y*p.y) / (p.y*p.y + p.x*p.x);
    83.  
    84.             float x = uv.x*_Droop;
    85.             float y = x.x*x.x;
    86.  
    87.             p.y *= (p.y*p.y) / (p.y*p.y + y*y);
    88.             p.z -= min(dist, y);
    89.  
    90.             v.vertex = p;
    91.            
    92.         }
    93.  
    94.  
    95.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    96.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    97.         // #pragma instancing_options assumeuniformscaling
    98.         UNITY_INSTANCING_BUFFER_START(Props)
    99.             // put more per-instance properties here
    100.         UNITY_INSTANCING_BUFFER_END(Props)
    101.  
    102.         void surf (Input IN, inout SurfaceOutputStandard o)
    103.         {
    104.             // Albedo comes from a texture tinted by color
    105.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    106.             o.Albedo = c.rgb;
    107.             // Metallic and smoothness come from slider variables
    108.             o.Metallic = _Metallic;
    109.             o.Smoothness = _Glossiness;
    110.             o.Alpha = c.a;
    111.         }
    112.         ENDCG
    113.     }
    114. }
    115.  
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Perhaps try removing the LightMode from your Tags? It might be prevent surface shader from generating the required passes.
     
  3. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    33
    Hmm.. not it writes to depth, but only at certain angles o_O

    Looks like something in my vertex shader is screwed. Im scaling with respect to the object pivot by multiplying v.vertex. I'm not touching the w coordinate, perhaps thats screwing things up?
     
    Last edited: Jan 30, 2020
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Try making a new Material and set the shader on it, it could be your existing Material has some serialized values set on it from messing around that are affecting how it renders. (Also ensure you still have
    addshadow
    in your pragma line). Perhaps post your whole shader against as it stands now.
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
  6. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    mgear likes this.