protected override unsafe void OnUpdate() { using (var entities = new NativeArray <Entity>( TerrainSettings.ChunkCount * TerrainSettings.ArrayChunk, Allocator.Temp )) { EntityManager.CreateEntity(_entityArchetypeNoise, entities); } var meshCalculationGrid = new MeshCalculationGrid ( TerrainSettings.Resolution, 1f / TerrainSettings.Resolution, TerrainSettings.TotalVertices, TerrainSettings.TotalTriangles ); meshCalculationGrid.Schedule(64).Complete(); _vertices.NativeInject(meshCalculationGrid.Vertices); _triangles.NativeInject(meshCalculationGrid.Triangles); using (var entities = new NativeArray <Entity>(TerrainSettings.ChunkCount, Allocator.Temp)) { EntityManager.CreateEntity(_entityArchetypeMesh, entities); for (var i = 0; i < TerrainSettings.ChunkCount; i++) { var mesh = new Mesh(); mesh.SetVertices(_vertices); mesh.SetTriangles(_triangles, 0); mesh.SetNormals(_vertices); mesh.MarkDynamic(); _renderMesh.mesh = mesh; PostUpdateCommands.SetSharedComponent(entities[i], _renderMesh); PostUpdateCommands.SetComponent(entities[i], new RenderBounds { Value = new AABB { Center = float3.zero, Extents = math.float3(0.5f, 1f, 0.5f) } }); } } var entity = EntityManager.CreateEntity(EntityManager.CreateArchetype(typeof(VerticesData))); var verticesData = EntityManager.GetBuffer <VerticesData>(entity); verticesData.ResizeUninitialized(TerrainSettings.TotalVertices); verticesData.InjectData(meshCalculationGrid.Vertices.GetUnsafeReadOnlyPtr(), TerrainSettings.TotalVertices); meshCalculationGrid.Dispose(); _triangles.Clear(); _vertices.Clear(); }
private protected override Mesh CreateMesh(MeshData meshData) { var mesh = new Mesh(); using (var meshCalculationGrid = new MeshCalculationGrid(meshData)) { meshCalculationGrid.Schedule(64).Complete(); mesh.vertices = meshCalculationGrid.Vertices.ToArray(); mesh.triangles = meshCalculationGrid.Triangles.ToArray(); } return(mesh); }