sort chunks
This commit is contained in:
@@ -15,7 +15,7 @@ window.Minecraft = class {
|
|||||||
Block.create();
|
Block.create();
|
||||||
|
|
||||||
// Create world
|
// Create world
|
||||||
this.world = new World();
|
this.world = new World(this);
|
||||||
this.worldRenderer.scene.add(this.world.group);
|
this.worldRenderer.scene.add(this.world.group);
|
||||||
|
|
||||||
// Create player
|
// Create player
|
||||||
@@ -78,7 +78,7 @@ window.Minecraft = class {
|
|||||||
this.window.mouseMotionX = 0;
|
this.window.mouseMotionX = 0;
|
||||||
this.window.mouseMotionY = 0;
|
this.window.mouseMotionY = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (this.world.updateLights()) ;
|
while (this.world.updateLights()) ;
|
||||||
|
|
||||||
// Render the game
|
// Render the game
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ window.WorldRenderer = class {
|
|||||||
this.webRenderer.shadowMap.enabled = true;
|
this.webRenderer.shadowMap.enabled = true;
|
||||||
this.webRenderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
|
this.webRenderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap
|
||||||
this.webRenderer.autoClear = false;
|
this.webRenderer.autoClear = false;
|
||||||
|
this.webRenderer.sortObjects = false;
|
||||||
this.webRenderer.setClearColor(0x000000, 0);
|
this.webRenderer.setClearColor(0x000000, 0);
|
||||||
this.webRenderer.clear();
|
this.webRenderer.clear();
|
||||||
|
|
||||||
@@ -102,15 +103,16 @@ window.WorldRenderer = class {
|
|||||||
|
|
||||||
renderChunks(cameraChunkX, cameraChunkZ) {
|
renderChunks(cameraChunkX, cameraChunkZ) {
|
||||||
let world = this.minecraft.world;
|
let world = this.minecraft.world;
|
||||||
|
|
||||||
let renderDistance = WorldRenderer.RENDER_DISTANCE;
|
let renderDistance = WorldRenderer.RENDER_DISTANCE;
|
||||||
|
|
||||||
|
// Load chunks
|
||||||
for (let x = -renderDistance; x <= renderDistance; x++) {
|
for (let x = -renderDistance; x <= renderDistance; x++) {
|
||||||
for (let z = -renderDistance; z <= renderDistance; z++) {
|
for (let z = -renderDistance; z <= renderDistance; z++) {
|
||||||
world.getChunkAt(cameraChunkX + x, cameraChunkZ + z);
|
world.getChunkAt(cameraChunkX + x, cameraChunkZ + z);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update chunks
|
||||||
for (let [index, chunk] of world.chunks) {
|
for (let [index, chunk] of world.chunks) {
|
||||||
let distanceX = Math.abs(cameraChunkX - chunk.x);
|
let distanceX = Math.abs(cameraChunkX - chunk.x);
|
||||||
let distanceZ = Math.abs(cameraChunkZ - chunk.z);
|
let distanceZ = Math.abs(cameraChunkZ - chunk.z);
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ window.Chunk = class {
|
|||||||
|
|
||||||
this.group = new THREE.Object3D();
|
this.group = new THREE.Object3D();
|
||||||
this.group.matrixAutoUpdate = false;
|
this.group.matrixAutoUpdate = false;
|
||||||
|
this.group.chunkX = x;
|
||||||
|
this.group.chunkZ = z;
|
||||||
|
|
||||||
this.loaded = false;
|
this.loaded = false;
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ window.World = class {
|
|||||||
|
|
||||||
static TOTAL_HEIGHT = ChunkSection.SIZE * 16 - 1;
|
static TOTAL_HEIGHT = ChunkSection.SIZE * 16 - 1;
|
||||||
|
|
||||||
constructor() {
|
constructor(minecraft) {
|
||||||
|
this.minecrat = minecraft;
|
||||||
|
|
||||||
this.group = new THREE.Object3D();
|
this.group = new THREE.Object3D();
|
||||||
this.group.matrixAutoUpdate = false;
|
this.group.matrixAutoUpdate = false;
|
||||||
this.chunks = new Map();
|
this.chunks = new Map();
|
||||||
@@ -14,7 +16,16 @@ window.World = class {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onTick() {
|
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) {
|
getChunkAt(x, z) {
|
||||||
|
|||||||
Reference in New Issue
Block a user