forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChunkManager.js
More file actions
81 lines (73 loc) · 2.37 KB
/
ChunkManager.js
File metadata and controls
81 lines (73 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { BufferGeometry, BufferAttribute, Mesh } from 'three'
import { TWEEN } from 'three/examples/jsm/libs/tween.module.min.js'
import vec3 from 'vec3'
class ChunkManager {
constructor (game) {
this.game = game
this.cellMesh = new Map()
}
addChunk (cellId, vert) {
const geometry = new BufferGeometry()
geometry.setAttribute('position', new BufferAttribute(new Float32Array(vert.positions), 3))
geometry.setAttribute('normal', new BufferAttribute(new Float32Array(vert.normals), 3))
geometry.setAttribute('uv', new BufferAttribute(new Float32Array(vert.uvs), 2))
geometry.setAttribute('color', new BufferAttribute(new Float32Array(vert.colors), 3))
geometry.matrixAutoUpdate = true
const mesh = this.cellMesh.get(cellId)
if (mesh === undefined) {
const newMesh = new Mesh(geometry, this.game.world.material)
newMesh.matrixAutoUpdate = true
newMesh.frustumCulled = false
newMesh.onAfterRender = () => {
newMesh.frustumCulled = true
newMesh.onAfterRender = function () {}
}
this.cellMesh.set(cellId, newMesh)
this.game.scene.add(newMesh)
newMesh.position.y = -32
const to = {
y: 0
}
new TWEEN.Tween(newMesh.position)
.to(to, 1000)
.easing(TWEEN.Easing.Quadratic.Out)
.onComplete(() => {
newMesh.matrixAutoUpdate = true
newMesh.geometry.matrixAutoUpdate = true
})
.start()
if (this.game.world.lastPlayerChunk !== null) {
this.updateRenderOrder(JSON.parse(this.game.world.lastPlayerChunk))
}
} else {
this.cellMesh.get(cellId).geometry = geometry
}
}
removeChunk (cellId) {
if (this.cellMesh.get(cellId) !== undefined) {
this.cellMesh.get(cellId).geometry.dispose()
this.game.scene.remove(this.cellMesh.get(cellId))
this.cellMesh.delete(cellId)
this.game.renderer.renderLists.dispose()
}
}
updateRenderOrder (cell) {
for (const [k, v] of this.cellMesh) {
const x = vec3(this.game.world.chunkTerrain.strToVec(k))
v.renderOrder = -vec3(...cell).distanceTo(x)
}
}
reset () {
for (const i of this.cellMesh) {
if (i[1].geometry !== undefined) {
i[1].geometry.dispose()
this.game.scene.remove(i[1])
}
}
this.cellMesh.clear()
}
update () {
TWEEN.update()
}
}
export { ChunkManager }