Ejemplo n.º 1
0
        // Construct the Armature object from mesh skeleton KV data.
        public void ConstructFromNTRO(NTROStruct skeletonData, Dictionary <int, int> remapTable)
        {
            var boneNames     = skeletonData.Get <NTROArray>("m_boneName").ToArray <string>();
            var boneParents   = skeletonData.Get <NTROArray>("m_nParent").ToArray <short>();
            var bonePositions = skeletonData.Get <NTROArray>("m_bonePosParent").ToArray <Vector3>();
            var boneRotations = skeletonData.Get <NTROArray>("m_boneRotParent").ToArray <Vector4>();

            // Initialise bone array
            Bones = new Bone[boneNames.Length];

            //Add all bones to the list
            for (var i = 0; i < boneNames.Length; i++)
            {
                var name = boneNames[i];

                var position = new OpenTK.Vector3(bonePositions[i].X, bonePositions[i].Y, bonePositions[i].Z);
                var rotation = new Quaternion(boneRotations[i].X, boneRotations[i].Y, boneRotations[i].Z, boneRotations[i].W);

                // Create bone
                var index = remapTable.ContainsKey(i) ? remapTable[i] : -1;
                var bone  = new Bone(name, index, position, rotation);

                if (boneParents[i] != -1)
                {
                    bone.SetParent(Bones[boneParents[i]]);
                    Bones[boneParents[i]].AddChild(bone);
                }

                Bones[i] = bone;
            }

            FindRoots();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Construct an animation class from the animation description
        /// </summary>
        /// <param name="animDesc"></param>
        /// <param name="decodeKey"></param>
        /// <param name="decoderArray"></param>
        /// <param name="segmentArray"></param>
        private void ConstructFromDesc(NTROStruct animDesc, NTROStruct decodeKey, AnimDecoderType[] decoderArray, NTROArray segmentArray)
        {
            // Get animation properties
            Name = animDesc.Get <string>("m_name");
            Fps  = animDesc.Get <float>("fps");

            // Only consider first frame block for now
            var pData           = animDesc.Get <NTROArray>("m_pData").Get <NTROStruct>(0);
            var frameBlockArray = pData.Get <NTROArray>("m_frameblockArray").ToArray <NTROStruct>();

            FrameCount = pData.Get <int>("m_nFrames");
            Frames     = new Frame[FrameCount];

            // Figure out each frame
            for (var frame = 0; frame < FrameCount; frame++)
            {
                // Create new frame object
                Frames[frame] = new Frame();

                // Read all frame blocks
                foreach (var frameBlock in frameBlockArray)
                {
                    var startFrame = frameBlock.Get <int>("m_nStartFrame");
                    var endFrame   = frameBlock.Get <int>("m_nEndFrame");

                    // Only consider blocks that actual contain info for this frame
                    if (frame >= startFrame && frame <= endFrame)
                    {
                        var segmentIndexArray = frameBlock.Get <NTROArray>("m_segmentIndexArray").ToArray <int>();

                        foreach (var segmentIndex in segmentIndexArray)
                        {
                            var segment = segmentArray.Get <NTROStruct>(segmentIndex);
                            ReadSegment(frame - startFrame, segment, decodeKey, decoderArray, ref Frames[frame]);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Read segment
        /// </summary>
        /// <param name="frame"></param>
        /// <param name="segment"></param>
        /// <param name="decodeKey"></param>
        /// <param name="decoderArray"></param>
        /// <param name="outFrame"></param>
        private void ReadSegment(int frame, NTROStruct segment, NTROStruct decodeKey, AnimDecoderType[] decoderArray, ref Frame outFrame)
        {
            // Clamp the frame number to be between 0 and the maximum frame
            frame = frame < 0 ? 0 : frame;
            frame = frame >= FrameCount ? FrameCount - 1 : frame;

            var localChannel = segment.Get <int>("m_nLocalChannel");
            var dataChannel  = decodeKey.Get <NTROArray>("m_dataChannelArray").Get <NTROStruct>(localChannel);
            var boneNames    = dataChannel.Get <NTROArray>("m_szElementNameArray").ToArray <string>();

            var channelAttribute = dataChannel.Get <string>("m_szVariableName");

            // Read container
            var container = segment.Get <NTROArray>("m_container").ToArray <byte>();

            using (var containerReader = new BinaryReader(new MemoryStream(container)))
            {
                var elementIndexArray = dataChannel.Get <NTROArray>("m_nElementIndexArray").ToArray <int>();
                var elementBones      = new int[decodeKey.Get <int>("m_nChannelElements")];
                for (var i = 0; i < elementIndexArray.Length; i++)
                {
                    elementBones[elementIndexArray[i]] = i;
                }

                // Read header
                var decoder     = decoderArray[containerReader.ReadInt16()];
                var cardinality = containerReader.ReadInt16();
                var numBones    = containerReader.ReadInt16();
                var totalLength = containerReader.ReadInt16();

                // Read bone list
                var elements = new List <int>();
                for (var i = 0; i < numBones; i++)
                {
                    elements.Add(containerReader.ReadInt16());
                }

                // Skip data to find the data for the current frame.
                // Structure is just | Bone 0 - Frame 0 | Bone 1 - Frame 0 | Bone 0 - Frame 1 | Bone 1 - Frame 1|
                if (containerReader.BaseStream.Position + (decoder.Size() * frame * numBones) < containerReader.BaseStream.Length)
                {
                    containerReader.BaseStream.Position += decoder.Size() * frame * numBones;
                }

                // Read animation data for all bones
                for (var element = 0; element < numBones; element++)
                {
                    // Get the bone we are reading for
                    var bone = elementBones[elements[element]];

                    // Look at the decoder to see what to read
                    switch (decoder)
                    {
                    case AnimDecoderType.CCompressedStaticFullVector3:
                    case AnimDecoderType.CCompressedFullVector3:
                    case AnimDecoderType.CCompressedDeltaVector3:
                        outFrame.SetAttribute(boneNames[bone], channelAttribute, new Vector3(
                                                  containerReader.ReadSingle(),
                                                  containerReader.ReadSingle(),
                                                  containerReader.ReadSingle()));
                        break;

                    case AnimDecoderType.CCompressedAnimVector3:
                    case AnimDecoderType.CCompressedStaticVector3:
                        outFrame.SetAttribute(boneNames[bone], channelAttribute, new Vector3(
                                                  ReadHalfFloat(containerReader),
                                                  ReadHalfFloat(containerReader),
                                                  ReadHalfFloat(containerReader)));
                        break;

                    case AnimDecoderType.CCompressedAnimQuaternion:
                        outFrame.SetAttribute(boneNames[bone], channelAttribute, ReadQuaternion(containerReader));
                        break;

#if DEBUG
                    default:
                        if (channelAttribute != "data")
                        {
                            Console.WriteLine($"Unhandled animation bone decoder type '{decoder}'");
                        }

                        break;
#endif
                    }
                }
            }
        }