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)
v2f vert (appdata_base v)
float4 lOriginWorld = mul(_Object2World, float4(0,0,0,1));
out.pos = mul (_Object2World, v.vertex);
float3 lDistance = out.pos - lOriginWorld;
out.pos.x = lOriginWorld.x + lDistance.x;
out.pos.y = lOriginWorld.y + lDistance.y;
out.pos.z = lOriginWorld.z + lDistance.z * 0.05;
out.pos = mul(UNITY_MATRIX_VP, out.pos);
Ok since this question got already bumped i quickly created a test case in my UA project. I creates a script that will offset a given mesh at runtime by a fix offset:
public class MeshShift : MonoBehaviour
public Vector3 offset;
var mf = GetComponent<MeshFilter>();
var mesh = Instantiate(mf.sharedMesh);
mf.sharedMesh = mesh;
var vertices = mesh.vertices;
for(int i = 0; i < vertices.Length; i++)
vertices *+= offset;*
mesh.vertices = vertices;
This allowed me to quickly “convert” a mesh and move the origin. I simply used a default cube and offset it by 0.5 on the y-axis. That will make the origin of the mesh being at the bottom center of the cube.
I created a new unlit shader and basically copy& paste the vertex shader. I had to adjust a few names as “out” was an invalid variable name. However the actual functionality is exactly the same. When i test that shader on my shifted cube, everything works as it should:
As you can see the origin of the object stays at the same point after a 70° rotation and the scaling is still applied correctly around that pivot.
Note that positive world-z-axis is to the right. So it’s the side view of the cube