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

_Object2World vs UI Canvas?

Discussion in 'Shaders' started by brownboot67, Jul 25, 2015.

  1. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    I have a shader that offsets position based on a sin wave. I am offsetting the phase of the wave based on world position like so:

    Code (csharp):
    1.  
    2. //get "transform" position
    3. float4 centerPos =  mul(_Object2World, float4(0.0,0.0,0.0,1.0));
    4.  
    5. //correct our phase scale property for safe cross-compile... 0,0,0 spits an error
    6. float3 scale3 = float3(_PosPhase+0.0000001, _PosPhase, _PosPhase);
    7.  
    8. //dot world position w/ our scale            
    9. float posPhase = dot(centerPos.xyz,scale3);
    10.  
    11. float wave = sin(_Time.x + posPhase);
    12.  
    13. o.vertex.xyz = (v.vertex.x, v.vertex.y + wave, v.vertex.z);
    14.  
    This works fine on a 3d object. But applying it to either a GameObject with a Transform and SpriteRenderer or a RectTransform with Image centerPos.xyz always equals (0,0,0).

    Our suspicion is the Canvas' transform is the value being returned? But oddly enough driving the phase based on vertex position is fine (albeit not the effect I want), ala:
    Code (csharp):
    1.  
    2. float3 worldPos = mul(_Object2World, v.vertex).xyz;
    3.  
    Any ideas on a solution in shader?