Exemplo n.º 1
0
        private IEnumerator GenerateBonePoseFile()
        {
            yield return(null);

            var context = (Context)data;
            var model   = ModelReader.LoadMmdModel(context.ModelPath, new ModelReadConfig {
                GlobalToonPath = ""
            });
            var motion = new VmdReader().Read(context.MotionPath);

            Debug.Log("motion load finished" + motion.Length + " frames");
            var generator =
                BonePoseFileGenerator.GenerateAsync(model, motion, context.SavePath, 1.0f / 60.0f, 5.0f, 1.0f / 120.0f);

            _generator = generator;
            while (true)
            {
                var generatorStatus = generator.Status;
                if (generatorStatus == BonePoseFileGenerator.GenerateStatus.Failed ||
                    generatorStatus == BonePoseFileGenerator.GenerateStatus.Finished ||
                    generatorStatus == BonePoseFileGenerator.GenerateStatus.Canceled)
                {
                    break;
                }
                if (generatorStatus == BonePoseFileGenerator.GenerateStatus.CalculatingFrames)
                {
                    _tipText.text = "已计算" + generator.CalculatedFrames + "帧, 共" + generator.TotalFrames + "帧。 已完成"
                                    + 100L * generator.CalculatedFrames / generator.TotalFrames + "%";
                }
                yield return(null);
            }
            if (generator.Status == BonePoseFileGenerator.GenerateStatus.Finished)
            {
                MainSceneController.Instance.ChangeCurrentBonePoseFile(context.SavePath);
            }
            ClosePage();
            switch (generator.Status)
            {
            case BonePoseFileGenerator.GenerateStatus.Finished:
                ShowPage <OkDialog>(new OkDialog.Context
                {
                    Title = "提示",
                    Tip   =
                        "计算完成。现在可以流畅地播放了。以后如果需要再次播放同模型配同动作,选择模型、动作后可以再载入已保存的动作结果文件。记得在选择结果文件时可能需要先点“扫描”才能看到计算结果文件。",
                    OnOk = () => { OnceTipPage.ShowOnceTip(TipNames.WhereIsBonePoseFile); }
                });
                break;

            case BonePoseFileGenerator.GenerateStatus.Failed:
                ShowPage <OkDialog>(new OkDialog.Context
                {
                    Title = "提示",
                    Tip   = "计算发生错误。"
                });
                break;
            }
        }
Exemplo n.º 2
0
        private void DoLoadModel(string filePath)
        {
            Debug.LogFormat("start load model {0}", filePath);
            _model = ModelReader.LoadMmdModel(filePath, _modelReadConfig);
            Release();
            var directoryInfo = new FileInfo(filePath).Directory;

            if (directoryInfo == null)
            {
                throw new MmdFileParseException(filePath + " does not belong to any directory.");
            }
            var relativePath = directoryInfo.FullName;

            _materialLoader = new MaterialLoader(new TextureLoader(relativePath, DefaultMaxTextureSize));
            var vertCount = _model.Vertices.Length;

            if (vertCount <= 65535)
            {
                var mesh = new Mesh
                {
                    vertices = new Vector3[vertCount],
                    normals  = new Vector3[vertCount]
                };
                var triangleCount = _model.TriangleIndexes.Length / 3;
                var triangles     = _model.TriangleIndexes;
                FillSubMesh(mesh, triangleCount, triangles);
                var uv    = ExtratUv(_model.Vertices);
                var uvVec = new Vector2[vertCount];
                Utils.MmdUvToUnityUv(uv, uvVec);
                mesh.uv          = uvVec;
                mesh.boneWeights = _model.Vertices.Select(x => ConvertBoneWeight(x.SkinningOperator)).ToArray();
                ReleasePreviousMeshes();
                Mesh = mesh;
                mesh.RecalculateBounds();
                _partIndexes = null;
            }
            else
            {
                var triangleCount = _model.TriangleIndexes.Length / 3;
                var triangles     = _model.TriangleIndexes;
                var uv            = ExtratUv(_model.Vertices);
                ;
                var uvVec = new Vector2[vertCount];
                Utils.MmdUvToUnityUv(uv, uvVec);
                FillPartMeshes(triangleCount, triangles, uv);
            }
            _poser          = new Poser(_model);
            _physicsReactor = new BulletPyhsicsReactor();
            _physicsReactor.AddPoser(_poser);
            InitMesh();
            Debug.LogFormat("load model finished {0}", filePath);
        }
Exemplo n.º 3
0
        async Task <bool> LoadModel(string filePath)
        {
            ModelPath = filePath;
            try
            {
                DirectoryInfo directoryInfo = new FileInfo(filePath).Directory;
                if (directoryInfo == null)
                {
                    throw new MMDFileParseException(filePath + " does not belong to any directory.");
                }

                RawMMDModel    = ModelReader.LoadMmdModel(filePath, modelReadConfig);
                transform.name = RawMMDModel.Name;
                string         relativePath   = directoryInfo.FullName;
                MaterialLoader materialLoader = new MaterialLoader(new TextureLoader(relativePath, MaxTextureSize));
                int            vertCount      = RawMMDModel.Vertices.Length;
                int            triangleCount  = RawMMDModel.TriangleIndexes.Length / 3;
                int[]          triangles      = RawMMDModel.TriangleIndexes;
                string         meshName       = Path.GetFileName(filePath);
                Mesh           mesh           = new Mesh
                {
                    name     = meshName,
                    vertices = new Vector3[vertCount],
                    normals  = new Vector3[vertCount]
                };
                mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
                FillSubMesh(mesh, triangleCount, triangles);
                float[] uv = ExtratUv(RawMMDModel.Vertices);
                mesh.uv          = Utils.MmdUvToUnityUv(uv);
                mesh.vertices    = RawMMDModel.Vertices.Select(x => x.Coordinate).ToArray();
                mesh.normals     = RawMMDModel.Vertices.Select(x => x.Normal).ToArray();
                mesh.boneWeights = RawMMDModel.Vertices.Select(x => ConvertBoneWeight(x.SkinningOperator)).ToArray();
                mesh.RecalculateBounds();
                Mesh = mesh;

                await AttachVertexMorph(mesh);
                await SearchForMaterialMorph();

                Utils.ClearAllTransformChild(transform);
                (GameObject rootObj, Transform[] bones) = CreateBones(model: RawMMDModel, parentOfAll: gameObject);

                SkinnedMeshRenderer          = rootObj.AddComponent <SkinnedMeshRenderer>();
                SkinnedMeshRenderer.enabled  = false;
                SkinnedMeshRenderer.quality  = SkinQuality.Bone4;
                SkinnedMeshRenderer.rootBone = rootObj.transform;

                InitializeBoneDictionaries(bones);

                Materials = LoadModelMaterials(materialLoader, new MMDUnityConfig(), RawMMDModel);
                ReorderRender(Materials);

                SkinnedMeshRenderer.sharedMesh = mesh;
                BuildBindpose(Mesh, bones, SkinnedMeshRenderer);
                SkinnedMeshRenderer.materials = Materials;

                MaterialMorphRemover.HideMaterialMorphs(this);

                AttachPhysicComponents(bones, this);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogException(e);
                return(false);
            }
            return(true);
        }