-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
394 lines (375 loc) · 13.5 KB
/
index.html
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!DOCTYPE HTML>
<html>
<head>
<title>Modulateur</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/97/three.min.js" crossorigin="anonymous"></script>
<!--script src="three.min.js"></script-->
<link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div class="display"></div>
<script id="fslib-const" type="x-shader/x-fragment">
#define PI 3.1415926535897932384626433832795
#define COLOR_STEP 0.00392156862745098039
</script>
<script id="fslib-hsv" type="x-shader/x-fragment">
// http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
vec3 rgb2hsv(vec3 c)
{
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
// http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
</script>
<script id="fslib-math" type="x-shader/x-fragment">
float modI(float a,float b) {
float m=a-floor((a+0.5)/b)*b;
return floor(m+0.5);
}
float clampDist(float val, float minVal, float maxVal) {
float nval = val - minVal;
float rng = maxVal - minVal;
float quot = pow(nval/rng, 2.);
return clamp(quot*2.-1., -1., 1.);
}
</script>
<script id="fslib-uniforms" type="x-shader/x-fragment">
uniform float time;
uniform vec2 resolution;
uniform sampler2D source;
uniform float outMix;
varying vec2 vUv;
</script>
<script id="fs-cyclechannel" type="x-shader/x-fragment">
#import uniforms;
#import const;
#import math;
void main( void )
{
vec2 position = vUv;
vec2 center = vec2(position.x*2.-1., position.y*2.-1.);
vec4 color = texture2D(source, position);
vec3 val = vec3(
clampDist(color.r, COLOR_STEP, 1.),
clampDist(color.g, COLOR_STEP, 1.),
clampDist(color.b, COLOR_STEP, 1.)
);
gl_FragColor = vec4(mix(color.rgb + val * COLOR_STEP , color.rgb, .7), 1.0);
}
</script>
<script id="fs-cyclehue" type="x-shader/x-fragment">
#import uniforms;
#import hsv;
#import const;
void main( void )
{
vec2 position = vUv;
vec4 color = texture2D(source, position);
vec3 hsv = rgb2hsv(color.rgb);
hsv.r = mod((hsv.r + COLOR_STEP*1.) , 1.);
if (hsv.g < COLOR_STEP*128.) {
hsv.g += COLOR_STEP;
}
hsv.g = clamp(hsv.g, COLOR_STEP, 1.);
hsv.b = clamp(hsv.b, COLOR_STEP, 1.);
gl_FragColor = vec4(mix(hsv2rgb(hsv), color.rgb, .0), 1.0);
}
</script>
<script id="fs-waves" type="x-shader/x-fragment">
#import uniforms;
#import const;
void main( void ) {
vec2 position = vUv;
vec2 center = vec2(position.x*2.-1., position.y*2.-1.);
vec4 color = texture2D(source, position);
float add = 0.0;
add += sin( center.x * cos( time / 17. ) * resolution.x / 120. ) * sin( center.y * cos( time / 17. ) * resolution.y / 120. ) ;
add -= sin( center.x * sin( time / 3. ) * resolution.x / 30. ) * sin( center.y * sin( time / 3. ) * resolution.y / 30. ) ;
add *= cos( center.x * sin( time / 13. ) * resolution.x / 60. ) * cos( center.y * sin( time / 13. ) * resolution.y / 60. ) ;
add *= cos( center.x * sin( time / 19. ) * resolution.x / 240. ) + cos( center.y * sin( time / 19. ) * resolution.y / 240. ) ;
add *= sin( center.x * cos( time / 43. ) * resolution.x / 100. ) + sin( center.y * cos( time / 43. ) * resolution.y / 100. ) ;
vec3 value = clamp(color.rgb + add, COLOR_STEP, 1.);
gl_FragColor = vec4(mix(value, color.rgb, .95), 1. );
}
</script>
<script id="fs-blur" type="x-shader/x-fragment">
#import uniforms;
#import const;
void main( void ) {
vec2 position = vUv;
vec4 color = texture2D(source, position);
vec4 smoothed = vec4(0.0);
smoothed += texture2D(source, position) * 0.2;
smoothed += texture2D(source, position + vec2(-1./resolution.x,-1./resolution.y)) * 0.2;
smoothed += texture2D(source, position + vec2(1./resolution.x,1./resolution.y)) * 0.2;
smoothed += texture2D(source, position + vec2(1./resolution.x,-1./resolution.y)) * 0.2;
smoothed += texture2D(source, position + vec2(-1./resolution.x,1./resolution.y)) * 0.2;
gl_FragColor = vec4(mix(smoothed.rgb, color.rgb, .9), 1. );
}
</script>
<script>
function makeEmptyTexture(width, height, color) {
width = width || 1
height = height || 1
color = color || {}
const size = width * height
const data = new Uint8Array(3 * size)
const r = ((color.r || 0) * 0xFF) << 0
const g = ((color.g || 0) * 0xFF) << 0
const b = ((color.b || 0) * 0xFF) << 0
for ( var i = 0; i < size; i ++ ) {
var stride = i * 3;
data[stride] = r;
data[stride + 1] = g;
data[stride + 2] = b;
}
var texture = new THREE.DataTexture( data, width, height, THREE.RGBFormat );
texture.needsUpdate = true
return texture
}
function precompileShader(name) {
let shaderSrc = document.querySelector('#fs-'+name).textContent
let i = 10 // import limit
let imported = {}
while(--i>0) {
const match = shaderSrc.match(/#import\s*([^\s;]+)/)
if (match && match.length == 2) {
let replacement = "";
if (!imported[match[1]]) {
replacement = document.querySelector('#fslib-'+match[1]).textContent
imported[match[1]] = true
}
shaderSrc = shaderSrc.replace(/#import\s*[^\s]+;/, replacement)
} else {
break;
}
}
return shaderSrc
}
function makeUniforms(shader) {
const rx = /uniform\s+([^\s;]+)\s+([^\s;]+)(\s*\[\s*([0-9]+)\s*\])?\s*;/g
const uniforms = {}
let match
do {
match = rx.exec(shader)
if (match) {
let len = match[4]?parseInt(match[4]):0
const ref = uniforms[match[2]] = {}
if (len) ref.value = []
switch (match[1]) {
case "int":
ref.type = len?"iv1":"i"
if (len) while (ref.value.length<len) ref.value.push(0)
else ref.value = 0
break
case "float":
ref.type = len?"fv1":"f"
if (len) while (ref.value.length<len) ref.value.push(0.)
else ref.value = 0.
break
case "vec2":
ref.type = len?"v2v":"v2"
if (len) while (ref.value.length<len) ref.value.push(new THREE.Vector2(0., 0.))
else ref.value = new THREE.Vector2(0,0)
break
case "vec3":
ref.type = len?"v3v":"v3"
if (len) while (ref.value.length<len) ref.value.push(new THREE.Vector3(0., 0., 0.))
else ref.value = new THREE.Vector3(0., 0., 0.)
break
case "vec4":
ref.type = len?"v4v":"v4"
if (len) while (ref.value.length<len) ref.value.push(new THREE.Vector4(0., 0., 0., 0.))
else ref.value = new THREE.Vector4(0., 0., 0., 0.)
break
case "mat4":
ref.type = len?"m4v":"m4"
if (len) while (ref.value.length<len) ref.value.push(new THREE.Matrix4())
else ref.value = new THREE.Matrix4()
break
case "sampler2D":
ref.type = len?"tv":"t"
if (len) while (ref.value.length<len) ref.value.push(BLANK_TEXTURE)
else ref.value = BLANK_TEXTURE
break
default:
console.warn("unsupported uniform", match[0])
}
}
} while (match)
return uniforms
}
const BLANK_TEXTURE = makeEmptyTexture()
const DEFAULT_VERTEX_SHADER = [
'varying vec2 vUv;',
'void main() {',
'vUv = uv;',
'vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
'gl_Position = projectionMatrix * mvPosition;',
'}',
].join("")
const DEFAULT_FRAGMENT_SHADER = [
'varying vec2 vUv;',
'uniform sampler2D source;',
'void main() {',
'gl_FragColor = texture2D(source, vUv);',
'}',
].join("")
const SHADER_PASS_DEFAULTS = {
width: 1,
height: 1,
fragmentShader: DEFAULT_FRAGMENT_SHADER,
vertexShader: DEFAULT_VERTEX_SHADER,
offscreen: false,
}
function ShaderPass(opts) {
Object.assign(this, SHADER_PASS_DEFAULTS, opts)
this.scene = new THREE.Scene()
this.camera = new THREE.OrthographicCamera(-1, 1, 1 , -1, 1, 100)
this.camera.position.z = 1
if (this.offscreen) {
this.buffer = new THREE.WebGLRenderTarget(
this.width, this.height,
{ minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter}
)
}
if (this.fragmentShader && this.vertexShader) {
this.uniforms = Object.assign(
{},
makeUniforms(this.vertexShader),
makeUniforms(this.fragmentShader)
)
this.material = new THREE.ShaderMaterial({
uniforms: this.uniforms,
vertexShader: this.vertexShader,
fragmentShader: this.fragmentShader
})
} else {
throw new Error("fragment and vertex shader mandatory")
}
this.geometry = new THREE.PlaneBufferGeometry(2, 2, 1, 1)
this.mesh = new THREE.Mesh(this.geometry, this.material)
this.scene.add(this.mesh)
}
ShaderPass.prototype.contructor = ShaderPass
ShaderPass.prototype.resize = function(width, height) {
this.width = width
this.height = height
this.camera.left = -1
this.camera.right = 1
this.camera.top = 1
this.camera.bottom = -1
this.camera.updateProjectionMatrix()
if (this.offscreen) {
this.buffer.setSize(this.width, this.height)
}
}
ShaderPass.prototype.render = function(renderer) {
if (this.offscreen) renderer.render(this.scene, this.camera, this.buffer)
else renderer.render(this.scene, this.camera)
}
function Composer(width, height) {
this.width = width
this.height = height
this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false })
this.renderer.setClearColor( 0x000000, 1. )
this.renderer.setSize(this.width, this.height)
this.renderer.setPixelRatio(window.devicePixelRatio)
this.clock = new THREE.Clock()
this.bufferPass = new ShaderPass({offscreen: true})
this.renderPass = new ShaderPass()
this.passes = []
console.log(this)
}
Composer.prototype.contructor = Composer
Composer.prototype.resize = function(width, height) {
this.width = width
this.height = height
this.renderer.setSize(this.width, this.height)
this.bufferPass.resize(this.width, this.height)
this.passes.forEach((pass) => {
pass.resize(this.width, this.height)
this.updatePassResolutionUniform(pass)
})
}
Composer.prototype.updatePassResolutionUniform = function(pass) {
if(pass.uniforms.hasOwnProperty("resolution") && pass.uniforms.resolution.type === "v2") {
pass.uniforms.resolution.value.x = this.width
pass.uniforms.resolution.value.y = this.height
}
}
Composer.prototype.chainPasses = function() {
let source = this.bufferPass.buffer.texture
this.renderPass.uniforms.source.value = source
this.passes.forEach((pass) => {
if (pass.uniforms.hasOwnProperty("source")) {
pass.uniforms.source.value = source
}
this.updatePassResolutionUniform(pass)
source = pass.buffer.texture
})
this.bufferPass.uniforms.source.value = source
}
Composer.prototype.setPasses = function(passes) {
passes.forEach((pass) => {
this.passes.push(
new ShaderPass(
Object.assign(
{},
pass,
{ width: this.width, height: this.height, offscreen: true }
)
)
)
})
this.chainPasses()
}
Composer.prototype.updateDynamicPassUniforms = function(pass) {
for (var key in pass.uniforms) {
if(key === "time" && pass.uniforms[key].type === "f") {
pass.uniforms[key].value = this.clock.elapsedTime
}
}
}
Composer.prototype.render = function() {
this.clock.getDelta()
this.passes.forEach((pass) => {
this.updateDynamicPassUniforms(pass)
pass.render(this.renderer)
})
this.bufferPass.render(this.renderer)
this.renderPass.render(this.renderer)
}
let composer = new Composer(window.innerWidth, window.innerHeight)
composer.setPasses([
{ fragmentShader: precompileShader('cyclechannel') },
{ fragmentShader: precompileShader('waves') },
{ fragmentShader: precompileShader('cyclehue') },
{ fragmentShader: precompileShader('blur') }
])
document.querySelector(".display").appendChild(composer.renderer.domElement)
function resize() {
composer.resize(window.innerWidth, window.innerHeight)
}
window.addEventListener("resize", resize)
function loop() {
requestAnimationFrame(() => {
composer.render()
loop()
})
}
loop()
resize()
</script>
</body>
</html>