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 Movement

Discussion in 'Shaders' started by Discord, Nov 11, 2016.

  1. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,008
    Hey guys,

    New to working with shaders, and I've run into a bit of an issue. I've successfully managed to move the vertices in a mesh along a sin wave, but now I'd like to restrict which vertices move based on an image.



    I'm using the above gradient to try and only move the vertices at the top of the mesh, while leaving the ones at the bottom in place.

    I'm trying to do a lookup of the image and use the associated value to influence how much the vertex moves, but it's not working as expected. Both the top and bottom of the mesh is moving, and the center seems to stay relatively in the same space.

    Code (csharp):
    1.  
    2. void vert (inout appdata_full v, out Input o) {
    3.        
    4.             UNITY_INITIALIZE_OUTPUT(Input,o);
    5.  
    6.             float4 tex = tex2Dlod(_MeshDeformationMask, float4(v.texcoord.xy, 0, 0));
    7.             v.vertex.x += sin((v.vertex.y + _Time * _MeshDeform1) * _MeshDeform2 ) * _MeshDeform3 * tex.y;
    8.         }
    9.  
    I'm struggling to figure out which variables are available to me to work with, and I've only gotten this far after looking at examples online. Any help or suggestions people can offer would be really appreciated.

    Thanks!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,208
    There's nothing obvious from the code, though I don't know what your model / UVs look like. If you use that texture as your main texture map and show it as the albedo does it show as you would expect?

    You could also just use the model's normals to determine "up" in either world or model space, or use a UV channel / vertex color to store values instead of using UVs and a texture depending on your goal. Something as simple as that gradient could be replicated with saturate(v.texcoord.y * 2.0 - 1.0);
     
  3. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,008
    bgolus, first you helped me with a tip on making KinoFog work, and now you're here to the rescue again. You're my new favorite person.

    Using vertex colors wasn't an option, but using saturate(v.texcoord.y * 2.0 - 1.0) was just what I needed.

    Now I just need to figure out what I need to do to get shadows matching up with my moving vertices. :confused:
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,208
  5. Discord

    Discord

    Joined:
    Mar 19, 2009
    Posts:
    1,008
    Thanks bgolus, that did the trick! For anyone interested,
    #pragma surface surf StandardSpecular vertex:vert addshadow
    is the full line that did the trick for me.
     
    Last edited: Nov 13, 2016