public Mesh BuildMeshOld() { marker.Begin(); // get all the faces List <Vector3> vertices = new List <Vector3>(); List <int> triangles = new List <int>(); List <Vector2> uvs = new List <Vector2>(); List <Vector3> normals = new List <Vector3>(); int vertexCount = 0; for (int x = 0; x < chunkWidth; x++) { for (int y = 0; y < chunkHeight; y++) { for (int z = 0; z < chunkWidth; z++) { Block block = chunkBlocks[x, y, z]; if (block == null) { continue; } int newVertices = block.addVoxelMeshData(vertices, triangles, uvs, normals, vertexCount); vertexCount += newVertices; } } } NativeArray <int> returnCounts = new NativeArray <int>(2, Allocator.TempJob); MeshCreateJob job = new MeshCreateJob(); job.blockData = blockData; job.verts = meshVertices; job.tris = triVerts; job.sizex = chunkWidth; job.sizey = chunkHeight; job.sizez = chunkWidth; job.counts = returnCounts; JobHandle handle = job.Schedule(); handle.Complete(); int vertCount = job.counts[0]; int triCount = job.counts[1]; returnCounts.Dispose(); marker.End(); Mesh mesh = new Mesh(); mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; var layout = new[] { new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3), new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3), new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2) }; if (true) { mesh.SetVertexBufferParams(vertCount, layout); mesh.SetVertexBufferData(job.verts, 0, 0, vertCount); int[] trilist = new int[triCount]; for (int t = 0; t < triCount; t++) { trilist[t] = job.tris[t]; } mesh.triangles = trilist; } else { if (true) { Vector3[] verts = new Vector3[vertCount]; Vector3[] norms = new Vector3[vertCount]; Vector2[] uv = new Vector2[vertCount]; for (int i = 0; i < vertCount; i++) { ChunkMeshVertexData data = job.verts[i]; verts[i] = data.pos; norms[i] = data.normal; uv[i] = data.uv; } mesh.vertices = verts; mesh.normals = norms; mesh.uv = uv; int[] trilist = new int[triCount]; for (int t = 0; t < triCount; t++) { trilist[t] = job.tris[t]; } mesh.triangles = trilist; } else { mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.uv = uvs.ToArray(); mesh.normals = normals.ToArray(); } } mesh.RecalculateBounds(); return(mesh); }
public Mesh BuildMesh() { marker.Begin(); //// get all the faces //List<Vector3> vertices = new List<Vector3>(); //List<int> triangles = new List<int>(); //List<Vector2> uvs = new List<Vector2>(); //List<Vector3> normals = new List<Vector3>(); //int vertexCount = 0; //for (int x = 0; x < chunkWidth; x++) //{ // for (int y = 0; y < chunkHeight; y++) // { // for (int z = 0; z < chunkWidth; z++) // { // Block block = chunkBlocks[x, y, z]; // if (block == null) // { // continue; // } // int newVertices = block.addVoxelMeshData(vertices, triangles, uvs, normals, vertexCount); // vertexCount += newVertices; // } // } //} NativeArray <int> returnCounts = new NativeArray <int>(2, Allocator.TempJob); MeshCreateJob job = new MeshCreateJob(); job.blockData = blockData; job.verts = meshVertices; job.tris = triVerts; job.sizex = chunkWidth; job.sizey = chunkHeight; job.sizez = chunkWidth; job.counts = returnCounts; JobHandle handle = job.Schedule(); handle.Complete(); int vertCount = job.counts[0]; int triCount = job.counts[1]; returnCounts.Dispose(); Mesh mesh = new Mesh(); mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; var layout = new[] { new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3), new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3), new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2) }; mesh.SetVertexBufferParams(vertCount, layout); mesh.SetVertexBufferData(job.verts, 0, 0, vertCount); int[] trilist = new int[triCount]; for (int t = 0; t < triCount; t++) { trilist[t] = job.tris[t]; } mesh.triangles = trilist; mesh.RecalculateBounds(); marker.End(); return(mesh); }