Пример #1
0
        private void DoGenerate(MmdModel model, MmdMotion motion, string savePath, float frameStepLength, float timeAfterMotionFinish, float physicsStepLength)
        {
            try
            {
                _status = GenerateStatus.Preparing;
                if (physicsStepLength > frameStepLength)
                {
                    physicsStepLength = frameStepLength;
                }
                var poser          = new Poser(model);
                var motionPlayer   = new MotionPlayer(motion, poser);
                var physicsReactor = new BulletPyhsicsReactor();

                var totalTimeLength = motion.Length / 30.0 + timeAfterMotionFinish;
                var totalStepCount  = (int)(totalTimeLength / frameStepLength) + 1;
                var playPos         = 0.0;
                var maxSubSteps     = (int)(frameStepLength / physicsStepLength) + 1;

                using (var fileStream = new FileStream(savePath, FileMode.Create))
                {
                    using (var bufferedStream = new BufferedStream(fileStream))
                    {
                        using (var binaryWriter = new BinaryWriter(bufferedStream))
                        {
                            WriteHeader(binaryWriter, model, totalStepCount + 1, frameStepLength);
                            _status           = GenerateStatus.CalculatingFrames;
                            _totalFrames      = totalStepCount + 1;
                            _calculatedFrames = 0;
                            physicsReactor.AddPoser(poser);
                            motionPlayer.SeekFrame(0);
                            poser.PrePhysicsPosing();
                            physicsReactor.Reset();
                            poser.PostPhysicsPosing();
                            WritePose(binaryWriter, poser);
                            _calculatedFrames = 1;
                            for (var i = 0; i < totalStepCount; ++i)
                            {
                                playPos += frameStepLength;
                                motionPlayer.SeekTime(playPos);
                                poser.PrePhysicsPosing();
                                physicsReactor.React(frameStepLength, maxSubSteps, physicsStepLength);
                                poser.PostPhysicsPosing();
                                WritePose(binaryWriter, poser);
                                _calculatedFrames = i + 2;
                            }
                        }
                    }
                }
                _status = GenerateStatus.Finished;
            }
            catch (Exception e)
            {
                _status = GenerateStatus.Failed;
                Debug.LogException(e);
            }
        }
Пример #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);
        }