-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader_loader.mjs
38 lines (30 loc) · 979 Bytes
/
shader_loader.mjs
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
import * as THREE from 'three';
function enableShaders(material) {
material.fragmentShader = material.userData.frag;
material.vertexShader = material.userData.vtx;
material.needsUpdate = true;
}
function attachVtxShader(txt, material) {
material.userData.vtx = txt;
if (material.userData.frag) {
enableShaders(material);
}
}
function attachFragShader(txt, material) {
material.userData.frag = txt;
if (material.userData.vtx) {
enableShaders(material);
}
}
export default class ShaderLoader {
#loader;
constructor() {
this.#loader = new THREE.FileLoader();
}
// loads the vertex and fragment shader located at the given location and adds them to the given ShaderMaterial
// once both are loaded
load(vertex, fragment, material) {
this.#loader.load(vertex, txt => attachVtxShader(txt, material));
this.#loader.load(fragment, txt => attachFragShader(txt, material));
}
}