Esempio n. 1
0
        public void HighlightBone(remParser parser, remMesh remMesh, int boneIdx, bool show)
        {
            List <List <remVertex> > submeshVertLists   = new List <List <remVertex> >(remMesh.numMats);
            List <List <ushort> >    submeshFaceLists   = new List <List <ushort> >(remMesh.numMats);
            List <int[]>             submeshVertIndices = new List <int[]>(remMesh.numMats);

            SplitMesh(remMesh, submeshVertLists, submeshFaceLists, submeshVertIndices);
            remSkin boneList = rem.FindSkin(remMesh.name, parser.SKIC);

            int submeshIdx = 0;

            for (AnimationMeshContainer mesh = (AnimationMeshContainer)meshFrames[0].MeshContainer;
                 mesh != null;
                 mesh = (AnimationMeshContainer)mesh.NextMeshContainer, submeshIdx++)
            {
                if (mesh.MeshData != null && mesh.MeshData.Mesh != null)
                {
                    List <remVertex> vertexList        = submeshVertLists[submeshIdx];
                    byte[][]         vertexBoneIndices = null;
                    float[][]        vertexWeights     = ConvertVertexWeights(vertexList, submeshVertIndices[submeshIdx], boneList, out vertexBoneIndices);
                    FillVertexBuffer(mesh.MeshData.Mesh, vertexList, vertexWeights, vertexBoneIndices, show ? boneIdx : -1);
                }
                if (mesh.BoneLines != null)
                {
                    for (int j = 0; j < BoneObjSize; j++)
                    {
                        mesh.BoneLines[boneIdx * BoneObjSize + j].Color = show ? Color.Crimson.ToArgb(): Color.CornflowerBlue.ToArgb();
                    }
                }
            }
        }
Esempio n. 2
0
        public RenderObjectREM(remParser parser, remMesh mesh)
        {
            HighlightSubmesh          = new HashSet <int>();
            highlightMaterial         = new Material();
            highlightMaterial.Ambient = new Color4(1, 1, 1, 1);
            highlightMaterial.Diffuse = new Color4(1, 0, 1, 0);

            this.device = Gui.Renderer.Device;

            if (Textures.Count + parser.MATC.Count > Textures.Capacity)
            {
                Textures.Capacity += parser.MATC.Count;
            }
            Materials = new Material[parser.MATC.Count];

            rootFrame = CreateHierarchy(parser, mesh, device, out meshFrames);

            AnimationController = new AnimationController(numFrames, 30, 30, 1);
            Frame.RegisterNamedMatrices(rootFrame, AnimationController);

            for (int i = 0; i < meshFrames.Count; i++)
            {
                if (i == 0)
                {
                    Bounds = meshFrames[i].Bounds;
                }
                else
                {
                    Bounds = BoundingBox.Merge(Bounds, meshFrames[i].Bounds);
                }
            }
        }
Esempio n. 3
0
        private static void SplitMesh(remMesh mesh, List <List <remVertex> > submeshVertLists, List <List <ushort> > submeshFaceLists, List <int[]> submeshVertIndices)
        {
            for (int i = 0; i < mesh.numFaces; i++)
            {
                while (mesh.faceMarks[i] >= submeshFaceLists.Count)
                {
                    List <ushort> newFaceList = new List <ushort>(mesh.numFaces);
                    submeshFaceLists.Add(newFaceList);
                    List <remVertex> newVertList = new List <remVertex>(mesh.numVertices);
                    submeshVertLists.Add(newVertList);
                    int[] newVertIndices = new int[mesh.numVertices];
                    for (int j = 0; j < mesh.numVertices; j++)
                    {
                        newVertIndices[j] = -1;
                    }
                    submeshVertIndices.Add(newVertIndices);
                }

                int submesh = mesh.faceMarks[i];
                for (int j = 0; j < 3; j++)
                {
                    int vertIdx = mesh.faces[i * 3 + j];
                    if (submeshVertIndices[submesh][vertIdx] < 0)
                    {
                        submeshVertIndices[submesh][vertIdx] = submeshVertLists[submesh].Count;
                        submeshVertLists[submesh].Add(mesh.vertices[vertIdx]);
                    }
                    submeshFaceLists[submesh].Add((ushort)submeshVertIndices[submesh][vertIdx]);
                }
            }
        }
Esempio n. 4
0
        public static HashSet <string> SearchHierarchy(remParser parser, remMesh mesh)
        {
            HashSet <string> exportFrames = new HashSet <string>();

            SearchHierarchy(parser.BONC.rootFrame, mesh, exportFrames);
            remSkin boneList = FindSkin(mesh.name, parser.SKIC);

            if (boneList != null)
            {
                for (int i = 0; i < boneList.Count; i++)
                {
                    if (!exportFrames.Contains(boneList[i].bone.ToString()))
                    {
                        remBone boneParent = FindFrame(boneList[i].bone, parser.BONC.rootFrame);
                        if (boneParent == null)
                        {
                            Report.ReportLog("Missing bone frame " + boneList[i].bone);
                            continue;
                        }
                        while (boneParent.Parent != null && exportFrames.Add(boneParent.name.ToString()))
                        {
                            boneParent = (remBone)boneParent.Parent;
                        }
                    }
                }
            }

            return(exportFrames);
        }
Esempio n. 5
0
        public void SetBoneMatrix(int meshIdx, int boneIdx,
                                  double m11, double m12, double m13, double m14,
                                  double m21, double m22, double m23, double m24,
                                  double m31, double m32, double m33, double m34,
                                  double m41, double m42, double m43, double m44)
        {
            remMesh        mesh        = Parser.MESC[meshIdx];
            remSkin        skin        = rem.FindSkin(mesh.name, Parser.SKIC);
            remBoneWeights boneWeights = skin[boneIdx];
            Matrix         m           = new Matrix();

            m.M11 = (float)m11;
            m.M12 = (float)m12;
            m.M13 = (float)m13;
            m.M14 = (float)m14;

            m.M21 = (float)m21;
            m.M22 = (float)m22;
            m.M23 = (float)m23;
            m.M24 = (float)m24;

            m.M31 = (float)m31;
            m.M32 = (float)m32;
            m.M33 = (float)m33;
            m.M34 = (float)m34;

            m.M41 = (float)m41;
            m.M42 = (float)m42;
            m.M43 = (float)m43;
            m.M44 = (float)m44;

            boneWeights.matrix = m;
        }
Esempio n. 6
0
        private void listViewAnimationTrack_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            List <DockContent> formREMList;

            if (!Gui.Docking.DockContents.TryGetValue(typeof(FormREM), out formREMList))
            {
                return;
            }

            foreach (FormREM formMesh in formREMList)
            {
                for (int i = 0; i < formMesh.renderObjectMeshes.Count; i++)
                {
                    RenderObjectREM mesh = formMesh.renderObjectMeshes[i];
                    if (mesh != null && formMesh.renderObjectIds[i] > -1)
                    {
                        remMesh remMesh = formMesh.Editor.Parser.MESC[i];
                        remSkin skin    = rem.FindSkin(remMesh.name, formMesh.Editor.Parser.SKIC);
                        if (skin == null)
                        {
                            continue;
                        }
                        remBoneWeights boneWeights = rem.FindBoneWeights(skin, new remId(e.Item.Text));
                        if (boneWeights == null)
                        {
                            continue;
                        }
                        mesh.HighlightBone(formMesh.Editor.Parser, remMesh, skin.IndexOf(boneWeights), e.IsSelected);
                        Gui.Renderer.Render();
                    }
                }
            }
        }
Esempio n. 7
0
        public remMesh Clone(bool submeshes, bool boneList, remParser parser, List <remSkin> clonedSkins)
        {
            remMesh mesh = new remMesh(numMats);

            mesh.name = new remId(name);
            rem.CopyUnknowns(this, mesh);

            if (submeshes)
            {
                for (int i = 0; i < numMats; i++)
                {
                    mesh.AddMaterial(new remId(materials[i]));
                }
                mesh.vertices = new remVertex[numVertices];
                for (int i = 0; i < numVertices; i++)
                {
                    mesh.vertices[i] = vertices[i].Clone();
                }
                mesh.faces     = (int[])faces.Clone();
                mesh.faceMarks = (int[])faceMarks.Clone();
            }

            remSkin skin = rem.FindSkin(name, parser.SKIC);

            if (skin != null)
            {
                skin      = skin.Clone();
                skin.mesh = mesh.name;
                clonedSkins.Add(skin);
            }

            return(mesh);
        }
Esempio n. 8
0
        public remBone Clone(bool mesh, bool childFrames, remParser parser, List <remMaterial> clonedMaterials, List <remMesh> clonedMeshes, List <remSkin> clonedSkins)
        {
            remBone frame = new remBone(Count);

            frame.name   = name;
            frame.matrix = matrix;

            if (mesh)
            {
                remMesh remMesh = rem.FindMesh(this, parser.MESC);
                if (remMesh != null)
                {
                    foreach (remId matId in remMesh.materials)
                    {
                        remMaterial mat = rem.FindMaterial(matId, parser.MATC);
                        if (!clonedMaterials.Contains(mat))
                        {
                            clonedMaterials.Add(mat.Clone());
                        }
                    }
                    remMesh clone = remMesh.Clone(true, true, parser, clonedSkins);
                    clone.frame = frame.name;
                    clonedMeshes.Add(clone);
                }
            }
            if (childFrames)
            {
                for (int i = 0; i < Count; i++)
                {
                    frame.AddChild(this[i].Clone(mesh, true, parser, clonedMaterials, clonedMeshes, clonedSkins));
                }
            }
            return(frame);
        }
Esempio n. 9
0
        public void CopyBoneWeights(int meshIdx, int boneIdx)
        {
            remMesh        mesh        = Parser.MESC[meshIdx];
            remSkin        skin        = rem.FindSkin(mesh.name, Parser.SKIC);
            remBoneWeights boneWeights = skin[boneIdx];

            skin.AddChild(boneWeights.Clone());
        }
Esempio n. 10
0
        public void SetBoneSRT(int meshIdx, int boneIdx, double sX, double sY, double sZ, double rX, double rY, double rZ, double tX, double tY, double tZ)
        {
            remMesh        mesh        = Parser.MESC[meshIdx];
            remSkin        skin        = rem.FindSkin(mesh.name, Parser.SKIC);
            remBoneWeights boneWeights = skin[boneIdx];

            boneWeights.matrix = FbxUtility.SRTToMatrix(new Vector3((float)sX, (float)sY, (float)sZ), new Vector3((float)rX, (float)rY, (float)rZ), new Vector3((float)tX, (float)tY, (float)tZ));
        }
Esempio n. 11
0
            public Mesh(remMesh mesh, remSkin skin)
            {
                name = mesh.name;
                InitChildren(mesh.numMats);

                for (int i = 0; i < mesh.numMats; i++)
                {
                    Submesh submesh = new Submesh(mesh, skin, i);
                    AddChild(submesh);
                }
            }
Esempio n. 12
0
        public void SetMeshName(int idx, string name)
        {
            remId   newName = new remId(name);
            remMesh mesh    = Parser.MESC[idx];
            remSkin skin    = rem.FindSkin(mesh.name, Parser.SKIC);

            if (skin != null)
            {
                skin.mesh = newName;
            }
            Parser.MESC[idx].name = newName;
        }
Esempio n. 13
0
        public static void RemoveMesh(remParser parser, remMesh mesh)
        {
            parser.MESC.RemoveChild(mesh);
            mesh.frame = null;

            remSkin skin = FindSkin(mesh.name, parser.SKIC);

            if (skin != null)
            {
                parser.SKIC.RemoveChild(skin);
            }
        }
Esempio n. 14
0
        public void RemoveSubmesh(int meshIdx, int submeshIdx)
        {
            remMesh mesh = Parser.MESC[meshIdx];

            if (mesh.numMats == 1)
            {
                RemoveMesh(meshIdx);
            }
            else
            {
                rem.RemoveSubmesh(Parser, mesh, submeshIdx);
            }
        }
Esempio n. 15
0
        void DeleteMeshesInSubframes(remBone frame)
        {
            remMesh mesh = rem.FindMesh(frame, Parser.MESC);

            if (mesh != null)
            {
                rem.RemoveMesh(Parser, mesh);
            }

            foreach (remBone child in frame)
            {
                DeleteMeshesInSubframes(child);
            }
        }
Esempio n. 16
0
        void MergeFrame(remBone srcParent, remBone destParent)
        {
            for (int i = 0; i < destParent.Count; i++)
            {
                var dest = destParent[i];
                for (int j = 0; j < srcParent.Count; j++)
                {
                    var src = srcParent[j];
                    if (src.name == dest.name)
                    {
                        MergeFrame(src, dest);

                        srcParent.RemoveChild(j);
                        remMesh mesh = rem.FindMesh(dest, Parser.MESC);
                        if (mesh != null)
                        {
                            rem.RemoveMesh(Parser, mesh);
                        }
                        destParent.RemoveChild(i);
                        destParent.InsertChild(i, src);
                        break;
                    }
                }
            }

            if (srcParent.name == destParent.name)
            {
                while (destParent.Count > 0)
                {
                    var     dest = destParent[0];
                    remMesh mesh = rem.FindMesh(dest, Parser.MESC);
                    if (mesh != null)
                    {
                        rem.RemoveMesh(Parser, mesh);
                    }
                    destParent.RemoveChild(0);
                    srcParent.AddChild(dest);
                }
            }
            else
            {
                while (srcParent.Count > 0)
                {
                    var src = srcParent[0];
                    srcParent.RemoveChild(0);
                    destParent.AddChild(src);
                }
            }
        }
Esempio n. 17
0
        private AnimationFrame CreateHierarchy(remParser parser, remMesh mesh, Device device, out List <AnimationFrame> meshFrames)
        {
            meshFrames = new List <AnimationFrame>(1);
            HashSet <string> extractFrames = rem.SearchHierarchy(parser, mesh);

#if !DONT_MIRROR
            Matrix orignalMatrix = parser.BONC.rootFrame.matrix;
            parser.BONC.rootFrame.matrix *= Matrix.Scaling(1f, 1f, -1f);
#endif
            AnimationFrame rootFrame = CreateFrame(parser.BONC.rootFrame, parser, extractFrames, mesh, device, Matrix.Identity, meshFrames);
            SetupBoneMatrices(rootFrame, rootFrame);
#if !DONT_MIRROR
            parser.BONC.rootFrame.matrix = orignalMatrix;
#endif
            return(rootFrame);
        }
Esempio n. 18
0
        public bool RemoveBone(int meshIdx, int boneIdx)
        {
            remMesh mesh     = Parser.MESC[meshIdx];
            remSkin boneList = rem.FindSkin(mesh.name, Parser.SKIC);

            if (boneList.Count == 1 && boneIdx == 0)
            {
                Parser.SKIC.RemoveChild(boneList);
                return(true);
            }
            else
            {
                boneList.RemoveChild(boneIdx);
                return(false);
            }
        }
Esempio n. 19
0
        public static void ExportMqo([DefaultVar] remParser parser, object[] meshNames, string dirPath, bool singleMqo, bool worldCoords)
        {
            List <remMesh> meshes = new List <remMesh>(meshNames.Length);

            foreach (string meshName in Utility.Convert <string>(meshNames))
            {
                remMesh mesh = rem.FindMesh(new remId(meshName), parser.MESC);
                if (mesh != null)
                {
                    meshes.Add(mesh);
                }
                else
                {
                    Report.ReportLog("Mesh " + meshName + " not found");
                }
            }
            Mqo.Exporter.Export(dirPath, parser, meshes, singleMqo, worldCoords);
        }
Esempio n. 20
0
        static void SearchHierarchy(remBone frame, remMesh mesh, HashSet <string> exportFrames)
        {
            if (frame.name == mesh.frame)
            {
                remBone parent = frame;
                while (parent.Parent != null)
                {
                    exportFrames.Add(parent.name.ToString());
                    parent = (remBone)parent.Parent;
                }
                return;
            }

            for (int i = 0; i < frame.Count; i++)
            {
                SearchHierarchy(frame[i], mesh, exportFrames);
            }
        }
Esempio n. 21
0
        public void RemoveMaterial(int idx)
        {
            remMaterial mat = Parser.MATC[idx];

            for (int i = 0; i < Parser.MESC.Count; i++)
            {
                remMesh mesh = Parser.MESC[i];
                for (int j = 0; j < mesh.numMats; j++)
                {
                    remId matId = mesh.materials[j];
                    if (matId == mat.name)
                    {
                        mesh.materials[j] = null;
                    }
                }
            }

            Parser.MATC.RemoveChild(idx);
        }
Esempio n. 22
0
        void DeleteReferringBones(remBone frame)
        {
            for (int i = 0; i < Parser.MESC.Count; i++)
            {
                remMesh mesh = Parser.MESC[i];
                remSkin skin = rem.FindSkin(mesh.name, Parser.SKIC);
                if (skin != null)
                {
                    remBoneWeights boneWeights = rem.FindBoneWeights(skin, frame.name);
                    if (boneWeights != null)
                    {
                        RemoveBone(i, skin.IndexOf(boneWeights));
                    }
                }
            }

            foreach (remBone child in frame)
            {
                DeleteReferringBones(child);
            }
        }
Esempio n. 23
0
        void ReplaceFrame(remBone newFrame, int destParentIdx)
        {
            if (destParentIdx < 0)
            {
                Parser.BONC.rootFrame.ChildList.Clear();
                foreach (remBone child in newFrame)
                {
                    Parser.BONC.rootFrame.AddChild(child);
                }
                Parser.MESC.ChildList.Clear();
                Parser.SKIC.ChildList.Clear();
            }
            else
            {
                var  destParent = Parser.BONC[destParentIdx];
                bool found      = false;
                for (int i = 0; i < destParent.Count; i++)
                {
                    var dest = destParent[i];
                    if (dest.name == newFrame.name)
                    {
                        remMesh mesh = rem.FindMesh(dest, Parser.MESC);
                        if (mesh != null)
                        {
                            rem.RemoveMesh(Parser, mesh);
                        }
                        destParent.RemoveChild(i);
                        destParent.InsertChild(i, newFrame);
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    destParent.AddChild(newFrame);
                }
            }
            RebuildBONC();
        }
Esempio n. 24
0
File: Fbx.cs Progetto: kkdevs/sb3u
        public static void ExportFbx([DefaultVar] remParser parser, object[] meshNames, object[] reaParsers, int startKeyframe, int endKeyframe, bool linear, bool EulerFilter, double filterPrecision, string path, string exportFormat, bool allFrames, bool skins, bool compatibility)
        {
            List <reaParser> reaParserList = null;

            if (reaParsers != null)
            {
                reaParserList = new List <reaParser>(Utility.Convert <reaParser>(reaParsers));
            }

            List <string>  meshStrList = new List <string>(Utility.Convert <string>(meshNames));
            List <remMesh> meshes      = new List <remMesh>(meshStrList.Count);

            foreach (string meshName in meshStrList)
            {
                remMesh mesh = rem.FindMesh(new remId(meshName), parser.MESC);
                if (mesh != null)
                {
                    meshes.Add(mesh);
                }
                else
                {
                    Report.ReportLog("Mesh " + meshName + " not found.");
                }
            }

            REMConverter imp = new REMConverter(parser, meshes);

            if (reaParserList != null)
            {
                foreach (reaParser reaParser in reaParserList)
                {
                    imp.ConvertAnimation(reaParser.ANIC, parser);
                }
            }

            FbxUtility.Export(path, imp, startKeyframe, endKeyframe, linear, EulerFilter, (float)filterPrecision, exportFormat, allFrames, false, skins, compatibility);
        }
Esempio n. 25
0
        public static remMesh CreateMesh(WorkspaceMesh mesh, out string[] materialNames, out int[] indices, out bool[] worldCoords, out bool[] replaceSubmeshesOption)
        {
            int numUncheckedSubmeshes = 0;

            foreach (ImportedSubmesh submesh in mesh.SubmeshList)
            {
                if (!mesh.isSubmeshEnabled(submesh))
                {
                    numUncheckedSubmeshes++;
                }
            }
            int numSubmeshes = mesh.SubmeshList.Count - numUncheckedSubmeshes;

            materialNames          = new string[numSubmeshes];
            indices                = new int[numSubmeshes];
            worldCoords            = new bool[numSubmeshes];
            replaceSubmeshesOption = new bool[numSubmeshes];

            remMesh newMesh = new remMesh(numSubmeshes);

            newMesh.name = new remId(mesh.Name);

            List <remVertex> newVertices  = new List <remVertex>();
            List <int>       newFaces     = new List <int>();
            List <int>       newFaceMarks = new List <int>();

            for (int i = 0, submeshIdx = 0; i < numSubmeshes; i++, submeshIdx++)
            {
                while (!mesh.isSubmeshEnabled(mesh.SubmeshList[submeshIdx]))
                {
                    submeshIdx++;
                }

                ImportedSubmesh submesh = mesh.SubmeshList[submeshIdx];

                newMesh.AddMaterial(new remId(submesh.Material));
                materialNames[i]          = submesh.Material;
                indices[i]                = submesh.Index;
                worldCoords[i]            = submesh.WorldCoords;
                replaceSubmeshesOption[i] = mesh.isSubmeshReplacingOriginal(submesh);

                List <ImportedFace> faceList = submesh.FaceList;
                newFaces.Capacity += faceList.Count * 3;
                int[] faceMarks = new int[faceList.Count];
                for (int j = 0; j < faceList.Count; j++)
                {
                    ImportedFace face = faceList[j];
                    for (int k = 0; k < 3; k++)
                    {
                        newFaces.Add(face.VertexIndices[k] + newVertices.Count);
                    }
                    faceMarks[j] = i;
                }
                newFaceMarks.AddRange(faceMarks);

                List <ImportedVertex> vertexList = submesh.VertexList;
                newVertices.Capacity += vertexList.Count;
                for (int j = 0; j < vertexList.Count; j++)
                {
                    ImportedVertex vert      = vertexList[j];
                    remVertex      newVertex = new remVertex();

                    if (submesh.WorldCoords)
                    {
                        newVertex.Position = vert.Position;
                        newVertex.Normal   = vert.Normal;
                    }
                    else
                    {
                        newVertex.Position = new Vector3(vert.Position.X, -vert.Position.Z, vert.Position.Y);
                        newVertex.Normal   = new Vector3(vert.Normal.X, -vert.Normal.Z, vert.Normal.Y);
                    }
                    newVertex.UV = new Vector2(vert.UV[0], vert.UV[1]);
                    newVertices.Add(newVertex);
                }
            }
            newMesh.vertices  = newVertices.ToArray();
            newMesh.faces     = newFaces.ToArray();
            newMesh.faceMarks = newFaceMarks.ToArray();

            return(newMesh);
        }
Esempio n. 26
0
        public static void RemoveSubmesh(remParser parser, remMesh mesh, int submeshIdx)
        {
            List <int> newFaces     = new List <int>(mesh.numFaces * 3);
            List <int> newFaceMarks = new List <int>(mesh.numFaces);

            bool[] usedVertices = new bool[mesh.numVertices];
            for (int i = 0; i < mesh.faceMarks.Length; i++)
            {
                if (mesh.faceMarks[i] != submeshIdx)
                {
                    newFaceMarks.Add(mesh.faceMarks[i] < submeshIdx ? mesh.faceMarks[i] : mesh.faceMarks[i] - 1);

                    for (int j = i * 3; j < i * 3 + 3; j++)
                    {
                        int vertIdx = mesh.faces[j];
                        newFaces.Add(vertIdx);
                        usedVertices[vertIdx] = true;
                    }
                }
            }
            int[]            vertIdxMap = new int[mesh.numVertices];
            List <remVertex> vertList   = new List <remVertex>(mesh.numVertices);
            int numNewVerts             = 0;

            for (int i = 0; i < mesh.numVertices; i++)
            {
                if (usedVertices[i])
                {
                    vertIdxMap[i] = numNewVerts++;
                    vertList.Add(mesh.vertices[i]);
                }
            }

            mesh.vertices = vertList.ToArray();
            for (int i = 0; i < newFaces.Count; i++)
            {
                newFaces[i] = vertIdxMap[newFaces[i]];
            }
            mesh.faces     = newFaces.ToArray();
            mesh.faceMarks = newFaceMarks.ToArray();
            mesh.materials.RemoveAt(submeshIdx);

            remSkin skin = rem.FindSkin(mesh.name, parser.SKIC);

            if (skin != null)
            {
                for (int i = 0; i < skin.Count; i++)
                {
                    remBoneWeights          bw             = skin[i];
                    Dictionary <int, float> newBoneWeights = new Dictionary <int, float>();
                    for (int j = 0; j < bw.numVertIdxWts; j++)
                    {
                        int oldVertIdx = bw.vertexIndices[j];
                        if (usedVertices[oldVertIdx])
                        {
                            newBoneWeights.Add(vertIdxMap[oldVertIdx], bw.vertexWeights[j]);
                        }
                    }
                    if (newBoneWeights.Count > 0)
                    {
                        bw.vertexIndices = new int[newBoneWeights.Count];
                        bw.vertexWeights = new float[newBoneWeights.Count];
                        newBoneWeights.Keys.CopyTo(bw.vertexIndices, 0);
                        newBoneWeights.Values.CopyTo(bw.vertexWeights, 0);
                    }
                    else
                    {
                        skin.RemoveChild(i);
                        i--;
                    }
                }
            }
        }
Esempio n. 27
0
        public static void ReplaceMesh(remBone frame, remParser parser, WorkspaceMesh mesh, List <ImportedMaterial> materials, List <ImportedTexture> textures, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod, bool meshFrameCorrection)
        {
            remMesh frameREMMesh = rem.FindMesh(frame, parser.MESC);

            int startPos = 0;

            if (meshFrameCorrection)
            {
                // frame.matrix = Matrix.Scaling(-1f, 1f, 1f) * Matrix.RotationYawPitchRoll(0f, (float)(Math.PI / 2), (float)Math.PI);
                frame.matrix     = Matrix.Identity;
                frame.matrix.M22 = frame.matrix.M33 = 0f;
                frame.matrix.M23 = frame.matrix.M32 = 1f;
                startPos         = mesh.Name.IndexOf("(Scale");
                if (startPos > 0)
                {
                    int   endPos = mesh.Name.IndexOf(')');
                    float scale;
                    if (Single.TryParse(mesh.Name.Substring(startPos + 7, endPos - startPos - 7), out scale))
                    {
                        frame.matrix *= Matrix.Scaling(new Vector3(scale));
                    }
                    remId newFrameName = new remId(mesh.Name.Substring(0, startPos));
                    if (newFrameName != frame.name)
                    {
                        if (rem.FindFrame(newFrameName, parser.BONC.rootFrame) == null)
                        {
                            frame.name = newFrameName;
                        }
                        else
                        {
                            Report.ReportLog("Warning! Cant rename frame (and mesh) " + mesh.Name + " automatically to " + newFrameName + ".");
                        }
                    }
                }
            }

            Matrix  transform      = Matrix.Scaling(-1f, 1f, 1f);
            remBone transformFrame = frame;

            while (transformFrame != parser.BONC.rootFrame)
            {
                transform     *= transformFrame.matrix;
                transformFrame = transformFrame.Parent as remBone;
            }
            transform.Invert();

            string[] materialNames;
            int[]    indices;
            bool[]   worldCoords;
            bool[]   replaceSubmeshesOption;
            remMesh  newREMMesh = CreateMesh(mesh, out materialNames, out indices, out worldCoords, out replaceSubmeshesOption);

            if (startPos > 0)
            {
                newREMMesh.name = frame.name;
            }
            Mesh newMesh = new Mesh(newREMMesh, CreateBoneList(mesh, transform));

            remSkin frameMeshSkin = null;
            Mesh    frameMesh     = null;

            if (frameREMMesh != null)
            {
                newMesh.name  = frameREMMesh.name;
                frameMeshSkin = rem.FindSkin(frameREMMesh.name, parser.SKIC);
                frameMesh     = new Mesh(frameREMMesh, frameMeshSkin);
            }

            Submesh[]      replaceSubmeshes = frameMesh != null ? new Submesh[frameMesh.Count] : null;
            List <Submesh> addSubmeshes     = new List <Submesh>(newMesh.Count);

            for (int i = 0; i < newMesh.Count; i++)
            {
                remMaterial mat = rem.FindMaterial(new remId(materialNames[i]), parser.MATC);
                if (materials != null)
                {
                    if (mat == null)
                    {
                        mat = CreateMaterial(ImportedHelpers.FindMaterial(materialNames[i], materials));
                        parser.MATC.AddChild(mat);
                    }

/*					if (textures != null)
 *                                      {
 *                                              string texName = materials[i].Textures[0];
 *                                              remMaterial texMat = rem.FindMaterial(parser.MATC, new remId(texName));
 *                                              if (texMat == null)
 *                                              {
 *                                                      for (int k = 0; k < textures.Count; k++)
 *                                                      {
 *                                                              if (textures[k].Name == texName)
 *                                                              {
 * //									texMat = CreateTexture(textures[k], Path.GetDirectoryName(parser.ODFPath));
 *                                                                      break;
 *                                                              }
 *                                                      }
 *                                              }
 *                                      }*/
                }

                Submesh newSubmesh = newMesh[i];
                if (mat != null)
                {
                    newSubmesh.MaterialName = mat.name;
                }

                if (worldCoords[i])
                {
                    List <remVertex> newVertexList = newSubmesh.VertexList;
                    for (int j = 0; j < newVertexList.Count; j++)
                    {
                        newVertexList[j].Position = Vector3.TransformCoordinate(newVertexList[j].Position, transform);
                    }
                }

                Submesh baseSubmesh            = null;
                List <remBoneWeights> newBones = null;
                int idx = indices[i];
                if ((frameMesh != null) && (idx >= 0) && (idx < frameMesh.Count))
                {
                    baseSubmesh = frameMesh[idx];

                    if ((bonesMethod == CopyMeshMethod.CopyOrder) || (bonesMethod == CopyMeshMethod.CopyNear))
                    {
                        List <remBoneWeights> baseBones = baseSubmesh.BoneList;
                        if (baseBones != null)
                        {
                            newBones = new List <remBoneWeights>(baseBones.Count);
                            foreach (remBoneWeights boneWeights in baseBones)
                            {
                                remBoneWeights copy = boneWeights.Clone();
                                newBones.Add(copy);
                            }
                            newSubmesh.BoneList = newBones;
                        }
                    }
                    else if (bonesMethod == CopyMeshMethod.Replace)
                    {
                        newBones = newSubmesh.BoneList;
                    }
                }
                else
                {
                    newBones = newSubmesh.BoneList;
                }

                if (baseSubmesh != null)
                {
                    if (normalsMethod == CopyMeshMethod.CopyOrder)
                    {
                        rem.CopyNormalsOrder(baseSubmesh.VertexList, newSubmesh.VertexList);
                    }
                    else if (normalsMethod == CopyMeshMethod.CopyNear)
                    {
                        rem.CopyNormalsNear(baseSubmesh.VertexList, newSubmesh.VertexList);
                    }

                    if (bonesMethod == CopyMeshMethod.CopyOrder)
                    {
                        rem.CopyBonesOrder(baseSubmesh.VertexList, newSubmesh.VertexList, newBones);
                    }
                    else if (bonesMethod == CopyMeshMethod.CopyNear)
                    {
                        rem.CopyBonesNear(baseSubmesh.VertexList, newSubmesh.VertexList, newBones);
                    }
                }

                if ((baseSubmesh != null) && merge && replaceSubmeshesOption[i])
                {
                    replaceSubmeshes[idx] = newSubmesh;
                }
                else
                {
                    addSubmeshes.Add(newSubmesh);
                }
            }

            if ((frameMesh != null) && merge)
            {
                newMesh.ChildList.Clear();
                newMesh.ChildList.Capacity = replaceSubmeshes.Length + addSubmeshes.Count;
                for (int i = 0, submeshesRemoved = 0; i < replaceSubmeshes.Length; i++)
                {
                    if (replaceSubmeshes[i] == null)
                    {
                        Submesh newSubmesh = frameMesh[i - submeshesRemoved++];
                        newMesh.AddChild(newSubmesh);
                    }
                    else
                    {
                        newMesh.AddChild(replaceSubmeshes[i]);
                    }
                }
                newMesh.ChildList.AddRange(addSubmeshes);
            }

            remSkin skin;

            newREMMesh       = newMesh.CreateMesh(out skin);
            newREMMesh.frame = frame.name;
            if (frameREMMesh != null)
            {
                CopyUnknowns(frameREMMesh, newREMMesh);
                parser.MESC.InsertChild(parser.MESC.IndexOf(frameREMMesh), newREMMesh);

                RemoveMesh(parser, frameREMMesh);
            }
            else
            {
                CreateUnknowns(newREMMesh);
                parser.MESC.AddChild(newREMMesh);
            }
            if (skin.Count > 0)
            {
                parser.SKIC.AddChild(skin);
            }
        }
Esempio n. 28
0
        public void RemoveMesh(int idx)
        {
            remMesh mesh = Parser.MESC[idx];

            rem.RemoveMesh(Parser, mesh);
        }
Esempio n. 29
0
        public void SetMeshUnknowns(int idx, object[] unknowns)
        {
            remMesh mesh = Parser.MESC[idx];

            mesh.unknown = new int[] { (int)unknowns[0], (int)unknowns[1] };
        }
Esempio n. 30
0
        private AnimationFrame CreateFrame(remBone frame, remParser parser, HashSet <string> extractFrames, remMesh mesh, Device device, Matrix combinedParent, List <AnimationFrame> meshFrames)
        {
            AnimationFrame animationFrame = new AnimationFrame();

            animationFrame.Name = frame.name.ToString();
            animationFrame.TransformationMatrix = frame.matrix;
            animationFrame.OriginalTransform    = animationFrame.TransformationMatrix;
            animationFrame.CombinedTransform    = combinedParent * animationFrame.TransformationMatrix;

            if (frame.name == mesh.frame)
            {
                ExtendedMaterial[] materials = new ExtendedMaterial[mesh.numMats];

                List <List <remVertex> > submeshVertLists   = new List <List <remVertex> >(mesh.numMats);
                List <List <ushort> >    submeshFaceLists   = new List <List <ushort> >(mesh.numMats);
                List <int[]>             submeshVertIndices = new List <int[]>(mesh.numMats);
                SplitMesh(mesh, submeshVertLists, submeshFaceLists, submeshVertIndices);

                remSkin       boneList        = rem.FindSkin(mesh.name, parser.SKIC);
                bool          skinned         = boneList != null;
                int           numBones        = skinned ? boneList.Count : 0;
                List <string> boneNamesList   = new List <string>(numBones);
                List <Matrix> boneOffsetsList = new List <Matrix>(numBones);
                for (int boneIdx = 0; boneIdx < numBones; boneIdx++)
                {
                    boneNamesList.Add(boneList[boneIdx].bone.ToString());
                    boneOffsetsList.Add(boneList[boneIdx].matrix);
                }
                List <string> boneFrameParentNames    = new List <string>(numBones);
                List <Matrix> boneFrameParentMatrices = new List <Matrix>(numBones);
                for (int boneIdx = 0; boneIdx < numBones; boneIdx++)
                {
                    remBone boneFrame = rem.FindFrame(boneList[boneIdx].bone, parser.BONC.rootFrame);
                    if (boneFrame == null)
                    {
                        continue;
                    }
                    remBone boneFrameParent = boneFrame.Parent;
                    if (!boneNamesList.Contains(boneFrameParent.name) && !boneFrameParentNames.Contains(boneFrameParent.name))
                    {
                        boneFrameParentNames.Add(boneFrameParent.name);
                        Matrix incompleteMeshFrameCorrection = Matrix.Invert(frame.matrix);
                        boneFrameParentMatrices.Add(incompleteMeshFrameCorrection * Matrix.Invert(boneFrame.matrix) * boneList[boneIdx].matrix);
                    }
                }
                boneNamesList.AddRange(boneFrameParentNames);
                string[] boneNames = boneNamesList.ToArray();
                boneOffsetsList.AddRange(boneFrameParentMatrices);
                Matrix[] boneOffsets = boneOffsetsList.ToArray();

                AnimationMeshContainer[] meshContainers = new AnimationMeshContainer[submeshFaceLists.Count];
                Vector3 min = new Vector3(Single.MaxValue);
                Vector3 max = new Vector3(Single.MinValue);
                for (int i = 0; i < submeshFaceLists.Count; i++)
                {
                    List <ushort>    faceList   = submeshFaceLists[i];
                    List <remVertex> vertexList = submeshVertLists[i];

                    Mesh animationMesh = new Mesh(device, faceList.Count, vertexList.Count, MeshFlags.Managed, PositionBlendWeightsIndexedNormalTexturedColoured.Format);

                    using (DataStream indexStream = animationMesh.LockIndexBuffer(LockFlags.None))
                    {
                        for (int j = 0; j < faceList.Count; j++)
                        {
                            indexStream.Write(faceList[j]);
                        }
                        animationMesh.UnlockIndexBuffer();
                    }

                    byte[][]  vertexBoneIndices = null;
                    float[][] vertexWeights     = ConvertVertexWeights(vertexList, submeshVertIndices[i], boneList, out vertexBoneIndices);
                    FillVertexBuffer(animationMesh, vertexList, vertexWeights, vertexBoneIndices, -1);

                    var normalLines = new PositionBlendWeightsIndexedColored[vertexList.Count * 2];
                    for (int j = 0; j < vertexList.Count; j++)
                    {
                        remVertex vertex      = vertexList[j];
                        Vector3   position    = vertex.Position;
                        Vector3   normal      = vertex.Normal;
                        float[]   boneWeights = vertexWeights[j];

                        normalLines[j * 2]       = new PositionBlendWeightsIndexedColored(position, boneWeights, vertexBoneIndices[j], Color.Coral.ToArgb());
                        normalLines[(j * 2) + 1] = new PositionBlendWeightsIndexedColored(position + normal, boneWeights, vertexBoneIndices[j], Color.Blue.ToArgb());

#if !DONT_MIRROR
                        position.Z *= -1f;
#endif
                        min = Vector3.Minimize(min, position);
                        max = Vector3.Maximize(max, position);
                    }

                    AnimationMeshContainer meshContainer = new AnimationMeshContainer();
                    meshContainer.Name        = animationFrame.Name;
                    meshContainer.MeshData    = new MeshData(animationMesh);
                    meshContainer.NormalLines = normalLines;
                    meshContainer.BoneNames   = boneNames;
                    meshContainer.BoneOffsets = boneOffsets;
                    meshContainers[i]         = meshContainer;

                    remMaterial mat = rem.FindMaterial(mesh.materials[i], parser.MATC);
                    if (mat != null)
                    {
                        Material material3D = new Material();
                        material3D.Ambient  = new Color4(mat.ambient);
                        material3D.Diffuse  = new Color4(mat.diffuse);
                        material3D.Emissive = new Color4(mat.emissive);
                        material3D.Specular = new Color4(mat.specular);
                        material3D.Power    = mat.specularPower;
                        int matIdx = parser.MATC.IndexOf(mat);
                        Materials[matIdx]           = material3D;
                        meshContainer.MaterialIndex = matIdx;

                        int texIdx = 0;
                        if (mat.texture != null && !TextureDic.TryGetValue(mat.texture.ToString(), out texIdx))
                        {
                            ImportedTexture importedTex = null;
                            if (!ImportedTextures.TryGetValue(mat.texture.ToString(), out importedTex))
                            {
                                importedTex = rem.ImportedTexture(mat.texture, parser.RemPath, true);
                                if (importedTex == null)
                                {
                                    Report.ReportLog("Export textures of TEXH.FPK!");
                                    continue;
                                }
                                ImportedTextures.Add(mat.texture.ToString(), importedTex);
                            }
                            Texture memTex = Texture.FromMemory(device, importedTex.Data);
                            texIdx = TextureDic.Count;
                            TextureDic.Add(mat.texture.ToString(), texIdx);
                            Textures.Add(memTex);
                        }
                        meshContainer.TextureIndex = texIdx;
                    }
                }

                for (int i = 0; i < (meshContainers.Length - 1); i++)
                {
                    meshContainers[i].NextMeshContainer = meshContainers[i + 1];
                }

                min = Vector3.TransformCoordinate(min, animationFrame.CombinedTransform);
                max = Vector3.TransformCoordinate(max, animationFrame.CombinedTransform);
                animationFrame.Bounds        = new BoundingBox(min, max);
                animationFrame.MeshContainer = meshContainers[0];
                meshFrames.Add(animationFrame);
            }

            for (int i = 0; i < frame.Count; i++)
            {
                remBone child = frame[i];
                if (extractFrames.Contains(child.name.ToString()))
                {
                    AnimationFrame childAnimationFrame = CreateFrame(child, parser, extractFrames, mesh, device, animationFrame.CombinedTransform, meshFrames);
                    childAnimationFrame.Parent = animationFrame;
                    animationFrame.AppendChild(childAnimationFrame);
                }
            }

            numFrames++;
            return(animationFrame);
        }