Example #1
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);
            }
        }
Example #2
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);
            }
        }
Example #3
0
        public static void ReplaceMesh(xxFrame frame, xxParser parser, WorkspaceMesh mesh, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod, bool targetFullMesh)
        {
            Matrix  transform      = Matrix.Identity;
            xxFrame transformFrame = frame;

            while (transformFrame != null)
            {
                transform      = transformFrame.Matrix * transform;
                transformFrame = (xxFrame)transformFrame.Parent;
            }
            transform.Invert();

            string[] materialNames;
            int[]    indices;
            bool[]   worldCoords;
            bool[]   replaceSubmeshesOption;
            xxMesh   xxMesh = CreateMesh(mesh, parser.Format, out materialNames, out indices, out worldCoords, out replaceSubmeshesOption);

            List <xxVertex> allVertices = null;

            if (frame.Mesh == null)
            {
                CreateUnknowns(xxMesh);
            }
            else
            {
                CopyUnknowns(frame.Mesh, xxMesh);

                if ((bonesMethod == CopyMeshMethod.CopyOrder) || (bonesMethod == CopyMeshMethod.CopyNear))
                {
                    xxMesh.BoneList = new List <xxBone>(frame.Mesh.BoneList.Count);
                    for (int i = 0; i < frame.Mesh.BoneList.Count; i++)
                    {
                        xxMesh.BoneList.Add(frame.Mesh.BoneList[i].Clone());
                    }
                }

                if (targetFullMesh && (normalsMethod == CopyMeshMethod.CopyNear || bonesMethod == CopyMeshMethod.CopyNear))
                {
                    allVertices = new List <xxVertex>();
                    HashSet <Vector3> posSet = new HashSet <Vector3>(new VertexPositionComparer());
                    foreach (xxSubmesh submesh in frame.Mesh.SubmeshList)
                    {
                        allVertices.Capacity = allVertices.Count + submesh.VertexList.Count;
                        foreach (xxVertex vertex in submesh.VertexList)
                        {
                            if (!posSet.Contains(vertex.Position))
                            {
                                posSet.Add(vertex.Position);
                                allVertices.Add(vertex);
                            }
                        }
                    }
                }
            }

            xxSubmesh[]      replaceSubmeshes = (frame.Mesh == null) ? null : new xxSubmesh[frame.Mesh.SubmeshList.Count];
            List <xxSubmesh> addSubmeshes     = new List <xxSubmesh>(xxMesh.SubmeshList.Count);

            for (int i = 0; i < xxMesh.SubmeshList.Count; i++)
            {
                for (int j = 0; j < parser.MaterialList.Count; j++)
                {
                    if (parser.MaterialList[j].Name == materialNames[i])
                    {
                        xxMesh.SubmeshList[i].MaterialIndex = j;
                        break;
                    }
                }

                xxSubmesh       xxSubmesh    = xxMesh.SubmeshList[i];
                List <xxVertex> xxVertexList = xxSubmesh.VertexList;
                if (worldCoords[i])
                {
                    for (int j = 0; j < xxVertexList.Count; j++)
                    {
                        xxVertexList[j].Position = Vector3.TransformCoordinate(xxVertexList[j].Position, transform);
                    }
                }

                xxSubmesh baseSubmesh = null;
                int       idx         = indices[i];
                if ((frame.Mesh != null) && (idx >= 0) && (idx < frame.Mesh.SubmeshList.Count))
                {
                    baseSubmesh = frame.Mesh.SubmeshList[idx];
                    CopyUnknowns(baseSubmesh, xxSubmesh, parser.Format, xxMesh.NumVector2PerVertex);
                }
                else
                {
                    CreateUnknowns(xxSubmesh, parser.Format, xxMesh.NumVector2PerVertex);
                }

                if (baseSubmesh != null)
                {
                    if (normalsMethod == CopyMeshMethod.CopyOrder)
                    {
                        xx.CopyNormalsOrder(baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }
                    else if (normalsMethod == CopyMeshMethod.CopyNear)
                    {
                        xx.CopyNormalsNear(targetFullMesh ? allVertices : baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }

                    if (bonesMethod == CopyMeshMethod.CopyOrder)
                    {
                        xx.CopyBonesOrder(baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }
                    else if (bonesMethod == CopyMeshMethod.CopyNear)
                    {
                        xx.CopyBonesNear(targetFullMesh ? allVertices : baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }
                }

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

            if ((frame.Mesh != null) && merge)
            {
                xxMesh.SubmeshList = new List <xxSubmesh>(replaceSubmeshes.Length + addSubmeshes.Count);
                List <xxSubmesh> copiedSubmeshes = new List <xxSubmesh>(replaceSubmeshes.Length);
                for (int i = 0; i < replaceSubmeshes.Length; i++)
                {
                    if (replaceSubmeshes[i] == null)
                    {
                        xxSubmesh xxSubmesh = frame.Mesh.SubmeshList[i].Clone();
                        copiedSubmeshes.Add(xxSubmesh);
                        xxMesh.SubmeshList.Add(xxSubmesh);
                    }
                    else
                    {
                        xxMesh.SubmeshList.Add(replaceSubmeshes[i]);
                    }
                }
                xxMesh.SubmeshList.AddRange(addSubmeshes);

                if ((frame.Mesh.BoneList.Count == 0) && (xxMesh.BoneList.Count > 0))
                {
                    for (int i = 0; i < copiedSubmeshes.Count; i++)
                    {
                        List <xxVertex> vertexList = copiedSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            vertexList[j].BoneIndices = new byte[4] {
                                0xFF, 0xFF, 0xFF, 0xFF
                            };
                        }
                    }
                }
                else if ((frame.Mesh.BoneList.Count > 0) && (xxMesh.BoneList.Count == 0))
                {
                    for (int i = 0; i < replaceSubmeshes.Length; i++)
                    {
                        if (replaceSubmeshes[i] != null)
                        {
                            List <xxVertex> vertexList = replaceSubmeshes[i].VertexList;
                            for (int j = 0; j < vertexList.Count; j++)
                            {
                                vertexList[j].BoneIndices = new byte[4] {
                                    0xFF, 0xFF, 0xFF, 0xFF
                                };
                            }
                        }
                    }
                    for (int i = 0; i < addSubmeshes.Count; i++)
                    {
                        List <xxVertex> vertexList = addSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            vertexList[j].BoneIndices = new byte[4] {
                                0xFF, 0xFF, 0xFF, 0xFF
                            };
                        }
                    }
                }
                else if ((frame.Mesh.BoneList.Count > 0) && (xxMesh.BoneList.Count > 0))
                {
                    byte[] boneIdxMap;
                    xxMesh.BoneList = MergeBoneList(frame.Mesh.BoneList, xxMesh.BoneList, out boneIdxMap);
                    for (int i = 0; i < replaceSubmeshes.Length; i++)
                    {
                        if (replaceSubmeshes[i] != null)
                        {
                            List <xxVertex> vertexList = replaceSubmeshes[i].VertexList;
                            for (int j = 0; j < vertexList.Count; j++)
                            {
                                byte[] boneIndices = vertexList[j].BoneIndices;
                                vertexList[j].BoneIndices = new byte[4];
                                for (int k = 0; k < 4; k++)
                                {
                                    vertexList[j].BoneIndices[k] = boneIndices[k] < 0xFF ? boneIdxMap[boneIndices[k]] : (byte)0xFF;
                                }
                            }
                        }
                    }
                    for (int i = 0; i < addSubmeshes.Count; i++)
                    {
                        List <xxVertex> vertexList = addSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            byte[] boneIndices = vertexList[j].BoneIndices;
                            vertexList[j].BoneIndices = new byte[4];
                            for (int k = 0; k < 4; k++)
                            {
                                vertexList[j].BoneIndices[k] = boneIndices[k] < 0xFF ? boneIdxMap[boneIndices[k]] : (byte)0xFF;
                            }
                        }
                    }
                }
            }

            if ((xxMesh.NumVector2PerVertex > 0) || ((frame.Mesh != null) && merge))
            {
                xxMesh.VertexListDuplicate = CreateVertexListDup(xxMesh.SubmeshList);
            }

            frame.Mesh = xxMesh;
            SetBoundingBox(frame);
        }
Example #4
0
 private void radioButtonNormalsCopyOrder_CheckedChanged(object sender, EventArgs e)
 {
     NormalsMethod = CopyMeshMethod.CopyOrder;
 }
Example #5
0
 private void radioButtonNormalsReplace_CheckedChanged(object sender, EventArgs e)
 {
     NormalsMethod = CopyMeshMethod.Replace;
 }
Example #6
0
 private void radioButtonBonesCopyOrder_CheckedChanged(object sender, EventArgs e)
 {
     BonesMethod = CopyMeshMethod.CopyOrder;
 }
Example #7
0
        public static void ReplaceMesh(odfFrame frame, odfParser parser, WorkspaceMesh mesh, List<ImportedMaterial> materials, List<ImportedTexture> textures, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod)
        {
            Matrix transform = Matrix.Identity;
            odfFrame transformFrame = frame;
            while (transformFrame != null)
            {
                transform *= transformFrame.Matrix;
                transformFrame = transformFrame.Parent as odfFrame;
            }
            transform.Invert();

            string[] materialNames;
            int[] indices;
            bool[] worldCoords;
            bool[] replaceSubmeshesOption;
            odfMesh newMesh = CreateMesh(mesh, parser.MeshSection._FormatType, out materialNames, out indices, out worldCoords, out replaceSubmeshesOption);

            odfMesh frameMesh = odf.FindMeshListSome(frame.MeshId, parser.MeshSection);
            if (frameMesh != null)
            {
                if (parser.UsedIDs == null) // prevent misleading error message
                {
                    parser.CollectObjectIDs();
                }
                newMesh.Id = frameMesh.Id;
                newMesh.Name = frameMesh.Name;
                parser.MeshSection.InsertChild(parser.MeshSection.IndexOf(frameMesh), newMesh);
            }
            else
            {
                newMesh.Id = parser.GetNewID(typeof(odfMesh));
                frame.MeshId = newMesh.Id;
                parser.MeshSection.AddChild(newMesh);
            }

            Dictionary<ObjectID, ObjectID> submeshIDtranslation = new Dictionary<ObjectID, ObjectID>(newMesh.Count);
            odfSubmesh[] replaceSubmeshes = frameMesh != null ? new odfSubmesh[frameMesh.Count] : null;
            List<odfSubmesh> addSubmeshes = new List<odfSubmesh>(newMesh.Count);
            for (int i = 0; i < newMesh.Count; i++)
            {
                ObjectID[] texIDs = new ObjectID[4] { ObjectID.INVALID, ObjectID.INVALID, ObjectID.INVALID, ObjectID.INVALID };
                odfMaterial mat = odf.FindMaterialInfo(materialNames[i], parser.MaterialSection);
                if (materials != null && mat == null)
                {
                    ImportedMaterial impMat = ImportedHelpers.FindMaterial(materialNames[i], materials);
                    if (impMat != null)
                    {
                        mat = CreateMaterial(impMat, parser.GetNewID(typeof(odfMaterial)));
                        parser.MaterialSection.AddChild(mat);
                        for (int j = 0; j < impMat.Textures.Length; j++)
                        {
                            string texName = impMat.Textures[j];
                            odfTexture tex = odf.FindTextureInfo(texName, parser.TextureSection);
                            if (tex == null)
                            {
                                ImportedTexture impTex = ImportedHelpers.FindTexture(texName, textures);
                                if (impTex != null)
                                {
                                    tex = CreateTexture(impTex, parser.GetNewID(typeof(odfTexture)), parser.TextureSection._FormatType, Path.GetDirectoryName(parser.ODFPath));
                                    parser.TextureSection.AddChild(tex);
                                    texIDs[j] = tex.Id;
                                }
                            }
                            else
                            {
                                texIDs[j] = tex.Id;
                            }
                        }
                    }
                }

                odfSubmesh newSubmesh = newMesh[i];
                newSubmesh.Id = parser.GetNewID(typeof(odfSubmesh));
                newSubmesh.MaterialId = mat != null ? mat.Id : ObjectID.INVALID;
                newSubmesh.TextureIds = texIDs;

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

                odfSubmesh baseSubmesh = null;
                odfBoneList newBones = null;
                int newBonesIdx = -1;
                int idx = indices[i];
                if ((frameMesh != null) && (idx >= 0) && (idx < frameMesh.Count))
                {
                    baseSubmesh = frameMesh[idx];
                    submeshIDtranslation.Add(newSubmesh.Id, baseSubmesh.Id);
                    for (int j = 0; j < baseSubmesh.TextureIds.Length; j++)
                    {
                        ObjectID texID = baseSubmesh.TextureIds[j];
                        newSubmesh.TextureIds[j] = texID;
                    }
                    newSubmesh.Name = new ObjectName(baseSubmesh.Name.Name, baseSubmesh.Name.Info);
                    CopyUnknowns(baseSubmesh, newSubmesh, parser.MeshSection._FormatType);

                    if ((bonesMethod == CopyMeshMethod.CopyOrder) || (bonesMethod == CopyMeshMethod.CopyNear))
                    {
                        odfBoneList baseBones = odf.FindBoneList(baseSubmesh.Id, parser.EnvelopeSection);
                        if (baseBones != null)
                        {
                            newBones = baseBones.Clone();
                            newBones.Id = ObjectID.INVALID;// parser.GetNewID(typeof(odfBoneList));
                            newBones.SubmeshId = newSubmesh.Id;
                            newBonesIdx = parser.EnvelopeSection.IndexOf(baseBones);
                        }
                    }
                    else if (bonesMethod == CopyMeshMethod.Replace)
                    {
                        newBones = CreateBoneList(ObjectID.INVALID/*parser.GetNewID(typeof(odfBoneList))*/, frame.Id, newSubmesh, mesh.BoneList, transform, parser.FrameSection.RootFrame);
                        newBonesIdx = parser.EnvelopeSection.Count;
                    }
                }
                else
                {
                    CreateUnknowns(newSubmesh, parser.MeshSection._FormatType);

                    newBones = CreateBoneList(ObjectID.INVALID/*parser.GetNewID(typeof(odfBoneList))*/, frame.Id, newSubmesh, mesh.BoneList, transform, parser.FrameSection.RootFrame);
                    newBonesIdx = parser.EnvelopeSection.Count;
                }
                if (newBones != null)
                {
                    parser.EnvelopeSection.InsertChild(newBonesIdx, newBones);
                }

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

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

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

            if ((frameMesh != null) && merge)
            {
                newMesh.Clear();
                newMesh.Capacity = replaceSubmeshes.Length + addSubmeshes.Count;
                for (int i = 0, submeshesRemoved = 0; i < replaceSubmeshes.Length; i++)
                {
                    if (replaceSubmeshes[i] == null)
                    {
                        odfSubmesh newSubmesh = frameMesh[i - submeshesRemoved++];
                        frameMesh.RemoveChild(newSubmesh); // save the bone list from being deleted in RemoveMesh
                        newMesh.AddChild(newSubmesh);
                    }
                    else
                    {
                        newMesh.AddChild(replaceSubmeshes[i]);
                    }
                }
                newMesh.AddRange(addSubmeshes);
            }

            if (frameMesh != null)
            {
                RemoveMesh(parser, frameMesh, frame, false);
                parser.UsedIDs.Add((int)newMesh.Id, typeof(odfMesh));
                frame.MeshId = newMesh.Id;
                List<ObjectID> removeKeyList = new List<ObjectID>();
                foreach (odfSubmesh submesh in newMesh)
                {
                    ObjectID newSubmeshID = submesh.Id;
                    ObjectID baseSubmeshID;
                    if (submeshIDtranslation.TryGetValue(newSubmeshID, out baseSubmeshID))
                    {
                        if (odf.FindBoneList(baseSubmeshID, parser.EnvelopeSection) == null)
                        {
                            odfBoneList boneList = odf.FindBoneList(newSubmeshID, parser.EnvelopeSection);
                            if (boneList != null)
                                boneList.SubmeshId = baseSubmeshID;
                            submesh.Id = baseSubmeshID;
                            parser.UsedIDs.Remove((int)newSubmeshID);
                        }

                        foreach (KeyValuePair<ObjectID, ObjectID> pair in submeshIDtranslation)
                        {
                            if (pair.Value == baseSubmeshID)
                            {
                                removeKeyList.Add(pair.Key);
                            }
                        }
                        foreach (ObjectID removeId in removeKeyList)
                        {
                            submeshIDtranslation.Remove(removeId);
                        }
                        removeKeyList.Clear();
                    }
                }
            }
        }
Example #8
0
 private void radioButtonBonesReplace_CheckedChanged(object sender, EventArgs e)
 {
     BonesMethod = CopyMeshMethod.Replace;
 }
Example #9
0
        public static void ReplaceMesh(odfFrame frame, odfParser parser, WorkspaceMesh mesh, List <ImportedMaterial> materials, List <ImportedTexture> textures, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod)
        {
            Matrix   transform      = Matrix.Identity;
            odfFrame transformFrame = frame;

            while (transformFrame != null)
            {
                transform     *= transformFrame.Matrix;
                transformFrame = transformFrame.Parent as odfFrame;
            }
            transform.Invert();

            string[] materialNames;
            int[]    indices;
            bool[]   worldCoords;
            bool[]   replaceSubmeshesOption;
            odfMesh  newMesh = CreateMesh(mesh, parser.MeshSection._FormatType, out materialNames, out indices, out worldCoords, out replaceSubmeshesOption);

            odfMesh frameMesh = odf.FindMeshListSome(frame.MeshId, parser.MeshSection);

            if (frameMesh != null)
            {
                if (parser.UsedIDs == null)                 // prevent misleading error message
                {
                    parser.CollectObjectIDs();
                }
                newMesh.Id   = frameMesh.Id;
                newMesh.Name = frameMesh.Name;
                parser.MeshSection.InsertChild(parser.MeshSection.IndexOf(frameMesh), newMesh);
            }
            else
            {
                newMesh.Id   = parser.GetNewID(typeof(odfMesh));
                frame.MeshId = newMesh.Id;
                parser.MeshSection.AddChild(newMesh);
            }

            Dictionary <ObjectID, ObjectID> submeshIDtranslation = new Dictionary <ObjectID, ObjectID>(newMesh.Count);

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

            for (int i = 0; i < newMesh.Count; i++)
            {
                ObjectID[] texIDs = new ObjectID[4] {
                    ObjectID.INVALID, ObjectID.INVALID, ObjectID.INVALID, ObjectID.INVALID
                };
                odfMaterial mat = odf.FindMaterialInfo(materialNames[i], parser.MaterialSection);
                if (materials != null && mat == null)
                {
                    ImportedMaterial impMat = ImportedHelpers.FindMaterial(materialNames[i], materials);
                    if (impMat != null)
                    {
                        mat = CreateMaterial(impMat, parser.GetNewID(typeof(odfMaterial)));
                        parser.MaterialSection.AddChild(mat);
                        for (int j = 0; j < impMat.Textures.Length; j++)
                        {
                            string     texName = impMat.Textures[j];
                            odfTexture tex     = odf.FindTextureInfo(texName, parser.TextureSection);
                            if (tex == null)
                            {
                                ImportedTexture impTex = ImportedHelpers.FindTexture(texName, textures);
                                if (impTex != null)
                                {
                                    tex = CreateTexture(impTex, parser.GetNewID(typeof(odfTexture)), parser.TextureSection._FormatType, Path.GetDirectoryName(parser.ODFPath));
                                    parser.TextureSection.AddChild(tex);
                                    texIDs[j] = tex.Id;
                                }
                            }
                            else
                            {
                                texIDs[j] = tex.Id;
                            }
                        }
                    }
                }

                odfSubmesh newSubmesh = newMesh[i];
                newSubmesh.Id         = parser.GetNewID(typeof(odfSubmesh));
                newSubmesh.MaterialId = mat != null ? mat.Id : ObjectID.INVALID;
                newSubmesh.TextureIds = texIDs;

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

                odfSubmesh  baseSubmesh = null;
                odfBoneList newBones    = null;
                int         newBonesIdx = -1;
                int         idx         = indices[i];
                if ((frameMesh != null) && (idx >= 0) && (idx < frameMesh.Count))
                {
                    baseSubmesh = frameMesh[idx];
                    submeshIDtranslation.Add(newSubmesh.Id, baseSubmesh.Id);
                    for (int j = 0; j < baseSubmesh.TextureIds.Length; j++)
                    {
                        ObjectID texID = baseSubmesh.TextureIds[j];
                        newSubmesh.TextureIds[j] = texID;
                    }
                    newSubmesh.Name = new ObjectName(baseSubmesh.Name.Name, baseSubmesh.Name.Info);
                    CopyUnknowns(baseSubmesh, newSubmesh, parser.MeshSection._FormatType);

                    if ((bonesMethod == CopyMeshMethod.CopyOrder) || (bonesMethod == CopyMeshMethod.CopyNear))
                    {
                        odfBoneList baseBones = odf.FindBoneList(baseSubmesh.Id, parser.EnvelopeSection);
                        if (baseBones != null)
                        {
                            newBones           = baseBones.Clone();
                            newBones.Id        = ObjectID.INVALID;                     // parser.GetNewID(typeof(odfBoneList));
                            newBones.SubmeshId = newSubmesh.Id;
                            newBonesIdx        = parser.EnvelopeSection.IndexOf(baseBones);
                        }
                    }
                    else if (bonesMethod == CopyMeshMethod.Replace)
                    {
                        newBones    = CreateBoneList(ObjectID.INVALID /*parser.GetNewID(typeof(odfBoneList))*/, frame.Id, newSubmesh, mesh.BoneList, transform, parser.FrameSection.RootFrame);
                        newBonesIdx = parser.EnvelopeSection.Count;
                    }
                }
                else
                {
                    CreateUnknowns(newSubmesh, parser.MeshSection._FormatType);

                    newBones    = CreateBoneList(ObjectID.INVALID /*parser.GetNewID(typeof(odfBoneList))*/, frame.Id, newSubmesh, mesh.BoneList, transform, parser.FrameSection.RootFrame);
                    newBonesIdx = parser.EnvelopeSection.Count;
                }
                if (newBones != null)
                {
                    parser.EnvelopeSection.InsertChild(newBonesIdx, newBones);
                }

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

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

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

            if ((frameMesh != null) && merge)
            {
                newMesh.Clear();
                newMesh.Capacity = replaceSubmeshes.Length + addSubmeshes.Count;
                for (int i = 0, submeshesRemoved = 0; i < replaceSubmeshes.Length; i++)
                {
                    if (replaceSubmeshes[i] == null)
                    {
                        odfSubmesh newSubmesh = frameMesh[i - submeshesRemoved++];
                        frameMesh.RemoveChild(newSubmesh);                         // save the bone list from being deleted in RemoveMesh
                        newMesh.AddChild(newSubmesh);
                    }
                    else
                    {
                        newMesh.AddChild(replaceSubmeshes[i]);
                    }
                }
                newMesh.AddRange(addSubmeshes);
            }

            if (frameMesh != null)
            {
                RemoveMesh(parser, frameMesh, frame, false);
                parser.UsedIDs.Add((int)newMesh.Id, typeof(odfMesh));
                frame.MeshId = newMesh.Id;
                List <ObjectID> removeKeyList = new List <ObjectID>();
                foreach (odfSubmesh submesh in newMesh)
                {
                    ObjectID newSubmeshID = submesh.Id;
                    ObjectID baseSubmeshID;
                    if (submeshIDtranslation.TryGetValue(newSubmeshID, out baseSubmeshID))
                    {
                        if (odf.FindBoneList(baseSubmeshID, parser.EnvelopeSection) == null)
                        {
                            odfBoneList boneList = odf.FindBoneList(newSubmeshID, parser.EnvelopeSection);
                            if (boneList != null)
                            {
                                boneList.SubmeshId = baseSubmeshID;
                            }
                            submesh.Id = baseSubmeshID;
                            parser.UsedIDs.Remove((int)newSubmeshID);
                        }

                        foreach (KeyValuePair <ObjectID, ObjectID> pair in submeshIDtranslation)
                        {
                            if (pair.Value == baseSubmeshID)
                            {
                                removeKeyList.Add(pair.Key);
                            }
                        }
                        foreach (ObjectID removeId in removeKeyList)
                        {
                            submeshIDtranslation.Remove(removeId);
                        }
                        removeKeyList.Clear();
                    }
                }
            }
        }
Example #10
0
        public static void ReplaceMeshRenderer(Transform frame, Transform rootBone, Animator parser, List <Material> materials, WorkspaceMesh mesh, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod, bool targetFullMesh)
        {
            Matrix transform = Transform.WorldTransform(frame);

            int[]  indices;
            bool[] worldCoords;
            bool[] replaceSubmeshesOption;
            SkinnedMeshRenderer sMesh = CreateSkinnedMeshRenderer(parser, materials, mesh, transform, out indices, out worldCoords, out replaceSubmeshesOption);
            vMesh destMesh            = new Operations.vMesh(sMesh, true, false);

            SkinnedMeshRenderer sFrameMesh = frame.m_GameObject.instance.FindLinkedComponent(UnityClassID.SkinnedMeshRenderer);
            MeshRenderer        frameMeshR = sFrameMesh;

            if (sFrameMesh == null)
            {
                frameMeshR = frame.m_GameObject.instance.FindLinkedComponent(UnityClassID.MeshRenderer);
            }
            Mesh frameMesh = frameMeshR != null?Operations.GetMesh(frameMeshR) : null;

            vMesh          srcMesh     = null;
            List <vVertex> allVertices = null;

            if (frameMeshR == null || frameMesh == null)
            {
                sMesh.m_RootBone = new PPtr <Transform>(rootBone);
                if (rootBone != null)
                {
                    sMesh.m_Mesh.instance.m_RootBoneNameHash = parser.m_Avatar.instance.BoneHash(rootBone.m_GameObject.instance.m_Name);
                }
                if (frameMeshR != null)
                {
                    CopyUnknowns(frameMeshR, sMesh);
                }
            }
            else
            {
                if (sFrameMesh != null)
                {
                    sMesh.m_RootBone = new PPtr <Transform>(sFrameMesh.m_RootBone.instance);
                    sMesh.m_Mesh.instance.m_RootBoneNameHash = frameMesh.m_RootBoneNameHash;
                }
                else
                {
                    sMesh.m_RootBone = new PPtr <Transform>((Component)null);
                }

                srcMesh = new Operations.vMesh(frameMeshR, true, false);
                CopyUnknowns(frameMeshR, sMesh);

                if (targetFullMesh && (normalsMethod == CopyMeshMethod.CopyNear || bonesMethod == CopyMeshMethod.CopyNear))
                {
                    allVertices = new List <vVertex>();
                    HashSet <Vector3> posSet = new HashSet <Vector3>();
                    foreach (vSubmesh submesh in srcMesh.submeshes)
                    {
                        allVertices.Capacity = allVertices.Count + submesh.vertexList.Count;
                        foreach (vVertex vertex in submesh.vertexList)
                        {
                            if (!posSet.Contains(vertex.position))
                            {
                                posSet.Add(vertex.position);
                                allVertices.Add(vertex);
                            }
                        }
                    }
                }
            }

            transform.Invert();
            vSubmesh[]      replaceSubmeshes = (srcMesh == null) ? null : new vSubmesh[srcMesh.submeshes.Count];
            List <vSubmesh> addSubmeshes     = new List <vSubmesh>(destMesh.submeshes.Count);

            for (int i = 0; i < destMesh.submeshes.Count; i++)
            {
                vSubmesh       submesh     = destMesh.submeshes[i];
                List <vVertex> vVertexList = submesh.vertexList;
                if (worldCoords[i])
                {
                    for (int j = 0; j < vVertexList.Count; j++)
                    {
                        vVertexList[j].position = Vector3.TransformCoordinate(vVertexList[j].position, transform);
                    }
                }

                vSubmesh baseSubmesh = null;
                int      idx         = indices[i];
                if ((srcMesh != null) && (idx >= 0) && (idx < frameMesh.m_SubMeshes.Count))
                {
                    baseSubmesh = srcMesh.submeshes[idx];
                    CopyUnknowns(frameMesh.m_SubMeshes[idx], sMesh.m_Mesh.instance.m_SubMeshes[i]);
                }

                if (baseSubmesh != null)
                {
                    if (normalsMethod == CopyMeshMethod.CopyOrder)
                    {
                        Operations.CopyNormalsOrder(baseSubmesh.vertexList, submesh.vertexList);
                    }
                    else if (normalsMethod == CopyMeshMethod.CopyNear)
                    {
                        Operations.CopyNormalsNear(targetFullMesh ? allVertices : baseSubmesh.vertexList, submesh.vertexList);
                    }

                    if (baseSubmesh.vertexList[0].weights != null)
                    {
                        if (bonesMethod == CopyMeshMethod.CopyOrder)
                        {
                            Operations.CopyBonesOrder(baseSubmesh.vertexList, submesh.vertexList);
                        }
                        else if (bonesMethod == CopyMeshMethod.CopyNear)
                        {
                            Operations.CopyBonesNear(targetFullMesh ? allVertices : baseSubmesh.vertexList, submesh.vertexList);
                        }
                    }
                }

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

            if ((srcMesh != null) && merge)
            {
                destMesh.submeshes = new List <vSubmesh>(replaceSubmeshes.Length + addSubmeshes.Count);
                List <vSubmesh> copiedSubmeshes = new List <vSubmesh>(replaceSubmeshes.Length);
                for (int i = 0; i < replaceSubmeshes.Length; i++)
                {
                    if (replaceSubmeshes[i] == null)
                    {
                        vSubmesh srcSubmesh = srcMesh.submeshes[i];
                        copiedSubmeshes.Add(srcSubmesh);
                        destMesh.submeshes.Add(srcSubmesh);
                    }
                    else
                    {
                        destMesh.submeshes.Add(replaceSubmeshes[i]);
                    }
                }
                destMesh.submeshes.AddRange(addSubmeshes);

                if ((sFrameMesh == null || sFrameMesh.m_Bones.Count == 0) && (sMesh.m_Bones.Count > 0))
                {
                    for (int i = 0; i < copiedSubmeshes.Count; i++)
                    {
                        List <vVertex> vertexList = copiedSubmeshes[i].vertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            vertexList[j].boneIndices = new int[4] {
                                0, 0, 0, 0
                            };
                            vertexList[j].weights = new float[4] {
                                0, 0, 0, 0
                            };
                        }
                    }
                }
                else if (sFrameMesh != null && sFrameMesh.m_Bones.Count > 0)
                {
                    int[] boneIdxMap;
                    sMesh.m_Bones = MergeBoneList(sFrameMesh.m_Bones, sMesh.m_Bones, out boneIdxMap);
                    uint[]   boneHashes   = new uint[sMesh.m_Bones.Count];
                    Matrix[] poseMatrices = new Matrix[sMesh.m_Bones.Count];
                    for (int i = 0; i < sFrameMesh.m_Bones.Count; i++)
                    {
                        boneHashes[i]   = sFrameMesh.m_Mesh.instance.m_BoneNameHashes[i];
                        poseMatrices[i] = sFrameMesh.m_Mesh.instance.m_BindPose[i];
                    }
                    for (int i = 0; i < boneIdxMap.Length; i++)
                    {
                        boneHashes[boneIdxMap[i]]   = sMesh.m_Mesh.instance.m_BoneNameHashes[i];
                        poseMatrices[boneIdxMap[i]] = sMesh.m_Mesh.instance.m_BindPose[i];
                    }
                    sMesh.m_Mesh.instance.m_BoneNameHashes.Clear();
                    sMesh.m_Mesh.instance.m_BoneNameHashes.AddRange(boneHashes);
                    sMesh.m_Mesh.instance.m_BindPose.Clear();
                    sMesh.m_Mesh.instance.m_BindPose.AddRange(poseMatrices);

                    if (bonesMethod == CopyMeshMethod.Replace)
                    {
                        for (int i = 0; i < replaceSubmeshes.Length; i++)
                        {
                            if (replaceSubmeshes[i] != null)
                            {
                                List <vVertex> vertexList = replaceSubmeshes[i].vertexList;
                                if (vertexList[0].boneIndices != null)
                                {
                                    for (int j = 0; j < vertexList.Count; j++)
                                    {
                                        int[] boneIndices = vertexList[j].boneIndices;
                                        vertexList[j].boneIndices = new int[4];
                                        for (int k = 0; k < 4; k++)
                                        {
                                            vertexList[j].boneIndices[k] = boneIdxMap[boneIndices[k]];
                                        }
                                    }
                                }
                            }
                        }
                        for (int i = 0; i < addSubmeshes.Count; i++)
                        {
                            List <vVertex> vertexList = addSubmeshes[i].vertexList;
                            if (vertexList[0].boneIndices != null)
                            {
                                for (int j = 0; j < vertexList.Count; j++)
                                {
                                    int[] boneIndices = vertexList[j].boneIndices;
                                    vertexList[j].boneIndices = new int[4];
                                    for (int k = 0; k < 4; k++)
                                    {
                                        vertexList[j].boneIndices[k] = boneIdxMap[boneIndices[k]];
                                    }
                                }
                            }
                        }
                    }
                }
            }
            destMesh.Flush();

            if (frameMeshR != null)
            {
                frame.m_GameObject.instance.RemoveLinkedComponent(frameMeshR);
                //parser.file.RemoveSubfile(frameMeshR);
                if (frameMesh != null)
                {
                    //parser.file.RemoveSubfile(frameMesh);
                    parser.file.ReplaceSubfile(frameMesh, sMesh.m_Mesh.asset);
                }
                parser.file.ReplaceSubfile(frameMeshR, sMesh);
            }
            frame.m_GameObject.instance.AddLinkedComponent(sMesh);

            AssetBundle bundle = parser.file.Bundle;

            if (bundle != null)
            {
                if (frameMeshR != null)
                {
                    if (frameMesh != null)
                    {
                        bundle.ReplaceComponent(frameMesh, sMesh.m_Mesh.asset);
                    }
                    bundle.ReplaceComponent(frameMeshR, sMesh);
                }
                else
                {
                    bundle.RegisterForUpdate(parser.m_GameObject.asset);
                }
            }
        }
Example #11
0
        public static void ReplaceMeshRenderer(Transform frame, Transform rootBone, Animator parser, List<Material> materials, WorkspaceMesh mesh, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod, bool targetFullMesh)
        {
            Matrix transform = Transform.WorldTransform(frame);

            int[] indices;
            bool[] worldCoords;
            bool[] replaceSubmeshesOption;
            SkinnedMeshRenderer sMesh = CreateSkinnedMeshRenderer(parser, materials, mesh, transform, out indices, out worldCoords, out replaceSubmeshesOption);
            vMesh destMesh = new Operations.vMesh(sMesh, true, false);

            SkinnedMeshRenderer sFrameMesh = frame.m_GameObject.instance.FindLinkedComponent(UnityClassID.SkinnedMeshRenderer);
            MeshRenderer frameMeshR = sFrameMesh;
            if (sFrameMesh == null)
            {
                frameMeshR = frame.m_GameObject.instance.FindLinkedComponent(UnityClassID.MeshRenderer);
            }
            Mesh frameMesh = frameMeshR != null ? Operations.GetMesh(frameMeshR) : null;
            vMesh srcMesh = null;
            List<vVertex> allVertices = null;
            if (frameMeshR == null || frameMesh == null)
            {
                sMesh.m_RootBone = new PPtr<Transform>(rootBone);
                if (rootBone != null)
                {
                    sMesh.m_Mesh.instance.m_RootBoneNameHash = parser.m_Avatar.instance.BoneHash(rootBone.m_GameObject.instance.m_Name);
                }
                if (frameMeshR != null)
                {
                    CopyUnknowns(frameMeshR, sMesh);
                }
            }
            else
            {
                if (sFrameMesh != null)
                {
                    sMesh.m_RootBone = new PPtr<Transform>(sFrameMesh.m_RootBone.instance);
                    sMesh.m_Mesh.instance.m_RootBoneNameHash = frameMesh.m_RootBoneNameHash;
                }
                else
                {
                    sMesh.m_RootBone = new PPtr<Transform>((Component)null);
                }

                srcMesh = new Operations.vMesh(frameMeshR, true, false);
                CopyUnknowns(frameMeshR, sMesh);

                if (targetFullMesh && (normalsMethod == CopyMeshMethod.CopyNear || bonesMethod == CopyMeshMethod.CopyNear))
                {
                    allVertices = new List<vVertex>();
                    HashSet<Vector3> posSet = new HashSet<Vector3>();
                    foreach (vSubmesh submesh in srcMesh.submeshes)
                    {
                        allVertices.Capacity = allVertices.Count + submesh.vertexList.Count;
                        foreach (vVertex vertex in submesh.vertexList)
                        {
                            if (!posSet.Contains(vertex.position))
                            {
                                posSet.Add(vertex.position);
                                allVertices.Add(vertex);
                            }
                        }
                    }
                }
            }

            transform.Invert();
            vSubmesh[] replaceSubmeshes = (srcMesh == null) ? null : new vSubmesh[srcMesh.submeshes.Count];
            List<vSubmesh> addSubmeshes = new List<vSubmesh>(destMesh.submeshes.Count);
            for (int i = 0; i < destMesh.submeshes.Count; i++)
            {
                vSubmesh submesh = destMesh.submeshes[i];
                List<vVertex> vVertexList = submesh.vertexList;
                if (worldCoords[i])
                {
                    for (int j = 0; j < vVertexList.Count; j++)
                    {
                        vVertexList[j].position = Vector3.TransformCoordinate(vVertexList[j].position, transform);
                    }
                }

                vSubmesh baseSubmesh = null;
                int idx = indices[i];
                if ((srcMesh != null) && (idx >= 0) && (idx < frameMesh.m_SubMeshes.Count))
                {
                    baseSubmesh = srcMesh.submeshes[idx];
                    CopyUnknowns(frameMesh.m_SubMeshes[idx], sMesh.m_Mesh.instance.m_SubMeshes[i]);
                }

                if (baseSubmesh != null)
                {
                    if (normalsMethod == CopyMeshMethod.CopyOrder)
                    {
                        Operations.CopyNormalsOrder(baseSubmesh.vertexList, submesh.vertexList);
                    }
                    else if (normalsMethod == CopyMeshMethod.CopyNear)
                    {
                        Operations.CopyNormalsNear(targetFullMesh ? allVertices : baseSubmesh.vertexList, submesh.vertexList);
                    }

                    if (baseSubmesh.vertexList[0].weights != null)
                    {
                        if (bonesMethod == CopyMeshMethod.CopyOrder)
                        {
                            Operations.CopyBonesOrder(baseSubmesh.vertexList, submesh.vertexList);
                        }
                        else if (bonesMethod == CopyMeshMethod.CopyNear)
                        {
                            Operations.CopyBonesNear(targetFullMesh ? allVertices : baseSubmesh.vertexList, submesh.vertexList);
                        }
                    }
                }

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

            if ((srcMesh != null) && merge)
            {
                destMesh.submeshes = new List<vSubmesh>(replaceSubmeshes.Length + addSubmeshes.Count);
                List<vSubmesh> copiedSubmeshes = new List<vSubmesh>(replaceSubmeshes.Length);
                for (int i = 0; i < replaceSubmeshes.Length; i++)
                {
                    if (replaceSubmeshes[i] == null)
                    {
                        vSubmesh srcSubmesh = srcMesh.submeshes[i];
                        copiedSubmeshes.Add(srcSubmesh);
                        destMesh.submeshes.Add(srcSubmesh);
                    }
                    else
                    {
                        destMesh.submeshes.Add(replaceSubmeshes[i]);
                    }
                }
                destMesh.submeshes.AddRange(addSubmeshes);

                if ((sFrameMesh == null || sFrameMesh.m_Bones.Count == 0) && (sMesh.m_Bones.Count > 0))
                {
                    for (int i = 0; i < copiedSubmeshes.Count; i++)
                    {
                        List<vVertex> vertexList = copiedSubmeshes[i].vertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            vertexList[j].boneIndices = new int[4] { 0, 0, 0, 0 };
                            vertexList[j].weights = new float[4] { 0, 0, 0, 0 };
                        }
                    }
                }
                else if (sFrameMesh != null && sFrameMesh.m_Bones.Count > 0)
                {
                    int[] boneIdxMap;
                    sMesh.m_Bones = MergeBoneList(sFrameMesh.m_Bones, sMesh.m_Bones, out boneIdxMap);
                    uint[] boneHashes = new uint[sMesh.m_Bones.Count];
                    Matrix[] poseMatrices = new Matrix[sMesh.m_Bones.Count];
                    for (int i = 0; i < sFrameMesh.m_Bones.Count; i++)
                    {
                        boneHashes[i] = sFrameMesh.m_Mesh.instance.m_BoneNameHashes[i];
                        poseMatrices[i] = sFrameMesh.m_Mesh.instance.m_BindPose[i];
                    }
                    for (int i = 0; i < boneIdxMap.Length; i++)
                    {
                        boneHashes[boneIdxMap[i]] = sMesh.m_Mesh.instance.m_BoneNameHashes[i];
                        poseMatrices[boneIdxMap[i]] = sMesh.m_Mesh.instance.m_BindPose[i];
                    }
                    sMesh.m_Mesh.instance.m_BoneNameHashes.Clear();
                    sMesh.m_Mesh.instance.m_BoneNameHashes.AddRange(boneHashes);
                    sMesh.m_Mesh.instance.m_BindPose.Clear();
                    sMesh.m_Mesh.instance.m_BindPose.AddRange(poseMatrices);

                    if (bonesMethod == CopyMeshMethod.Replace)
                    {
                        for (int i = 0; i < replaceSubmeshes.Length; i++)
                        {
                            if (replaceSubmeshes[i] != null)
                            {
                                List<vVertex> vertexList = replaceSubmeshes[i].vertexList;
                                if (vertexList[0].boneIndices != null)
                                {
                                    for (int j = 0; j < vertexList.Count; j++)
                                    {
                                        int[] boneIndices = vertexList[j].boneIndices;
                                        vertexList[j].boneIndices = new int[4];
                                        for (int k = 0; k < 4; k++)
                                        {
                                            vertexList[j].boneIndices[k] = boneIdxMap[boneIndices[k]];
                                        }
                                    }
                                }
                            }
                        }
                        for (int i = 0; i < addSubmeshes.Count; i++)
                        {
                            List<vVertex> vertexList = addSubmeshes[i].vertexList;
                            if (vertexList[0].boneIndices != null)
                            {
                                for (int j = 0; j < vertexList.Count; j++)
                                {
                                    int[] boneIndices = vertexList[j].boneIndices;
                                    vertexList[j].boneIndices = new int[4];
                                    for (int k = 0; k < 4; k++)
                                    {
                                        vertexList[j].boneIndices[k] = boneIdxMap[boneIndices[k]];
                                    }
                                }
                            }
                        }
                    }
                }
            }
            destMesh.Flush();

            if (frameMeshR != null)
            {
                frame.m_GameObject.instance.RemoveLinkedComponent(frameMeshR);
                //parser.file.RemoveSubfile(frameMeshR);
                if (frameMesh != null)
                {
                    //parser.file.RemoveSubfile(frameMesh);
                    parser.file.ReplaceSubfile(frameMesh, sMesh.m_Mesh.asset);
                }
                parser.file.ReplaceSubfile(frameMeshR, sMesh);
            }
            frame.m_GameObject.instance.AddLinkedComponent(sMesh);

            AssetBundle bundle = parser.file.Bundle;
            if (bundle != null)
            {
                if (frameMeshR != null)
                {
                    if (frameMesh != null)
                    {
                        bundle.ReplaceComponent(frameMesh, sMesh.m_Mesh.asset);
                    }
                    bundle.ReplaceComponent(frameMeshR, sMesh);
                }
                else
                {
                    bundle.RegisterForUpdate(parser.m_GameObject.asset);
                }
            }
        }
Example #12
0
        public static void ReplaceMesh(xxFrame frame, xxParser parser, WorkspaceMesh mesh, bool merge, CopyMeshMethod normalsMethod, CopyMeshMethod bonesMethod)
        {
            Matrix transform = Matrix.Identity;
            xxFrame transformFrame = frame;
            while (transformFrame != null)
            {
                transform = transformFrame.Matrix * transform;
                transformFrame = (xxFrame)transformFrame.Parent;
            }
            transform.Invert();

            string[] materialNames;
            int[] indices;
            bool[] worldCoords;
            bool[] replaceSubmeshesOption;
            xxMesh xxMesh = CreateMesh(mesh, parser.Format, out materialNames, out indices, out worldCoords, out replaceSubmeshesOption);

            if (frame.Mesh == null)
            {
                CreateUnknowns(xxMesh);
            }
            else
            {
                CopyUnknowns(frame.Mesh, xxMesh);

                if ((bonesMethod == CopyMeshMethod.CopyOrder) || (bonesMethod == CopyMeshMethod.CopyNear))
                {
                    xxMesh.BoneList = new List<xxBone>(frame.Mesh.BoneList.Count);
                    for (int i = 0; i < frame.Mesh.BoneList.Count; i++)
                    {
                        xxMesh.BoneList.Add(frame.Mesh.BoneList[i].Clone());
                    }
                }
            }

            xxSubmesh[] replaceSubmeshes = (frame.Mesh == null) ? null : new xxSubmesh[frame.Mesh.SubmeshList.Count];
            List<xxSubmesh> addSubmeshes = new List<xxSubmesh>(xxMesh.SubmeshList.Count);
            for (int i = 0; i < xxMesh.SubmeshList.Count; i++)
            {
                for (int j = 0; j < parser.MaterialList.Count; j++)
                {
                    if (parser.MaterialList[j].Name == materialNames[i])
                    {
                        xxMesh.SubmeshList[i].MaterialIndex = j;
                        break;
                    }
                }

                xxSubmesh xxSubmesh = xxMesh.SubmeshList[i];
                List<xxVertex> xxVertexList = xxSubmesh.VertexList;
                if (worldCoords[i])
                {
                    for (int j = 0; j < xxVertexList.Count; j++)
                    {
                        xxVertexList[j].Position = Vector3.TransformCoordinate(xxVertexList[j].Position, transform);
                    }
                }

                xxSubmesh baseSubmesh = null;
                int idx = indices[i];
                if ((frame.Mesh != null) && (idx >= 0) && (idx < frame.Mesh.SubmeshList.Count))
                {
                    baseSubmesh = frame.Mesh.SubmeshList[idx];
                    CopyUnknowns(baseSubmesh, xxSubmesh, parser.Format, xxMesh.NumVector2PerVertex);
                }
                else
                {
                    CreateUnknowns(xxSubmesh, parser.Format, xxMesh.NumVector2PerVertex);
                }

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

                    if (bonesMethod == CopyMeshMethod.CopyOrder)
                    {
                        xx.CopyBonesOrder(baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }
                    else if (bonesMethod == CopyMeshMethod.CopyNear)
                    {
                        xx.CopyBonesNear(baseSubmesh.VertexList, xxSubmesh.VertexList);
                    }
                }

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

            if ((frame.Mesh != null) && merge)
            {
                xxMesh.SubmeshList = new List<xxSubmesh>(replaceSubmeshes.Length + addSubmeshes.Count);
                List<xxSubmesh> copiedSubmeshes = new List<xxSubmesh>(replaceSubmeshes.Length);
                for (int i = 0; i < replaceSubmeshes.Length; i++)
                {
                    if (replaceSubmeshes[i] == null)
                    {
                        xxSubmesh xxSubmesh = frame.Mesh.SubmeshList[i].Clone();
                        copiedSubmeshes.Add(xxSubmesh);
                        xxMesh.SubmeshList.Add(xxSubmesh);
                    }
                    else
                    {
                        xxMesh.SubmeshList.Add(replaceSubmeshes[i]);
                    }
                }
                xxMesh.SubmeshList.AddRange(addSubmeshes);

                if ((frame.Mesh.BoneList.Count == 0) && (xxMesh.BoneList.Count > 0))
                {
                    for (int i = 0; i < copiedSubmeshes.Count; i++)
                    {
                        List<xxVertex> vertexList = copiedSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            vertexList[j].BoneIndices = new byte[4] { 0xFF, 0xFF, 0xFF, 0xFF };
                        }
                    }
                }
                else if ((frame.Mesh.BoneList.Count > 0) && (xxMesh.BoneList.Count == 0))
                {
                    for (int i = 0; i < replaceSubmeshes.Length; i++)
                    {
                        if (replaceSubmeshes[i] != null)
                        {
                            List<xxVertex> vertexList = replaceSubmeshes[i].VertexList;
                            for (int j = 0; j < vertexList.Count; j++)
                            {
                                vertexList[j].BoneIndices = new byte[4] { 0xFF, 0xFF, 0xFF, 0xFF };
                            }
                        }
                    }
                    for (int i = 0; i < addSubmeshes.Count; i++)
                    {
                        List<xxVertex> vertexList = addSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            vertexList[j].BoneIndices = new byte[4] { 0xFF, 0xFF, 0xFF, 0xFF };
                        }
                    }
                }
                else if ((frame.Mesh.BoneList.Count > 0) && (xxMesh.BoneList.Count > 0))
                {
                    byte[] boneIdxMap;
                    xxMesh.BoneList = MergeBoneList(frame.Mesh.BoneList, xxMesh.BoneList, out boneIdxMap);
                    for (int i = 0; i < replaceSubmeshes.Length; i++)
                    {
                        if (replaceSubmeshes[i] != null)
                        {
                            List<xxVertex> vertexList = replaceSubmeshes[i].VertexList;
                            for (int j = 0; j < vertexList.Count; j++)
                            {
                                byte[] boneIndices = vertexList[j].BoneIndices;
                                vertexList[j].BoneIndices = new byte[4];
                                for (int k = 0; k < 4; k++)
                                {
                                    vertexList[j].BoneIndices[k] = boneIndices[k] < 0xFF ? boneIdxMap[boneIndices[k]] : (byte)0xFF;
                                }
                            }
                        }
                    }
                    for (int i = 0; i < addSubmeshes.Count; i++)
                    {
                        List<xxVertex> vertexList = addSubmeshes[i].VertexList;
                        for (int j = 0; j < vertexList.Count; j++)
                        {
                            byte[] boneIndices = vertexList[j].BoneIndices;
                            vertexList[j].BoneIndices = new byte[4];
                            for (int k = 0; k < 4; k++)
                            {
                                vertexList[j].BoneIndices[k] = boneIndices[k] < 0xFF ? boneIdxMap[boneIndices[k]] : (byte)0xFF;
                            }
                        }
                    }
                }
            }

            if ((xxMesh.NumVector2PerVertex > 0) || ((frame.Mesh != null) && merge))
            {
                xxMesh.VertexListDuplicate = CreateVertexListDup(xxMesh.SubmeshList);
            }

            frame.Mesh = xxMesh;
            SetBoundingBox(frame);
        }