Exemple #1
0
            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();
            }
Exemple #2
0
            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;
                            }
                        }
                    }
                }
            }