Skip to content

Commit

Permalink
Update SkeletalModelRenderer.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7c13 committed Jul 28, 2023
1 parent a62a634 commit 6199307
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions Assets/Scripts/Pal3/Renderer/SkeletalModelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class SkeletalModelRenderer : MonoBehaviour, IDisposable

private readonly Dictionary<int, Bone> _bones = new ();

private GameObject _rootBoneObject;
private GameObject[] _meshObjects;
private RenderMeshComponent[] _renderMeshComponents;

Expand Down Expand Up @@ -252,7 +253,11 @@ private void SetupBone(BoneNode boneNode, Bone parentBone)

Bone bone = new (boneNode.Name, boneGo, boneNode);

if (parentBone != null)
if (parentBone == null)
{
_rootBoneObject = boneGo;
}
else
{
_bones.Add(boneNode.Id, bone);
Matrix4x4 translationMatrix = Matrix4x4.Translate(boneNode.Translation);
Expand Down Expand Up @@ -323,18 +328,23 @@ private void RenderSubMesh(MshMesh subMesh, int subMeshIndex)

Vector3[] verts = vertices.ToArray();
int[] tris = triangles.ToArray();
Vector2[] uvsArray = uvs.ToArray();
Vector2[] uvs1 = uvs.ToArray();
Vector2[] uvs2 = null;

var meshRenderer = _meshObjects[subMeshIndex].AddComponent<StaticMeshRenderer>();
Mesh renderMesh = meshRenderer.Render(
ref verts,
ref tris,
ref normals,
ref uvsArray,
ref uvsArray,
ref uvs1,
ref uvs2,
ref materials,
true);

renderMesh.RecalculateNormals();
renderMesh.RecalculateTangents();
renderMesh.RecalculateBounds();

_renderMeshComponents[subMeshIndex] = new RenderMeshComponent
{
Mesh = renderMesh,
Expand Down Expand Up @@ -448,6 +458,12 @@ public void Dispose()
_bones.Clear();
}

if (_rootBoneObject != null)
{
Destroy(_rootBoneObject);
_rootBoneObject = null;
}

_indexBuffer.Clear();
_vertexBuffer.Clear();
}
Expand Down

0 comments on commit 6199307

Please sign in to comment.