Search Unity

Secondary Texture Offset

Discussion in 'Shaders' started by drJones, Jul 17, 2007.

  1. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    is there any reason why something like this (from the wiki) does not animate a secondary texture when for instance i change "_MainTex" to "_AlphaTex"? i've tried a few different scenarios nothing seems to animate except the main texture.

    Code (csharp):
    1.  
    2. var uvAnimationTileX = 4;
    3. var uvAnimationTileY = 4;
    4. var framesPerSecond = 10.0;
    5.  
    6. function Update ()
    7. {
    8.     // Calculate index
    9.     var index : int = Time.time * framesPerSecond;
    10.     // repeat when exhausting all frames
    11.     // index = index % (uvAnimationTileX * uvAnimationTileY);
    12.    
    13.     // Size of every tile
    14.     var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
    15.    
    16.     // split into horizontal and vertical index
    17.     var uIndex = index % uvAnimationTileX;
    18.     var vIndex = index / uvAnimationTileX;
    19.  
    20.     // build offset
    21.     // v coordinate is the bottom of the image in opengl so we need to invert.
    22.     var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);
    23.    
    24.     renderer.material.SetTextureOffset ("_AlphaTex", offset);
    25.     renderer.material.SetTextureScale ("_AlphaTex", size);
    26. }
    27.  
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Does the shader actually use "_AlphaTex" name for that second texture?
     
  3. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    yep - i would open whatever shader i was working with copy the text directly from that. hold on though - i just tried to throw together an example with the terrain 2-layer this script - it seemed to work fine.

    Code (csharp):
    1. var offset = 0.0;
    2. var offsetSpeed = 0.5;
    3.  
    4. function Update()
    5. {
    6.     offset = Time.time * offsetSpeed;
    7.     renderer.material.SetTextureOffset ("_Mask", Vector2(-offset, 0));
    8. }
    (i was fooling around with stuff for explosivo just remembered i wanted to ask this - but now i have to remember what shader i was using at the time ; )
     
  4. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    i found one example - the XRay shader. trying to animate "_RampTex" doesn't seem to do anything. there was another that i couldn't get going though but i can't seem to find it atm - i was trying all sorts of wacky things a few weeks ago now i've forgotten ; )

    i'm working now but i will look more later if you want make up a package for you to check out.
     
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Well, it does not work in XRay shader because it uses a custom vertex program where it does not apply texture scale&offset (this might be for simplicity or for performance reasons, or both).

    All built-in Unity shaders do apply texture scale&offset, but custom written ones... well, you get what is implemented there, and a vertex shader might apply scale&offset, or it might not.
     
  6. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    ah i see, thanks for the info ; )