mirror of
https://github.com/Floriansylvain/ThatsMyWorld.git
synced 2026-08-19 11:43:16 +02:00
feat: chunks WIP
This commit is contained in:
+22
-15
@@ -16,36 +16,43 @@ 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++) {
|
||||||
const max = Math.ceil(Math.sin(i / 30) * 10 + Math.cos(j / 30) * 10)
|
for (let j = this.position.z; j < this.radius + this.position.z; j++) {
|
||||||
for (let k = -16; k <= max; k++) {
|
const max = Math.ceil(Math.sin(i / 30) * 10 + Math.cos(j / 30) * 10)
|
||||||
const position = new Vector3(i, k, j)
|
for (let k = -16; k <= max; k++) {
|
||||||
let textureName = "stone"
|
const position = new Vector3(i, k, j)
|
||||||
if (k === max) textureName = "grass"
|
let textureName = "stone"
|
||||||
else if (k > max - 6) textureName = "dirt"
|
if (k === max) textureName = "grass"
|
||||||
this.chunk.push({ position, textureName })
|
else if (k > max - 6) textureName = "dirt"
|
||||||
this.chunkSet.add(position.toArray().toString())
|
this.chunk.push({ position, textureName })
|
||||||
|
this.chunkSet.add(position.toArray().toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
resolve("")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
initializeFaces() {
|
initializeFaces() {
|
||||||
|
|||||||
+27
-12
@@ -31,22 +31,36 @@ async function loadTextures() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onTexturesLoaded() {
|
function onTexturesLoaded() {
|
||||||
const radius = 128
|
const radius = 16
|
||||||
const chunk = new Chunk(radius, textures)
|
|
||||||
|
|
||||||
chunk.countVisibleFaces()
|
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 cubes = [] as Block[]
|
const chunks = positions.map(
|
||||||
chunk.chunk.forEach(({ position, textureName }) => {
|
(position) => new Chunk(radius, textures, position)
|
||||||
const faces = chunk.filterFaces(position, textureName)
|
)
|
||||||
cubes.push(new Block(faces, position))
|
chunks.map((chunk) =>
|
||||||
})
|
chunk.generateChunk().then(() => {
|
||||||
|
chunk.initializeFaces()
|
||||||
|
chunk.countVisibleFaces()
|
||||||
|
|
||||||
Object.values(chunk.faces).forEach((faceArray) => {
|
const cubes = [] as Block[]
|
||||||
faceArray.forEach((face) => {
|
chunk.chunk.forEach(({ position, textureName }) => {
|
||||||
if (face.instancedMesh) scene.add(face.instancedMesh)
|
const faces = chunk.filterFaces(position, textureName)
|
||||||
|
cubes.push(new Block(faces, position))
|
||||||
|
})
|
||||||
|
|
||||||
|
Object.values(chunk.faces).forEach((faceArray) => {
|
||||||
|
faceArray.forEach((face) => {
|
||||||
|
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user