From 02ea689dc54ee659c8a0ab998718a2c82892320f Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 17 Jun 2022 17:15:58 +0900 Subject: [PATCH] =?UTF-8?q?BoneWeight=E3=81=AE=E4=BB=98=E4=B8=8E=E3=82=92?= =?UTF-8?q?=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs | 3 +++ .../Runtime/UniGLTF/IO/MeshIO/MeshUploader.cs | 3 ++- .../Runtime/UniGLTF/IO/MeshIO/MeshWithMaterials.cs | 8 +++++++- .../Runtime/UniGLTF/IO/MeshIO/SkinningInfo.cs | 3 +-- Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs | 14 ++++++++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs index 683b63c42..0a150b360 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs @@ -30,6 +30,7 @@ namespace UniGLTF public bool HasNormal { get; private set; } = true; public string Name { get; private set; } + public bool AssignBoneWeight { get; private set; } public MeshData(int vertexCapacity, int indexCapacity) { @@ -318,6 +319,7 @@ namespace UniGLTF var texCoords1 = primitives.GetTexCoords1(data, positions.Length); var colors = primitives.GetColors(data, positions.Length); var skinning = SkinningInfo.Create(data, gltfMesh, primitives); + AssignBoneWeight = skinning.AssignBoneWeight; CheckAttributeUsages(primitives); @@ -449,6 +451,7 @@ namespace UniGLTF var texCoords1 = primitives.GetTexCoords1(data, positions.Length); var colors = primitives.GetColors(data, positions.Length); var skinning = SkinningInfo.Create(data, gltfMesh, primitives); + AssignBoneWeight = skinning.AssignBoneWeight; CheckAttributeUsages(primitives); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshUploader.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshUploader.cs index adfb0c100..906d71e47 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshUploader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshUploader.cs @@ -137,7 +137,8 @@ namespace UniGLTF var result = new MeshWithMaterials { Mesh = mesh, - Materials = data.MaterialIndices.Select(materialFromIndex).ToArray() + Materials = data.MaterialIndices.Select(materialFromIndex).ToArray(), + AssignBoneWeight = data.AssignBoneWeight, }; await awaitCaller.NextFrame(); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshWithMaterials.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshWithMaterials.cs index 42c2e11d4..200bf6ae7 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshWithMaterials.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshWithMaterials.cs @@ -9,7 +9,13 @@ namespace UniGLTF public Mesh Mesh; public Material[] Materials; + /// + /// BoneWeight が無い && BlendShape が有るの場合に、BoneWeightを付与する。 + /// 付与した場合に true になる。 + /// + public bool AssignBoneWeight = false; + // 複数のノードから参照されうる - public List Renderers=new List(); // SkinnedMeshRenderer or MeshRenderer + public List Renderers = new List(); // SkinnedMeshRenderer or MeshRenderer } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinningInfo.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinningInfo.cs index 640551fb1..9737779cc 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinningInfo.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/SkinningInfo.cs @@ -8,8 +8,7 @@ namespace UniGLTF public static SkinningInfo Create(GltfData data, glTFMesh mesh, glTFPrimitives primitives) { - // var hasMorphTarget = HasMorphTarget(mesh); - var hasMorphTarget = false; + var hasMorphTarget = HasMorphTarget(mesh); var positions = data.GLTF.accessors[primitives.attributes.POSITION]; var skinning = new SkinningInfo diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs index 48f646e2e..b40436ae7 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/NodeImporter.cs @@ -119,6 +119,20 @@ namespace UniGLTF renderer.sharedMaterials = mesh.Materials; // invisible in loading renderer.enabled = false; + + if (mesh.AssignBoneWeight) + { + renderer.bones = new[] { renderer.transform }; + + // + // calc default matrices + // https://docs.unity3d.com/ScriptReference/Mesh-bindposes.html + // + var meshCoords = renderer.transform; + var calculatedBindPoses = renderer.bones.Select(bone => bone.worldToLocalMatrix * meshCoords.localToWorldMatrix).ToArray(); + mesh.Mesh.bindposes = calculatedBindPoses; + } + mesh.Renderers.Add(renderer); } }