-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (72 loc) · 1.63 KB
/
index.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
/*
tags: basic
<p> This example shows how to use copyTexImage2D to implement feedback effects </p>
*/
const regl = require('regl')()
const mouse = require('mouse-change')()
const pixels = regl.texture()
let scale = 1
const drawFeedback = regl({
frag: `
precision highp float;
uniform sampler2D texture;
uniform vec2 mouse;
uniform float scale;
varying vec2 uv;
vec3 color(float intensity){
return vec3(pow(intensity, 0.8));
}
float julia(vec2 z, vec2 c) {
const int ITER = 900;
const float B = 2.0;
float n = 0.0;
for(int i = 0; i < ITER; i++) {
if(dot(z,z) >= B*B) break;
float x = z.x*z.x - z.y*z.y;
z.y = 2.0*z.x*z.y + c.y;
z.x = x + c.x;
n += 1.0;
}
float sn = n - log(log(length(z))/log(B))/log(2.0);
return sn / float(ITER);
}
void main () {
vec2 c = mouse * vec2(3.0,2.0) - vec2(1.5, 1.0);
vec2 z = uv * vec2(3.0,2.0) - vec2(1.5, 1.0);
float d = julia(z/scale, c);
// float d = mandel(uv, mouse);
gl_FragColor = vec4(color(d), 1.0);
}`,
vert: `
precision mediump float;
attribute vec2 position;
varying vec2 uv;
void main () {
uv = position;
gl_Position = vec4(2.0 * position - 1.0, 0, 1);
}`,
attributes: {
position: [
-2, 0,
0, -2,
2, 2]
},
uniforms: {
texture: pixels,
mouse: ({pixelRatio, viewportHeight, viewportWidth }) => [
pixelRatio * mouse.x / viewportWidth,
1 - pixelRatio * mouse.y / viewportHeight
],
scale
},
count: 3
})
regl.frame(function () {
regl.clear({
color: [0, 0, 0, 1]
})
drawFeedback()
pixels({
copy: true
})
})