public void ApplyVoxelBrush(Vector3 point, VoxelBrush2D brush) { EditChunkDataAtPoint(point, brush, voxelChunks); TriangulateData(voxelChunks, out MeshData[] meshDataChunks); for (int i = 0; i < voxelChunks.Length; i++) { voxelChunks[i].MeshFilter.mesh = meshDataChunks[i].ToMesh(); } }
private void EditChunkDataAtPoint(Vector3 point, VoxelBrush2D brush, VoxelChunk[] voxelChunks) { // Find closest voxel to hit point var chunk_closestI = 0; var voxel_closestI = 0; for (int ci = 0; ci < voxelChunks.Length; ci++) { for (int vi = 0; vi < voxelChunks[ci].Voxels.Length; vi++) { if (Vector3.Distance(voxelChunks[ci].Voxels[vi].Position + voxelChunks[ci].Position, point) < Vector3.Distance(voxelChunks[chunk_closestI].Voxels[voxel_closestI].Position + voxelChunks[chunk_closestI].Position, point)) { voxel_closestI = vi; chunk_closestI = ci; } } } // Establish where the voxel is in its chunk int voxel_Y = System.Math.DivRem(voxel_closestI, CellResolution + 1, out int voxel_X); // Modify voxels based on active brush for (int i = 0; i < brush.ValueDirectionPairs.Length; i++) { //Get this chunk's x and y indicies int chunk_editI = chunk_closestI; int chunk_X = 0; int chunk_Y = 0; for (int chunk_i = 0; chunk_i < (ChunkCount.x) * ChunkCount.y; chunk_i++) { if (chunk_i == chunk_editI) { break; } chunk_X++; if (chunk_X == ChunkCount.x) { chunk_Y++; chunk_X = 0; } } // Try edit the voxel bool abort = false; TryEditVoxelOnAxis( voxel_X, chunk_X, brush.ValueDirectionPairs[i].XDir, dimensionOffset: 1, out int editXI, ref chunk_editI, ref abort); TryEditVoxelOnAxis( voxel_Y, chunk_Y, brush.ValueDirectionPairs[i].YDir, dimensionOffset: ChunkCount.x, out int editYI, ref chunk_editI, ref abort ); if (!abort) { voxelChunks[chunk_editI] .Voxels[editXI + (editYI * (CellResolution + 1))] .Value = brush.ValueDirectionPairs[i].Value; } } }