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
@@ -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) {