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

Animated UVs work in frag but not in vert...

Discussion in 'Shaders' started by Gcs_deV, Jul 18, 2014.

  1. Gcs_deV

    Gcs_deV

    Joined:
    Jun 19, 2013
    Posts:
    22
    I use the following shader code to animate uvs :

    void vert (inout appdata_full v, out Input o) {
    o.uv_BumpMap = v.texcoord;
    }
    void surf (Input IN, inout EditorSurfaceOutput o) {
    fixed tm = _Time.y * 30;
    fixed ty = floor(tm / 8);
    fixed2 uv = IN.uv_BumpMap + fixed2(floor(tm - ty * 8), ty) * 0.125;
    o.Normal = UnpackNormal( tex2D(_BumpMap,uv));
    }

    and this works nicely, so I wanted to move the coordinate math part into the vert function like so:

    void vert (inout appdata_full v, out Input o) {
    fixed tm = _Time.y * 30;
    fixed ty = floor(tm / 8);
    fixed tx = floor(tm - ty * 8);
    o.uv_BumpMap = v.texcoord + fixed2(tx * 0.125, ty * 0.125);
    }
    void surf (Input IN, inout EditorSurfaceOutput o) {
    o.Normal = UnpackNormal( tex2D(_BumpMap,IN.uv_BumpMap));
    }

    but this doesn't work and I really can't see why not. Does _Time work differently in the vert program as opposed to frag?
    Can anyone help?
     
    Last edited: Jul 18, 2014
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    I'm guessing that uv_BumpMap is being overwritten by Unity's own UV coordinate handling. You might want to rename the variable?
     
  3. Gcs_deV

    Gcs_deV

    Joined:
    Jun 19, 2013
    Posts:
    22
    @Farfarer Thanks for answering.
    I had tried changing the variable name before but it had the result of eliminating the bump effect though no errors were thrown.
    For now, I got it to work by passing both the unaltered uv_BumpMap, and an offset variable from the vert program, then adding them together in frag. Not a pretty solution but it works.
    I'd still like to understand why the first solution didn't work though.
     
  4. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    When you ask Unity for the uv_BumpMap variable, it takes the default UV coordintes of your mesh and then scales/offsets them to account for the tiling/offset values in the texture. It will do this even if they are left at the default values (1 tile, 0 offset in both U and V).

    But you're trying to write to that same variable, which is probably getting overwritten by Unity itself.
     
  5. Gcs_deV

    Gcs_deV

    Joined:
    Jun 19, 2013
    Posts:
    22
    Got it! You're absolutely right. uv_BumpMap gets set automatically post vert program as part of the surface shader "magic" so my changes were indeed being overwritten.
    Many thanks again for answering.