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

Wave simulation - normal-space to world-space problem

Discussion in 'Shaders' started by Quatum1000, Mar 28, 2015.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Hi everyone,

    I want to display a "wave flow" on a simple diffuse map that should move always in the same direction. Doens't matter of rotating the object. On the surf shader I use a standard normal map. The UV's offset are simply moved by a script.

    Now the "wave" moves along the local space rotation of the object. But this is the wrong behavior for this effect.

    I use a surf shader to add this effect to o.specular too.

    Code (CSharp):
    1.  
    2. struct Input {
    3.   half2 uv_BumpMap;
    4. }
    5.  
    6. void surf(.....)  {
    7.  
    8. ...
    9. fixed4 BumpMap  = tex2D (_BumpMap, IN.uv_BumpMap);
    10. fixed3 n          = UnpackNormal(BumpMap);
    11. ...
    12.  
    13. }
    14.  
    15.  
    Is it possible translate the local space into world space coordinates? (Before accessing the tex2D() of the bump map)
    Or is there any other solution on the normal map to display the flow into the same direction?

    Untitled-1.jpg
     
    Last edited: Mar 28, 2015
  2. echo4papa

    echo4papa

    Joined:
    Mar 26, 2015
    Posts:
    158
    _Object2World matrix is in the UnityCG.cginc

    However, you'll probably want to covert your UVs in a vertex program. Convert to world space perform your transform(wave movement) then convert back to object space and let your surface shader operate as normal.

    Code (CSharp):
    1. float4 worldUV = mul(v.texcoord0, _Object2World);
    2. //do stuff to worldUV
    3. float4 finalUV = mul(worldUV,_World2Object);
    4. o.texcoord0 = finalUV;
    I think that will work(on my phone at the moment).