Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void updateLightMap(Block[][][] blocks){
- lightMap = new byte[chunkSizeXZ][chunkSizeY][chunkSizeXZ];
- // Logger.info("Updating Voxel Lightmap.");
- Stack<Vector3i> litPoints = new Stack<Vector3i>();
- for (int x = 0; x < chunkSizeXZ; x++) {
- for (int y = 0; y < chunkSizeY; y++) {
- for (int z = 0; z < chunkSizeXZ; z++) {
- Block block = blocks[x][y][z];
- if(block.LightOutput() > 0){
- //Logger.info("Light Source: " + x + ", " + z);
- // litPoints.push(new Vector3i(x, y, z));
- lightMap[x][y][z] = block.LightOutput();
- //
- litPoints.push(new Vector3i(x + 1, y, z));
- litPoints.push(new Vector3i(x - 1, y, z));
- litPoints.push(new Vector3i(x, y + 1, z));
- litPoints.push(new Vector3i(x, y - 1, z));
- litPoints.push(new Vector3i(x, y, z + 1));
- litPoints.push(new Vector3i(x, y, z - 1));
- }else if(x == chunkSizeXZ - 1 || z == chunkSizeXZ - 1 || x == 0 || z == 0){
- litPoints.push(new Vector3i(x, y, z));
- }
- }
- }
- }
- byte[][][] visited = new byte[chunkSizeXZ][chunkSizeY][chunkSizeXZ];
- Vector3i point;
- int x, y, z;
- long startTimeLoopPoints = System.nanoTime();
- while(!litPoints.isEmpty()){
- point = litPoints.pop();
- x = point.x;
- y = point.y;
- z = point.z;
- if(outOfChunkBounds(x, y, z) || visited[x][y][z] > 1)
- continue;
- if(blocks[x][y][z].LightOutput() <= 1)
- if(blocks[x][y][z].blockType() != 0)
- continue;
- if(!hasNonAirNeighbour(x, y, z) && !hasNonAirNeighbourDiagonal(x, y, z))
- continue;
- byte highestLightLvl = getHighestNeighbourLightLevel(x, y, z);
- if(highestLightLvl <= 1) continue;
- setLightLvl(x, y, z, (byte)(highestLightLvl - 1));
- visited[x][y][z]++;
- //TODO: Check and add neighbours
- litPoints.push(new Vector3i(x + 1, y, z));
- litPoints.push(new Vector3i(x - 1, y, z));
- litPoints.push(new Vector3i(x, y + 1, z));
- litPoints.push(new Vector3i(x, y - 1, z));
- litPoints.push(new Vector3i(x, y, z + 1));
- litPoints.push(new Vector3i(x, y, z - 1));
- }
- long estimatedTimeLoopPoints = System.nanoTime() - startTimeLoopPoints;
- Logger.debug("Chunk(" + pos + ") -> updateLightMap -> Loop Points -> Execution Time: " + (estimatedTimeLoopPoints / 1000000) + "ms");
- // Logger.info("Complete Light Map Algorithm!");
- }
- private byte getHighestNeighbourLightLevel(int x, int y, int z){
- long startTime = System.nanoTime();
- byte lightLvl = 1;
- byte tmpLvl = getLightLvl(x + 1, y, z);
- if(tmpLvl > lightLvl)
- lightLvl = tmpLvl;
- tmpLvl = getLightLvl(x - 1, y, z);
- if(tmpLvl > lightLvl)
- lightLvl = tmpLvl;
- tmpLvl = getLightLvl(x, y + 1, z);
- if(tmpLvl > lightLvl)
- lightLvl = tmpLvl;
- tmpLvl = getLightLvl(x, y - 1, z);
- if(tmpLvl > lightLvl)
- lightLvl = tmpLvl;
- tmpLvl = getLightLvl(x, y, z + 1);
- if(tmpLvl > lightLvl)
- lightLvl = tmpLvl;
- tmpLvl = getLightLvl(x, y, z - 1);
- if(tmpLvl > lightLvl)
- lightLvl = tmpLvl;
- // if(getLightLvl(x, y, z) > lightLvl)
- // lightLvl = tmpLvl;
- long endTime = System.nanoTime() - startTime;
- if(endTime / 1000000 > 0) Logger.debug("Chunk(" + pos + ") -> getHighestLightLevel() -> Execution Time: " + (endTime / 1000000) + "ms");
- return lightLvl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement