예제 #1
0
        public odfFace Clone()
        {
            odfFace newFace = new odfFace();

            newFace.VertexIndices = (ushort[])VertexIndices.Clone();
            return(newFace);
        }
예제 #2
0
        public static List <odfFace> ParseFaceList(BinaryReader reader, int numFaces)
        {
            List <odfFace> faceList = new List <odfFace>(numFaces);

            for (int faceIdx = 0; faceIdx < numFaces; faceIdx++)
            {
                odfFace face = new odfFace();
                face.VertexIndices = reader.ReadUInt16Array(3);

                faceList.Add(face);
            }
            return(faceList);
        }
예제 #3
0
파일: odfOps.cs 프로젝트: kkdevs/sb3u
        public static void CalculateNormals(List <Tuple <List <odfFace>, List <odfVertex> > > pairList, float threshold)
        {
            if (threshold < 0)
            {
                VertexRef[][] vertRefArray = new VertexRef[pairList.Count][];
                for (int i = 0; i < pairList.Count; i++)
                {
                    List <odfVertex> vertList = pairList[i].Item2;
                    vertRefArray[i] = new VertexRef[vertList.Count];
                    for (int j = 0; j < vertList.Count; j++)
                    {
                        odfVertex vert    = vertList[j];
                        VertexRef vertRef = new VertexRef();
                        vertRef.vert       = vert;
                        vertRef.norm       = new Vector3();
                        vertRefArray[i][j] = vertRef;
                    }
                }

                for (int i = 0; i < pairList.Count; i++)
                {
                    List <odfFace> faceList = pairList[i].Item1;
                    for (int j = 0; j < faceList.Count; j++)
                    {
                        odfFace face = faceList[j];
                        Vector3 v1   = vertRefArray[i][face.VertexIndices[1]].vert.Position - vertRefArray[i][face.VertexIndices[2]].vert.Position;
                        Vector3 v2   = vertRefArray[i][face.VertexIndices[0]].vert.Position - vertRefArray[i][face.VertexIndices[2]].vert.Position;
                        Vector3 norm = Vector3.Cross(v2, v1);
                        norm.Normalize();
                        for (int k = 0; k < face.VertexIndices.Length; k++)
                        {
                            vertRefArray[i][face.VertexIndices[k]].norm += norm;
                        }
                    }
                }

                for (int i = 0; i < vertRefArray.Length; i++)
                {
                    for (int j = 0; j < vertRefArray[i].Length; j++)
                    {
                        Vector3 norm = vertRefArray[i][j].norm;
                        norm.Normalize();
                        vertRefArray[i][j].vert.Normal = norm;
                    }
                }
            }
            else
            {
                int vertCount = 0;
                for (int i = 0; i < pairList.Count; i++)
                {
                    vertCount += pairList[i].Item2.Count;
                }

                VertexRefComparerX vertRefComparerX = new VertexRefComparerX();
                List <VertexRef>   vertRefListX     = new List <VertexRef>(vertCount);
                VertexRef[][]      vertRefArray     = new VertexRef[pairList.Count][];
                for (int i = 0; i < pairList.Count; i++)
                {
                    var vertList = pairList[i].Item2;
                    vertRefArray[i] = new VertexRef[vertList.Count];
                    for (int j = 0; j < vertList.Count; j++)
                    {
                        odfVertex vert    = vertList[j];
                        VertexRef vertRef = new VertexRef();
                        vertRef.vert       = vert;
                        vertRef.norm       = new Vector3();
                        vertRefArray[i][j] = vertRef;
                        vertRefListX.Add(vertRef);
                    }
                }
                vertRefListX.Sort(vertRefComparerX);

                for (int i = 0; i < pairList.Count; i++)
                {
                    var faceList = pairList[i].Item1;
                    for (int j = 0; j < faceList.Count; j++)
                    {
                        odfFace face = faceList[j];
                        Vector3 v1   = vertRefArray[i][face.VertexIndices[1]].vert.Position - vertRefArray[i][face.VertexIndices[2]].vert.Position;
                        Vector3 v2   = vertRefArray[i][face.VertexIndices[0]].vert.Position - vertRefArray[i][face.VertexIndices[2]].vert.Position;
                        Vector3 norm = Vector3.Cross(v2, v1);
                        norm.Normalize();
                        for (int k = 0; k < face.VertexIndices.Length; k++)
                        {
                            vertRefArray[i][face.VertexIndices[k]].norm += norm;
                        }
                    }
                }

                float squaredThreshold = threshold * threshold;
                while (vertRefListX.Count > 0)
                {
                    VertexRef        vertRef  = vertRefListX[vertRefListX.Count - 1];
                    List <VertexRef> dupList  = new List <VertexRef>();
                    List <VertexRef> dupListX = GetAxisDups(vertRef, vertRefListX, 0, threshold, null);
                    foreach (VertexRef dupRef in dupListX)
                    {
                        if (((vertRef.vert.Position.Y - dupRef.vert.Position.Y) <= threshold) &&
                            ((vertRef.vert.Position.Z - dupRef.vert.Position.Z) <= threshold) &&
                            ((vertRef.vert.Position - dupRef.vert.Position).LengthSquared() <= squaredThreshold))
                        {
                            dupList.Add(dupRef);
                        }
                    }
                    vertRefListX.RemoveAt(vertRefListX.Count - 1);

                    Vector3 norm = vertRef.norm;
                    foreach (VertexRef dupRef in dupList)
                    {
                        norm += dupRef.norm;
                        vertRefListX.Remove(dupRef);
                    }
                    norm.Normalize();

                    vertRef.vert.Normal = norm;
                    foreach (VertexRef dupRef in dupList)
                    {
                        dupRef.vert.Normal = norm;
                        vertRefListX.Remove(dupRef);
                    }
                }
            }
        }
예제 #4
0
        public static odfMesh CreateMesh(WorkspaceMesh mesh, int subMeshFormat, 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];

            odfMesh newMesh = new odfMesh(new ObjectName(String.Empty, null), null, numSubmeshes);

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

                ImportedSubmesh submesh = mesh.SubmeshList[submeshIdx];

                odfSubmesh newSubmesh = new odfSubmesh(new ObjectName(String.Empty, null), null, subMeshFormat);
                newMesh.AddChild(newSubmesh);

                newSubmesh.MaterialId     = ObjectID.INVALID;
                materialNames[i]          = submesh.Material;
                indices[i]                = submesh.Index;
                worldCoords[i]            = submesh.WorldCoords;
                replaceSubmeshesOption[i] = mesh.isSubmeshReplacingOriginal(mesh.SubmeshList[submeshIdx]);

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

                    newVertex.Normal      = vert.Normal;
                    newVertex.UV          = new Vector2(vert.UV[0], vert.UV[1]);
                    newVertex.Weights     = (float[])vert.Weights.Clone();
                    newVertex.BoneIndices = (byte[])vert.BoneIndices.Clone();
                    newVertex.Position    = vert.Position;
                    newVertexList.Add(newVertex);
                }
                newSubmesh.VertexList = newVertexList;

                List <ImportedFace> faceList    = submesh.FaceList;
                List <odfFace>      newFaceList = new List <odfFace>(faceList.Count);
                for (int j = 0; j < faceList.Count; j++)
                {
                    int[]   vertexIndices = faceList[j].VertexIndices;
                    odfFace newFace       = new odfFace();
                    newFace.VertexIndices = new ushort[3] {
                        (ushort)vertexIndices[0], (ushort)vertexIndices[1], (ushort)vertexIndices[2]
                    };
                    newFaceList.Add(newFace);
                }
                newSubmesh.FaceList = newFaceList;
            }

            return(newMesh);
        }