Skip to content

Commit 926f79f

Browse files
committed
allow literals on lhs before transpile, dont transpile uniform default vals
1 parent b751657 commit 926f79f

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

src/webgl/ShaderGen.js

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ function shadergen(p5, fn) {
3838
const generatedModifyArgument = generator.generate();
3939
console.log("SRC STRING: ", generatorFunction);
4040
console.log("NEW OPTIONS:", generatedModifyArgument)
41-
4241
return oldModify.call(this, generatedModifyArgument);
4342
}
4443
else {
@@ -58,7 +57,7 @@ function shadergen(p5, fn) {
5857
}
5958

6059
const ASTCallbacks = {
61-
VariableDeclarator(node) {
60+
VariableDeclarator(node, _state, _ancestors) {
6261
if (node.init.callee && node.init.callee.name.startsWith('uniform')) {
6362
const uniformNameLiteral = {
6463
type: 'Literal',
@@ -69,7 +68,7 @@ function shadergen(p5, fn) {
6968
},
7069
// The callbacks for AssignmentExpression and BinaryExpression handle
7170
// operator overloading including +=, *= assignment expressions
72-
AssignmentExpression(node) {
71+
AssignmentExpression(node, _state, _ancestors) {
7372
if (node.operator !== '=') {
7473
const methodName = replaceBinaryOperator(node.operator.replace('=',''));
7574
const rightReplacementNode = {
@@ -89,7 +88,35 @@ function shadergen(p5, fn) {
8988
node.right = rightReplacementNode;
9089
}
9190
},
92-
BinaryExpression(node) {
91+
BinaryExpression(node, _state, ancestors) {
92+
// Don't convert uniform default values to node methods, as
93+
// they should be evaluated at runtime, not compiled.
94+
const isUniform = (ancestor) => {
95+
return ancestor.type === 'CallExpression'
96+
&& ancestor.callee?.type === 'Identifier'
97+
&& ancestor.callee?.name.startsWith('uniform');
98+
}
99+
if (ancestors.some(isUniform)) {
100+
return;
101+
}
102+
// If the left hand side of an expression is one of these types,
103+
// we should construct a node from it.
104+
const unsafeTypes = ["Literal", "ArrayExpression"]
105+
if (unsafeTypes.includes(node.left.type)) {
106+
const leftReplacementNode = {
107+
type: "CallExpression",
108+
callee: {
109+
type: "Identifier",
110+
name: "makeNode",
111+
},
112+
arguments: [node.left, node.right]
113+
}
114+
node.left = leftReplacementNode;
115+
console.log(escodegen.generate(leftReplacementNode))
116+
}
117+
118+
// Replace the binary operator with a call expression
119+
// in other words a call to BaseNode.mult(), .div() etc.
93120
node.type = 'CallExpression';
94121
node.callee = {
95122
type: "MemberExpression",
@@ -103,6 +130,14 @@ function shadergen(p5, fn) {
103130
},
104131
}
105132

133+
// This unfinished function lets you do 1 * 10
134+
// and turns it into float.mult(10)
135+
fn.makeNode = function(leftValue, rightValue) {
136+
if (typeof leftValue === 'number') {
137+
return new FloatNode(leftValue);
138+
}
139+
}
140+
106141
// Javascript Node API.
107142
// These classes are for expressing GLSL functions in Javascript without
108143
// needing to transpile the user's code.
@@ -192,7 +227,6 @@ function shadergen(p5, fn) {
192227
for (let componentName of this.componentNames) {
193228
valueArgs.push(this[componentName])
194229
}
195-
console.log(this, valueArgs)
196230
const replacement = nodeConstructors[this.type](valueArgs)
197231
line += this.type + " " + this.temporaryVariable + " = " + this.toGLSL(context) + ";";
198232
line += `\n` + this.temporaryVariable + " = " + replacement.toGLSL(context) + ";";
@@ -515,7 +549,7 @@ function shadergen(p5, fn) {
515549

516550
Object.keys(availableHooks).forEach((hookName) => {
517551
const hookTypes = originalShader.hookTypes(hookName)
518-
console.log(hookTypes);
552+
// console.log(hookTypes);
519553

520554
this[hookTypes.name] = function(userOverride) {
521555
let argNodes = []

0 commit comments

Comments
 (0)