Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Scale from object's center

Discussion in 'Shaders' started by Phantomx, Dec 2, 2015.

  1. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Hi,
    I'm trying to apply a scale on an object in the vertex shader. At the moment the scale takes the origin as the scale pivot. how do you scale from the object's center or pivot instead?
    (this is for 2D not 3D )

    my code for the scale at the moment:

    Code (CSharp):
    1. half4x4 resizeMatrix = UNITY_MATRIX_MVP;
    2. resizeMatrix[0][0] *=  _ScaleSize;
    3. resizeMatrix[1][1] *=  _ScaleSize;
    4.  
    5. OUT.vertex = mul(resizeMatrix, IN.vertex);
    thanks!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,171
    OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex * _SizeScale);

    The IN.vertex should be from the center of the mesh that's being scaled. If it's still scaling from a different location it means that it's part of a batch, in which case the answer is "you can't", or rather it needs to not be batched.

    Since you said "in 2D", I assume you're using sprites in which case the answer might be "you can't, do it in code."
     
  3. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Yes I'm using sprites, and I managed to do it after all.

    Code (CSharp):
    1. float4x4 mvp = mul(UNITY_MATRIX_VP,_Object2World);
    2. mvp = mul(mvp,_ScaleMatrix);
    3.                
    4. OUT.vertex = mul(mvp, IN.vertex);
    And I use some C# to create my _ScaleMatrix.
     
unityunity