예제 #1
0
        public MeshDrawerRigged(Mesh mesh, BoneController skelet, List <Morph> mor = null) : base(mesh, null, mor)
        {
            skeleton = skelet;
            UniformBufferManager ubm = UniformBufferManager.GetInstance;

            uniformBufferSkeleton = (UniformBufferSkeleton)ubm.GetBuffer("skeleton");
        }
예제 #2
0
 public MorphSkeleton(string name, string nameEng, int count, BoneController bc)
 {
     Name           = name;
     NameEng        = nameEng;
     boneController = bc;
     bones          = new Tuple <int, Vector3, Quaternion> [count];
 }
예제 #3
0
        void Instalize(AnimationFrame start)
        {
            foreach (var pair in boneReference)
            {
                Vector4 rot = start.BonePoritions[pair.Value].Rotation;
                Vector3 pos = start.BonePoritions[pair.Value].Position;

                Matrix4 localInv = Matrix4.Identity;

                Quaternion rotation = Quaternion.Identity;
                if (_animation.GetRotationType == Animation.RotationType.Quaternion)
                {
                    rotation = new Quaternion(rot.Xyz, rot.W);
                }
                else
                {
                    rotation = Quaternion.FromAxisAngle(Vector3.UnitZ, rot.Z) * Quaternion.FromAxisAngle(Vector3.UnitY, rot.Y) * Quaternion.FromAxisAngle(Vector3.UnitX, rot.X);
                }

                if (_animation.TransType == Animation.TransformType.LocalRelative)
                {
                    BoneController.GetBone(pair.Key).SetTransform(rotation, pos);
                }
                else if (_animation.TransType == Animation.TransformType.LocalAbsolute)
                {
                    BoneController.GetBone(pair.Key).InitialLocalTransform = Matrix4.CreateFromQuaternion(rotation) * Matrix4.CreateTranslation(pos);
                }
            }
        }
예제 #4
0
        public MeshDrawerRigged(Mesh mesh, Material[] mats, BoneController skelet, List <Morph> mor = null) : base(mesh, mats, mor)
        {
            skeleton = skelet;
            UniformBufferManager ubm = UniformBufferManager.GetInstance;

            uniformBufferSkeleton = (UniformBufferSkeleton)ubm.GetBuffer("skeleton");
            modelSkinner          = new ModelSkinning(mesh);
        }
예제 #5
0
        void ReadBones()
        {
            reader.BaseStream.Position = 0x34;
            int boneOffset = (int)reader.BaseStream.Position + reader.ReadInt32();

            reader.BaseStream.Position = boneOffset + 0x8;
            int boneCount = reader.ReadInt32();

            int[] boneOffsets = new int[boneCount];
            for (int i = 0; i < boneCount; i++)
            {
                boneOffsets[i] = (int)reader.BaseStream.Position + reader.ReadInt32();
            }

            bones = new Bone[boneCount];
            string[] parents = new string[boneCount];
            for (int i = 0; i < boneCount; i++)
            {
                reader.BaseStream.Position = boneOffsets[i];
                int magicNumber = reader.ReadInt32();
                reader.BaseStream.Position = boneOffsets[i] + 4;
                int nameOffset = (int)reader.BaseStream.Position + reader.ReadInt32();
                reader.BaseStream.Position = nameOffset;
                string boneName = reader.readString();

                reader.BaseStream.Position = boneOffsets[i] + 8;
                Matrix4 boneMatrix = new Matrix4(
                    reader.readVector4(),
                    reader.readVector4(),
                    reader.readVector4(),
                    reader.readVector4()
                    );
                reader.BaseStream.Position = boneOffsets[i] + 0x38;
                Vector3 bonePosition = reader.readVector3();
                reader.BaseStream.Position = boneOffsets[i] + 0x48;
                int boneParentOffset = (int)reader.BaseStream.Position + reader.ReadInt32();
                reader.BaseStream.Position = boneParentOffset;
                parents[i] = reader.readString();
                Bone bone = new Bone(boneName, boneMatrix, -1);
                bone.Index    = i;
                bone.Position = bonePosition;
                bones[i]      = bone;
                if (magicNumber < 0x5000)
                {
                    continue;
                }
            }

            for (int i = 0; i < boneCount; i++)
            {
                bones[i].ParentIndex = Array.FindIndex(bones, (element) => element.Name == parents[i]);
            }
            boneControll = new BoneController(bones, true);
        }
예제 #6
0
 public PhysicsManager(RigidContainer[] rigits, JointContainer[] jcons, BoneController bons)
 {
     //setup world physics
     bones = bons;
     //instalize delegates
     prePhysics  = (m) => { };
     postPhysics = (m) => { };
     Rigits      = rigits;
     Jcons       = jcons;
     //CreateGeneric6DofSpringConstraint();
     Type = typeof(PhysicsManager);
 }
예제 #7
0
        void ReadBones(Reader reader)
        {
            int bonesCount = reader.ReadInt32();
            var bones      = new Bone[bonesCount];
            int maxLevel   = 0;

            for (int i = 0; i < bonesCount; i++)
            {
                string name    = reader.readString();
                string nameEng = reader.readString();

                Vector3 Position = reader.readVector3() * multipler;

                int parentIndex = 0;
                if (header.GetBoneIndexSize == 2)
                {
                    parentIndex = unchecked ((short)reader.readVal(header.GetBoneIndexSize));
                }
                else
                {
                    parentIndex = reader.readVal(header.GetBoneIndexSize);
                }

                int    Level = reader.ReadInt32();
                byte[] flags = reader.ReadBytes(2);

                Bone bone = new Bone(name, nameEng, Position, parentIndex, flags);
                bone.Level = Level;
                if (bone.tail)
                {
                    reader.readVal(header.GetBoneIndexSize);
                }
                else
                {
                    reader.readVector3();
                }

                if (bone.InheritRotation || bone.InheritTranslation)
                {
                    bone.ParentInheritIndex = reader.readVal(header.GetBoneIndexSize);
                    bone.ParentInfluence    = reader.ReadSingle();
                }

                bone.Parent2Local = Matrix4.Identity;
                //probably cant be both true
                if (bone.FixedAxis)
                {
                    Vector3 X     = reader.readVector3();
                    Vector3 Z     = Vector3.Cross(X, new Vector3(0f, 1f, 0f));
                    Vector3 Y     = Vector3.Cross(Z, X);
                    Matrix3 local = new Matrix3(X, Y, Z);
                    local.Normalize();
                    bone.Parent2Local = new Matrix4(local);
                }
                if (bone.LocalCoordinate)
                {
                    Vector3 X     = reader.readVector3();
                    Vector3 Z     = reader.readVector3();
                    Vector3 Y     = Vector3.Cross(Z, X);
                    Matrix3 local = new Matrix3(X, Y, Z);
                    local.Normalize();
                    bone.Parent2Local = new Matrix4(local);
                }

                if (bone.ExternalPdeform)
                {
                    reader.ReadInt32();
                }

                if (bone.IK)
                {
                    BoneIK ikBone = new BoneIK();
                    ikBone.Target     = reader.readVal(header.GetBoneIndexSize);
                    ikBone.LoopCount  = reader.ReadInt32();
                    ikBone.AngleLimit = reader.ReadSingle();
                    int      count = reader.ReadInt32();
                    IKLink[] links = new IKLink[count];
                    for (int n = 0; n < count; n++)
                    {
                        IKLink link = new IKLink();
                        link.Bone = reader.readVal(header.GetBoneIndexSize);
                        if (reader.ReadByte() == 1)
                        {
                            link.IsLimit  = true;
                            link.LimitMin = -reader.readVector3();
                            link.LimitMax = -reader.readVector3();
                        }
                        links[n] = link;
                    }
                    ikBone.Links = links;
                    bone.IKData  = ikBone;
                }

                if (maxLevel < bone.Level)
                {
                    maxLevel = bone.Level;
                }

                bones[i] = bone;
            }

            var reverce = Matrix4.CreateScale(new Vector3(1, 1, -1));

            //convert position from model to parent bone
            for (int i = bones.Length - 1; i > -1; i--)
            {
                Bone bone = bones[i];
                if (bone.ParentIndex >= 0 && bone.ParentIndex < bones.Length)
                {
                    bone.Parent2Local = Matrix4.CreateTranslation(bone.Position - bones[bone.ParentIndex].Position);
                }

                //Convert left to right coordinates
                bone.Parent2Local = reverce * bone.Parent2Local * reverce;
            }


            boneOrder = new int[bones.Length];
            int m = 0;

            for (int n = 0; n <= maxLevel; n++)
            {
                for (int i = 0; i < bones.Length; i++)
                {
                    if (bones[i].Level == n)
                    {
                        boneOrder[m] = i;
                        m++;
                    }
                }
            }

            boneController = new BoneController(bones, boneOrder);
        }
예제 #8
0
        public void SyncBone2Body(OpenTK.Mathematics.Matrix4 world)
        {
            var mat = (BoneController.GetBone(BoneID).TransformMatrix *world).Convert();

            Body.MotionState.WorldTransform = startTransform * mat;
        }
예제 #9
0
        internal void Update(float delta)
        {
            animController?.Update();
            if (!_isPlaing)
            {
                return;
            }
            _isEnded = false;

            _time += delta;

            _time = (_time >= _length + _frameLength) ? _time % _length : _time;

            int prevFrame = (int)(_time / _frameLength);
            int curFrame  = (prevFrame == _animation.frames.Length - 1) ? 0 : prevFrame + 1;

            float frameDelta = _time / _frameLength;

            frameDelta = frameDelta - (int)frameDelta;

            AnimationFrame frame1 = _animation.frames[prevFrame], frame2 = _animation.frames[curFrame];

            foreach (var pair in boneReference)
            {
                Quaternion rotation = Quaternion.Identity;

                Vector3 pos = frame1.BonePoritions[pair.Value].Position + (frame2.BonePoritions[pair.Value].Position - frame1.BonePoritions[pair.Value].Position) * frameDelta;
                if (_animation.GetRotationType == Animation.RotationType.Quaternion)
                {
                    Quaternion prevQuat = new Quaternion(frame1.BonePoritions[pair.Value].Rotation.Xyz, frame1.BonePoritions[pair.Value].Rotation.W);
                    Quaternion nextQuat = new Quaternion(frame2.BonePoritions[pair.Value].Rotation.Xyz, frame2.BonePoritions[pair.Value].Rotation.W);
                    rotation = Quaternion.Slerp(prevQuat, nextQuat, frameDelta);
                }
                else
                {
                    var deltaRot = frame2.BonePoritions[pair.Value].Rotation - frame1.BonePoritions[pair.Value].Rotation;
                    CycleDeltaCheck(ref deltaRot.X);
                    CycleDeltaCheck(ref deltaRot.Y);
                    CycleDeltaCheck(ref deltaRot.Z);
                    Vector4 rot = frame1.BonePoritions[pair.Value].Rotation + deltaRot * frameDelta;
                    rotation = Quaternion.FromAxisAngle(Vector3.UnitZ, rot.Z) * Quaternion.FromAxisAngle(Vector3.UnitY, rot.Y) * Quaternion.FromAxisAngle(Vector3.UnitX, rot.X);
                }

                //skip masked out bone
                if (!BoneController.GetBone(pair.Key).FollowAnimation)
                {
                    continue;
                }

                if (_animation.TransType == Animation.TransformType.LocalRelative)
                {
                    BoneController.GetBone(pair.Key).SetTransform(rotation, pos);
                }
                else if (_animation.TransType == Animation.TransformType.LocalAbsolute)
                {
                    BoneController.GetBone(pair.Key).InitialLocalTransform = Matrix4.CreateFromQuaternion(rotation) * Matrix4.CreateTranslation(pos);
                }
            }

            if (curFrame == _animation.frames.Length - 1)
            {
                if (!IsRepeat)
                {
                    //Pause();
                    _isPlaing = false;
                    _isEnded  = true;
                }
            }
        }
예제 #10
0
 public Animator(BoneController bc) : base(typeof(Animator))
 {
     BoneController = bc;
 }