コード例 #1
0
ファイル: ObjFile.cs プロジェクト: ual002/XwaOptEditor
        public void AddVertexNormals()
        {
            foreach (var face in this.Meshes.SelectMany(t => t.FaceGroups).SelectMany(t => t.Faces))
            {
                ObjIndex verticesIndex      = face.VerticesIndex;
                ObjIndex vertexNormalsIndex = face.VertexNormalsIndex;

                if (vertexNormalsIndex.A < 0 || vertexNormalsIndex.B < 0 || vertexNormalsIndex.C < 0 || (verticesIndex.D >= 0 && vertexNormalsIndex.D < 0))
                {
                    ObjVector3 normal = ObjVector3.Normal(
                        this.Vertices.ElementAtOrDefault(verticesIndex.A),
                        this.Vertices.ElementAtOrDefault(verticesIndex.B),
                        this.Vertices.ElementAtOrDefault(verticesIndex.C));

                    this.VertexNormals.Add(normal);

                    vertexNormalsIndex.A = this.VertexNormals.Count - 1;
                    vertexNormalsIndex.B = this.VertexNormals.Count - 1;
                    vertexNormalsIndex.C = this.VertexNormals.Count - 1;
                    vertexNormalsIndex.D = verticesIndex.D < 0 ? -1 : this.VertexNormals.Count - 1;

                    face.VertexNormalsIndex = vertexNormalsIndex;
                }
            }
        }
コード例 #2
0
ファイル: ObjFile.cs プロジェクト: ual002/XwaOptEditor
        private void CompactVertexNormalsBuffer()
        {
            if (this.VertexNormals.Count == 0)
            {
                return;
            }

            bool[] isUsed = new bool[this.VertexNormals.Count];

            foreach (int i in this.Meshes
                     .SelectMany(t => t.FaceGroups)
                     .SelectMany(t => t.Faces)
                     .Select(t => t.VertexNormalsIndex)
                     .SelectMany(t => new int[] { t.A, t.B, t.C, t.D }))
            {
                if (i >= 0 && i < this.VertexNormals.Count)
                {
                    isUsed[i] = true;
                }
            }

            List <ObjVector3> newValues = new List <ObjVector3>(this.VertexNormals.Count);

            int[] newIndices = new int[this.VertexNormals.Count];

            for (int i = 0; i < this.VertexNormals.Count; i++)
            {
                if (!isUsed[i])
                {
                    continue;
                }

                ObjVector3 value = this.VertexNormals[i];
                int        index = -1;

                for (int j = 0; j < newValues.Count; j++)
                {
                    if (newValues[j] == value)
                    {
                        index = j;
                        break;
                    }
                }

                if (index == -1)
                {
                    newIndices[i] = newValues.Count;
                    newValues.Add(value);
                }
                else
                {
                    newIndices[i] = index;
                }
            }

            newValues.TrimExcess();

            this.VertexNormals = newValues;

            foreach (var face in this.Meshes
                     .SelectMany(t => t.FaceGroups)
                     .SelectMany(t => t.Faces))
            {
                ObjIndex index = face.VertexNormalsIndex;

                face.VertexNormalsIndex = new ObjIndex(
                    index.A < 0 ? -1 : newIndices[index.A],
                    index.B < 0 ? -1 : newIndices[index.B],
                    index.C < 0 ? -1 : newIndices[index.C],
                    index.D < 0 ? -1 : newIndices[index.D]);
            }
        }