From e9c94de242cd0c341288d0e95a9bf353775bfca1 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 23 Apr 2021 17:40:23 +0900 Subject: [PATCH] export position constraint --- .../Constraint/ConstraintDestination.cs | 6 +- .../Constraint/VRM10AimConstraint.cs | 6 +- .../Constraint/VRM10PositionConstraint.cs | 2 +- .../VRM10/Runtime/IO/RuntimeVrmConverter.cs | 5 +- Assets/VRM10/Runtime/IO/Vrm10Exporter.cs | 62 +++++++++++++------ 5 files changed, 57 insertions(+), 24 deletions(-) diff --git a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs index 505c387d8..21cf3b0f2 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/ConstraintDestination.cs @@ -35,7 +35,7 @@ namespace UniVRM10 } } - public void ApplyTranslation(Vector3 delta, float weight) + public void ApplyTranslation(Vector3 delta, float weight, Transform modelRoot = null) { var value = m_initial.Translation + delta * weight; switch (m_coords) @@ -48,6 +48,10 @@ namespace UniVRM10 m_transform.localPosition = value; break; + case ObjectSpace.model: + m_transform.position = modelRoot.localToWorldMatrix.MultiplyPoint(value); + break; + default: throw new NotImplementedException(); } diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs index 5f2529c48..eba3d3f5b 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10AimConstraint.cs @@ -16,9 +16,9 @@ namespace UniVRM10 [SerializeField] public Transform Source = default; - // [SerializeField] - // [Range(0, 10.0f)] - // float Weight = 1.0f; + [SerializeField] + [Range(0, 10.0f)] + public float Weight = 1.0f; /// /// Forward diff --git a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs index a8b9e1b6d..103cbb535 100644 --- a/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs +++ b/Assets/VRM10/Runtime/Components/Constraint/VRM10PositionConstraint.cs @@ -70,7 +70,7 @@ namespace UniVRM10 } var delta = FreezeAxes.Freeze(m_src.TranslationDelta); - m_dst.ApplyTranslation(delta, Weight); + m_dst.ApplyTranslation(delta, Weight, ModelRoot); } } } diff --git a/Assets/VRM10/Runtime/IO/RuntimeVrmConverter.cs b/Assets/VRM10/Runtime/IO/RuntimeVrmConverter.cs index a1cc905f2..afe1b3369 100644 --- a/Assets/VRM10/Runtime/IO/RuntimeVrmConverter.cs +++ b/Assets/VRM10/Runtime/IO/RuntimeVrmConverter.cs @@ -112,7 +112,10 @@ namespace UniVRM10 var mesh = CreateMesh(filter.sharedMesh, meshRenderer, Materials); Model.MeshGroups.Add(mesh); Nodes[renderer.gameObject].MeshGroup = mesh; - Meshes.Add(filter.sharedMesh, mesh); + if (!Meshes.ContainsKey(filter.sharedMesh)) + { + Meshes.Add(filter.sharedMesh, mesh); + } } } } diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs index 272f52dfc..8b308a8da 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs @@ -358,29 +358,31 @@ namespace UniVRM10 var constraints = vrmController.GetComponentsInChildren(); foreach (var constraint in constraints) { + UniGLTF.Extensions.VRMC_node_constraint.VRMC_node_constraint vrmConstraint = default; switch (constraint) { case VRM10PositionConstraint positionConstraint: - ExportPostionConstraint(positionConstraint, model, converter); + vrmConstraint = ExportPostionConstraint(positionConstraint, model, converter); break; case VRM10RotationConstraint rotationConstraint: - ExportRotationConstraint(rotationConstraint, model, converter); + vrmConstraint = ExportRotationConstraint(rotationConstraint, model, converter); break; case VRM10AimConstraint aimConstraint: - ExportAimConstraint(aimConstraint, model, converter); + vrmConstraint = ExportAimConstraint(aimConstraint, model, converter); break; default: throw new NotImplementedException(); } - } - } - void ExportPostionConstraint(VRM10PositionConstraint c, Model model, RuntimeVrmConverter converter) - { - throw new NotImplementedException(); + // serialize to gltfNode + var node = converter.Nodes[constraint.gameObject]; + var nodeIndex = model.Nodes.IndexOf(node); + var gltfNode = Storage.Gltf.nodes[nodeIndex]; + UniGLTF.Extensions.VRMC_node_constraint.GltfSerializer.SerializeTo(ref gltfNode.extensions, vrmConstraint); + } } static bool[] ToArray(AxisMask mask) @@ -393,9 +395,27 @@ namespace UniVRM10 }; } - void ExportRotationConstraint(VRM10RotationConstraint c, Model model, RuntimeVrmConverter converter) + static UniGLTF.Extensions.VRMC_node_constraint.VRMC_node_constraint ExportPostionConstraint(VRM10PositionConstraint c, Model model, RuntimeVrmConverter converter) { - var vrmConstraint = new UniGLTF.Extensions.VRMC_node_constraint.VRMC_node_constraint + return new UniGLTF.Extensions.VRMC_node_constraint.VRMC_node_constraint + { + Constraint = new UniGLTF.Extensions.VRMC_node_constraint.Constraint + { + Position = new UniGLTF.Extensions.VRMC_node_constraint.PositionConstraint + { + Source = model.Nodes.IndexOf(converter.Nodes[c.Source.gameObject]), + SourceSpace = c.SourceCoordinate, + DestinationSpace = c.DestinationCoordinate, + FreezeAxes = ToArray(c.FreezeAxes), + Weight = c.Weight, + } + }, + }; + } + + static UniGLTF.Extensions.VRMC_node_constraint.VRMC_node_constraint ExportRotationConstraint(VRM10RotationConstraint c, Model model, RuntimeVrmConverter converter) + { + return new UniGLTF.Extensions.VRMC_node_constraint.VRMC_node_constraint { Constraint = new UniGLTF.Extensions.VRMC_node_constraint.Constraint { @@ -409,17 +429,23 @@ namespace UniVRM10 }, }, }; - - // serialize to gltfNode - var node = converter.Nodes[c.gameObject]; - var nodeIndex = model.Nodes.IndexOf(node); - var gltfNode = Storage.Gltf.nodes[nodeIndex]; - UniGLTF.Extensions.VRMC_node_constraint.GltfSerializer.SerializeTo(ref gltfNode.extensions, vrmConstraint); } - static void ExportAimConstraint(VRM10AimConstraint c, Model model, RuntimeVrmConverter converter) + static UniGLTF.Extensions.VRMC_node_constraint.VRMC_node_constraint ExportAimConstraint(VRM10AimConstraint c, Model model, RuntimeVrmConverter converter) { - throw new NotImplementedException(); + return new UniGLTF.Extensions.VRMC_node_constraint.VRMC_node_constraint + { + Constraint = new UniGLTF.Extensions.VRMC_node_constraint.Constraint + { + Aim = new UniGLTF.Extensions.VRMC_node_constraint.AimConstraint + { + Source = model.Nodes.IndexOf(converter.Nodes[c.Source.gameObject]), + AimVector = ReverseX(c.AimVector), + UpVector = ReverseX(c.UpVector), + Weight = c.Weight, + }, + }, + }; } static UniGLTF.Extensions.VRMC_vrm.MeshAnnotation ExportMeshAnnotation(RendererFirstPersonFlags flags, Func getIndex)