From a8039d46c87858bdbb8067d634b41776a536006d Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 30 Mar 2021 15:16:19 +0900 Subject: [PATCH] WIP LoadVrm --- Assets/VRM10/Runtime/IO/ModelLoader.cs | 36 ---- Assets/VRM10/Runtime/IO/Vrm10Exporter.cs | 5 - .../Runtime/IO/Vrm10ExporterExtensions.cs | 3 - Assets/VRM10/Runtime/IO/Vrm10Storage.cs | 197 +----------------- 4 files changed, 1 insertion(+), 240 deletions(-) diff --git a/Assets/VRM10/Runtime/IO/ModelLoader.cs b/Assets/VRM10/Runtime/IO/ModelLoader.cs index 997567fb4..a7bf1c4ab 100644 --- a/Assets/VRM10/Runtime/IO/ModelLoader.cs +++ b/Assets/VRM10/Runtime/IO/ModelLoader.cs @@ -77,43 +77,7 @@ namespace UniVRM10 } } - // animation - model.Animations.AddRange(Enumerable.Range(0, storage.AnimationCount).Select(x => storage.CreateAnimation(x, model.Nodes))); - - // VRM - LoadVrm(model, storage); - return model; } - - static bool LoadVrm(Model model, Vrm10Storage storage) - { - if (!storage.HasVrm) - { - return false; - } - - var meta = storage.CreateVrmMeta(model.Textures); - - var Vrm = new Vrm(meta, storage.VrmExporterVersion, storage.VrmSpecVersion); - model.Vrm = Vrm; - - storage.LoadVrmHumanoid(model.Nodes); - - if (!model.CheckVrmHumanoid()) - { - throw new Exception("CheckVrmHumanoid"); - } - - Vrm.ExpressionManager = storage.CreateVrmExpression(model.MeshGroups, model.Materials, model.Nodes); - - Vrm.SpringBone = storage.CreateVrmSpringBone(model.Nodes); - - Vrm.FirstPerson = storage.CreateVrmFirstPerson(model.Nodes, model.MeshGroups); - - Vrm.LookAt = storage.CreateVrmLookAt(); - - return true; - } } } diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs index 2dd2bfde8..7bce4fde5 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs @@ -184,10 +184,5 @@ namespace UniVRM10 nodes = root.Children.Select(child => nodes.IndexOfThrow(child)).ToArray() }); } - - public void ExportAnimations(List animations, List nodes, ExportArgs option) - { - // throw new System.NotImplementedException(); - } } } diff --git a/Assets/VRM10/Runtime/IO/Vrm10ExporterExtensions.cs b/Assets/VRM10/Runtime/IO/Vrm10ExporterExtensions.cs index ec647e37d..cdbbbee21 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10ExporterExtensions.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10ExporterExtensions.cs @@ -73,9 +73,6 @@ namespace UniVRM10 // node exporter.ExportNodes(m.Root, m.Nodes, m.MeshGroups, option); - // animation - exporter.ExportAnimations(m.Animations, m.Nodes, option); - return exporter.ToBytes(); } } diff --git a/Assets/VRM10/Runtime/IO/Vrm10Storage.cs b/Assets/VRM10/Runtime/IO/Vrm10Storage.cs index a28127e64..1b547f323 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Storage.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Storage.cs @@ -383,15 +383,6 @@ namespace UniVRM10 public int MeshCount => Gltf.meshes.Count; - // TODO: - public int AnimationCount => 0; - - public string VrmExporterVersion => Gltf.asset.generator; - - public bool HasVrm => gltfVrm != null; - - public string VrmSpecVersion => gltfVrm?.SpecVersion; - public Node CreateNode(int index) { var x = Gltf.nodes[index]; @@ -595,197 +586,11 @@ namespace UniVRM10 return (meshIndex, skinIndex); } - public Animation CreateAnimation(int index, List nodes) - { - throw new NotImplementedException(); - } - public Meta CreateVrmMeta(List textures) { return gltfVrm.Meta.FromGltf(textures); } - static void AssignHumanoid(List nodes, UniGLTF.Extensions.VRMC_vrm.HumanBone humanBone, VrmLib.HumanoidBones key) - { - if (humanBone != null && humanBone.Node.HasValue) - { - nodes[humanBone.Node.Value].HumanoidBone = key; - } - } - - public void LoadVrmHumanoid(List nodes) - { - if (gltfVrm.Humanoid != null) - { - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.Hips, HumanoidBones.hips); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftUpperLeg, HumanoidBones.leftUpperLeg); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightUpperLeg, HumanoidBones.rightUpperLeg); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftLowerLeg, HumanoidBones.leftLowerLeg); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightLowerLeg, HumanoidBones.rightLowerLeg); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftFoot, HumanoidBones.leftFoot); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightFoot, HumanoidBones.rightFoot); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.Spine, HumanoidBones.spine); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.Chest, HumanoidBones.chest); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.Neck, HumanoidBones.neck); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.Head, HumanoidBones.head); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftShoulder, HumanoidBones.leftShoulder); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightShoulder, HumanoidBones.rightShoulder); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftUpperArm, HumanoidBones.leftUpperArm); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightUpperArm, HumanoidBones.rightUpperArm); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftLowerArm, HumanoidBones.leftLowerArm); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightLowerArm, HumanoidBones.rightLowerArm); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftHand, HumanoidBones.leftHand); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightHand, HumanoidBones.rightHand); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftToes, HumanoidBones.leftToes); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightToes, HumanoidBones.rightToes); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftEye, HumanoidBones.leftEye); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightEye, HumanoidBones.rightEye); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.Jaw, HumanoidBones.jaw); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftThumbProximal, HumanoidBones.leftThumbProximal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftThumbIntermediate, HumanoidBones.leftThumbIntermediate); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftThumbDistal, HumanoidBones.leftThumbDistal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftIndexProximal, HumanoidBones.leftIndexProximal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftIndexIntermediate, HumanoidBones.leftIndexIntermediate); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftIndexDistal, HumanoidBones.leftIndexDistal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftMiddleProximal, HumanoidBones.leftMiddleProximal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftMiddleIntermediate, HumanoidBones.leftMiddleIntermediate); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftMiddleDistal, HumanoidBones.leftMiddleDistal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftRingProximal, HumanoidBones.leftRingProximal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftRingIntermediate, HumanoidBones.leftRingIntermediate); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftRingDistal, HumanoidBones.leftRingDistal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftLittleProximal, HumanoidBones.leftLittleProximal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftLittleIntermediate, HumanoidBones.leftLittleIntermediate); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.LeftLittleDistal, HumanoidBones.leftLittleDistal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightThumbProximal, HumanoidBones.rightThumbProximal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightThumbIntermediate, HumanoidBones.rightThumbIntermediate); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightThumbDistal, HumanoidBones.rightThumbDistal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightIndexProximal, HumanoidBones.rightIndexProximal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightIndexIntermediate, HumanoidBones.rightIndexIntermediate); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightIndexDistal, HumanoidBones.rightIndexDistal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightMiddleProximal, HumanoidBones.rightMiddleProximal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightMiddleIntermediate, HumanoidBones.rightMiddleIntermediate); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightMiddleDistal, HumanoidBones.rightMiddleDistal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightRingProximal, HumanoidBones.rightRingProximal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightRingIntermediate, HumanoidBones.rightRingIntermediate); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightRingDistal, HumanoidBones.rightRingDistal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightLittleProximal, HumanoidBones.rightLittleProximal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightLittleIntermediate, HumanoidBones.rightLittleIntermediate); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.RightLittleDistal, HumanoidBones.rightLittleDistal); - AssignHumanoid(nodes, gltfVrm.Humanoid.HumanBones.UpperChest, HumanoidBones.upperChest); - } - } - - public ExpressionManager CreateVrmExpression(List _, List materials, List nodes) - { - if (gltfVrm.Expressions != null) - { - var expressionManager = new ExpressionManager(); - foreach (var x in gltfVrm.Expressions) - { - expressionManager.ExpressionList.Add(x.FromGltf(nodes, materials)); - } - return expressionManager; - } - - return null; - } - - static VrmSpringBoneCollider CreateCollider(UniGLTF.Extensions.VRMC_node_collider.ColliderShape z) - { - if (z.Sphere != null) - { - return VrmSpringBoneCollider.CreateSphere(z.Sphere.Offset.ToVector3(), z.Sphere.Radius.Value); - } - if (z.Capsule != null) - { - return VrmSpringBoneCollider.CreateCapsule(z.Capsule.Offset.ToVector3(), z.Capsule.Radius.Value, z.Capsule.Tail.ToVector3()); - } - throw new NotImplementedException(); - } - - public SpringBoneManager CreateVrmSpringBone(List nodes) - { - if ((gltfVrmSpringBone is null)) - { - return null; - } - - var springBoneManager = new SpringBoneManager(); - - // springs - if (gltfVrmSpringBone.Springs != null) - { - foreach (var gltfSpring in gltfVrmSpringBone.Springs) - { - var springBone = new SpringBone(); - springBone.Comment = gltfSpring.Name; - - // joint - foreach (var gltfJoint in gltfSpring.Joints) - { - var joint = new SpringJoint(nodes[gltfJoint.Node.Value]); - joint.HitRadius = gltfJoint.HitRadius.Value; - joint.DragForce = gltfJoint.DragForce.Value; - joint.GravityDir = gltfJoint.GravityDir.ToVector3(); - joint.GravityPower = gltfJoint.GravityPower.Value; - joint.Stiffness = gltfJoint.Stiffness.Value; - springBone.Joints.Add(joint); - } - - // collider - springBone.Colliders.AddRange(gltfSpring.Colliders.Select(colliderNode => - { - if (UniGLTF.Extensions.VRMC_node_collider.GltfDeserializer.TryGet(Gltf.nodes[colliderNode].extensions, - out UniGLTF.Extensions.VRMC_node_collider.VRMC_node_collider extension)) - { - var collider = new SpringBoneColliderGroup(nodes[colliderNode], extension.Shapes.Select(x => - { - if (x.Sphere != null) - { - return VrmSpringBoneCollider.CreateSphere(x.Sphere.Offset.ToVector3(), x.Sphere.Radius.Value); - } - else if (x.Capsule != null) - { - return VrmSpringBoneCollider.CreateCapsule(x.Capsule.Offset.ToVector3(), x.Capsule.Radius.Value, x.Capsule.Tail.ToVector3()); - } - else - { - throw new NotImplementedException(); - } - })); - return collider; - } - else - { - return null; - } - }).Where(x => x != null)); - - springBoneManager.Springs.Add(springBone); - } - } - - return springBoneManager; - } - - public FirstPerson CreateVrmFirstPerson(List nodes, List meshGroups) - { - if (gltfVrm.FirstPerson == null) - { - return null; - } - return gltfVrm.FirstPerson.FromGltf(nodes); - } - - public LookAt CreateVrmLookAt() - { - if (gltfVrm.LookAt == null) - { - return null; - } - return gltfVrm.LookAt.FromGltf(); - } - public ArraySegment GetBufferBytes(UniGLTF.glTFBufferView bufferView) { if (!bufferView.buffer.TryGetValidIndex(Gltf.buffers.Count, out int bufferViewBufferIndex)) @@ -801,4 +606,4 @@ namespace UniVRM10 return Buffers[index].Bytes; } } -} \ No newline at end of file +}