Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
0
        public bool Step()
        {
            if (_bonePoseImagesStore.Count() >= _frameCacheSize)
            {
                return(false);
            }
            var actualStepLength = _stepLength;

            _timePos += _stepLength;
            if (_autoStepLength && _timePos < _lastMissedPos)
            {
                //Debug.LogWarningFormat("auto step length triggered, _timePos={0}, _lastMissedPos={1}", _timePos, _lastMissedPos);
                actualStepLength = _lastMissedPos - _timePos + _stepLength;
                _timePos         = _lastMissedPos;
            }
            if (!_poseMode)
            {
                _motionPlayer.SeekTime(_timePos);
            }
            _poser.PrePhysicsPosing(false);
            //var tickCount = Environment.TickCount;
            _physicsReactor.React(actualStepLength, 2, actualStepLength);
            //Debug.LogFormat("{0} ms for physics", Environment.TickCount - tickCount);
            _poser.PostPhysicsPosing();
            var image = GetBonePoseImage(_poser);

            _bonePoseImagesStore.Enqueue(new BonePoseFrame(_timePos, image));
            //Debug.LogFormat("{0} frames in pose cache", cachedCount);
            return(true);
        }
Ejemplo n.º 3
0
        private void Update()
        {
            if (!Playing)
            {
                return;
            }
            var deltaTime = Time.deltaTime;

            _playTime += deltaTime;
            if (_bonePoseFileStorage != null)
            {
                var poses = _bonePoseFileStorage.GetBonePose(_playTime);
                var start = Environment.TickCount;
                UpdateBonesByBonePoseImage(poses);
                Debug.LogFormat("read pose from file cost {0} ms", Environment.TickCount - start);
                UpdateMesh(_playTime);
            }
            else if (PhysicsMode == PhysicsModeEnum.Bullet)
            {
                double poseTime;
                if (UpdateBonesByPreCalculate(_playTime, out poseTime))
                {
                    UpdateMesh(poseTime);
                }
            }
            else
            {
                _motionPlayer.SeekTime(_playTime);
                _poser.PrePhysicsPosing(false);
                _poser.PostPhysicsPosing();
                if (PhysicsMode == PhysicsModeEnum.Unity)
                {
                    UpdateBones(true);
                }
                else
                {
                    UpdateBones();
                }
                UpdateMesh(_playTime);
            }
        }
Ejemplo n.º 4
0
        public BonePosePreCalculator(MmdPose pose, Poser poser, BulletPyhsicsReactor physicsReactor, double stepLength, double startTimePos, int frameCacheSize, bool autoStepLength)
        {
            _poseMode            = true;
            _poser               = poser;
            _physicsReactor      = physicsReactor;
            _stepLength          = stepLength;
            _bonePoseImagesStore = new BlockingQueue <BonePoseFrame>(frameCacheSize);
            _timePos             = startTimePos;
            _autoStepLength      = autoStepLength;

            poser.ResetPosing();
            SetPoseToPoser(pose, _poser);
            _poser.PrePhysicsPosing();
            _physicsReactor.Reset();
            _poser.PostPhysicsPosing();
            var image = GetBonePoseImage(_poser);

            _bonePoseImagesStore.Enqueue(new BonePoseFrame(startTimePos, image));
        }
Ejemplo n.º 5
0
        public BonePosePreCalculator(Poser poser, BulletPyhsicsReactor physicsReactor, MotionPlayer motionPlayer, double stepLength, double startTimePos, int frameCacheSize, bool autoStepLength)
        {
            _poseMode            = false;
            _poser               = poser;
            _physicsReactor      = physicsReactor;
            _motionPlayer        = motionPlayer;
            _stepLength          = stepLength;
            _bonePoseImagesStore = new BlockingQueue <BonePoseFrame>(frameCacheSize);
            _timePos             = startTimePos;
            _autoStepLength      = autoStepLength;

            _motionPlayer.SeekTime(startTimePos);
            _poser.PrePhysicsPosing();
            _physicsReactor.Reset();
            _poser.PostPhysicsPosing();
            var image = GetBonePoseImage(_poser);

            _bonePoseImagesStore.Enqueue(new BonePoseFrame(startTimePos, image));
        }
Ejemplo n.º 6
0
        public BonePosePreCalculator(BonePoseCalculatorWorker worker, MmdPose pose, Poser poser, BulletPyhsicsReactor physicsReactor, float stepLength, float startTimePos, int frameCacheSize, bool autoStepLength)
        {
            _poseMode            = true;
            _poser               = poser;
            _physicsReactor      = physicsReactor;
            _stepLength          = stepLength;
            _bonePoseImagesStore = new SynchronizedQueue <BonePoseFrame>();
            _timePos             = startTimePos;
            _autoStepLength      = autoStepLength;
            _frameCacheSize      = frameCacheSize;
            poser.ResetPosing();
            SetPoseToPoser(pose, _poser);
            _poser.PrePhysicsPosing();
            _physicsReactor.Reset();
            _poser.PostPhysicsPosing();
            var image = GetBonePoseImage(_poser);

            _bonePoseImagesStore.Enqueue(new BonePoseFrame(startTimePos, image));
            _worker = worker;
        }