Skip to content

Commit c206ef2

Browse files
committed
Fix clashing constuctor and function
1 parent 1e41b56 commit c206ef2

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

src/strands/ir_types.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const DataType = {
6363
mat3: { fnName: "mat3x3", baseType: BaseType.MAT, dimension:3, priority: 0, },
6464
mat4: { fnName: "mat4x4", baseType: BaseType.MAT, dimension:4, priority: 0, },
6565
defer: { fnName: null, baseType: BaseType.DEFER, dimension: null, priority: -1 },
66-
sampler2D: { fnName: "texture", baseType: BaseType.SAMPLER2D, dimension: 1, priority: -10 },
66+
sampler2D: { fnName: "sampler2D", baseType: BaseType.SAMPLER2D, dimension: 1, priority: -10 },
6767
}
6868
export const structType = function (hookType) {
6969
let T = hookType.type === undefined ? hookType : hookType.type;
@@ -85,7 +85,32 @@ export function isStructType(typeName) {
8585
return !isNativeType(typeName);
8686
}
8787
export function isNativeType(typeName) {
88-
return Object.keys(DataType).includes(typeName);
88+
// Check if it's in DataType keys (internal names like 'float4')
89+
if (Object.keys(DataType).includes(typeName)) {
90+
return true;
91+
}
92+
93+
// Check if it's a GLSL type name (like 'vec4', 'float', etc.)
94+
const glslNativeTypes = {
95+
'float': true,
96+
'vec2': true,
97+
'vec3': true,
98+
'vec4': true,
99+
'int': true,
100+
'ivec2': true,
101+
'ivec3': true,
102+
'ivec4': true,
103+
'bool': true,
104+
'bvec2': true,
105+
'bvec3': true,
106+
'bvec4': true,
107+
'mat2': true,
108+
'mat3': true,
109+
'mat4': true,
110+
'sampler2D': true
111+
};
112+
113+
return !!glslNativeTypes[typeName];
89114
}
90115
export const GenType = {
91116
FLOAT: { baseType: BaseType.FLOAT, dimension: null, priority: 3 },
@@ -98,7 +123,7 @@ export function typeEquals(nodeA, nodeB) {
98123
export const TypeInfoFromGLSLName = Object.fromEntries(
99124
Object.values(DataType)
100125
.filter(info => info.fnName !== null)
101-
.map(info => [info.fnName === 'texture' ? 'sampler2D' : info.fnName, info])
126+
.map(info => [info.fnName, info])
102127
);
103128
export const OpCode = {
104129
Binary: {

test/unit/webgl/p5.Shader.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,5 +1066,79 @@ suite('p5.Shader', function() {
10661066
assert.approximately(pixelColor[0], 127, 5); // 0.5 * 255 ≈ 127
10671067
});
10681068
});
1069+
1070+
suite('filter shader hooks', () => {
1071+
test('handle getColor hook with non-struct return type', () => {
1072+
myp5.createCanvas(50, 50, myp5.WEBGL);
1073+
1074+
const testShader = myp5.baseFilterShader().modify(() => {
1075+
myp5.getColor((inputs, canvasContent) => {
1076+
// Simple test - just return a constant color
1077+
return [1.0, 0.5, 0.0, 1.0]; // Orange color
1078+
});
1079+
}, { myp5 });
1080+
1081+
// Create a simple scene to filter
1082+
myp5.background(0, 0, 255); // Blue background
1083+
1084+
// Apply the filter
1085+
myp5.filter(testShader);
1086+
1087+
// Check that the filter was applied (should be orange)
1088+
const pixelColor = myp5.get(25, 25);
1089+
assert.approximately(pixelColor[0], 255, 5); // Red channel should be 255
1090+
assert.approximately(pixelColor[1], 127, 5); // Green channel should be ~127
1091+
assert.approximately(pixelColor[2], 0, 5); // Blue channel should be 0
1092+
});
1093+
1094+
test('simple vector multiplication in filter shader', () => {
1095+
myp5.createCanvas(50, 50, myp5.WEBGL);
1096+
1097+
const testShader = myp5.baseFilterShader().modify(() => {
1098+
myp5.getColor((inputs, canvasContent) => {
1099+
// Test simple scalar * vector operation
1100+
const scalar = 0.5;
1101+
const vector = [1, 2];
1102+
const result = scalar * vector;
1103+
return [result.x, result.y, 0, 1];
1104+
});
1105+
}, { myp5 });
1106+
});
1107+
1108+
test('handle complex filter shader with for loop and vector operations', () => {
1109+
myp5.createCanvas(50, 50, myp5.WEBGL);
1110+
1111+
const testShader = myp5.baseFilterShader().modify(() => {
1112+
const r = myp5.uniformFloat(() => 3); // Small value for testing
1113+
myp5.getColor((inputs, canvasContent) => {
1114+
let sum = [0, 0, 0, 0];
1115+
let samples = 1;
1116+
1117+
for (let i = 0; i < r; i++) {
1118+
samples++;
1119+
sum += myp5.texture(canvasContent, inputs.texCoord + (i / r) * [
1120+
myp5.sin(4 * myp5.PI * i / r),
1121+
myp5.cos(4 * myp5.PI * i / r)
1122+
]);
1123+
}
1124+
1125+
return sum / samples;
1126+
});
1127+
}, { myp5 });
1128+
1129+
// Create a simple scene to filter
1130+
myp5.background(255, 0, 0); // Red background
1131+
1132+
// Apply the filter
1133+
myp5.filter(testShader);
1134+
1135+
// The result should be some variation of the red background
1136+
const pixelColor = myp5.get(25, 25);
1137+
// Just verify it ran without crashing - exact color will depend on sampling
1138+
assert.isNumber(pixelColor[0]);
1139+
assert.isNumber(pixelColor[1]);
1140+
assert.isNumber(pixelColor[2]);
1141+
});
1142+
});
10691143
});
10701144
});

0 commit comments

Comments
 (0)