-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvisual-3d.ts
More file actions
265 lines (219 loc) · 7.43 KB
/
visual-3d.ts
File metadata and controls
265 lines (219 loc) · 7.43 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* @license
* SPDX-License-Identifier: Apache-2.0
*/
// tslint:disable:organize-imports
// tslint:disable:ban-malformed-import-paths
// tslint:disable:no-new-decorators
import {LitElement, css, html} from 'lit';
import {customElement, property} from 'lit/decorators.js';
import {Analyser} from './analyser';
import * as THREE from 'three';
import {EXRLoader} from 'three/addons/loaders/EXRLoader.js';
import {EffectComposer} from 'three/addons/postprocessing/EffectComposer.js';
import {RenderPass} from 'three/addons/postprocessing/RenderPass.js';
import {ShaderPass} from 'three/addons/postprocessing/ShaderPass.js';
import {UnrealBloomPass} from 'three/addons/postprocessing/UnrealBloomPass.js';
import {FXAAShader} from 'three/addons/shaders/FXAAShader.js';
import {fs as backdropFS, vs as backdropVS} from './backdrop-shader';
import {vs as sphereVS} from './sphere-shader';
/**
* 3D live audio visual.
*/
@customElement('gdm-live-audio-visuals-3d')
export class GdmLiveAudioVisuals3D extends LitElement {
private inputAnalyser!: Analyser;
private outputAnalyser!: Analyser;
private camera!: THREE.PerspectiveCamera;
private backdrop!: THREE.Mesh;
private composer!: EffectComposer;
private sphere!: THREE.Mesh;
private prevTime = 0;
private rotation = new THREE.Vector3(0, 0, 0);
private _outputNode!: AudioNode;
@property()
set outputNode(node: AudioNode) {
this._outputNode = node;
this.outputAnalyser = new Analyser(this._outputNode);
}
get outputNode() {
return this._outputNode;
}
private _inputNode!: AudioNode;
@property()
set inputNode(node: AudioNode) {
this._inputNode = node;
this.inputAnalyser = new Analyser(this._inputNode);
}
get inputNode() {
return this._inputNode;
}
private canvas!: HTMLCanvasElement;
static styles = css`
:host {
display: block;
width: 100%;
height: 100%;
position: absolute;
inset: 0;
}
canvas {
width: 100% !important;
height: 100% !important;
position: absolute;
inset: 0;
image-rendering: pixelated;
}
`;
connectedCallback() {
super.connectedCallback();
}
private init() {
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x100c14);
const backdrop = new THREE.Mesh(
new THREE.IcosahedronGeometry(10, 5),
new THREE.RawShaderMaterial({
uniforms: {
resolution: {value: new THREE.Vector2(1, 1)},
rand: {value: 0},
},
vertexShader: backdropVS,
fragmentShader: backdropFS,
glslVersion: THREE.GLSL3,
}),
);
backdrop.material.side = THREE.BackSide;
scene.add(backdrop);
this.backdrop = backdrop;
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000,
);
camera.position.set(2, -2, 5);
this.camera = camera;
const renderer = new THREE.WebGLRenderer({
canvas: this.canvas,
antialias: !true,
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio / 1);
const geometry = new THREE.IcosahedronGeometry(1, 10);
new EXRLoader().load('piz_compressed.exr', (texture: THREE.Texture) => {
texture.mapping = THREE.EquirectangularReflectionMapping;
const exrCubeRenderTarget = pmremGenerator.fromEquirectangular(texture);
sphereMaterial.envMap = exrCubeRenderTarget.texture;
sphere.visible = true;
});
const pmremGenerator = new THREE.PMREMGenerator(renderer);
pmremGenerator.compileEquirectangularShader();
const sphereMaterial = new THREE.MeshStandardMaterial({
color: 0x000010,
metalness: 0.5,
roughness: 0.1,
emissive: 0x000010,
emissiveIntensity: 1.5,
});
sphereMaterial.onBeforeCompile = (shader) => {
shader.uniforms.time = {value: 0};
shader.uniforms.inputData = {value: new THREE.Vector4()};
shader.uniforms.outputData = {value: new THREE.Vector4()};
sphereMaterial.userData.shader = shader;
shader.vertexShader = sphereVS;
};
const sphere = new THREE.Mesh(geometry, sphereMaterial);
scene.add(sphere);
sphere.visible = false;
this.sphere = sphere;
const renderPass = new RenderPass(scene, camera);
const bloomPass = new UnrealBloomPass(
new THREE.Vector2(window.innerWidth, window.innerHeight),
5,
0.5,
0,
);
const fxaaPass = new ShaderPass(FXAAShader);
const composer = new EffectComposer(renderer);
composer.addPass(renderPass);
// composer.addPass(fxaaPass);
composer.addPass(bloomPass);
this.composer = composer;
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
const dPR = renderer.getPixelRatio();
const w = window.innerWidth;
const h = window.innerHeight;
backdrop.material.uniforms.resolution.value.set(w * dPR, h * dPR);
renderer.setSize(w, h);
composer.setSize(w, h);
fxaaPass.material.uniforms['resolution'].value.set(
1 / (w * dPR),
1 / (h * dPR),
);
}
window.addEventListener('resize', onWindowResize);
onWindowResize();
this.animation();
}
private animation() {
requestAnimationFrame(() => this.animation());
this.inputAnalyser.update();
this.outputAnalyser.update();
const t = performance.now();
const dt = (t - this.prevTime) / (1000 / 60);
this.prevTime = t;
const backdropMaterial = this.backdrop.material as THREE.RawShaderMaterial;
const sphereMaterial = this.sphere.material as THREE.MeshStandardMaterial;
backdropMaterial.uniforms.rand.value = Math.random() * 10000;
if (sphereMaterial.userData.shader) {
this.sphere.scale.setScalar(
1 + (0.2 * this.outputAnalyser.data[1]) / 255,
);
const f = 0.001;
this.rotation.x += (dt * f * 0.5 * this.outputAnalyser.data[1]) / 255;
this.rotation.z += (dt * f * 0.5 * this.inputAnalyser.data[1]) / 255;
this.rotation.y += (dt * f * 0.25 * this.inputAnalyser.data[2]) / 255;
this.rotation.y += (dt * f * 0.25 * this.outputAnalyser.data[2]) / 255;
const euler = new THREE.Euler(
this.rotation.x,
this.rotation.y,
this.rotation.z,
);
const quaternion = new THREE.Quaternion().setFromEuler(euler);
const vector = new THREE.Vector3(0, 0, 5);
vector.applyQuaternion(quaternion);
this.camera.position.copy(vector);
this.camera.lookAt(this.sphere.position);
sphereMaterial.userData.shader.uniforms.time.value +=
(dt * 0.1 * this.outputAnalyser.data[0]) / 255;
sphereMaterial.userData.shader.uniforms.inputData.value.set(
(1 * this.inputAnalyser.data[0]) / 255,
(0.1 * this.inputAnalyser.data[1]) / 255,
(10 * this.inputAnalyser.data[2]) / 255,
0,
);
sphereMaterial.userData.shader.uniforms.outputData.value.set(
(2 * this.outputAnalyser.data[0]) / 255,
(0.1 * this.outputAnalyser.data[1]) / 255,
(10 * this.outputAnalyser.data[2]) / 255,
0,
);
}
this.composer.render();
}
protected firstUpdated() {
this.canvas = this.shadowRoot!.querySelector('canvas') as HTMLCanvasElement;
this.init();
}
protected render() {
return html`<canvas></canvas>`;
}
}
declare global {
interface HTMLElementTagNameMap {
'gdm-live-audio-visuals-3d': GdmLiveAudioVisuals3D;
}
}