Hi, I would like to make a matrix which is passed to a shader like “_Projector” matrix. It was not so difficult if localToWorld matrix had no scale. However, if localToWorld matrix had scale, it seemed like the scale parameters were applied twice to the vertices. To check this problem, I tested the following script:
using UnityEngine;
public class ManualTransform : MonoBehaviour {
void OnWillRenderObject()
{
//Matrix4x4 m = renderer.localToWorldMatrix;
Matrix4x4 m = Matrix4x4.identity;
m.SetColumn(3, renderer.localToWorldMatrix.GetColumn(3));
renderer.material.SetMatrix("mL2W", m);
}
}
And draw a sphere with this shader:
Shader "Custom/ManualTransform" {
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4x4 mL2W;
float4 vert(float4 vertex : POSITION) : SV_POSITION
{
return mul(UNITY_MATRIX_VP, mul(mL2W, vertex));
}
fixed4 frag() : COLOR
{
return fixed4(1,0,0,1);
}
ENDCG
}
}
}
Even though, the matrix “mL2W” was identity, the sphere was deformed when I set some scale. It seems like the vertices are already scaled before they are passed to the vertex shader. So, if I use the code in the line comment in OnWillRenderObject, scale parameters will be applied twice.
Here is a workaround:
public class ManualTransform : MonoBehaviour {
void OnWillRenderObject()
{
Vector3 scale = transform.localScale;
Matrix4x4 m = renderer.localToWorldMatrix * Matrix4x4.Scale(new Vector3(1.0f/scale.x, 1.0f/scale.y, 1.0f/scale.z));
renderer.material.SetMatrix("mL2W", m);
}
}
However, it doesn’t look a good idea, because I’m not sure if the vertices are always scaled before vertex shader.
So, my questions are:
- Is it true that vertices of a mesh are scaled before they are passed to a vertex shader?
- If so, does it happen always, or under some conditions? Is there any way to check if the vertices are already scaled or not, like renderer.isPartOfStaticBatch?
- Shouldn’t renderer.localToWorldMatrix have no scale, if vertices are already scaled?
- Is there a better way to make a matrix which contains localToWorld matrix with scale?
It would be appreciated if I could get answers for any of them. Thanks in advance!