working light system

This commit is contained in:
LabyStudio
2022-02-02 15:18:45 +01:00
parent 1fc627b501
commit 010d883273
6 changed files with 28 additions and 46 deletions
+2 -2
View File
@@ -78,11 +78,11 @@ window.Minecraft = class {
this.window.mouseMotionX = 0;
this.window.mouseMotionY = 0;
}
while (this.world.updateLights()) ;
// Render the game
this.worldRenderer.render(partialTicks);
while (this.world.updateLights()) ;
}
onTick() {
@@ -133,7 +133,7 @@ window.WorldRenderer = class {
chunkSection.render();
// Queue for rebuild
if (chunkSection.isQueuedForRebuild() && !this.chunkSectionUpdateQueue.includes(chunkSection)) {
if (chunkSection.isModified && !this.chunkSectionUpdateQueue.includes(chunkSection)) {
this.chunkSectionUpdateQueue.push(chunkSection);
}
} else {
+11 -13
View File
@@ -35,7 +35,6 @@ window.Chunk = class {
highest = this.heightMap[z << 4 | x] & 0xff;
}
}
}
this.highestY = highest;
@@ -46,11 +45,11 @@ window.Chunk = class {
}
this.queueForRebuild();
this.setModifiedAllSections();
}
updateBlockLight() {
this.setModifiedAllSections();
}
notifyNeighbors(x, z) {
@@ -71,7 +70,7 @@ window.Chunk = class {
} else if (height < y) {
this.world.updateLight(EnumSkyBlock.SKY, x, height, z, x, y, z);
}
this.queueForRebuild();
this.setModifiedAllSections();
}
updateHeightMap(relX, y, relZ) {
@@ -80,7 +79,7 @@ window.Chunk = class {
if (y > currentHighestY) {
highestY = y;
}
while (highestY > 0 && Block.lightOpacity[this.getBlockAt(relX, highestY - 1, relZ)] === 0) {
while (highestY > 0 && Block.lightOpacity[this.getBlockAt(relX, highestY, relZ)] === 0) {
highestY--;
}
if (highestY === currentHighestY) {
@@ -138,7 +137,7 @@ window.Chunk = class {
if (highestY !== prevHeight) {
this.world.updateLight(EnumSkyBlock.SKY, x - 1, highestY, z - 1, x + 1, prevHeight, z + 1);
}
this.queueForRebuild();
this.setModifiedAllSections();
}
setLightAt(sourceType, x, y, z, level) {
@@ -189,7 +188,7 @@ window.Chunk = class {
//this.data.setNibble(i, j, k, i1);
this.queueForRebuild();
this.setModifiedAllSections();
return true;
}
@@ -219,14 +218,13 @@ window.Chunk = class {
}
}
queueForRebuild() {
for (let y = 0; y < this.sections.length; y++) {
this.sections[y].queueForRebuild();
}
}
isLoaded() {
return this.loaded;
}
setModifiedAllSections() {
for (let y = 0; y < this.sections.length; y++) {
this.sections[y].isModified = true;
}
}
}
@@ -20,7 +20,7 @@ window.ChunkSection = class {
this.group = new THREE.Object3D();
this.group.matrixAutoUpdate = false;
this.queuedForRebuild = true;
this.isModified = false;
this.blocks = [];
this.blockLight = [];
@@ -44,7 +44,7 @@ window.ChunkSection = class {
}
rebuild(renderer) {
this.queuedForRebuild = false;
this.isModified = false;
this.group.clear();
// Start drawing chunk section
@@ -80,6 +80,8 @@ window.ChunkSection = class {
setBlockAt(x, y, z, typeId) {
let index = y << 8 | z << 4 | x;
this.blocks[index] = typeId;
this.isModified = true;
}
setLightAt(sourceType, x, y, z, lightLevel) {
@@ -91,6 +93,8 @@ window.ChunkSection = class {
if (sourceType === EnumSkyBlock.BLOCK) {
this.blockLight[index] = lightLevel;
}
this.isModified = true;
}
getTotalLightAt(x, y, z) {
@@ -127,12 +131,4 @@ window.ChunkSection = class {
}
return true;
}
queueForRebuild() {
this.queuedForRebuild = true;
}
isQueuedForRebuild() {
return this.queuedForRebuild;
}
}
+6 -18
View File
@@ -11,9 +11,6 @@ window.World = class {
this.generator = new WorldGenerator(this, Date.now() % 100000);
this.lightUpdateQueue = [];
this.lightUpdateProcesses = 0;
this.lightUpdates = 0;
}
onTick() {
@@ -24,7 +21,7 @@ window.World = class {
let index = x + (z << 16);
let chunk = this.chunks.get(index);
if (typeof chunk === 'undefined') {
let chunk = new Chunk(this, x, z);
chunk = new Chunk(this, x, z);
// Generate new chunk
this.generator.generateChunk(chunk);
@@ -85,13 +82,7 @@ window.World = class {
return false;
}
static prev = 0;
updateLight(sourceType, x1, y1, z1, x2, y2, z2, notifyNeighbor = true) {
if (this.lightUpdates >= 50) {
return;
}
let centerX = (x2 + x1) / 2;
let centerZ = (z2 + z1) / 2;
@@ -114,11 +105,6 @@ window.World = class {
}
}
if (World.prev !== this.lightUpdateQueue.length && this.lightUpdateQueue.length % 100 === 0) {
World.prev = this.lightUpdateQueue.length;
console.log(this.lightUpdateQueue.length + " in queue");
}
this.lightUpdateQueue.push(new MetadataChunkBlock(sourceType, x1, y1, z1, x2, y2, z2));
if (this.lightUpdateQueue.length > 0x186a0) {
this.lightUpdateQueue = [];
@@ -230,10 +216,10 @@ window.World = class {
}
onBlockChanged(x, y, z) {
this.queueForRebuildInRegion(x - 1, y - 1, z - 1, x + 1, y + 1, z + 1);
this.setModified(x - 1, y - 1, z - 1, x + 1, y + 1, z + 1);
}
queueForRebuildInRegion(minX, minY, minZ, maxX, maxY, maxZ) {
setModified(minX, minY, minZ, maxX, maxY, maxZ) {
// To chunk coordinates
minX = minX >> 4;
maxX = maxX >> 4;
@@ -249,7 +235,9 @@ window.World = class {
for (let x = minX; x <= maxX; x++) {
for (let y = minY; y <= maxY; y++) {
for (let z = minZ; z <= maxZ; z++) {
this.getChunkSectionAt(x, y, z).queueForRebuild();
if (this.chunkExists(x >> 4, z >> 4)) {
this.getChunkSectionAt(x, y, z).isModified = true;
}
}
}
}
@@ -32,8 +32,8 @@ window.WorldGenerator = class {
for (let relZ = 0; relZ < ChunkSection.SIZE; relZ++) {
// Absolute position of the block
let x = chunk.x * ChunkSection.SIZE + relX;
let z = chunk.z * ChunkSection.SIZE + relZ;
let x = chunk.x * ChunkSection.SIZE + relX + 10000; // TODO fix this 10000 offset
let z = chunk.z * ChunkSection.SIZE + relZ + 10000;
// Extract height value of the noise
let heightValue = this.groundHeightNoise.perlin(x, z);