Skip to content

Commit 1fbbedd

Browse files
committed
Allow uniform names to be manually specified
1 parent 839371b commit 1fbbedd

2 files changed

Lines changed: 72 additions & 9 deletions

File tree

src/strands/strands_transpiler.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,34 @@ const ASTCallbacks = {
111111
VariableDeclarator(node, _state, ancestors) {
112112
if (ancestors.some(nodeIsUniform)) { return; }
113113
if (nodeIsUniform(node.init)) {
114-
const uniformNameLiteral = {
115-
type: 'Literal',
116-
value: node.id.name
114+
// Only inject the variable name if the first argument isn't already a string
115+
if (node.init.arguments.length === 0 ||
116+
node.init.arguments[0].type !== 'Literal' ||
117+
typeof node.init.arguments[0].value !== 'string') {
118+
const uniformNameLiteral = {
119+
type: 'Literal',
120+
value: node.id.name
121+
}
122+
node.init.arguments.unshift(uniformNameLiteral);
117123
}
118-
node.init.arguments.unshift(uniformNameLiteral);
119124
}
120125
if (nodeIsVarying(node.init)) {
121-
const varyingNameLiteral = {
122-
type: 'Literal',
123-
value: node.id.name
126+
// Only inject the variable name if the first argument isn't already a string
127+
if (
128+
node.init.arguments.length === 0 ||
129+
node.init.arguments[0].type !== 'Literal' ||
130+
typeof node.init.arguments[0].value !== 'string'
131+
) {
132+
const varyingNameLiteral = {
133+
type: 'Literal',
134+
value: node.id.name
135+
}
136+
node.init.arguments.unshift(varyingNameLiteral);
137+
_state.varyings[node.id.name] = varyingNameLiteral;
138+
} else {
139+
// Still track it as a varying even if name wasn't injected
140+
_state.varyings[node.id.name] = node.init.arguments[0];
124141
}
125-
node.init.arguments.unshift(varyingNameLiteral);
126-
_state.varyings[node.id.name] = varyingNameLiteral;
127142
}
128143
},
129144
Identifier(node, _state, ancestors) {

test/unit/webgl/p5.Shader.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,54 @@ suite('p5.Shader', function() {
426426
myp5.plane(myp5.width, myp5.height);
427427
}).not.toThrowError();
428428
});
429+
430+
test('handle custom uniform names with automatic values', () => {
431+
myp5.createCanvas(50, 50, myp5.WEBGL);
432+
const testShader = myp5.baseMaterialShader().modify(() => {
433+
// Variable name is 'brightness' but uniform name is 'customBrightness'
434+
const brightness = myp5.uniformFloat('customBrightness', () => 0.8);
435+
myp5.getPixelInputs(inputs => {
436+
inputs.color = [brightness, brightness, brightness, 1.0];
437+
return inputs;
438+
});
439+
}, { myp5 });
440+
441+
myp5.noStroke();
442+
myp5.shader(testShader);
443+
myp5.plane(myp5.width, myp5.height);
444+
445+
// Check that the shader uses the automatic value (0.8)
446+
const pixelColor = myp5.get(25, 25);
447+
assert.approximately(pixelColor[0], 204, 5); // 0.8 * 255 = 204
448+
assert.approximately(pixelColor[1], 204, 5);
449+
assert.approximately(pixelColor[2], 204, 5);
450+
});
451+
452+
test('handle custom uniform names with manual setUniform', () => {
453+
myp5.createCanvas(50, 50, myp5.WEBGL);
454+
const testShader = myp5.baseMaterialShader().modify(() => {
455+
// Variable name is 'brightness' but uniform name is 'customBrightness'
456+
const brightness = myp5.uniformFloat('customBrightness');
457+
myp5.getPixelInputs(inputs => {
458+
inputs.color = [brightness, brightness, brightness, 1.0];
459+
return inputs;
460+
});
461+
}, { myp5 });
462+
463+
// Set the uniform using the custom name
464+
testShader.setUniform('customBrightness', 0.6);
465+
466+
myp5.noStroke();
467+
myp5.shader(testShader);
468+
myp5.plane(myp5.width, myp5.height);
469+
470+
// Check that the shader uses the manual value (0.6)
471+
const pixelColor = myp5.get(25, 25);
472+
assert.approximately(pixelColor[0], 153, 5); // 0.6 * 255 = 153
473+
assert.approximately(pixelColor[1], 153, 5);
474+
assert.approximately(pixelColor[2], 153, 5);
475+
});
476+
429477
suite('if statement conditionals', () => {
430478
test('handle simple if statement with true condition', () => {
431479
myp5.createCanvas(50, 50, myp5.WEBGL);

0 commit comments

Comments
 (0)