internal void TransformFrameAbsolute(SdkMeshFile file, double time)
        {
            int tick = file.GetAnimationKeyFromTime(time);

            if (this.AnimationFrameIndex != -1)
            {
                SdkMeshAnimationFrame animationFrame   = file.AnimationFrames[this.AnimationFrameIndex];
                SdkMeshAnimationKey   animationKey     = animationFrame.AnimationKeys[tick];
                SdkMeshAnimationKey   animationKeyOrig = animationFrame.AnimationKeys[0];

                XMMatrix mTrans1 = XMMatrix.Translation(-animationKeyOrig.Translation.X, -animationKeyOrig.Translation.Y, -animationKeyOrig.Translation.Z);
                XMMatrix mTrans2 = XMMatrix.Translation(animationKey.Translation.X, animationKey.Translation.Y, animationKey.Translation.Z);

                XMVector quat1 = animationKeyOrig.Orientation;
                quat1 = XMQuaternion.Inverse(quat1);
                XMMatrix mRot1  = XMMatrix.RotationQuaternion(quat1);
                XMMatrix mInvTo = mTrans1 * mRot1;

                XMVector quat2 = animationKey.Orientation;
                XMMatrix mRot2 = XMMatrix.RotationQuaternion(quat2);
                XMMatrix mFrom = mRot2 * mTrans2;

                XMMatrix mOutput = mInvTo * mFrom;
                this.TransformedFrameMatrix = mOutput;
            }
        }
        internal void TransformFrame(SdkMeshFile file, XMMatrix parentWorld, double time)
        {
            // Get the tick data
            XMMatrix mLocalTransform;

            int tick = file.GetAnimationKeyFromTime(time);

            if (this.AnimationFrameIndex == -1)
            {
                mLocalTransform = this.Matrix;
            }
            else
            {
                SdkMeshAnimationFrame animationFrame = file.AnimationFrames[this.AnimationFrameIndex];
                SdkMeshAnimationKey   animationKey   = animationFrame.AnimationKeys[tick];

                // turn it into a matrix (Ignore scaling for now)
                XMFloat3 parentPos  = animationKey.Translation;
                XMMatrix mTranslate = XMMatrix.Translation(parentPos.X, parentPos.Y, parentPos.Z);

                XMVector quat = animationKey.Orientation;

                if (XMVector4.Equal(quat, XMVector.Zero))
                {
                    quat = XMQuaternion.Identity;
                }

                quat = XMQuaternion.Normalize(quat);
                XMMatrix mQuat = XMMatrix.RotationQuaternion(quat);
                mLocalTransform = mQuat * mTranslate;
            }

            // Transform ourselves
            XMMatrix mLocalWorld = mLocalTransform * parentWorld;

            this.TransformedFrameMatrix = mLocalWorld;
            this.WorldPoseFrameMatrix   = mLocalWorld;

            // Transform our siblings
            if (this.SiblingFrameIndex != -1)
            {
                file.Frames[this.SiblingFrameIndex].TransformFrame(file, parentWorld, time);
            }

            // Transform our children
            if (this.ChildFrameIndex != -1)
            {
                file.Frames[this.ChildFrameIndex].TransformFrame(file, mLocalWorld, time);
            }
        }
        internal void TransformBindPoseFrame(SdkMeshFile file, XMMatrix parentWorld)
        {
            // Transform ourselves
            XMMatrix mLocalWorld = this.Matrix * parentWorld;

            this.BindPoseFrameMatrix = mLocalWorld;

            // Transform our siblings
            if (this.SiblingFrameIndex != -1)
            {
                file.Frames[this.SiblingFrameIndex].TransformBindPoseFrame(file, parentWorld);
            }

            // Transform our children
            if (this.ChildFrameIndex != -1)
            {
                file.Frames[this.ChildFrameIndex].TransformBindPoseFrame(file, mLocalWorld);
            }
        }
示例#4
0
        public static SdkMeshFile FromFile(D3D11Device device, D3D11DeviceContext deviceContext, string fileName)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (deviceContext == null)
            {
                throw new ArgumentNullException(nameof(deviceContext));
            }

            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            var file = new SdkMeshFile
            {
                _d3dDevice        = device,
                _d3dDeviceContext = deviceContext,
                FilePath          = fileName,
                FileDirectory     = Path.GetDirectoryName(fileName)
            };

            SdkMeshRawFile rawFile = SdkMeshRawFile.FromFile(file.FilePath);

            foreach (SdkMeshRawMaterial rawMaterial in rawFile.Materials)
            {
                var material = new SdkMeshMaterial(file._d3dDevice, file._d3dDeviceContext, file.FileDirectory, rawMaterial);
                file.Materials.Add(material);
            }

            foreach (SdkMeshRawMesh rawMesh in rawFile.Meshes)
            {
                var mesh = new SdkMeshMesh(file._d3dDevice, rawFile, rawMesh);
                file.Meshes.Add(mesh);
            }

            foreach (SdkMeshRawFrame rawFrame in rawFile.Frames)
            {
                var frame = new SdkMeshFrame(rawFrame);
                file.Frames.Add(frame);
            }

            string animFilePath = file.FilePath + "_anim";

            if (File.Exists(animFilePath))
            {
                SdkMeshRawAnimFile rawAnimFile = SdkMeshRawAnimFile.FromFile(animFilePath);

                file.AnimationFrameTransformType = rawAnimFile.Header.FrameTransformType;
                file.AnimationKeysCount          = rawAnimFile.Header.NumAnimationKeys;
                file.AnimationFPS = rawAnimFile.Header.AnimationFPS;

                foreach (SdkMeshRawAnimFrameData rawFrame in rawAnimFile.AnimationFrames)
                {
                    var frame = new SdkMeshAnimationFrame(rawFrame);
                    file.AnimationFrames.Add(frame);
                }

                for (int index = 0; index < file.AnimationFrames.Count; index++)
                {
                    SdkMeshFrame frame = file.FindFrame(file.AnimationFrames[index].FrameName);

                    if (frame != null)
                    {
                        frame.UpdateAnimationFrameIndex(index);
                    }
                }
            }

            return(file);
        }