Search Unity

Strange unity_ObjectToWorld behaviour

Discussion in 'Shaders' started by Timurio, Dec 18, 2019.

  1. Timurio

    Timurio

    Joined:
    Jan 28, 2016
    Posts:
    7
    Hello!

    There is some problem with HLSL and unity_ObjectToWorld.
    I'm trying to find sprite render's pivot world space position using:
    Code (CSharp):
    1. objectWorldPos = mul(unity_ObjectToWorld, float4(0, 0, 0, 1));
    But it always return zero vector. When I apply this shader to the 3D object, for example cube it working great and return right world space postion.

    It's ok? Maybe sprite renderers has another way to find it's pivot?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    A sprite mesh is generated at runtime pre-transformed into world space, so the rendered mesh's pivot is at the world origin. If you have multiple sprites all using the same sprite texture / sprite atlas and material they will all be rendered as a single mesh (depending on sorting order) so they do not have individual pivots. Basically, there's no way to get the original Sprite Renderer's transform position.

    There used to be tools that would let you add additional per-vertex data to the sprite mesh which would let you store the pivot in the vertex UVs, but that's been removed. You could have the pivot be a material property and set the position via a c# script for each individual sprite's material, but then each sprite has a unique material and is an additional draw call. You could use instanced quads instead, which do retain their per-object object to world matrix, but they don't work well sprite atlases. Or you could use a particle system to render your sprites and use the custom vertex streams to include the position data, which works with sprite atlases but requires a bit more management on the c# side since you can't place individual particles with out custom c# scripts.
     
    Timurio likes this.
  3. Timurio

    Timurio

    Joined:
    Jan 28, 2016
    Posts:
    7
    Thanks for information!