Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7c13 committed Jul 28, 2023
1 parent f193cca commit 3e5f32b
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions Assets/Scripts/Pal3/Renderer/SkeletalModelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private void UpdateBone(Bone bone, uint tick)
boneGo.transform.localPosition = localPosition;
boneGo.transform.localRotation = localRotation;

Matrix4x4 curPoseToModelMatrix = Matrix4x4.Rotate(localRotation) * Matrix4x4.Translate(localPosition);
Matrix4x4 curPoseToModelMatrix = Matrix4x4.Translate(localPosition) * Matrix4x4.Rotate(localRotation) ;

if (!bone.IsRoot())
{
Expand Down Expand Up @@ -239,7 +239,7 @@ private void RenderBone(BoneNode boneNode, Bone parentBone)
boneGo.transform.SetParent(parentBone == null ? gameObject.transform : parentBone.GameObject.transform);

// Display gizmo
RenderBoneGizmo(boneGo, boneNode);
RenderBoneGizmo(boneGo);

boneGo.transform.localPosition = boneNode.Translation;
boneGo.transform.localRotation = boneNode.Rotation;
Expand All @@ -257,8 +257,8 @@ private void RenderBone(BoneNode boneNode, Bone parentBone)
_bones.Add(boneNode.Id, bone);
var transMatrix = Matrix4x4.Translate(boneNode.Translation);
var rotMatrix = Matrix4x4.Rotate(boneNode.Rotation);
bone.BindPoseModelToBoneSpace = Matrix4x4.Inverse(transMatrix)
* Matrix4x4.Inverse(rotMatrix)
bone.BindPoseModelToBoneSpace = Matrix4x4.Inverse(rotMatrix)
* Matrix4x4.Inverse(transMatrix)
* parentBone.BindPoseModelToBoneSpace;
}

Expand All @@ -270,7 +270,7 @@ private void RenderBone(BoneNode boneNode, Bone parentBone)
}
}

private void RenderBoneGizmo(GameObject boneGo, BoneNode bone)
private void RenderBoneGizmo(GameObject boneGo)
{
var meshFilter = boneGo.AddComponent<MeshFilter>();
var meshRenderer = boneGo.AddComponent<MeshRenderer>();
Expand Down Expand Up @@ -310,7 +310,7 @@ private void RenderSubMesh(MshMesh subMesh, int subMeshIndex)

List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
Vector3[] normals = null; // TODO
Vector3[] normals = null;
List<Vector2> uvs = new List<Vector2>();

_indexBuffer[subMeshIndex] = new List<int>();
Expand Down Expand Up @@ -375,14 +375,13 @@ private Vector3[] BuildVerticesWithCurrentSkeleton(MshMesh subMesh, int subMeshI
int boneId = vert.BoneIds[0];
Bone bone = _bones[boneId];

Vector4 homoPos = new Vector4(vert.Position.x, vert.Position.y, vert.Position.z, 1.0f);
Vector4 posInBone = bone.BindPoseModelToBoneSpace * homoPos;
Vector4 resultPos = bone.CurrentPoseToModelMatrix * posInBone;
Vector4 originalPosition = new Vector4(vert.Position.x, vert.Position.y, vert.Position.z, 1.0f);
Vector4 currentPosition = bone.CurrentPoseToModelMatrix * bone.BindPoseModelToBoneSpace * originalPosition;

_vertexBuffer[subMeshIndex][i] = new Vector3(
resultPos.x/resultPos.w,
resultPos.y/resultPos.w,
resultPos.z/resultPos.w);
currentPosition.x / currentPosition.w,
currentPosition.y / currentPosition.w,
currentPosition.z / currentPosition.w);
}

Vector3[] vertices = _renderMeshComponents[subMeshIndex].Mesh.vertices;
Expand Down

0 comments on commit 3e5f32b

Please sign in to comment.