Search Unity

Question Wave Shader Not Working

Discussion in 'Shaders' started by LostALife, Sep 9, 2021.

  1. LostALife

    LostALife

    Joined:
    Jul 19, 2020
    Posts:
    30
    I'm trying to make my first shader and wanted to make a simple wave shader using cos on the vertices.
    Im following this tutorial:

    The problem is when this shader is placed on a material on a quad it doesn't move at all.
    Im aware 4 vertices isnt what i want but there should still be some movement right?
    Anyway im not sure what to do so i would appreciate help.

    Code (CSharp):
    1. Shader "Custom/Waves" {
    2.     Properties {
    3.         _Color("Color", Color) = (1, 1, 1, 1)
    4.         _WaveHeight("Height", Range(0,2)) = 1.0
    5.         _Speed("Speed", Range(0,200)) = 100
    6.     }
    7.  
    8.     SubShader{
    9.         Tags{
    10.             "RenderType" = "Opaque"
    11.         }
    12.  
    13.         Pass {
    14.             CGPROGRAM
    15.  
    16.             #pragma vertex VertexFunc
    17.             #pragma fragment fragmentFunc
    18.  
    19.             float4 _Color;
    20.             float _Height;
    21.             float _Speed;
    22.  
    23.             struct vertexInput {
    24.                 float4 vertex : POSITION;
    25.             };
    26.        
    27.             struct vertexOutput {
    28.                 float4 pos : SV_POSITION;
    29.             };
    30.  
    31.             vertexOutput VertexFunc(vertexInput IN) {
    32.                 vertexOutput o;
    33.  
    34.                 float4 worldPos = mul(unity_ObjectToWorld, IN.vertex);
    35.  
    36.                 float displacement = (cos(worldPos.y) + cos(worldPos.x + (_Speed * _Time)));
    37.                 worldPos.y = worldPos.y + (displacement * _Height);
    38.  
    39.                 o.pos = mul(UNITY_MATRIX_VP, worldPos);
    40.                 return o;
    41.             }
    42.  
    43.             float4 fragmentFunc(vertexOutput IN) : COLOR {
    44.                 return _Color;
    45.             }
    46.  
    47.             ENDCG
    48.         }
    49.     }
    50. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    _Time
    is a
    float4
    value. If you use a
    float4
    value with
    float
    values, as you're doing above, then only the
    .x
    component is used.
    _Time.x
    is equivalent to
    Time.timeSinceLevelLoad / 20f
    in the game view when in play mode. That might be slow enough you're simply not seeing it move. Otherwise when not in play mode the game view does not (or infrequently) updates
    _Time
    .

    You want to use
    _Time.y
    in most case, as that's the actual unmodified time.

    If you're in the scene view,
    _Time
    has values unique to the scene view, and you need to have the Animated Materials option enabled for, well, materials to animate. Otherwise the
    _Time
    value the shader gets in the scene view is not updated.
    upload_2021-9-10_14-32-59.png
     
    kcastagnini likes this.
  3. LostALife

    LostALife

    Joined:
    Jul 19, 2020
    Posts:
    30
    I dont have the option to enable animated materials in that dropdown. Also im using the 3D template not URP if that makes any difference. Even using time.y nothing happens to during play mode.