-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
239 lines (201 loc) · 5.58 KB
/
index.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import './style.css'
import * as dat from 'dat.gui'
import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js'
import {DRACOLoader} from 'three/examples/jsm/loaders/DRACOLoader.js'
import firefliesVertexShader from './shaders/fireflies/vertex.glsl'
import firefliesFragmentShader from './shaders/fireflies/fragment.glsl'
import portalVertexShader from './shaders/portal/vertex.glsl'
import portalFragmentShader from './shaders/portal/fragment.glsl'
/**
* Base
*/
// Debug
const debugObject = {}
const gui = new dat.GUI({
width: 400,
})
// Canvas
const canvas = document.querySelector('canvas.webgl')
// Scene
const scene = new THREE.Scene()
/**
* Loaders
*/
// Texture loader
const textureLoader = new THREE.TextureLoader()
// Draco loader
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('draco/')
// GLTF loader
const gltfLoader = new GLTFLoader()
gltfLoader.setDRACOLoader(dracoLoader)
/**
* Textures
*/
const bakedTexture = textureLoader.load('baked.jpg')
bakedTexture.flipY = false
bakedTexture.encoding = THREE.sRGBEncoding
/**
* Materials
*/
// Baked
const bakedMaterial = new THREE.MeshBasicMaterial({map: bakedTexture})
debugObject.portalColorStart = '#5EDAFC'
debugObject.portalColorEnd = '#8EEFF7'
gui.addColor(debugObject, 'portalColorStart').onChange(() => {
portalLightMaterial.uniforms.uColorStart.value.set(
debugObject.portalColorStart,
)
})
gui.addColor(debugObject, 'portalColorEnd').onChange(() => {
portalLightMaterial.uniforms.uColorEnd.value.set(debugObject.portalColorEnd)
})
// Pole light material
const portalLightMaterial = new THREE.ShaderMaterial({
uniforms: {
uTime: {value: 0},
uColorStart: {value: new THREE.Color(debugObject.portalColorStart)},
uColorEnd: {value: new THREE.Color(debugObject.portalColorEnd)},
},
vertexShader: portalVertexShader,
fragmentShader: portalFragmentShader,
})
const poleLightMaterial = new THREE.MeshBasicMaterial({color: 0xee9911})
/**
* Model
*/
gltfLoader.load('portal.glb', (gltf) => {
const bakedMesh = gltf.scene.children.find((child) => child.name === 'baked')
const poleLightAMesh = gltf.scene.children.find(
(child) => child.name === 'poleLightA',
)
const poleLightBMesh = gltf.scene.children.find(
(child) => child.name === 'poleLightB',
)
const portalLightMesh = gltf.scene.children.find(
(child) => child.name === 'portalLight',
)
bakedMesh.material = bakedMaterial
portalLightMesh.material = portalLightMaterial
poleLightAMesh.material = poleLightMaterial
poleLightBMesh.material = poleLightMaterial
scene.add(gltf.scene)
})
/**
* Fireflies
*/
// Geometry
const firefliesGeometry = new THREE.BufferGeometry()
const firefliesCount = 30
const positionArray = new Float32Array(firefliesCount * 3)
const scaleArray = new Float32Array(firefliesCount)
for (let i = 0; i < firefliesCount; i++) {
positionArray[i * 3 + 0] = (Math.random() - 0.5) * 4
positionArray[i * 3 + 1] = Math.random() * 1.5
positionArray[i * 3 + 2] = (Math.random() - 0.5) * 4
scaleArray[i] = Math.random()
}
firefliesGeometry.setAttribute(
'position',
new THREE.BufferAttribute(positionArray, 3),
)
firefliesGeometry.setAttribute(
'aScale',
new THREE.BufferAttribute(scaleArray, 1),
)
// Material
const firefliesMaterial = new THREE.ShaderMaterial({
uniforms: {
uTime: {value: 0},
uPixelRatio: {value: Math.min(window.devicePixelRatio, 2)},
uSize: {value: 250},
},
vertexShader: firefliesVertexShader,
fragmentShader: firefliesFragmentShader,
transparent: true,
blending: THREE.AdditiveBlending,
depthWrite: false,
})
gui
.add(firefliesMaterial.uniforms.uSize, 'value')
.min(0)
.max(500)
.step(1)
.name('size')
// Points
const fireflies = new THREE.Points(firefliesGeometry, firefliesMaterial)
scene.add(fireflies)
/**
* Sizes
*/
const sizes = {
width: window.innerWidth,
height: window.innerHeight,
}
window.addEventListener('resize', () => {
// Update sizes
sizes.width = window.innerWidth
sizes.height = window.innerHeight
// Update camera
camera.aspect = sizes.width / sizes.height
camera.updateProjectionMatrix()
// Update renderer
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
// Update fireflies
firefliesMaterial.uniforms.uPixelRatio.value = Math.min(
window.devicePixelRatio,
2,
)
})
/**
* Camera
*/
// Base camera
const camera = new THREE.PerspectiveCamera(
45,
sizes.width / sizes.height,
0.1,
100,
)
camera.position.x = 6.5
camera.position.y = 6.5
camera.position.z = 6.5
scene.add(camera)
// Controls
const controls = new OrbitControls(camera, canvas)
controls.enableDamping = true
/**
* Renderer
*/
const renderer = new THREE.WebGLRenderer({
canvas: canvas,
antialias: true,
})
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
renderer.outputEncoding = THREE.sRGBEncoding
debugObject.clearColor = '#191f20'
renderer.setClearColor(debugObject.clearColor)
gui.addColor(debugObject, 'clearColor').onChange(() => {
renderer.setClearColor(debugObject.clearColor)
})
/**
* Animate
*/
const clock = new THREE.Clock()
const tick = () => {
const elapsedTime = clock.getElapsedTime()
// Update materials
portalLightMaterial.uniforms.uTime.value = elapsedTime
firefliesMaterial.uniforms.uTime.value = elapsedTime
// Update controls
controls.update()
// Render
renderer.render(scene, camera)
// Call tick again on the next frame
window.requestAnimationFrame(tick)
}
tick()