@@ -38,7 +38,6 @@ function shadergen(p5, fn) {
38
38
const generatedModifyArgument = generator . generate ( ) ;
39
39
console . log ( "SRC STRING: " , generatorFunction ) ;
40
40
console . log ( "NEW OPTIONS:" , generatedModifyArgument )
41
-
42
41
return oldModify . call ( this , generatedModifyArgument ) ;
43
42
}
44
43
else {
@@ -58,7 +57,7 @@ function shadergen(p5, fn) {
58
57
}
59
58
60
59
const ASTCallbacks = {
61
- VariableDeclarator ( node ) {
60
+ VariableDeclarator ( node , _state , _ancestors ) {
62
61
if ( node . init . callee && node . init . callee . name . startsWith ( 'uniform' ) ) {
63
62
const uniformNameLiteral = {
64
63
type : 'Literal' ,
@@ -69,7 +68,7 @@ function shadergen(p5, fn) {
69
68
} ,
70
69
// The callbacks for AssignmentExpression and BinaryExpression handle
71
70
// operator overloading including +=, *= assignment expressions
72
- AssignmentExpression ( node ) {
71
+ AssignmentExpression ( node , _state , _ancestors ) {
73
72
if ( node . operator !== '=' ) {
74
73
const methodName = replaceBinaryOperator ( node . operator . replace ( '=' , '' ) ) ;
75
74
const rightReplacementNode = {
@@ -89,7 +88,35 @@ function shadergen(p5, fn) {
89
88
node . right = rightReplacementNode ;
90
89
}
91
90
} ,
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.
93
120
node . type = 'CallExpression' ;
94
121
node . callee = {
95
122
type : "MemberExpression" ,
@@ -103,6 +130,14 @@ function shadergen(p5, fn) {
103
130
} ,
104
131
}
105
132
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
+
106
141
// Javascript Node API.
107
142
// These classes are for expressing GLSL functions in Javascript without
108
143
// needing to transpile the user's code.
@@ -192,7 +227,6 @@ function shadergen(p5, fn) {
192
227
for ( let componentName of this . componentNames ) {
193
228
valueArgs . push ( this [ componentName ] )
194
229
}
195
- console . log ( this , valueArgs )
196
230
const replacement = nodeConstructors [ this . type ] ( valueArgs )
197
231
line += this . type + " " + this . temporaryVariable + " = " + this . toGLSL ( context ) + ";" ;
198
232
line += `\n` + this . temporaryVariable + " = " + replacement . toGLSL ( context ) + ";" ;
@@ -515,7 +549,7 @@ function shadergen(p5, fn) {
515
549
516
550
Object . keys ( availableHooks ) . forEach ( ( hookName ) => {
517
551
const hookTypes = originalShader . hookTypes ( hookName )
518
- console . log ( hookTypes ) ;
552
+ // console.log(hookTypes);
519
553
520
554
this [ hookTypes . name ] = function ( userOverride ) {
521
555
let argNodes = [ ]
0 commit comments