···
1
+
const VERSION_300 = '#version 300 es';
'attribute vec2 vPos;\n' +
' gl_Position = vec4(vPos, 0.0, 1.0);\n' +
7
-
'#version 300 es\n' +
' gl_Position = vPos;\n' +
···
DATE.getMilliseconds() * 0.001;
return [year, month, day, time] as const;
28
+
const preprocessShader = (source: string, isES300: boolean) => {
30
+
let output = source;
32
+
if (output.startsWith(VERSION_300)) {
33
+
output = output.slice(VERSION_300.length + 1);
34
+
header += `${VERSION_300}\n`;
37
+
if (!/^\s*precision /.test(output)) header += 'precision highp float;\n';
39
+
if (output.includes('iChannel0')) header += 'uniform sampler2D iChannel0;\n';
40
+
if (output.includes('iResolution')) header += 'uniform vec2 iResolution;\n';
41
+
if (output.includes('iChannelResolution'))
42
+
header += 'uniform vec3 iChannelResolution[1];\n';
43
+
if (output.includes('iTime')) header += 'uniform float iTime;\n';
44
+
if (output.includes('iTimeDelta')) header += 'uniform float iTimeDelta;\n';
45
+
if (output.includes('iFrame')) header += 'uniform float iFrame;\n';
46
+
if (output.includes('iChannel')) header += 'uniform float iChannel;\n';
47
+
if (output.includes('iDate')) header += 'uniform vec4 iDate;\n';
49
+
if (isES300 && output.includes('gl_FragColor'))
50
+
header += 'out vec4 gl_FragColor;\n';
51
+
if (isES300 && output.includes('gl_FragCoord'))
52
+
header += 'in vec2 gl_FragCoord;\n';
54
+
if (!/main\s*\(/.test(output)) {
55
+
const ioRe = /\(\s*out\s+vec4\s+(\S+)\s*,\s*in\s+vec2\s+(\S+)\s*\)/g;
56
+
const io = ioRe.exec(source);
57
+
output = output.replace(/mainImage\s*\(/, 'main(').replace(ioRe, '()');
58
+
if (io && io[1] !== 'gl_FragColor')
59
+
header += `#define ${io[1]} gl_FragColor\n`;
60
+
if (io && io[2] !== 'gl_FragCoord')
61
+
header += `#define ${io[2]} gl_FragCoord.xy\n`;
64
+
if (!isES300) output = output.replace(/texture\s+\(/g, 'texture2D(');
66
+
return `${header}\n${output}`;
···
updateFragShader(fragSource: string) {
fragSource = fragSource.trim();
119
-
gl.shaderSource(fragShader, fragSource);
162
+
const isES300 = /\s+#version 300/i.test(fragSource);
163
+
gl.shaderSource(fragShader, preprocessShader(fragSource, isES300));
gl.compileShader(fragShader);
122
-
const vertShader = /\s+#version 300/i.test(fragSource)
165
+
const vertShader = isES300 ? vertShader300 : vertShader100;
gl.attachShader(program, vertShader);
gl.attachShader(program, fragShader);