-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
106 lines (85 loc) · 2.26 KB
/
main.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
let WIDTH = 640
let HEIGHT = 480
let aspectRatio = WIDTH / HEIGHT
let scene, camera, renderer
let controls
let terrain
let prevTime = 0
let time = 0
let init = () => {
scene = new THREE.Scene()
camera = new THREE.PerspectiveCamera(
75, // field of view
aspectRatio, // aspect ratio
0.01, // near clipping plane
100000 // far clipping plane
)
// let light = new THREE.DirectionalLight(0xffddcc, 1)
// let ambient = new THREE.AmbientLight(0xaaddff, 0.25)
let light = new THREE.DirectionalLight(0xffffff, 1)
let ambient = new THREE.AmbientLight(0xffffff, 0.25)
light.position.set(500, 500, 500)
scene.add(light)
scene.add(ambient)
let x = 64 // grid width
let z = 64 // grid depth
let y = 64 // grid height
let scale = 200 // grid size
let freq = 0.005 // noise frequency
let octaves = 5
// let lacunarity = 2.1042
let lacunarity = 1.8715
let persistence = 1/lacunarity
let LODMap
switch(2) {
case 0:
LODMap= [
[0]
]
break
case 1:
LODMap = [
[0, 2], // x
[2, 2]
// z
]
break
case 2:
LODMap = [
[ 0, 0, 1],
[ 0, 0, 1],
[ 1, 1, 1]
]
case 3:
LODMap = [
[ 0, 0, 1, 2],
[ 0, 0, 1, 2],
[ 1, 1, 1, 2],
[ 2, 2, 2, 2],
]
}
terrain = new Terrain(LODMap, x, z, y, scale, freq,
octaves, lacunarity, persistence)
_.values(terrain.chunks).forEach(chunk => scene.add(chunk.mesh))
let height = 150
let speed = 60000
controls = new PointerLockControls(camera, terrain, height, speed)
scene.add(controls.getObject())
renderer = new THREE.WebGLRenderer()
renderer.setSize(WIDTH, HEIGHT)
renderer.setClearColor(0x99ddff)
document
.getElementById('canvas-container')
.appendChild(renderer.domElement)
}
let render = () => {
requestAnimationFrame(render)
time += 1/60 * 1000
let dt = (time - prevTime) / 1000
let cameraPosition = controls.update(dt)
terrain.update(cameraPosition, scene)
prevTime = time
renderer.render(scene, camera)
}
init()
render()