-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy path_shaderBase.js
More file actions
95 lines (92 loc) · 2.53 KB
/
_shaderBase.js
File metadata and controls
95 lines (92 loc) · 2.53 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
import VantaBase from './_base.js'
export {VANTA} from './_base.js'
const win = typeof window == 'object'
let THREE = win && window.THREE
export default class ShaderBase extends VantaBase {
constructor(userOptions) {
THREE = userOptions.THREE || THREE
THREE.Color.prototype.toVector = function(){
return new THREE.Vector3(this.r, this.g, this.b)
}
super(userOptions)
this.updateUniforms = this.updateUniforms.bind(this)
}
init(){
this.mode = 'shader'
this.uniforms = {
iTime: {
type: "f",
value: 1.0
},
iResolution: {
type: "v2",
value: new THREE.Vector2(1, 1)
},
iDpr: {
type: "f",
value: window.devicePixelRatio || 1
},
iMouse: {
type: "v2",
value: new THREE.Vector2(this.mouseX || 0, this.mouseY || 0)
}
}
super.init()
if (this.fragmentShader) {
this.initBasicShader()
}
}
setOptions(userOptions){
super.setOptions(userOptions)
this.updateUniforms()
}
initBasicShader(fragmentShader = this.fragmentShader, vertexShader = this.vertexShader) {
if (!vertexShader) {
vertexShader = "uniform float uTime;\nuniform vec2 uResolution;\nvoid main() {\n gl_Position = vec4( position, 1.0 );\n}"
}
this.updateUniforms()
if (typeof this.valuesChanger === "function") {
this.valuesChanger() // Some effects define this themselves
}
const material = new THREE.ShaderMaterial({
uniforms: this.uniforms,
vertexShader: vertexShader,
fragmentShader: fragmentShader
})
const texPath = this.options.texturePath
if (texPath) {
this.uniforms.iTex = {
type: "t",
value: new THREE.TextureLoader().load(texPath)
}
}
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), material)
this.scene.add(mesh)
this.camera = new THREE.Camera()
this.camera.position.z = 1
}
updateUniforms() {
const newUniforms = {}
let k, v
for (k in this.options) {
v = this.options[k]
if (k.toLowerCase().indexOf('color') !== -1) {
newUniforms[k] = {
type: "v3",
value: new THREE.Color(v).toVector()
}
} else if (typeof v === 'number') {
newUniforms[k] = {
type: "f",
value: v
}
}
}
return Object.assign(this.uniforms, newUniforms)
}
resize(){
super.resize()
this.uniforms.iResolution.value.x = this.width / this.scale
this.uniforms.iResolution.value.y = this.height / this.scale
}
}