コード例 #1
0
        /// <summary>
        /// Append a new face to the ProBuilderMesh.
        /// </summary>
        /// <param name="mesh">The mesh target.</param>
        /// <param name="positions">The new vertex positions to add.</param>
        /// <param name="colors">The new colors to add (must match positions length).</param>
        /// <param name="uvs">The new uvs to add (must match positions length).</param>
        /// <param name="face">A face with the new triangle indexes. The indexes should be 0 indexed.</param>
        /// <param name="common"></param>
        /// <returns>The new face as referenced on the mesh.</returns>
        internal static Face AppendFace(
            this ProBuilderMesh mesh,
            Vector3[] positions,
            Color[] colors,
            Vector2[] uv0s,
            Vector4[] uv2s,
            Vector4[] uv3s,
            Face face,
            int[] common)
        {
            if (mesh == null)
            {
                throw new ArgumentNullException("mesh");
            }

            if (positions == null)
            {
                throw new ArgumentNullException("positions");
            }

            if (face == null)
            {
                throw new ArgumentNullException("face");
            }

            int faceVertexCount = positions.Length;

            if (common == null)
            {
                common = new int[faceVertexCount];
                for (int i = 0; i < faceVertexCount; i++)
                {
                    common[i] = -1;
                }
            }

            int vertexCount = mesh.vertexCount;

            var mc  = mesh.HasArrays(MeshArrays.Color);
            var fc  = colors != null;
            var mt0 = mesh.HasArrays(MeshArrays.Texture0);
            var ft0 = uv0s != null;
            var mt2 = mesh.HasArrays(MeshArrays.Texture2);
            var ft2 = uv2s != null;
            var mt3 = mesh.HasArrays(MeshArrays.Texture3);
            var ft3 = uv3s != null;

            Vector3[]      newPositions = new Vector3[vertexCount + faceVertexCount];
            Color[]        newColors    = (mc || fc) ? new Color[vertexCount + faceVertexCount] : null;
            Vector2[]      newTexture0s = (mt0 || ft0) ? new Vector2[vertexCount + faceVertexCount] : null;
            List <Vector4> newTexture2s = (mt2 || ft2) ? new List <Vector4>() : null;
            List <Vector4> newTexture3s = (mt3 || ft3) ? new List <Vector4>() : null;

            List <Face> faces = new List <Face>(mesh.facesInternal);

            Array.Copy(mesh.positionsInternal, 0, newPositions, 0, vertexCount);
            Array.Copy(positions, 0, newPositions, vertexCount, faceVertexCount);

            if (mc || fc)
            {
                Array.Copy(mc ? mesh.colorsInternal : ArrayUtility.Fill(Color.white, vertexCount), 0, newColors, 0,
                           vertexCount);
                Array.Copy(fc ? colors : ArrayUtility.Fill(Color.white, faceVertexCount), 0, newColors, vertexCount,
                           colors.Length);
            }

            if (mt0 || ft0)
            {
                Array.Copy(mt0 ? mesh.texturesInternal : ArrayUtility.Fill(Vector2.zero, vertexCount), 0, newTexture0s, 0,
                           vertexCount);
                Array.Copy(ft0 ? uv0s : ArrayUtility.Fill(Vector2.zero, faceVertexCount), 0, newTexture0s,
                           mesh.texturesInternal.Length, faceVertexCount);
            }

            if (mt2 || ft2)
            {
                newTexture2s.AddRange(mt2 ? mesh.textures2Internal : new Vector4[vertexCount].ToList());
                newTexture2s.AddRange(ft2 ? uv2s : new Vector4[faceVertexCount]);
            }

            if (mt3 || ft3)
            {
                newTexture3s.AddRange(mt3 ? mesh.textures3Internal : new Vector4[vertexCount].ToList());
                newTexture3s.AddRange(ft3 ? uv3s : new Vector4[faceVertexCount]);
            }

            face.ShiftIndexesToZero();
            face.ShiftIndexes(vertexCount);

            faces.Add(face);

            for (int i = 0; i < common.Length; i++)
            {
                if (common[i] < 0)
                {
                    mesh.AddSharedVertex(new SharedVertex(new int[] { i + vertexCount }));
                }
                else
                {
                    mesh.AddToSharedVertex(common[i], i + vertexCount);
                }
            }

            mesh.positions         = newPositions;
            mesh.colors            = newColors;
            mesh.textures          = newTexture0s;
            mesh.faces             = faces;
            mesh.textures2Internal = newTexture2s;
            mesh.textures3Internal = newTexture3s;

            return(face);
        }