-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregl_test.js
202 lines (193 loc) · 4.54 KB
/
regl_test.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
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
var regl = createREGL({
optionalExtensions: [
"ext_texture_filter_anisotropic",
]
})
const height = window.innerHeight;
const width = window.innerWidth;
const config = createFlameConfigX6();
const furnance = new Furnance(regl._gl)
furnance.setConfig(config)
const seedShader = furnance.getSeedShaderCode()
const texScale = config.getTexScale();
const screenInitScale = config.getScreenInitScale()
const screenInitVal = config.getScreenInitVal()
const texCoord = screenInitScale * (furnance.TXfmPL(1, texScale) - furnance.TXfmPL(0, texScale))
const vertices = regl.buffer([
0, 0, 0, 0,
texCoord, 0, texCoord, 0,
0, texCoord, texCoord, 0,
])
const matrix = [ // TODO does it matter if this is hardcoded
2, 0, 0, 0,
0, 2, 0, 0,
0, 0, 0.009999999776482582, 0,
0, 0, 0, 1,
]
const color = [screenInitVal, screenInitVal, screenInitVal, 1]
const drawSeedTexture = regl({
vert: seedShader["vert"],
frag: seedShader["frag"],
attributes: {
a_Vertex: {
buffer: vertices,
size: 3,
}
},
uniforms: {
u_color: color,
mvp_matrix: matrix,
},
primitive: "triangle strip",
count: 4,
depth: {
enable: false
},
blend: {
enable: true,
color: [0, 0, 0, 0],
func: {
dstAlpha: 1,
dstRGB: 1,
srcAlpha: 1,
srcRGB: 1,
},
equation: {
alpha: "add",
rgb: "add",
}
}
})
const perPixelMapShader = furnance.getPerPixelMapShaderCode(config.getFlameTransforms())
function mapColor(config, o) {
var i = 1,
n = 1,
l = 1,
s = config.getColorful();
const xfms = config.getFlameTransforms()
switch (o % 3) {
case 0:
i -= s;
break;
case 1:
n -= s;
break;
case 2:
l -= s;
}
var c = config.getMapExposure();
if (0 > c) {
var f;
(f = i), (i = n), (n = f), (f = l), (l = n), (n = f);
}
var u = Math.abs(c) * xfms[o].getWeight();
return new Float32Array([u * i, u * n, u * l, 1]);
}
function buildPerPixelUniforms(config) {
const uniforms = {};
const xfms = config.getFlameTransforms()
for (let i = 0; i < xfms.length; i++) {
const xfm = xfms[i]
const xfUniform = []
xfm.getXfUniforms(xfUniform)
xfUniform.forEach((v, k) => {
uniforms[`xf${xfm.getTag()}[${k}]`] = v
})
uniforms[`color${xfm.getTag()}`] = mapColor(config, i)
}
return uniforms;
}
const drawPerPixelMaps = regl({
vert: perPixelMapShader["vert"],
frag: perPixelMapShader["frag"],
uniforms: {
tex: regl.prop("texture"),
texscale: config.getTexScale(),
texscalei: 1/config.getTexScale(),
leg_gl_ModelViewMatrix: [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
],
leg_gl_ProjectionMatrix: [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
],
...buildPerPixelUniforms(config),
},
attributes: {
leg_gl_Vertex: {
buffer: [
-1, -1, 0.5, -1,
1, 0.5, 1, -1,
0.5, 1, 1, 0.5
],
size: 3,
}
},
primitive: "triangle strip",
count: 4,
depth: {
enable: false
},
blend: {
enable: false,
color: [0, 0, 0, 0],
func: {
dstAlpha: 1,
dstRGB: 1,
srcAlpha: 1,
srcRGB: 1,
},
equation: {
alpha: "add",
rgb: "add",
}
}
})
function getTextureLevels(config) {
const endLevel = config.getNLevel();
const textureLevels = Array(endLevel+1).fill(0).map(_ => []);
for (let i = 0; i <= endLevel; i++) {
const size = 1 << i
for (let j = 0; j < 2; j++) {
textureLevels[i][j] = regl.framebuffer({
color: regl.texture({
width: size,
height: size,
mag: "linear",
min: "mipmap",
wrap: ["repeat", "repeat"],
anisotropic: 16, // TODO seems like this isn't getting set
depthStencil: false
})
})
}
}
return textureLevels;
}
const textureLevels = getTextureLevels(config)
regl.frame(function ({tick}) {
for (let l = config.getFirstLevel(); l <= config.getLastLevel(); l++) {
for (let i = 0; i < config.getIterations(); i++) {
textureLevels[l][i%2].use(() => {
if (i === 0 ) {
if (l === config.getFirstLevel()) {
regl.clear({
color: [0, 0, 0, 1]
})
drawSeedTexture()
} else {
drawPerPixelMaps({ texture: textureLevels[l-1][1] })
}
} else {
drawPerPixelMaps({ texture: textureLevels[l][(i+1)%2] })
}
})
}
}
drawPerPixelMaps({ texture: textureLevels[config.getLastLevel()][0] })
})