I don’t know why the rotation I calculated is incorrect.
The two GameObjects in the image below have the same rotation.
The GameObject on the left uses Unity’s Transfrom, and the GameObject on the right uses my own calculated rotation matrix.
Reproduce it
- git clone GitHub - huangwwmm/Unity-Meteorites
- open Scene “Assets_Test\Test.unity”
- Please re-enable SphereDisperseMesh component of GameObject Meteorites. A if it is not displayed correctly.
Related code
Matrix calculation in SphereDisperseMesh.compute
float4x4 mat_local_s = {
s.x, 0, 0, 0,
0, s.y, 0, 0,
0, 0, s.z, 0,
0, 0, 0, 1
};
// rotation z
float4x4 mat_local_rz = {
cos(r.z), sin(r.z), 0, 0,
- sin(r.z), cos(r.z), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
// rotation x
float4x4 mat_local_rx = {
1, 0, 0, 0,
0, cos(r.x), -sin(r.x), 0,
0, sin(r.x), cos(r.x), 0,
0, 0, 0, 1
};
// rotation y
float4x4 mat_local_ry = {
cos(r.y), 0, sin(r.y), 0,
0, 1, 0, 0,
- sin(r.y), 0, cos(r.y), 0,
0, 0, 0, 1
};
// position
float4x4 mat_local_t = {
1, 0, 0, t.x,
0, 1, 0, t.y,
0, 0, 1, t.z,
0, 0, 0, 1,
};
float4x4 mat_local_tr = mul(mat_local_t, mul(mat_local_ry, mul(mat_local_rx, mat_local_rz)));
float4x4 mat_local_trs = mul(mat_local_t, mul(mat_local_ry, mul(mat_local_rx, mul(mat_local_rz, mat_local_s))));
_MeshStates[threadIdx].MatM = mul(_GlobalState[0].MatM, mat_local_tr);
_MeshStates[threadIdx].MatMVP = mul(_GlobalState[0].MatMVP, mat_local_trs);
The rotation of the right GameObject is in StartRendering() of SphereDisperseMesh.cs
m_MeshStates[iRole].LocalPosition = Vector3.zero;
m_MeshStates[iRole].LocalRotation = new Vector3(0, 0, 30);
m_MeshStates[iRole].LocalScale = Vector3.one;