// Mesh combiner functoin void CombineMeshes(Mesh[] shapes) { ShapeCacheData[] cache; if (shapes == null || shapes.Length == 0) { // The shape array is empty; use the default shape. cache = new ShapeCacheData[1]; cache[0] = new ShapeCacheData(null); } else { // Store the meshes into the shape cache. cache = new ShapeCacheData[shapes.Length]; for (var i = 0; i < shapes.Length; i++) { cache[i] = new ShapeCacheData(shapes[i]); } } // Count the number of vertices and indices in the shape cache. var vc_shapes = 0; var ic_shapes = 0; foreach (var s in cache) { vc_shapes += s.VertexCount; ic_shapes += s.IndexCount; } // If there is nothing, break. if (vc_shapes == 0) { return; } // Determine the number of copies. // - The number of vertices must be less than 64k. // - The number of copies must be less than 4096. var vc = 0; var ic = 0; for (_copyCount = 0; _copyCount < 4096; _copyCount++) { var s = cache[_copyCount % cache.Length]; if (vc + s.VertexCount > 65535) { break; } vc += s.VertexCount; ic += s.IndexCount; } // Create vertex arrays. var vertices = new Vector3[vc]; var normals = new Vector3[vc]; var tangents = new Vector4[vc]; var uv = new Vector2[vc]; var uv2 = new Vector2[vc]; var indicies = new int[ic]; for (int v_i = 0, i_i = 0, e_i = 0; v_i < vc; e_i++) { var s = cache[e_i % cache.Length]; s.CopyVerticesTo(vertices, v_i); s.CopyNormalsTo(normals, v_i); s.CopyTangentsTo(tangents, v_i); s.CopyUVTo(uv, v_i); s.CopyIndicesTo(indicies, i_i, v_i); var coord = new Vector2((float)e_i / _copyCount, 0); for (var i = 0; i < s.VertexCount; i++) { uv2[v_i + i] = coord; } v_i += s.VertexCount; i_i += s.IndexCount; } // Create a mesh object. _mesh = new Mesh(); _mesh.vertices = vertices; _mesh.normals = normals; _mesh.tangents = tangents; _mesh.uv = uv; _mesh.uv2 = uv2; _mesh.SetIndices(indicies, MeshTopology.Triangles, 0); ; // This only for temporary use. Don't save. _mesh.hideFlags = HideFlags.DontSave; // Avoid being culled. _mesh.bounds = new Bounds(Vector3.zero, Vector3.one * 1000); }
void CombineMeshes(Mesh[] shapes) { ShapeCacheData[] cache; if (shapes == null || shapes.Length == 0) { cache = new ShapeCacheData[1]; cache[0] = new ShapeCacheData(null); } else { cache = new ShapeCacheData[shapes.Length]; for (var i = 0; i < shapes.Length; i++) { cache[i] = new ShapeCacheData(shapes[i]); } } var vc_shapes = 0; var ic_shapes = 0; foreach (var s in cache) { vc_shapes += s.VertexCount; ic_shapes += s.IndexCount; } if (vc_shapes == 0) { return; } // 4096可自行调整 var vc = 0; var ic = 0; for (_copyCount = 0; _copyCount < 4096; _copyCount++) { var s = cache[_copyCount % cache.Length]; if (vc + s.VertexCount > 65535) { break; } vc += s.VertexCount; ic += s.IndexCount; } var vertices = new Vector3[vc]; var normals = new Vector3[vc]; var tangents = new Vector4[vc]; var uv = new Vector2[vc]; var uv2 = new Vector2[vc]; var indicies = new int[ic]; for (int v_i = 0, i_i = 0, e_i = 0; v_i < vc; e_i++) { var s = cache[e_i % cache.Length]; s.CopyVerticesTo(vertices, v_i); s.CopyNormalsTo(normals, v_i); s.CopyTangentsTo(tangents, v_i); s.CopyUVTo(uv, v_i); s.CopyIndicesTo(indicies, i_i, v_i); var coord = new Vector2(((float)e_i + 0.5f) / _copyCount, 0); for (var i = 0; i < s.VertexCount; i++) { uv2[v_i + i] = coord; } v_i += s.VertexCount; i_i += s.IndexCount; } _mesh = new Mesh(); _mesh.vertices = vertices; _mesh.normals = normals; _mesh.tangents = tangents; _mesh.uv = uv; _mesh.uv2 = uv2; _mesh.SetIndices(indicies, MeshTopology.Triangles, 0); _mesh.hideFlags = HideFlags.DontSave; _mesh.bounds = new Bounds(Vector3.zero, Vector3.one * 1000); }
// Mesh combiner functoin void CombineMeshes(Mesh[] shapes) { ShapeCacheData[] cache; if (shapes == null || shapes.Length == 0) { // The shape array is empty; use the default shape. cache = new ShapeCacheData[1]; cache[0] = new ShapeCacheData(null); } else { // Store the meshes into the shape cache. cache = new ShapeCacheData[shapes.Length]; for (var i = 0; i < shapes.Length; i++) cache[i] = new ShapeCacheData(shapes[i]); } // Count the number of vertices and indices in the shape cache. var vc_shapes = 0; var ic_shapes = 0; foreach (var s in cache) { vc_shapes += s.VertexCount; ic_shapes += s.IndexCount; } // If there is nothing, break. if (vc_shapes == 0) return; // Determine the number of copies. // - The number of vertices must be less than 64k. // - The number of copies must be less than 4096. var vc = 0; var ic = 0; for (_copyCount = 0; _copyCount < 4096; _copyCount++) { var s = cache[_copyCount % cache.Length]; if (vc + s.VertexCount > 65535) break; vc += s.VertexCount; ic += s.IndexCount; } // Create vertex arrays. var vertices = new Vector3[vc]; var normals = new Vector3[vc]; var tangents = new Vector4[vc]; var uv = new Vector2[vc]; var uv2 = new Vector2[vc]; var indicies = new int[ic]; for (int v_i = 0, i_i = 0, e_i = 0; v_i < vc; e_i++) { var s = cache[e_i % cache.Length]; s.CopyVerticesTo(vertices, v_i); s.CopyNormalsTo (normals, v_i); s.CopyTangentsTo(tangents, v_i); s.CopyUVTo (uv, v_i); s.CopyIndicesTo (indicies, i_i, v_i); var coord = new Vector2((float)e_i / _copyCount, 0); for (var i = 0; i < s.VertexCount; i++) uv2[v_i + i] = coord; v_i += s.VertexCount; i_i += s.IndexCount; } // Create a mesh object. _mesh = new Mesh(); _mesh.vertices = vertices; _mesh.normals = normals; _mesh.tangents = tangents; _mesh.uv = uv; _mesh.uv2 = uv2; _mesh.SetIndices(indicies, MeshTopology.Triangles, 0); _mesh.Optimize(); // This only for temporary use. Don't save. _mesh.hideFlags = HideFlags.DontSave; // Avoid being culled. _mesh.bounds = new Bounds(Vector3.zero, Vector3.one * 1000); }
//NGS: Mesh combiner functoin void DuplicateMesh(Mesh shape, int nSegments, int nSides) { ShapeCacheData cache = new ShapeCacheData(shape); //NGS: Count the number of vertices and indices in the shape cache. var vc_shapes = cache.VertexCount; var ic_shapes = cache.IndexCount; //NGS: vertex Count, Index Count var vc = 0; var ic = 0; int numCopies = nSegments * nSides; for (m_copyCount = 0; m_copyCount < numCopies; m_copyCount++) { if (vc + cache.VertexCount > DRAWCALL_MAX_VERTEX_COUNT) { Debug.LogFormat("Too many verts for one draw call. only got {0} copies instead of {1} requested", m_copyCount, numCopies); break; } vc += cache.VertexCount; ic += cache.IndexCount; } //NGS: Create vertex arrays. var vertices = new Vector3[vc]; var normals = new Vector3[vc]; var tangents = new Vector4[vc]; var uv = new Vector2[vc]; var uv2 = new Vector2[vc]; var indicies = new int[ic]; for (int v_i = 0, i_i = 0, n = 0; v_i < vc;) { cache.CopyVerticesTo(vertices, v_i); cache.CopyNormalsTo(normals, v_i); cache.CopyTangentsTo(tangents, v_i); cache.CopyUVTo(uv, v_i); cache.CopyIndicesTo(indicies, i_i, v_i); var coord = new Vector2( (float)(n % nSides) / (float)nSides, //NGS: side 0-1 Mathf.Floor((float)n / (float)nSides) / (float)nSegments //NGS: segment 0-1 ); for (var i = 0; i < cache.VertexCount; i++) { uv2[v_i + i] = coord; } v_i += cache.VertexCount; i_i += cache.IndexCount; n++; } m_mesh = new Mesh(); m_mesh.vertices = vertices; m_mesh.normals = normals; m_mesh.tangents = tangents; m_mesh.uv = uv; m_mesh.uv2 = uv2; m_mesh.SetIndices(indicies, MeshTopology.Triangles, 0); //NGS: This only for temporary use. Don't save. m_mesh.hideFlags = HideFlags.DontSave; //NGS: Avoid being culled. m_mesh.bounds = new Bounds(Vector3.zero, Vector3.one * DISABLE_CULLING_BOUNDS); }