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

Displace vertex with a scrolling noise texture?

Discussion in 'Shaders' started by bunp, Dec 14, 2020.

  1. bunp

    bunp

    Joined:
    Feb 21, 2019
    Posts:
    70
    OUT.uv2 = TRANSFORM_TEX(IN.uv2, _Noise) + (_Time * 0.5);
    IN.vertex.xy += IN.normal * IN.uv2;


    This is my code right now, it doesn't work, and I don't know why it doesn't work, or how to fix it, and I can't find any resources on the subject.

    Honestly trying to learn HLSL Shaderlab S*** has been nothing but suffering, pain, anguish. There is not a single good resource anywhere on the net unlike C#, and nothing works logically or rationally compared to C#.

    I tried the following, but all that did was cause my mesh to fly away slowly into space, instead of displace according to the noise texture's movement.

    OUT.uv2 = TRANSFORM_TEX(IN.uv2, _Noise) + (_Time * 0.5);
    IN.vertex.xy += IN.normal * OUT.uv2;
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    TRANSFORM_TEX
    does not sample the texture. It merely applies that texture's scale and offset values to your UV. A simple search of that function would have given you details on that. And there are tons of HLSL resources on the net, of course not as abundant as C# due to the relatively niche level of shader programming, but still almost always results covering a question that can be had. (that one isn't an HLSL function tho but simply a Unity helper macro found in UnityCG.cginc)

    What you want for sampling a texture in the vertex program is tex2Dlod().
    So
    float noise = tex2Dlod(_Noise, float4(OUT.uv2.xy, 0, 0)).r;
    gets you the grayscale noise.
    Then simply
    IN.vertex += IN.normal * noise;
    to offset based on normal direction and noise intensity.
     
    Last edited: Dec 15, 2020