Skip to content

Commit 7bae495

Browse files
committed
added automatic uniform names
1 parent 6c582b9 commit 7bae495

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/webgl/ShaderGen.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@ function shadergen(p5, fn) {
4848
}
4949

5050
const ASTCallbacks = {
51-
// TODO: automatically making uniforms
52-
53-
51+
VariableDeclarator(node) {
52+
if (node.init.callee && node.init.callee.name.slice(0, 7) === 'uniform') {
53+
const uniformNameLiteral = {
54+
type: 'Literal',
55+
value: node.id.name
56+
}
57+
node.init.arguments.unshift(uniformNameLiteral);
58+
}
59+
},
5460
// The callbacks for AssignmentExpression and BinaryExpression handle
5561
// operator overloading including +=, *= assignment expressions
5662
AssignmentExpression(node) {
@@ -632,7 +638,8 @@ function shadergen(p5, fn) {
632638
const uniformFnVariant = `uniform${uniformFns[type]}`;
633639
ShaderGenerator.prototype[uniformFnVariant] = function(name, defaultValue) {
634640
this.output.uniforms[`${type} ${name}`] = defaultValue;
635-
return new VariableNode(name, type);
641+
let safeType = type === 'sampler2D' ? 'vec4' : type;
642+
return new VariableNode(name, safeType);
636643
};
637644
fn[uniformFnVariant] = function (name, value) {
638645
return GLOBAL_SHADER[uniformFnVariant](name, value);

0 commit comments

Comments
 (0)