@@ -58,7 +58,7 @@ function shadergenerator(p5, fn) {
58
58
59
59
const ASTCallbacks = {
60
60
VariableDeclarator ( node , _state , _ancestors ) {
61
- if ( node . init . callee && node . init . callee . name . startsWith ( 'uniform' ) ) {
61
+ if ( node . init . callee && node . init . callee . name ? .startsWith ( 'uniform' ) ) {
62
62
const uniformNameLiteral = {
63
63
type : 'Literal' ,
64
64
value : node . id . name
@@ -344,11 +344,28 @@ function shadergenerator(p5, fn) {
344
344
// Function Call Nodes
345
345
class FunctionCallNode extends BaseNode {
346
346
constructor ( name , args , properties , isInternal = false ) {
347
- let returnType = properties . returnType ;
348
- if ( returnType === 'genType' ) {
349
- returnType = args [ 0 ] . type ;
347
+ let inferredType = args . find ( ( arg , i ) => {
348
+ properties . args [ i ] === 'genType'
349
+ && isShaderNode ( arg )
350
+ } ) ?. type ;
351
+ if ( ! inferredType ) {
352
+ let arrayArg = args . find ( arg => Array . isArray ( arg ) ) ;
353
+ inferredType = arrayArg ? `vec${ arrayArg . length } ` : undefined ;
354
+ }
355
+ if ( ! inferredType ) {
356
+ inferredType = 'float' ;
357
+ }
358
+ args = args . map ( ( arg , i ) => {
359
+ if ( ! isShaderNode ( arg ) ) {
360
+ const typeName = properties . args [ i ] === 'genType' ? inferredType : properties . args [ i ] ;
361
+ arg = nodeConstructors [ typeName ] ( arg ) ;
362
+ }
363
+ return arg ;
364
+ } )
365
+ if ( properties . returnType === 'genType' ) {
366
+ properties . returnType = inferredType ;
350
367
}
351
- super ( isInternal , returnType ) ;
368
+ super ( isInternal , properties . returnType ) ;
352
369
this . name = name ;
353
370
this . args = args ;
354
371
this . argumentTypes = properties . args ;
@@ -741,7 +758,7 @@ function shadergenerator(p5, fn) {
741
758
// genType clamp(genType x,
742
759
// float minVal,
743
760
// float maxVal);
744
- const builtInFunctions = {
761
+ const builtInGLSLFunctions = {
745
762
//////////// Trigonometry //////////
746
763
'acos' : { args : [ 'genType' ] , returnType : 'genType' , isp5Function : true } ,
747
764
'acosh' : { args : [ 'genType' ] , returnType : 'genType' , isp5Function : false } ,
@@ -804,7 +821,7 @@ function shadergenerator(p5, fn) {
804
821
'texture' : { args : [ 'sampler2D' , 'vec2' ] , returnType : 'vec4' , isp5Function : true } ,
805
822
}
806
823
807
- Object . entries ( builtInFunctions ) . forEach ( ( [ functionName , properties ] ) => {
824
+ Object . entries ( builtInGLSLFunctions ) . forEach ( ( [ functionName , properties ] ) => {
808
825
if ( properties . isp5Function ) {
809
826
const originalFn = fn [ functionName ] ;
810
827
0 commit comments