-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisf-fallback.js
More file actions
267 lines (224 loc) · 7.17 KB
/
isf-fallback.js
File metadata and controls
267 lines (224 loc) · 7.17 KB
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
// Fallback ISF implementation for when the main library fails to load
// This provides basic shader rendering capabilities
class FallbackISFRenderer {
constructor(gl) {
this.gl = gl;
this.uniforms = {};
this.program = null;
this.startTime = Date.now();
this.valid = false;
console.log('Using fallback ISF renderer');
}
loadSource(source) {
try {
// Parse ISF metadata
const jsonMatch = source.match(/\/\*(\{[\s\S]*?\})\*\//);
if (jsonMatch) {
this.metadata = JSON.parse(jsonMatch[1]);
this.parseUniforms();
}
// Create a basic shader program
this.createShaderProgram(source);
} catch (error) {
console.error('Error loading ISF source:', error);
this.error = error.message;
}
}
parseUniforms() {
this.uniforms = {
TIME: { type: 'float', value: 0 },
RENDERSIZE: { type: 'vec2', value: [512, 512] }
};
if (this.metadata && this.metadata.INPUTS) {
this.metadata.INPUTS.forEach(input => {
this.uniforms[input.NAME] = {
type: input.TYPE,
value: input.DEFAULT || 0
};
});
}
}
createShaderProgram(fragmentSource) {
const gl = this.gl;
// Basic vertex shader
const vertexSource = `
attribute vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}
`;
// Preprocess fragment shader
const processedFragment = this.preprocessFragment(fragmentSource);
// Create shaders
const vertexShader = this.createShader(gl.VERTEX_SHADER, vertexSource);
const fragmentShader = this.createShader(gl.FRAGMENT_SHADER, processedFragment);
if (!vertexShader || !fragmentShader) {
return;
}
// Create program
this.program = gl.createProgram();
gl.attachShader(this.program, vertexShader);
gl.attachShader(this.program, fragmentShader);
gl.linkProgram(this.program);
if (!gl.getProgramParameter(this.program, gl.LINK_STATUS)) {
this.error = 'Program linking failed: ' + gl.getProgramInfoLog(this.program);
console.error(this.error);
return;
}
// Create vertex buffer for full screen quad
this.createVertexBuffer();
this.valid = true;
}
preprocessFragment(source) {
// Remove JSON metadata
let processed = source.replace(/\/\*\{[\s\S]*?\}\*\//, '');
// Add necessary uniforms
const uniforms = [
'uniform float TIME;',
'uniform vec2 RENDERSIZE;'
];
if (this.metadata && this.metadata.INPUTS) {
this.metadata.INPUTS.forEach(input => {
switch (input.TYPE) {
case 'float':
uniforms.push(`uniform float ${input.NAME};`);
break;
case 'bool':
uniforms.push(`uniform bool ${input.NAME};`);
break;
case 'color':
uniforms.push(`uniform vec4 ${input.NAME};`);
break;
}
});
}
// Add precision and uniform declarations
processed = `
precision mediump float;
${uniforms.join('\n')}
${processed}
`;
// Fix ISF compatibility - move isf_FragNormCoord calculation into main function
if (processed.includes('isf_FragNormCoord')) {
// Add the compatibility variable declaration at the start of main
processed = processed.replace(
'void main() {',
'void main() {\n vec2 isf_FragNormCoord = gl_FragCoord.xy / RENDERSIZE.xy;'
);
}
return processed;
}
createShader(type, source) {
const gl = this.gl;
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
const error = gl.getShaderInfoLog(shader);
console.error('Shader compilation failed:', error);
console.log('Shader source:', source);
gl.deleteShader(shader);
return null;
}
return shader;
}
createVertexBuffer() {
const gl = this.gl;
// Create vertex buffer for fullscreen quad
this.vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
const vertices = new Float32Array([
-1, -1,
1, -1,
-1, 1,
1, 1
]);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
}
setValue(name, value) {
if (this.uniforms[name]) {
this.uniforms[name].value = value;
}
}
draw(canvas) {
if (!this.valid || !this.program) {
return;
}
const gl = this.gl;
// Use our program
gl.useProgram(this.program);
// Set up vertex attributes
const positionLocation = gl.getAttribLocation(this.program, 'position');
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
// Set uniforms
Object.keys(this.uniforms).forEach(name => {
const location = gl.getUniformLocation(this.program, name);
if (location !== null) {
const uniform = this.uniforms[name];
switch (uniform.type) {
case 'float':
gl.uniform1f(location, uniform.value);
break;
case 'vec2':
gl.uniform2fv(location, uniform.value);
break;
case 'bool':
gl.uniform1i(location, uniform.value ? 1 : 0);
break;
case 'color':
gl.uniform4fv(location, uniform.value);
break;
}
}
});
// Draw
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}
}
// Enhanced ISF Manager that can use fallback
class EnhancedISFManager extends ISFManager {
createISFRenderer(layer, source, canvas) {
console.log('Creating ISF renderer for layer:', layer.id);
const gl = canvas.getContext('webgl2') || canvas.getContext('webgl');
if (!gl) {
throw new Error('WebGL not supported');
}
let ISFRendererClass = null;
// Try to find the real ISF renderer first
if (typeof window.ISFRenderer !== 'undefined') {
ISFRendererClass = window.ISFRenderer;
} else if (typeof ISFRenderer !== 'undefined') {
ISFRendererClass = ISFRenderer;
} else if (typeof window.interactiveShaderFormat !== 'undefined' && window.interactiveShaderFormat.Renderer) {
ISFRendererClass = window.interactiveShaderFormat.Renderer;
} else {
// Use fallback
console.log('Using fallback ISF renderer');
ISFRendererClass = FallbackISFRenderer;
}
try {
const renderer = new ISFRendererClass(gl);
renderer.loadSource(source);
// Store renderer reference
this.renderers.set(layer.id, {
renderer,
canvas,
gl,
layer,
startTime: Date.now()
});
console.log('ISF renderer created successfully for layer', layer.id);
return renderer;
} catch (error) {
console.error('Error creating ISF renderer:', error);
throw error;
}
}
}
// Export both versions
window.EnhancedISFManager = EnhancedISFManager;
if (typeof window.ISFManager === 'undefined') {
window.ISFManager = EnhancedISFManager;
}