Ejemplo n.º 1
0
        private void Initialize(IEnumerable <IEnumerable <Topology.Face> > faceGroups, ITriangulation triangulation)
        {
            int maxFaceIndex = -1;
            var dynamicFaceSubmeshIndicesArray     = new int[65536];
            var dynamicFaceFirstVertexIndicesArray = new int[65536];

            _cachedIndexedVertexAttributeArrays = new IndexedVertexAttributeArrays();

            var submeshList = new List <Submesh>();

            var vertexAttributeArrays = new DynamicVertexAttributeArrays(_vertexAttributes, _maxVerticesPerSubmesh);
            var triangleIndices       = new List <int>();

            foreach (var faceGroup in faceGroups)
            {
                foreach (var face in faceGroup)
                {
                    var vertexCount = triangulation.GetVertexCount(face);

                    if (vertexAttributeArrays.index + vertexCount > _maxVerticesPerSubmesh)
                    {
                        triangulation.FinalizeSubmesh(submeshList.Count);
                        submeshList.Add(new Submesh(vertexAttributeArrays, triangleIndices));
                        vertexAttributeArrays.Reset();
                        triangleIndices.Clear();
                    }

                    maxFaceIndex = Mathf.Max(maxFaceIndex, face.index);
                    SetGrowableArrayElement(ref dynamicFaceSubmeshIndicesArray, face.index, submeshList.Count);
                    SetGrowableArrayElement(ref dynamicFaceFirstVertexIndicesArray, face.index, vertexAttributeArrays.index);

                    vertexAttributeArrays.Grow(vertexCount);

                    triangulation.BuildFace(face, vertexAttributeArrays, triangleIndices);
                }

                if (vertexAttributeArrays.index > 0)
                {
                    triangulation.FinalizeSubmesh(submeshList.Count);
                    submeshList.Add(new Submesh(vertexAttributeArrays, triangleIndices));
                    vertexAttributeArrays.Reset();
                    triangleIndices.Clear();
                }
            }

            _submeshes = submeshList.ToArray();

            var faceCount = maxFaceIndex + 1;

            _faceSubmeshIndices     = new int[faceCount];
            _faceFirstVertexIndices = new int[faceCount];
            Array.Copy(dynamicFaceSubmeshIndicesArray, _faceSubmeshIndices, faceCount);
            Array.Copy(dynamicFaceFirstVertexIndicesArray, _faceFirstVertexIndices, faceCount);
        }
Ejemplo n.º 2
0
            public Submesh(DynamicVertexAttributeArrays vertexAttributeArrays, List <int> triangleIndices)
            {
                mesh    = new Mesh();
                isDirty = false;

                var length = vertexAttributeArrays.index;

                if (CopyArray(ref positions, vertexAttributeArrays.positions, length))
                {
                    mesh.vertices = positions;
                }
                if (CopyArray(ref normals, vertexAttributeArrays.normals, length))
                {
                    mesh.normals = normals;
                }
                if (CopyArray(ref colors, vertexAttributeArrays.colors, length))
                {
                    mesh.colors = colors;
                }
                if (CopyArray(ref colors32, vertexAttributeArrays.colors32, length))
                {
                    mesh.colors32 = colors32;
                }
                if (CopyArray(ref uvs, vertexAttributeArrays.uvs, length))
                {
                    mesh.uv = uvs;
                }
                if (CopyArray(ref uvs2, vertexAttributeArrays.uvs2, length))
                {
                    mesh.uv2 = uvs2;
                }
                if (CopyArray(ref uvs3, vertexAttributeArrays.uvs3, length))
                {
                    mesh.uv3 = uvs3;
                }
                if (CopyArray(ref uvs4, vertexAttributeArrays.uvs4, length))
                {
                    mesh.uv4 = uvs4;
                }
                if (CopyArray(ref tangents, vertexAttributeArrays.tangents, length))
                {
                    mesh.tangents = tangents;
                }

                mesh.triangles = triangleIndices.ToArray();
                mesh.RecalculateBounds();
            }