private Mesh ToMesh(MarchingCubesJob.Out o, int vertexCount, int triangleCount) { if (vertexCount < 3 || triangleCount < 1) { return(null); } Utils.Profiler.BeginSample("[Dig] VoxelChunk.ToMesh"); var tData = digger.Terrain.terrainData; var mesh = new Mesh(); AddVertexData(mesh, o, vertexCount, triangleCount, tData); mesh.bounds = GetBounds(); if (digger.MaterialType == TerrainMaterialType.CTS) { Utils.Profiler.BeginSample("[Dig] VoxelChunk.ToMesh.RecalculateTangents"); mesh.RecalculateTangents(); Utils.Profiler.EndSample(); } Utils.Profiler.EndSample(); return(mesh); }
private void AddVertexData(Mesh mesh, MarchingCubesJob.Out o, int vertexCount, int triangleCount, TerrainData tData) { NativeArray <Vector3> .Copy(o.outVertices, DirectMeshAccess.VertexArray, vertexCount); NativeArray <int> .Copy(o.outTriangles, DirectMeshAccess.TriangleArray, triangleCount); NativeArray <Vector3> .Copy(o.outNormals, DirectMeshAccess.NormalArray, vertexCount); NativeArray <Color> .Copy(o.outColors, DirectMeshAccess.ColorArray, vertexCount); NativeArray <Vector2> .Copy(o.outUV1s, DirectMeshAccess.Uv0Array, vertexCount); NativeArray <uint> .Copy(o.outInfos, DirectMeshAccess.InfoArray, vertexCount); var uvs = DirectMeshAccess.Uv0Array; var normals = DirectMeshAccess.NormalArray; var infos = DirectMeshAccess.InfoArray; for (var i = 0; i < vertexCount; ++i) { var texInfo = infos[i]; if (texInfo == Voxel.Unaltered || texInfo == Voxel.OnSurface) { // near the terrain surface -> set same normal var uv = uvs[i]; normals[i] = tData.GetInterpolatedNormal(uv.x, uv.y); } } DirectMeshAccess.DirectSetTotal(mesh, vertexCount, triangleCount); if (digger.MaterialType == TerrainMaterialType.HDRP) { ListPool.ToVector2Lists(o.outUV2s, vertexCount, out var vector2List1, out var vector2List2); mesh.SetUVs(2, vector2List1); mesh.SetUVs(3, vector2List2); } else { mesh.SetUVs(1, ListPool.ToVector4List(o.outUV2s, vertexCount)); mesh.SetUVs(2, ListPool.ToVector4List(o.outUV3s, vertexCount)); mesh.SetUVs(3, ListPool.ToVector4List(o.outUV4s, vertexCount)); } }
private static Mesh ToMeshSimple(MarchingCubesJob.Out o, int vertexCount, int triangleCount) { if (vertexCount < 3 || triangleCount < 3) { return(null); } Utils.Profiler.BeginSample("[Dig] VoxelChunk.ToMeshSimple"); var mesh = new Mesh(); NativeArray <Vector3> .Copy(o.outVertices, DirectMeshAccess.VertexArray, vertexCount); NativeArray <int> .Copy(o.outTriangles, DirectMeshAccess.TriangleArray, triangleCount); NativeArray <Vector3> .Copy(o.outNormals, DirectMeshAccess.NormalArray, vertexCount); DirectMeshAccess.DirectSet(mesh, vertexCount, triangleCount); mesh.RecalculateBounds(); Utils.Profiler.EndSample(); return(mesh); }