feat: chunks WIP

This commit is contained in:
Florian Sylvain
2024-06-10 14:03:08 +02:00
parent 89f656f2f3
commit cbcfb8bedf
2 changed files with 49 additions and 27 deletions
+13 -6
View File
@@ -16,25 +16,30 @@ interface BlockData {
} }
export default class Chunk { export default class Chunk {
position: Vector3
chunk: BlockData[] chunk: BlockData[]
chunkSet: Set<string> chunkSet: Set<string>
radius: number radius: number
textures: { [name: string]: Texture[] } textures: { [name: string]: Texture[] }
faces: { [key: string]: Face[] } faces: { [key: string]: Face[] }
constructor(radius: number, textures: { [name: string]: Texture[] }) { constructor(
radius: number,
textures: { [name: string]: Texture[] },
position: Vector3
) {
this.position = position
this.chunk = [] this.chunk = []
this.chunkSet = new Set<string>() this.chunkSet = new Set<string>()
this.radius = radius this.radius = radius
this.textures = textures this.textures = textures
this.faces = {} this.faces = {}
this.generateChunk()
this.initializeFaces()
} }
generateChunk() { async generateChunk() {
for (let i = 0; i < this.radius; i++) { return new Promise((resolve) => {
for (let j = 0; j < this.radius; j++) { for (let i = this.position.x; i < this.radius + this.position.x; i++) {
for (let j = this.position.z; j < this.radius + this.position.z; j++) {
const max = Math.ceil(Math.sin(i / 30) * 10 + Math.cos(j / 30) * 10) const max = Math.ceil(Math.sin(i / 30) * 10 + Math.cos(j / 30) * 10)
for (let k = -16; k <= max; k++) { for (let k = -16; k <= max; k++) {
const position = new Vector3(i, k, j) const position = new Vector3(i, k, j)
@@ -46,6 +51,8 @@ export default class Chunk {
} }
} }
} }
resolve("")
})
} }
initializeFaces() { initializeFaces() {
+17 -2
View File
@@ -31,9 +31,21 @@ async function loadTextures() {
} }
function onTexturesLoaded() { function onTexturesLoaded() {
const radius = 128 const radius = 16
const chunk = new Chunk(radius, textures)
const positions = [] as THREE.Vector3[]
for (let i = -8; i < 8; i++) {
for (let j = -8; j < 8; j++) {
positions.push(new THREE.Vector3(i * radius, 0, j * radius))
}
}
const chunks = positions.map(
(position) => new Chunk(radius, textures, position)
)
chunks.map((chunk) =>
chunk.generateChunk().then(() => {
chunk.initializeFaces()
chunk.countVisibleFaces() chunk.countVisibleFaces()
const cubes = [] as Block[] const cubes = [] as Block[]
@@ -47,6 +59,8 @@ function onTexturesLoaded() {
if (face.instancedMesh) scene.add(face.instancedMesh) if (face.instancedMesh) scene.add(face.instancedMesh)
}) })
}) })
})
)
} }
camera.position.x = 15 camera.position.x = 15
@@ -55,6 +69,7 @@ camera.position.z = 15
function animate(elapsedTimeMs: number) { function animate(elapsedTimeMs: number) {
renderer.render(scene, camera) renderer.render(scene, camera)
console.log(renderer.info.render.calls)
controls.update() controls.update()
} }