Skip to content

Commit c054984

Browse files
committed
ChunkManager
1 parent 9a8b98c commit c054984

File tree

4 files changed

+155
-186
lines changed

4 files changed

+155
-186
lines changed

src/scripts/world/ChunkManager.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { BufferGeometry, BufferAttribute, Mesh } from 'three'
2+
3+
class ChunkManager {
4+
constructor (game) {
5+
this.game = game
6+
this.chunks = new Map()
7+
this.cellMesh = new Map()
8+
}
9+
10+
addChunk (cellId, vert) {
11+
this.chunks.set(cellId, vert)
12+
13+
const geometry = new BufferGeometry()
14+
geometry.setAttribute('position', new BufferAttribute(new Float32Array(vert.positions), 3))
15+
geometry.setAttribute('normal', new BufferAttribute(new Float32Array(vert.normals), 3))
16+
geometry.setAttribute('uv', new BufferAttribute(new Float32Array(vert.uvs), 2))
17+
geometry.setAttribute('color', new BufferAttribute(new Float32Array(vert.colors), 3))
18+
geometry.matrixAutoUpdate = false
19+
20+
const mesh = this.cellMesh.get(cellId)
21+
if (mesh === undefined) {
22+
const newMesh = new Mesh(geometry, this.game.world.material)
23+
newMesh.matrixAutoUpdate = false
24+
newMesh.frustumCulled = false
25+
newMesh.onAfterRender = () => {
26+
newMesh.frustumCulled = true
27+
newMesh.onAfterRender = function () {}
28+
}
29+
this.cellMesh.set(cellId, newMesh)
30+
this.game.scene.add(newMesh)
31+
if (this.game.world.lastPlayerChunk !== null) {
32+
this.game.world.updateRenderOrder(JSON.parse(this.game.world.lastPlayerChunk))
33+
}
34+
} else {
35+
this.cellMesh.get(cellId).geometry = geometry
36+
}
37+
}
38+
39+
removeChunk (cellId) {
40+
if (this.chunks.get(cellId) !== undefined) {
41+
this.cellMesh.get(cellId).geometry.dispose()
42+
this.game.scene.remove(this.cellMesh.get(cellId))
43+
this.cellMesh.delete(cellId)
44+
this.game.renderer.renderLists.dispose()
45+
}
46+
}
47+
48+
reset () {
49+
for (const i of this.cellMesh) {
50+
if (i[1].geometry !== undefined) {
51+
i[1].geometry.dispose()
52+
this.game.scene.remove(i[1])
53+
}
54+
}
55+
this.cellMesh.clear()
56+
this.chunks.clear()
57+
}
58+
}
59+
export { ChunkManager }

src/scripts/world/ChunkMerger.js

Whitespace-only changes.

src/scripts/world/ChunkTerrain.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,92 @@ class ChunkTerrain {
113113
return [main, neighbours]
114114
}
115115

116+
intersectsRay (start, end) {
117+
start.x += 0.5
118+
start.y += 0.5
119+
start.z += 0.5
120+
end.x += 0.5
121+
end.y += 0.5
122+
end.z += 0.5
123+
let dx = end.x - start.x
124+
let dy = end.y - start.y
125+
let dz = end.z - start.z
126+
const lenSq = dx * dx + dy * dy + dz * dz
127+
const len = Math.sqrt(lenSq)
128+
dx /= len
129+
dy /= len
130+
dz /= len
131+
let t = 0.0
132+
let ix = Math.floor(start.x)
133+
let iy = Math.floor(start.y)
134+
let iz = Math.floor(start.z)
135+
const stepX = dx > 0 ? 1 : -1
136+
const stepY = dy > 0 ? 1 : -1
137+
const stepZ = dz > 0 ? 1 : -1
138+
const txDelta = Math.abs(1 / dx)
139+
const tyDelta = Math.abs(1 / dy)
140+
const tzDelta = Math.abs(1 / dz)
141+
const xDist = stepX > 0 ? ix + 1 - start.x : start.x - ix
142+
const yDist = stepY > 0 ? iy + 1 - start.y : start.y - iy
143+
const zDist = stepZ > 0 ? iz + 1 - start.z : start.z - iz
144+
let txMax = txDelta < Infinity ? txDelta * xDist : Infinity
145+
let tyMax = tyDelta < Infinity ? tyDelta * yDist : Infinity
146+
let tzMax = tzDelta < Infinity ? tzDelta * zDist : Infinity
147+
let steppedIndex = -1
148+
while (t <= len) {
149+
const block = this.getBlock(ix, iy, iz)
150+
let voxel
151+
if (
152+
block.name === 'air' || block.name === 'cave_air' || block.name === 'void_air' || block.name === 'water'
153+
) {
154+
voxel = 0
155+
} else {
156+
voxel = 1
157+
}
158+
if (voxel) {
159+
return {
160+
position: [
161+
start.x + t * dx,
162+
start.y + t * dy,
163+
start.z + t * dz
164+
],
165+
normal: [
166+
steppedIndex === 0 ? -stepX : 0,
167+
steppedIndex === 1 ? -stepY : 0,
168+
steppedIndex === 2 ? -stepZ : 0
169+
],
170+
voxel
171+
}
172+
}
173+
if (txMax < tyMax) {
174+
if (txMax < tzMax) {
175+
ix += stepX
176+
t = txMax
177+
txMax += txDelta
178+
steppedIndex = 0
179+
} else {
180+
iz += stepZ
181+
t = tzMax
182+
tzMax += tzDelta
183+
steppedIndex = 2
184+
}
185+
} else {
186+
if (tyMax < tzMax) {
187+
iy += stepY
188+
t = tyMax
189+
tyMax += tyDelta
190+
steppedIndex = 1
191+
} else {
192+
iz += stepZ
193+
t = tzMax
194+
tzMax += tzDelta
195+
steppedIndex = 2
196+
}
197+
}
198+
}
199+
return null
200+
}
201+
116202
reset () {
117203
this.chunks = {}
118204
}

0 commit comments

Comments
 (0)