示例#1
0
        // Writing compressed & decompressing keyframes
        private RwCompressedKeyFrameData ReadCompressedKeyFrames(BinaryReader reader)
        {
            var compressedKeyFrames = new List <RwCompressedKeyFrame>();

            for (int i = 0; i < KeyFrames.Capacity; i++)
            {
                var compressedKeyFrame = new RwCompressedKeyFrame
                {
                    Time                = reader.ReadSingle(),
                    RotationX           = reader.ReadUInt16(),
                    RotationY           = reader.ReadUInt16(),
                    RotationZ           = reader.ReadUInt16(),
                    RotationW           = reader.ReadUInt16(),
                    TranslationX        = reader.ReadUInt16(),
                    TranslationY        = reader.ReadUInt16(),
                    TranslationZ        = reader.ReadUInt16(),
                    PreviousFrameOffset = reader.ReadInt32()
                };

                compressedKeyFrames.Add(compressedKeyFrame);
            }

            var customData = new RwCompressedKeyFrameCustomData
            {
                Offset = reader.ReadVector3(),
                Scalar = reader.ReadVector3()
            };

            return(new RwCompressedKeyFrameData(compressedKeyFrames, customData));
        }
示例#2
0
        private static void WriteCompressedKeyFrames(BinaryWriter writer, List <RwCompressedKeyFrame> compressedKeyFrames,
                                                     RwCompressedKeyFrameCustomData customData)
        {
            foreach (var compressedKeyFrame in compressedKeyFrames)
            {
                writer.Write(compressedKeyFrame.Time);
                writer.Write(compressedKeyFrame.RotationX);
                writer.Write(compressedKeyFrame.RotationY);
                writer.Write(compressedKeyFrame.RotationZ);
                writer.Write(compressedKeyFrame.RotationW);
                writer.Write(compressedKeyFrame.TranslationX);
                writer.Write(compressedKeyFrame.TranslationY);
                writer.Write(compressedKeyFrame.TranslationZ);
                writer.Write(compressedKeyFrame.PreviousFrameOffset);
            }

            writer.Write(customData.Offset);
            writer.Write(customData.Scalar);
        }
示例#3
0
 public RwCompressedKeyFrameData(List <RwCompressedKeyFrame> keyFrames, RwCompressedKeyFrameCustomData customData)
 {
     KeyFrames  = keyFrames;
     CustomData = customData;
 }
示例#4
0
        // Compress keyframes
        private void CompressKeyFrames(out List <RwCompressedKeyFrame> compressedKeyFrames, out RwCompressedKeyFrameCustomData customData)
        {
            var keyFrameToOffsetMap = new Dictionary <RwKeyFrame, int>();

            // Move translations >= 0
            customData = new RwCompressedKeyFrameCustomData
            {
                Offset = new Vector3
                         (
                    KeyFrames.Min(x => x.Translation.X),
                    KeyFrames.Min(x => x.Translation.Y),
                    KeyFrames.Min(x => x.Translation.Z)
                         )
            };

            compressedKeyFrames = new List <RwCompressedKeyFrame>();
            var offsetTranslations = new List <Vector3>();

            for (var i = 0; i < KeyFrames.Count; i++)
            {
                var keyFrame = KeyFrames[i];

                // add key frame to offset map for mapping the previous keyframe
                keyFrameToOffsetMap[keyFrame] = i * COMPRESSED_KEYFRAME_SIZE;

                // create the compressed key frame
                var compressedKeyFrame = new RwCompressedKeyFrame
                {
                    // time will remain the same
                    Time = keyFrame.Time,

                    // compress rotation quaternion
                    RotationX = CompressFloat(keyFrame.Rotation.X),
                    RotationY = CompressFloat(keyFrame.Rotation.Y),
                    RotationZ = CompressFloat(keyFrame.Rotation.Z),
                    RotationW = CompressFloat(keyFrame.Rotation.W),
                };

                // set to previous frame offset using the map
                if (keyFrame.Previous != null)
                {
                    compressedKeyFrame.PreviousFrameOffset = keyFrameToOffsetMap[keyFrame.Previous];
                }
                else
                {
                    compressedKeyFrame.PreviousFrameOffset = -1;
                }

                // add the newly created compressed key frame to the list
                // it's not yet fully initialized as the translations still
                // have to be processed
                compressedKeyFrames.Add(compressedKeyFrame);

                // offset the translation by the distance overall closest to zero
                var translation = keyFrame.Translation;
                translation.X -= customData.Offset.X;
                translation.Y -= customData.Offset.Y;
                translation.Z -= customData.Offset.Z;

                // add the offset translation to a seperate list
                offsetTranslations.Add(translation);
            }

            // calculate the translation scalar by taking the largest value of each
            // axis
            customData.Scalar = new Vector3(
                offsetTranslations.Max(x => x.X),
                offsetTranslations.Max(x => x.Y),
                offsetTranslations.Max(x => x.Z));

            // compress the translation using the offset translation and calculated scalar
            for (int i = 0; i < offsetTranslations.Count; i++)
            {
                compressedKeyFrames[i].TranslationX = CompressFloat(offsetTranslations[i].X / customData.Scalar.X);
                compressedKeyFrames[i].TranslationY = CompressFloat(offsetTranslations[i].Y / customData.Scalar.Y);
                compressedKeyFrames[i].TranslationZ = CompressFloat(offsetTranslations[i].Z / customData.Scalar.Z);
            }
        }
示例#5
0
        private void DecompressKeyframes(List <RwCompressedKeyFrame> compressedKeyFrames, RwCompressedKeyFrameCustomData customData)
        {
            var keyFrameByOffsetMap = new Dictionary <int, RwKeyFrame>();

            for (int i = 0; i < compressedKeyFrames.Count; i++)
            {
                var compressedKeyFrame = compressedKeyFrames[i];

                var keyFrame = new RwKeyFrame
                {
                    Time     = compressedKeyFrame.Time,
                    Rotation = new Quaternion
                               (
                        DecompressFloat(compressedKeyFrame.RotationX),
                        DecompressFloat(compressedKeyFrame.RotationY),
                        DecompressFloat(compressedKeyFrame.RotationZ),
                        DecompressFloat(compressedKeyFrame.RotationW)
                               ),
                    Translation = new Vector3
                                  (
                        (DecompressFloat(compressedKeyFrame.TranslationX) * customData.Scalar.X) +
                        customData.Offset.X,
                        (DecompressFloat(compressedKeyFrame.TranslationY) * customData.Scalar.Y) +
                        customData.Offset.Y,
                        (DecompressFloat(compressedKeyFrame.TranslationZ) * customData.Scalar.Z) +
                        customData.Offset.Z
                                  )
                };

                if (keyFrame.Time != 0.0f)
                {
                    keyFrame.Previous = keyFrameByOffsetMap[compressedKeyFrame.PreviousFrameOffset];
                }

                keyFrameByOffsetMap[i * COMPRESSED_KEYFRAME_SIZE] = keyFrame;

                keyFrame.Dirty = false;
                KeyFrames.Add(keyFrame);
            }
        }