···
return [year, month, day, time] as const;
28
-
const preprocessShader = (source: string, isES300: boolean) => {
28
+
const preprocessShader = (source: string) => {
30
-
let output = source;
30
+
let output = source.trim();
31
+
let isES300 = false;
if (output.startsWith(VERSION_300)) {
output = output.slice(VERSION_300.length + 1);
header += `${VERSION_300}\n`;
if (!/^\s*precision /.test(output)) header += 'precision highp float;\n';
40
+
if (!/main\s*\(/.test(output)) {
41
+
const ioRe = /\(\s*out\s+vec4\s+(\S+)\s*,\s*in\s+vec2\s+(\S+)\s*\)/g;
42
+
const io = ioRe.exec(source);
43
+
output = output.replace(/mainImage\s*\(/, 'main(').replace(ioRe, '()');
44
+
if (isES300 && io) {
45
+
header += `out vec4 ${io[1]};\n`;
46
+
if (io[2] !== 'gl_FragCoord')
47
+
header += `#define ${io[2]} gl_FragCoord.xy\n`;
49
+
if (io[1] !== 'gl_FragColor') header += `#define ${io[1]} gl_FragColor\n`;
50
+
if (io[2] !== 'gl_FragCoord')
51
+
header += `#define ${io[2]} gl_FragCoord.xy\n`;
55
+
if (isES300 && output.includes('gl_FragColor')) {
56
+
header += 'out vec4 aFragColor;\n';
57
+
header += '#define glFragColor aFragColor.xy\n';
if (output.includes('iChannel0')) header += 'uniform sampler2D iChannel0;\n';
if (output.includes('iResolution')) header += 'uniform vec2 iResolution;\n';
if (output.includes('iChannelResolution'))
···
if (output.includes('iChannel')) header += 'uniform float iChannel;\n';
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(');
70
+
if (isES300) output = output.replace(/texture2D\s*\(/g, 'texture(');
66
-
return `${header}\n${output}`;
73
+
source: `${header}\n${output}`,
···
updateFragShader(fragSource: string) {
161
-
fragSource = fragSource.trim();
162
-
const isES300 = /\s+#version 300/i.test(fragSource);
163
-
gl.shaderSource(fragShader, preprocessShader(fragSource, isES300));
170
+
const preprocessed = preprocessShader(fragSource);
171
+
gl.shaderSource(fragShader, preprocessed.source);
gl.compileShader(fragShader);
165
-
const vertShader = isES300 ? vertShader300 : vertShader100;
173
+
const vertShader = preprocessed.isES300 ? vertShader300 : vertShader100;
gl.attachShader(program, vertShader);
gl.attachShader(program, fragShader);