bool TransparencyCheck(NativeSlice <Voxel> voxels, int3 gridPosition) { if (VoxelUtil.BoundaryCheck(gridPosition, chunkSize)) { return(voxels[VoxelUtil.To1DIndex(gridPosition, chunkSize)].data != Voxel.VoxelType.Air); } else { int3 worldGridPosition = gridPosition + chunkPosition * chunkSize; int3 neighborChunkPosition = VoxelUtil.WorldToChunk(worldGridPosition, chunkSize); if (neighborHashMap.TryGetValue(neighborChunkPosition, out int voxelIndex)) { if (voxelIndex == -1) { return(false); } int3 position = VoxelUtil.WorldToGrid(worldGridPosition, neighborChunkPosition, chunkSize); NativeSlice <Voxel> neighborVoxels = voxelsWithNeighbor.Slice(voxelIndex * chunkSize.x * chunkSize.y * chunkSize.z, chunkSize.x * chunkSize.y * chunkSize.z); return(neighborVoxels[VoxelUtil.To1DIndex(position, chunkSize)].data != Voxel.VoxelType.Air); } return(false); } }
public void Execute() { for (int x = 0; x < chunkSize.x; x++) { for (int y = 0; y < chunkSize.y; y++) { for (int z = 0; z < chunkSize.z; z++) { int3 gridPosition = new int3(x, y, z); int index = VoxelUtil.To1DIndex(gridPosition, chunkSize); Voxel voxel = voxels[index]; if (voxel.data == Voxel.VoxelType.Air) { continue; } for (int direction = 0; direction < 6; direction++) { int3 neighborPosition = gridPosition + VoxelUtil.VoxelDirectionOffsets[direction]; if (TransparencyCheck(voxels, neighborPosition, chunkSize)) { continue; } AddQuadByDirection(direction, voxel.data, lightData[index], 1.0f, 1.0f, gridPosition, counter.Increment(), vertices, normals, uvs, colors, indices); } } } } }
public static bool TransparencyCheck(NativeArray <Voxel> voxels, int3 position, int chunkSize) { if (!BoundaryCheck(chunkSize, position)) { return(false); } return(voxels[VoxelUtil.To1DIndex(position, chunkSize)].data != Voxel.VoxelType.Air); }
public void Execute() { NativeHashMap <int3, Empty> hashMap = new NativeHashMap <int3, Empty>(chunkSize * chunkSize, Allocator.Temp); for (int direction = 0; direction < 6; direction++) { for (int depth = 0; depth < chunkSize; depth++) { for (int x = 0; x < chunkSize; x++) { for (int y = 0; y < chunkSize;) { int3 gridPosition = new int3 { [DirectionAlignedX[direction]] = x, [DirectionAlignedY[direction]] = y, [DirectionAlignedZ[direction]] = depth }; Voxel voxel = voxels[VoxelUtil.To1DIndex(gridPosition, chunkSize)]; if (voxel.data == Voxel.VoxelType.Air) { y++; continue; } if (hashMap.ContainsKey(gridPosition)) { y++; continue; } int3 neighborPosition = gridPosition + VoxelDirectionOffsets[direction]; if (TransparencyCheck(voxels, neighborPosition, chunkSize)) { y++; continue; } VoxelLight light = lightData[VoxelUtil.To1DIndex(gridPosition, chunkSize)]; hashMap.TryAdd(gridPosition, new Empty()); int height; for (height = 1; height + y < chunkSize; height++) { int3 nextPosition = gridPosition; nextPosition[DirectionAlignedY[direction]] += height; Voxel nextVoxel = voxels[VoxelUtil.To1DIndex(nextPosition, chunkSize)]; VoxelLight nextLight = lightData[VoxelUtil.To1DIndex(nextPosition, chunkSize)]; if (nextVoxel.data != voxel.data) { break; } if (!nextLight.CompareFace(light, direction)) { break; } if (hashMap.ContainsKey(nextPosition)) { break; } hashMap.TryAdd(nextPosition, new Empty()); } bool isDone = false; int width; for (width = 1; width + x < chunkSize; width++) { for (int dy = 0; dy < height; dy++) { int3 nextPosition = gridPosition; nextPosition[DirectionAlignedX[direction]] += width; nextPosition[DirectionAlignedY[direction]] += dy; Voxel nextVoxel = voxels[VoxelUtil.To1DIndex(nextPosition, chunkSize)]; VoxelLight nextLight = lightData[VoxelUtil.To1DIndex(nextPosition, chunkSize)]; if (nextVoxel.data != voxel.data || hashMap.ContainsKey(nextPosition) || !nextLight.CompareFace(light, direction)) { isDone = true; break; } } if (isDone) { break; } for (int dy = 0; dy < height; dy++) { int3 nextPosition = gridPosition; nextPosition[DirectionAlignedX[direction]] += width; nextPosition[DirectionAlignedY[direction]] += dy; hashMap.TryAdd(nextPosition, new Empty()); } } AddQuadByDirection(direction, voxel.data, light, width, height, gridPosition, counter, vertices, normals, uvs, colors, indices); y += height; } } hashMap.Clear(); } } hashMap.Dispose(); }
public void Execute() { for (int direction = 0; direction < 6; direction++) { for (int depth = 0; depth < chunkSize; depth++) { for (int x = 0; x < chunkSize; x++) { for (int y = 0; y < chunkSize;) { int3 gridPosition = new int3 { [DirectionAlignedX[direction]] = x, [DirectionAlignedY[direction]] = y, [DirectionAlignedZ[direction]] = depth }; int index = VoxelUtil.To1DIndex(gridPosition, chunkSize); Voxel voxel = voxels[index]; VoxelLight light = lightData[index]; if (voxel.data == Voxel.VoxelType.Air) { y++; continue; } int3 neighborPosition = gridPosition + VoxelDirectionOffsets[direction]; if (TransparencyCheck(voxels, neighborPosition, chunkSize)) { y++; continue; } int height; for (height = 1; height + y < chunkSize; height++) { int3 nextPosition = gridPosition; nextPosition[DirectionAlignedY[direction]] += height; int nextIndex = VoxelUtil.To1DIndex(nextPosition, chunkSize); Voxel nextVoxel = voxels[nextIndex]; VoxelLight nextLight = lightData[nextIndex]; if (nextVoxel.data != voxel.data) { break; } if (!light.CompareFace(nextLight, direction)) { break; } } AddQuadByDirection(direction, voxel.data, light, 1.0f, height, gridPosition, counter, vertices, normals, uvs, colors, indices); y += height; } } } } }