sort chunks

This commit is contained in:
LabyStudio
2022-02-02 16:22:33 +01:00
parent c5fd75de47
commit 9a70a3cc37
4 changed files with 19 additions and 4 deletions
+2 -2
View File
@@ -15,7 +15,7 @@ window.Minecraft = class {
Block.create();
// Create world
this.world = new World();
this.world = new World(this);
this.worldRenderer.scene.add(this.world.group);
// Create player
@@ -78,7 +78,7 @@ window.Minecraft = class {
this.window.mouseMotionX = 0;
this.window.mouseMotionY = 0;
}
while (this.world.updateLights()) ;
// Render the game
@@ -34,6 +34,7 @@ window.WorldRenderer = class {
this.webRenderer.shadowMap.enabled = true;
this.webRenderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
this.webRenderer.autoClear = false;
this.webRenderer.sortObjects = false;
this.webRenderer.setClearColor(0x000000, 0);
this.webRenderer.clear();
@@ -102,15 +103,16 @@ window.WorldRenderer = class {
renderChunks(cameraChunkX, cameraChunkZ) {
let world = this.minecraft.world;
let renderDistance = WorldRenderer.RENDER_DISTANCE;
// Load chunks
for (let x = -renderDistance; x <= renderDistance; x++) {
for (let z = -renderDistance; z <= renderDistance; z++) {
world.getChunkAt(cameraChunkX + x, cameraChunkZ + z);
}
}
// Update chunks
for (let [index, chunk] of world.chunks) {
let distanceX = Math.abs(cameraChunkX - chunk.x);
let distanceZ = Math.abs(cameraChunkZ - chunk.z);
@@ -7,6 +7,8 @@ window.Chunk = class {
this.group = new THREE.Object3D();
this.group.matrixAutoUpdate = false;
this.group.chunkX = x;
this.group.chunkZ = z;
this.loaded = false;
+12 -1
View File
@@ -2,7 +2,9 @@ window.World = class {
static TOTAL_HEIGHT = ChunkSection.SIZE * 16 - 1;
constructor() {
constructor(minecraft) {
this.minecrat = minecraft;
this.group = new THREE.Object3D();
this.group.matrixAutoUpdate = false;
this.chunks = new Map();
@@ -14,7 +16,16 @@ window.World = class {
}
onTick() {
let player = this.minecrat.player;
let cameraChunkX = Math.floor(player.x >> 4);
let cameraChunkZ = Math.floor(player.z >> 4);
// Update render order of chunks
this.group.children.sort((a, b) => {
let distance1 = Math.floor(Math.pow(a.chunkX - cameraChunkX, 2) + Math.pow(a.chunkZ - cameraChunkZ, 2));
let distance2 = Math.floor(Math.pow(b.chunkX - cameraChunkX, 2) + Math.pow(b.chunkZ - cameraChunkZ, 2));
return distance2 - distance1;
});
}
getChunkAt(x, z) {