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

Where is model space origin?

Discussion in 'Shaders' started by Waliactico, Oct 3, 2014.

  1. Waliactico

    Waliactico

    Joined:
    Jan 16, 2013
    Posts:
    91
    I have a problem with a shader regarding the origin position in model space.
    I want to scale an object in the world z axis (world space), so it looks "squashed" in the same plane orientation no matter which rotation the object has. To do that, I convert the point (0,0,0) from model space to world space, and scale its z related to that point.
    The problem is that the object has its pivot point in its base, so rotation is done from its base, but the scale is applied as if the origin I converted to world space is the object's centroid, so when I rotate it, it displaces in z axis.
    Shouldn't be model space's origin the object's pivot? If this is not like this, is there any way to get the object's pivot inside the shader, without passing it as an external variable?

    Thanks in advance!


    Here is the vertex shader: (only vertex, since it's the only thing relevant)

    Code (CSharp):
    1.  
    2.             v2f vert (appdata_base v)
    3.             {
    4.                 v2f out;
    5.                
    6.                 float4 lOriginWorld = mul(_Object2World, float4(0,0,0,1));
    7.                 out.pos     = mul (_Object2World, v.vertex);
    8.                
    9.                 float3 lDistance = out.pos - lOriginWorld;
    10.                
    11.                 out.pos.x = lOriginWorld.x + lDistance.x;
    12.                 out.pos.y = lOriginWorld.y + lDistance.y;
    13.                 out.pos.z = lOriginWorld.z + lDistance.z * 0.05;
    14.                                            
    15.                 out.pos    = mul(UNITY_MATRIX_VP, out.pos);
    16.                
    17.                 return out;
    18.             }
    19.  
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    "Shouldn't be model space's origin the object's pivot"

    Yes, it is. In Unity that is. For example 3dsmax has a hidden pivot, which is the actual pivot of the object. It's not per se the one you see on your screen. That's just a visual pivot which can have an offset from the actual pivot.

    If all you need is this simple squash, you can do this by scaling the object with a script rather than doing it in the shader. This has the advantage that you can place your own pivot in Unity that works as an override. Just place an empty gameobject _dummy next to the object you want to squash. Move _dummy until it is at the position of the pivot you want for the object. Then drag the object inside _dummy. And finally squash _dummy by a script.
     
  3. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Edit: No, sorry, got the wrong end of the stick. Ignore me :p
     
  4. Waliactico

    Waliactico

    Joined:
    Jan 16, 2013
    Posts:
    91
    @jvo3dc That's a clever workaround, but I'm trying to avoid non-uniform scaling due to its poor performance, so doing it in the vertex shader would better.
    What you say about the real pivot, is there any way to see where that is, or modify it on any extern tool?
     
  5. Waliactico

    Waliactico

    Joined:
    Jan 16, 2013
    Posts:
    91
    bububump
     
  6. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    I don't think non-uniform scaling has poorer performance than uniform scaling. The only issue I know is that you have to transform your normal in a different way when using non-uniform scaling. In either case it actually happens in the vertex shader, since that's where the world matrix is applied.

    This is not really a shader thing, but you can see the real pivot in max using maxscript. $.objectoffsetpos
     
  7. Waliactico

    Waliactico

    Joined:
    Jan 16, 2013
    Posts:
    91
  8. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Ah, I see they do the transformation of the normals on the CPU when scaling non-uniformly. It is actually quite possible to do it on the GPU. The impact on performance is very small in that case.

    The last time I used Maya is more than a decade ago, so I don't know.