示例#1
0
        private void SetupBoneMatrices(AnimationFrame frame, AnimationFrame root)
        {
            AnimationMeshContainer mesh = (AnimationMeshContainer)frame.MeshContainer;

            if (mesh != null)
            {
                byte             numBones   = (byte)mesh.BoneNames.Length;
                AnimationFrame[] boneFrames = null;
                PositionBlendWeightIndexedColored[] boneLines = null;
                if (numBones > 0)
                {
                    boneFrames = new AnimationFrame[numBones];
                    var boneDic = new Dictionary <string, byte>();
                    for (byte i = 0; i < numBones; i++)
                    {
                        string         boneName = mesh.BoneNames[i];
                        AnimationFrame bone     = (AnimationFrame)root.FindChild(boneName);
                        boneFrames[i] = bone;

                        boneDic.Add(boneName, i);
                    }

                    float boneWidth = 0.05f;
                    int   boneColor = Color.CornflowerBlue.ToArgb();
                    boneLines = new PositionBlendWeightIndexedColored[numBones * BoneObjSize];
                    for (byte i = 0; i < numBones; i++)
                    {
                        AnimationFrame bone = boneFrames[i];

                        byte boneParentId;
                        if ((bone != null) && (bone.Parent != null) && boneDic.TryGetValue(bone.Parent.Name, out boneParentId))
                        {
                            Matrix boneMatrix       = Matrix.Invert(mesh.BoneOffsets[i]);
                            Matrix boneParentMatrix = Matrix.Invert(mesh.BoneOffsets[boneParentId]);

                            Vector3 bonePos       = Vector3.TransformCoordinate(new Vector3(), boneMatrix);
                            Vector3 boneParentPos = Vector3.TransformCoordinate(new Vector3(), boneParentMatrix);

                            Vector3 direction     = bonePos - boneParentPos;
                            float   scale         = boneWidth * (1 + direction.Length() / 2);
                            Vector3 perpendicular = direction.Perpendicular();
                            Vector3 cross         = Vector3.Cross(direction, perpendicular);
                            perpendicular = Vector3.Normalize(perpendicular) * scale;
                            cross         = Vector3.Normalize(cross) * scale;

                            Vector3 bottomLeft  = -perpendicular + -cross + boneParentPos;
                            Vector3 bottomRight = -perpendicular + cross + boneParentPos;
                            Vector3 topLeft     = perpendicular + -cross + boneParentPos;
                            Vector3 topRight    = perpendicular + cross + boneParentPos;

                            boneLines[i * BoneObjSize]       = new PositionBlendWeightIndexedColored(bottomLeft, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 1] = new PositionBlendWeightIndexedColored(bottomRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 2] = new PositionBlendWeightIndexedColored(bottomRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 3] = new PositionBlendWeightIndexedColored(topRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 4] = new PositionBlendWeightIndexedColored(topRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 5] = new PositionBlendWeightIndexedColored(topLeft, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 6] = new PositionBlendWeightIndexedColored(topLeft, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 7] = new PositionBlendWeightIndexedColored(bottomLeft, boneParentId, boneColor);

                            boneLines[(i * BoneObjSize) + 8]  = new PositionBlendWeightIndexedColored(bottomLeft, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 9]  = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                            boneLines[(i * BoneObjSize) + 10] = new PositionBlendWeightIndexedColored(bottomRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 11] = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                            boneLines[(i * BoneObjSize) + 12] = new PositionBlendWeightIndexedColored(topLeft, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 13] = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                            boneLines[(i * BoneObjSize) + 14] = new PositionBlendWeightIndexedColored(topRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 15] = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                        }
                    }
                }

                while (mesh != null)
                {
                    if (mesh.NextMeshContainer == null)
                    {
                        mesh.BoneLines = boneLines;
                    }

                    mesh.BoneFrames = boneFrames;
                    mesh            = (AnimationMeshContainer)mesh.NextMeshContainer;
                }
            }

            if (frame.Sibling != null)
            {
                SetupBoneMatrices(frame.Sibling as AnimationFrame, root);
            }

            if (frame.FirstChild != null)
            {
                SetupBoneMatrices(frame.FirstChild as AnimationFrame, root);
            }
        }
        private void SetupBoneMatrices(AnimationFrame frame, AnimationFrame root)
        {
            AnimationMeshContainer mesh = (AnimationMeshContainer)frame.MeshContainer;
            if (mesh != null)
            {
                byte numBones = (byte)mesh.BoneNames.Length;
                AnimationFrame[] boneFrames = null;
                PositionBlendWeightIndexedColored[] boneLines = null;
                if (numBones > 0)
                {
                    boneFrames = new AnimationFrame[numBones];
                    var boneDic = new Dictionary<string, byte>();
                    for (byte i = 0; i < numBones; i++)
                    {
                        string boneName = mesh.BoneNames[i];
                        AnimationFrame bone = (AnimationFrame)root.FindChild(boneName);
                        boneFrames[i] = bone;

                        boneDic.Add(boneName, i);
                    }

                    float boneWidth = 0.05f;
                    int boneColor = Color.CornflowerBlue.ToArgb();
                    boneLines = new PositionBlendWeightIndexedColored[numBones * BoneObjSize];
                    for (byte i = 0; i < numBones; i++)
                    {
                        AnimationFrame bone = boneFrames[i];

                        byte boneParentId;
                        if ((bone != null) && (bone.Parent != null) && boneDic.TryGetValue(bone.Parent.Name, out boneParentId))
                        {
                            Matrix boneMatrix = Matrix.Invert(mesh.BoneOffsets[i]);
                            Matrix boneParentMatrix = Matrix.Invert(mesh.BoneOffsets[boneParentId]);

                            Vector3 bonePos = Vector3.TransformCoordinate(new Vector3(), boneMatrix);
                            Vector3 boneParentPos = Vector3.TransformCoordinate(new Vector3(), boneParentMatrix);

                            Vector3 direction = bonePos - boneParentPos;
                            float scale = boneWidth * (1 + direction.Length() / 2);
                            Vector3 perpendicular = direction.Perpendicular();
                            Vector3 cross = Vector3.Cross(direction, perpendicular);
                            perpendicular = Vector3.Normalize(perpendicular) * scale;
                            cross = Vector3.Normalize(cross) * scale;

                            Vector3 bottomLeft = -perpendicular + -cross + boneParentPos;
                            Vector3 bottomRight = -perpendicular + cross + boneParentPos;
                            Vector3 topLeft = perpendicular + -cross + boneParentPos;
                            Vector3 topRight = perpendicular + cross + boneParentPos;

                            boneLines[i * BoneObjSize] = new PositionBlendWeightIndexedColored(bottomLeft, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 1] = new PositionBlendWeightIndexedColored(bottomRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 2] = new PositionBlendWeightIndexedColored(bottomRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 3] = new PositionBlendWeightIndexedColored(topRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 4] = new PositionBlendWeightIndexedColored(topRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 5] = new PositionBlendWeightIndexedColored(topLeft, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 6] = new PositionBlendWeightIndexedColored(topLeft, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 7] = new PositionBlendWeightIndexedColored(bottomLeft, boneParentId, boneColor);

                            boneLines[(i * BoneObjSize) + 8] = new PositionBlendWeightIndexedColored(bottomLeft, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 9] = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                            boneLines[(i * BoneObjSize) + 10] = new PositionBlendWeightIndexedColored(bottomRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 11] = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                            boneLines[(i * BoneObjSize) + 12] = new PositionBlendWeightIndexedColored(topLeft, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 13] = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                            boneLines[(i * BoneObjSize) + 14] = new PositionBlendWeightIndexedColored(topRight, boneParentId, boneColor);
                            boneLines[(i * BoneObjSize) + 15] = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                        }
                    }
                }

                while (mesh != null)
                {
                    if (mesh.NextMeshContainer == null)
                    {
                        mesh.BoneLines = boneLines;
                    }

                    mesh.BoneFrames = boneFrames;
                    mesh = (AnimationMeshContainer)mesh.NextMeshContainer;
                }
            }

            if (frame.Sibling != null)
            {
                SetupBoneMatrices(frame.Sibling as AnimationFrame, root);
            }

            if (frame.FirstChild != null)
            {
                SetupBoneMatrices(frame.FirstChild as AnimationFrame, root);
            }
        }
示例#3
0
        private void SetupBoneMatrices(AnimationFrame frame, AnimationFrame root)
        {
            for (AnimationMeshContainer mesh = (AnimationMeshContainer)frame.MeshContainer;
                 mesh != null;
                 mesh = (AnimationMeshContainer)mesh.NextMeshContainer)
            {
                int numBones = mesh.BoneNames.Length;
                if (numBones <= 0)
                    continue;

                AnimationFrame[] boneFrames = new AnimationFrame[numBones];
                for (int i = 0; i < numBones; i++)
                {
                    AnimationFrame boneFrame = (AnimationFrame)root.FindChild(mesh.BoneNames[i]);
                    boneFrames[i] = boneFrame;
                }
                mesh.BoneFrames = boneFrames;

                int boneObjSize = 16;
                float boneWidth = 0.1f;
                int boneColor = Color.CornflowerBlue.ToArgb();
                PositionBlendWeightIndexedColored[] boneLines = new PositionBlendWeightIndexedColored[numBones * boneObjSize];
                for (byte i = 0; i < numBones; i++)
                {
                    AnimationFrame bone = boneFrames[i];

                    if (bone != null && bone.Parent != null)
                    {
                        byte parentBoneIdx = 0xFF;
                        for (byte j = 0; j < mesh.BoneNames.Length; j++)
                        {
                            if (mesh.BoneNames[j] == bone.Parent.Name)
                            {
                                parentBoneIdx = j;
                                break;
                            }
                        }
                        if (parentBoneIdx == 0xFF)
                        {
                            continue;
                        }
                        Matrix boneMatrix = Matrix.Invert(mesh.BoneOffsets[i]);
                        Matrix boneParentMatrix = Matrix.Invert(mesh.BoneOffsets[parentBoneIdx]);

                        Vector3 bonePos = Vector3.TransformCoordinate(new Vector3(), boneMatrix);
                        Vector3 boneParentPos = Vector3.TransformCoordinate(new Vector3(), boneParentMatrix);

                        Vector3 direction = bonePos - boneParentPos;
                        Vector3 perpendicular = direction.Perpendicular();
                        Vector3 cross = Vector3.Cross(direction, perpendicular);
                        perpendicular = Vector3.Normalize(perpendicular) * boneWidth;
                        cross = Vector3.Normalize(cross) * boneWidth;

                        Vector3 bottomLeft = -perpendicular + -cross + boneParentPos;
                        Vector3 bottomRight = -perpendicular + cross + boneParentPos;
                        Vector3 topLeft = perpendicular + -cross + boneParentPos;
                        Vector3 topRight = perpendicular + cross + boneParentPos;

                        boneLines[i * boneObjSize] = new PositionBlendWeightIndexedColored(bottomLeft, parentBoneIdx, boneColor);
                        boneLines[(i * boneObjSize) + 1] = new PositionBlendWeightIndexedColored(bottomRight, parentBoneIdx, boneColor);
                        boneLines[(i * boneObjSize) + 2] = new PositionBlendWeightIndexedColored(bottomRight, parentBoneIdx, boneColor);
                        boneLines[(i * boneObjSize) + 3] = new PositionBlendWeightIndexedColored(topRight, parentBoneIdx, boneColor);
                        boneLines[(i * boneObjSize) + 4] = new PositionBlendWeightIndexedColored(topRight, parentBoneIdx, boneColor);
                        boneLines[(i * boneObjSize) + 5] = new PositionBlendWeightIndexedColored(topLeft, parentBoneIdx, boneColor);
                        boneLines[(i * boneObjSize) + 6] = new PositionBlendWeightIndexedColored(topLeft, parentBoneIdx, boneColor);
                        boneLines[(i * boneObjSize) + 7] = new PositionBlendWeightIndexedColored(bottomLeft, parentBoneIdx, boneColor);

                        boneLines[(i * boneObjSize) + 8] = new PositionBlendWeightIndexedColored(bottomLeft, parentBoneIdx, boneColor);
                        boneLines[(i * boneObjSize) + 9] = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                        boneLines[(i * boneObjSize) + 10] = new PositionBlendWeightIndexedColored(bottomRight, parentBoneIdx, boneColor);
                        boneLines[(i * boneObjSize) + 11] = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                        boneLines[(i * boneObjSize) + 12] = new PositionBlendWeightIndexedColored(topLeft, parentBoneIdx, boneColor);
                        boneLines[(i * boneObjSize) + 13] = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                        boneLines[(i * boneObjSize) + 14] = new PositionBlendWeightIndexedColored(topRight, parentBoneIdx, boneColor);
                        boneLines[(i * boneObjSize) + 15] = new PositionBlendWeightIndexedColored(bonePos, i, boneColor);
                    }
                }

                mesh.BoneLines = boneLines;
            }

            if (frame.Sibling != null)
            {
                SetupBoneMatrices(frame.Sibling as AnimationFrame, root);
            }

            if (frame.FirstChild != null)
            {
                SetupBoneMatrices(frame.FirstChild as AnimationFrame, root);
            }
        }