Пример #1
0
            internal PhysicsObject ImportPhysics([NotNull] PmxBone[] bones, [NotNull] SwayController bodySway, [NotNull] SwayController headSway)
            {
                var bodies = new List <PmxRigidBody>();

                var swayColliders = new List <SwayCollider>();

                swayColliders.AddRange(bodySway.Colliders);
                swayColliders.AddRange(headSway.Colliders);

                var swayColliderArray = swayColliders.ToArray();

                AppendStaticBodies(bones, swayColliderArray, bodies);

                var swayBones = new List <SwayBone>();

                swayBones.AddRange(bodySway.SwayBones);
                swayBones.AddRange(headSway.SwayBones);

                var swayBoneArray = swayBones.ToArray();

                AppendSwayBones(bones, swayBoneArray, bodies);

                // TODO: fix rigid body rotation (with bone, e.g. the bones of arms)
                if (_pmxCreator._conversionConfig.FixTdaBindingPose)
                {
                    Trace.TraceWarning("Rigid body rotation correction for TDA pose has not been implemented yet!");
                }

                var joints = new List <PmxJoint>();

                AppendJoints(bones, bodies, joints);

                return(new PhysicsObject(bodies.ToArray(), joints.ToArray()));
            }
Пример #2
0
 internal ModelLoadResult([NotNull] GameObject head, [NotNull] GameObject body, [NotNull] SwayController headSway, [NotNull] SwayController bodySway)
 {
     Head     = head;
     Body     = body;
     HeadSway = headSway;
     BodySway = bodySway;
 }
Пример #3
0
        private void AddSwayColliders([NotNull] GameObject root, [NotNull] SwayController controller, [NotNull] Dictionary <string, GameObject> cache)
        {
            foreach (var swayCollider in controller.Colliders)
            {
                switch (swayCollider.Type)
                {
                case ColliderType.Sphere:
                case ColliderType.Capsule:
                    break;

                case ColliderType.Plane: {
                    // Not supported for now
                    continue;
                }

                case ColliderType.Bridge: {
                    // ???
                    continue;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }

                var colliderName = BreakPath(swayCollider.Path);
                var gameObject   = SearchObjectByName(root, colliderName, cache);
                Debug.Assert(gameObject != null);

                switch (swayCollider.Type)
                {
                case ColliderType.Sphere: {
                    var collider = gameObject.AddComponent <SphereCollider>();
                    collider.radius   = swayCollider.Radius;
                    collider.center   = swayCollider.Offset;
                    collider.material = defaultPhysicMaterial;

                    break;
                }

                case ColliderType.Capsule: {
                    var collider = gameObject.AddComponent <CapsuleCollider>();
                    collider.radius = swayCollider.Radius;
                    collider.center = swayCollider.Offset;
                    collider.height = swayCollider.Distance;
                    // https://docs.unity3d.com/ScriptReference/CapsuleCollider-direction.html
                    collider.direction = (int)swayCollider.Axis - 1;
                    collider.material  = defaultPhysicMaterial;

                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
Пример #4
0
        public static (SwayController Body, SwayController Head) LoadSwayControllers([NotNull] string bodyFilePath, [NotNull] string headFilePath)
        {
            SwayController LoadSwayController(string filePath)
            {
                SwayController result = null;

                using (var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    using (var bundle = new BundleFile(fileStream, false)) {
                        foreach (var assetFile in bundle.AssetFiles)
                        {
                            foreach (var preloadData in assetFile.PreloadDataList)
                            {
                                if (preloadData.KnownType != KnownClassID.TextAsset)
                                {
                                    continue;
                                }

                                var textAsset = preloadData.LoadAsTextAsset(true);

                                if (!textAsset.Name.EndsWith("_sway"))
                                {
                                    continue;
                                }

                                textAsset = preloadData.LoadAsTextAsset(false);

                                var text = textAsset.GetString();

                                Debug.Assert(!string.IsNullOrWhiteSpace(text));

                                result = SwayController.Parse(text);

                                break;
                            }
                        }
                    }
                }

                return(result);
            }

            var body = LoadSwayController(bodyFilePath);
            var head = LoadSwayController(headFilePath);

            SwayController.FixSwayReferences(body, head);

            return(Body : body, Head : head);
        }
Пример #5
0
        private void AddSwayBones([NotNull] GameObject root, [NotNull] SwayController controller, [NotNull] Dictionary <string, GameObject> cache)
        {
            foreach (var swayManager in controller.Managers)
            {
                foreach (var bone in swayManager.RootBones)
                {
                    AddSwayBone(root, bone, cache);
                }
            }

            foreach (var swayManager in controller.Managers)
            {
                foreach (var bone in swayManager.RootBones)
                {
                    AddBoneLinks(root, bone, cache);
                }
            }
        }
Пример #6
0
        private static SwayController LoadSwayController([NotNull] string filePath)
        {
            SwayController result = null;

            const string swayEnds = "_sway";

            var manager = new AssetsManager();

            manager.LoadFiles(filePath);

            foreach (var assetFile in manager.assetsFileList)
            {
                foreach (var obj in assetFile.Objects)
                {
                    if (obj.type != ClassIDType.TextAsset)
                    {
                        continue;
                    }

                    var textAsset = obj as TextAsset;

                    if (textAsset == null)
                    {
                        throw new ArgumentNullException(nameof(textAsset), "Sway data is null.");
                    }

                    if (!textAsset.m_Name.EndsWith(swayEnds))
                    {
                        continue;
                    }

                    var raw = textAsset.m_Script;
                    var str = Encoding.UTF8.GetString(raw);

                    str = ReplaceNewLine.Replace(str, "\n");

                    result = SwayAsset.Parse(str);

                    break;
                }
            }

            return(result);
        }
Пример #7
0
 public PmxModel CreateModel([NotNull] CompositeAvatar combinedAvatar, [NotNull] CompositeMesh combinedMesh, int bodyMeshVertexCount,
                             [NotNull] SwayController bodySway, [NotNull] SwayController headSway,
                             [NotNull] ConversionDetails details, out (string FileName, TexturedMaterial Material)[] materialList)
Пример #8
0
        public PmxModel CreateFrom([NotNull] Avatar combinedAvatar, [NotNull] Mesh combinedMesh, int bodyMeshVertexCount, [NotNull] string texturePrefix,
                                   [NotNull] SwayController bodySway, [NotNull] SwayController headSway)
        {
            var model = new PmxModel();

            model.Name           = "ミリシタ モデル00";
            model.NameEnglish    = "MODEL_00";
            model.Comment        = "製作:MillionDance" + Environment.NewLine + "©BANDAI NAMCO Entertainment Inc.";
            model.CommentEnglish = "Generated by MillionDance" + Environment.NewLine + "©BANDAI NAMCO Entertainment Inc.";

            var vertices = AddVertices(combinedAvatar, combinedMesh, bodyMeshVertexCount);

            model.Vertices = vertices;

            var indicies = AddIndices(combinedMesh);

            model.FaceTriangles = indicies;

            var bones = AddBones(combinedAvatar, combinedMesh, vertices);

            model.Bones = bones;

            if (ConversionConfig.Current.FixTdaBindingPose)
            {
                if (ConversionConfig.Current.SkeletonFormat == SkeletonFormat.Mmd)
                {
                    if (ConversionConfig.Current.TranslateBoneNamesToMmd)
                    {
                        FixTdaBonesAndVertices(bones, vertices);
                    }
                }
                else if (ConversionConfig.Current.SkeletonFormat == SkeletonFormat.Mltd)
                {
                }
                else
                {
                    throw new NotSupportedException("You must choose a motion source to determine skeleton format.");
                }
            }

            var materials = AddMaterials(combinedMesh, texturePrefix);

            model.Materials = materials;

            var emotionMorphs = AddEmotionMorphs(combinedMesh);

            model.Morphs = emotionMorphs;

            // PMX Editor requires at least one node (root), or it will crash because these code:

            /**
             * this.PXRootNode = new PXNode(base.RootNode);
             * this.PXExpressionNode = new PXNode(base.ExpNode);
             * this.PXNode.Clear();
             * this.PXNode.Capacity = base.NodeList.Count - 1; // notice this line
             */
            var nodes = AddNodes(bones, emotionMorphs);

            model.Nodes = nodes;

            if (ConversionConfig.Current.ImportPhysics)
            {
                (model.RigidBodies, model.Joints) = Physics.ImportPhysics(bones, bodySway, headSway);
            }

            return(model);
        }
Пример #9
0
            internal static (IReadOnlyList <PmxRigidBody> Bodies, IReadOnlyList <PmxJoint> Joints) ImportPhysics([NotNull] IReadOnlyList <PmxBone> bones, [NotNull] SwayController bodySway, [NotNull] SwayController headSway)
            {
                var bodies = new List <PmxRigidBody>();

                var swayColliders = new List <SwayCollider>();

                swayColliders.AddRange(bodySway.Colliders);
                swayColliders.AddRange(headSway.Colliders);

                AppendStaticBodies(bones, swayColliders, bodies);

                var swayBones = new List <SwayBone>();

                swayBones.AddRange(bodySway.SwayBones);
                swayBones.AddRange(headSway.SwayBones);

                AppendSwayBones(bones, swayBones, bodies);

                // TODO: fix rigid body rotation (with bone, e.g. the bones of arms)
                if (ConversionConfig.Current.FixTdaBindingPose)
                {
                    Trace.TraceWarning("Rigid body rotation correction for TDA pose has not been implemented yet!");
                }

                var joints = new List <PmxJoint>();

                AppendJoints(bones, bodies, joints);

                return(bodies, joints);
            }