-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
181 lines (151 loc) · 4.74 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// IMPORTANT
// HIDE CURSOR AFTER START
// ADD SOUND
// AUTOMATICALLY GO TO FULL SCREEN
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import Stats from 'three/addons/libs/stats.module.js'
import { Explosion } from './scenes/explosion'
import { CityAtStreetLevel } from './scenes/cityAtStreetLevel'
import { CityFromWindow } from './scenes/cityFromWindow'
import song from './assets/music/smoke-143172-cut.mp3'
import { GUI } from 'three/addons/libs/lil-gui.module.min.js'
import { OutputPass } from 'three/addons/postprocessing/OutputPass.js'
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js'
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js'
import { Credits } from './scenes/credits'
import { Viuh } from './scenes/viuh'
import { CubeRubber } from './scenes/CubeRubber'
import { SnakeBirth } from './scenes/snakeBirth'
let scene, scenes, renderer, stats, composer, current, timeout
init()
const loop = i => {
current = i
scene = scenes[i].scene
const controls = new OrbitControls(scene.getCamera, renderer.domElement)
controls.minDistance = 2
controls.maxDistance = 20
controls.enabled = false
controls.update()
composer = new EffectComposer(renderer)
composer.addPass(new RenderPass(scenes[i].scene.getScene, scenes[i].scene.getCamera))
scenes[i].scene.getEffectShaders.forEach(shaderPass => {
composer.addPass(shaderPass)
})
const outputPass = new OutputPass()
composer.addPass(outputPass)
if (scenes[i]?.time) {
timeout = Date.now() + scenes[i].time
} else {
timeout = false
}
}
function init () {
scenes = [
{
scene: new SnakeBirth(),
time: 5000
},
{
scene: new Viuh(),
time: 6000
},
{
scene: new CityAtStreetLevel(),
time: 7500
},
{
scene: new CityFromWindow(),
time: 9700
},
{
scene: new CubeRubber(),
time: 7500
},
{
scene: new Explosion(),
time: 10000
},
{
scene: new Credits()
}
]
// Stats
stats = new Stats()
const music = new window.Audio(song)
const welcomeGUI = new GUI()
welcomeGUI.hide()
const settings = {
fullScreen: import.meta.env.MODE === 'production',
music: import.meta.env.MODE === 'production',
stats: import.meta.env.MODE === 'development',
antialias: import.meta.env.MODE === 'production',
checkShaderErrors: import.meta.env.MODE === 'development',
pixelRatio: import.meta.env.MODE === 'production' ? window.devicePixelRatio : Number((window.devicePixelRatio * 0.70).toFixed(1)),
start: () => {
welcomeGUI.hide()
if (import.meta.env.MODE === 'production') document.body.style.cursor = 'none'
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: settings.antialias })
renderer.debug = { checkShaderErrors: settings.checkShaderErrors }
renderer.shadowMap.enabled = true
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.setClearColor(0x263238)
window.addEventListener('resize', onWindowResize)
document.body.appendChild(renderer.domElement)
renderer.localClippingEnabled = true
// Show or hide stats
if (settings.stats) document.body.appendChild(stats.dom)
renderer.setPixelRatio(settings.pixelRatio)
if (settings.fullScreen) {
if (!document.fullscreenElement) {
document.documentElement.requestFullscreen()
} else if (document.exitFullscreen) {
document.exitFullscreen()
}
}
if (settings.music) {
music.play()
music.addEventListener('playing', () => {
loop(0)
animate()
})
return
}
loop(0)
animate()
}
}
welcomeGUI.add(settings, 'antialias')
welcomeGUI.add(settings, 'fullScreen')
welcomeGUI.add(settings, 'music')
welcomeGUI.add(settings, 'stats')
welcomeGUI.add(settings, 'checkShaderErrors')
welcomeGUI.add(settings, 'pixelRatio', 0.1, window.devicePixelRatio, 0.1)
welcomeGUI.add(settings, 'start')
music.addEventListener('canplaythrough', () => {
welcomeGUI.show()
})
music.addEventListener('ended', () => {
welcomeGUI.show()
if (document.exitFullscreen) {
document.exitFullscreen()
}
})
}
function onWindowResize () {
scene.getCamera.aspect = window.innerWidth / window.innerHeight
scene.getCamera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
}
const animate = () => {
if (timeout && Date.now() >= timeout) {
loop(current + 1)
}
window.requestAnimationFrame(animate)
scene.animate()
stats.begin()
composer.render(scene.getScene, scene.getCamera)
stats.end()
}