-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractShapeBg.js
152 lines (123 loc) · 3.66 KB
/
AbstractShapeBg.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
import { Plane, Program, Mesh, Texture, RenderTarget, Camera, Color } from "../../ogl/src/index.js"
import { ColorBg } from "../ColorBg.js"
import { CanvasAbstractShapes } from "../../ogl/src/utils/CanvasAbstract.js"
export class AbstractShapeBg extends ColorBg {
constructor(params = {}) {
super(params, 6)
this.name = "abstract-shape"
this.options = this.params.options || {
noise: 0.05
}
this.canvasManager = new CanvasAbstractShapes(this.canvasW, this.canvasH, this.palette)
this.texture = new Texture(this.gl)
this.img = new Image()
this.start()
}
_resetSeed() {
this.canvasManager.reset(this.rng)
this._loadCanvas()
}
_loadCanvas() {
this.img.src = this.canvasManager.getCanvasData()
this.img.onload = () => {
this.texture.image = this.img
this.texture.needsUpdate = true
// canvasManager.destroy()
}
}
_makeMaterial() {
this._planeShader = new Program(this.gl, {
vertex: /* glsl */ `
#ifdef GL_ES
precision mediump float;
#endif
attribute vec3 position;
attribute vec2 uv;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform float uTime;
uniform float uWavy;
varying vec2 vUv;
void main() {
vUv = uv;
vec2 wavyCoord;
wavyCoord.s = sin( uTime + vUv.t * 15.0 );
wavyCoord.t = cos( uTime + vUv.s * 15.0 );
vec3 pos = position;
pos.x += wavyCoord.s * uWavy;
pos.y += wavyCoord.t * uWavy;
gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
}
`,
fragment: /* glsl */ `
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D tMap;
uniform float uNoiseAmount;
uniform float uTime;
uniform bool uAdd;
uniform vec3 u_color_0;
varying vec2 vUv;
float random(vec2 co) {
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
}
float blendScreen(float base, float blend) {
return 1.0-((1.0-base)*(1.0-blend));
}
vec3 blendScreen(vec3 base, vec3 blend) {
return vec3(blendScreen(base.r,blend.r),blendScreen(base.g,blend.g),blendScreen(base.b,blend.b));
}
void main() {
vec4 color = texture2D(tMap, vUv);
// if(uAdd) {
// vec3 colorAdd = u_color_0 * sin(uTime + vUv.x / 0.2) / 2.;
// color.rgb = blendScreen( color.rgb, colorAdd );
// }
float noise = (random(vUv) - 0.5) * uNoiseAmount;
color.rgb = color.rgb + color.rgb * noise;
gl_FragColor = vec4(color.rgb, 1.0);
}
`,
uniforms: {
tMap: { value: this.texture },
uNoiseAmount: { value: this.options.noise },
uWavy: { value: 10.0 },
uTime: { value: 0.0 },
uAdd: { value: false },
u_color_0: { value: new Color(this.palette[0]) }
}
})
}
_make() {
const planeGeo = new Plane(this.gl, {
width: this.canvasW * 1.06,
height: this.canvasH * 1.06,
widthSegments: 99,
heightSegments: 99
})
this._plane = new Mesh(this.gl, {
geometry: planeGeo,
program: this._planeShader
})
this._plane.setParent(this.scene)
}
_resetColors() {
this.canvasManager.colors(this.palette)
this.canvasManager.draw()
this._loadCanvas()
}
_animate() {
this._planeShader.uniforms.uTime.value = this.frame / 50
}
update(option, val) {
switch (option) {
case "noise":
this._planeShader.uniforms.uNoiseAmount.value = parseFloat(val)
break
case "wavy":
this._planeShader.uniforms.uWavy.value = parseFloat(val)
break
}
}
}