@@ -76,7 +76,7 @@ function shadergenerator(p5, fn) {
76
76
type : 'Literal' ,
77
77
value : node . operator ,
78
78
}
79
-
79
+
80
80
const standardReplacement = ( node ) => {
81
81
node . type = 'CallExpression'
82
82
node . callee = {
@@ -93,11 +93,11 @@ function shadergenerator(p5, fn) {
93
93
[ 'r' , 'g' , 'b' , 'a' ] ,
94
94
[ 's' , 't' , 'p' , 'q' ]
95
95
] ;
96
-
96
+
97
97
let isSwizzle = swizzleSets . some ( set =>
98
98
[ ...property ] . every ( char => set . includes ( char ) )
99
99
) && node . argument . type === 'MemberExpression' ;
100
-
100
+
101
101
if ( isSwizzle ) {
102
102
node . type = 'MemberExpression' ;
103
103
node . object = {
@@ -114,7 +114,7 @@ function shadergenerator(p5, fn) {
114
114
} ;
115
115
} else {
116
116
standardReplacement ( node ) ;
117
- }
117
+ }
118
118
} else {
119
119
standardReplacement ( node ) ;
120
120
}
@@ -139,7 +139,7 @@ function shadergenerator(p5, fn) {
139
139
}
140
140
} ,
141
141
Identifier ( node , _state , _ancestors ) {
142
- if ( _state . varyings [ node . name ]
142
+ if ( _state . varyings [ node . name ]
143
143
&& ! _ancestors . some ( a => a . type === 'AssignmentExpression' && a . left === node ) ) {
144
144
node . type = 'ExpressionStatement' ;
145
145
node . expression = {
@@ -297,7 +297,7 @@ function shadergenerator(p5, fn) {
297
297
this . useTemp = true ;
298
298
}
299
299
300
- assertUsedInConditional ( branch ) {
300
+ assertUsedInConditional ( branch ) {
301
301
this . usedInConditional = true ;
302
302
this . usedIn . push ( branch ) ;
303
303
this . forceTemporaryVariable ( ) ;
@@ -316,8 +316,8 @@ function shadergenerator(p5, fn) {
316
316
statement . dependsOnSatisfied . push ( this ) ;
317
317
}
318
318
if ( statement . usedIn . includes ( this ) && ! statement . usedInSatisfied . includes ( this ) ) {
319
- statement . usedInSatisfied . push ( this ) ;
320
- }
319
+ statement . usedInSatisfied . push ( this ) ;
320
+ }
321
321
if ( isDepsSatisfied ( ) && isUsedSatisfied ( ) ) {
322
322
statement . saveState ( context , isDepsSatisfied ( ) , isUsedSatisfied ( ) ) ;
323
323
}
@@ -334,7 +334,7 @@ function shadergenerator(p5, fn) {
334
334
diff = diff > 0 ? diff : undefined ;
335
335
this . dependsOn . forEach ( dependency => {
336
336
if ( dependency . isVector ) {
337
- const dependencies = dependency . originalComponents . map ( ( component , i ) =>
337
+ const dependencies = dependency . originalComponents . map ( ( component , i ) =>
338
338
component === dependency . currentComponents [ i ]
339
339
) ;
340
340
context . updateComponents ( dependency . node , diff , dependencies ) ;
@@ -345,14 +345,14 @@ function shadergenerator(p5, fn) {
345
345
} else {
346
346
result = this . toGLSL ( context ) ;
347
347
}
348
- this . checkConditionalDependencies ( context )
348
+ this . checkConditionalDependencies ( context )
349
349
return result ;
350
350
}
351
351
352
352
shouldUseTemporaryVariable ( ) {
353
353
if ( this . componentsChanged || hasTemporaryVariable ( this ) || this . useTemp ) { return true ; }
354
354
if ( this . isInternal || isVariableNode ( this ) || isConditionalNode ( this ) || this . type === 'sampler2D' ) { return false ; }
355
-
355
+
356
356
// return false;
357
357
// Swizzles must use temporary variables as otherwise they will not be registered
358
358
let score = 0 ;
@@ -480,7 +480,7 @@ function shadergenerator(p5, fn) {
480
480
this . originalValues = conformVectorParameters ( values , parseInt ( type . slice ( 3 ) ) ) ;
481
481
this . componentNames = [ 'x' , 'y' , 'z' , 'w' ] . slice ( 0 , this . originalValues . length ) ;
482
482
}
483
-
483
+
484
484
addVectorComponents ( ) {
485
485
const values = this . originalValues ;
486
486
this . componentsChanged = false ;
@@ -556,8 +556,8 @@ function shadergenerator(p5, fn) {
556
556
const findBestOverload = function ( best , current ) {
557
557
current = determineFunctionSignature ( current ) ;
558
558
if ( ! current . valid ) { return best ; }
559
- if ( ! best || current . similarity > best . similarity ) {
560
- best = current ;
559
+ if ( ! best || current . similarity > best . similarity ) {
560
+ best = current ;
561
561
}
562
562
return best ;
563
563
}
@@ -568,7 +568,7 @@ function shadergenerator(p5, fn) {
568
568
569
569
if ( ! functionSignature || ! functionSignature . valid ) {
570
570
const argsStrJoin = ( args ) => `(${ args . map ( ( arg ) => arg ) . join ( ', ' ) } )` ;
571
- const expectedArgsString = Array . isArray ( properties ) ?
571
+ const expectedArgsString = Array . isArray ( properties ) ?
572
572
properties . map ( prop => argsStrJoin ( prop . args ) ) . join ( ' or ' )
573
573
: argsStrJoin ( properties . args ) ;
574
574
const providedArgsString = argsStrJoin ( userArgs . map ( ( a ) => getType ( a ) ) ) ;
@@ -645,7 +645,7 @@ function shadergenerator(p5, fn) {
645
645
}
646
646
}
647
647
648
- //
648
+ //
649
649
class VaryingNode extends VariableNode {
650
650
constructor ( name , type , isInternal = false ) {
651
651
super ( name , type , isInternal ) ;
@@ -771,36 +771,36 @@ function shadergenerator(p5, fn) {
771
771
772
772
// Conditions and logical modifiers
773
773
BaseNode . prototype . equalTo = function ( other ) {
774
- return binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '==' ) ;
774
+ return binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '==' ) ;
775
775
}
776
776
777
777
BaseNode . prototype . greaterThan = function ( other ) {
778
- return binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '>' ) ;
778
+ return binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '>' ) ;
779
779
}
780
780
781
781
BaseNode . prototype . greaterThanEqualTo = function ( other ) {
782
- return binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '>=' ) ;
782
+ return binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '>=' ) ;
783
783
}
784
784
785
785
BaseNode . prototype . lessThan = function ( other ) {
786
- return binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '<' ) ;
786
+ return binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '<' ) ;
787
787
}
788
-
788
+
789
789
BaseNode . prototype . lessThanEqualTo = function ( other ) {
790
790
return binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '<=' ) ; }
791
-
791
+
792
792
BaseNode . prototype . not = function ( ) {
793
- return new UnaryExpressionNode ( this . condition , '!' , true ) ;
793
+ return new UnaryExpressionNode ( this . condition , '!' , true ) ;
794
794
}
795
-
795
+
796
796
BaseNode . prototype . or = function ( other ) {
797
- return new binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '||' , true ) ;
797
+ return new binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '||' , true ) ;
798
798
}
799
-
799
+
800
800
BaseNode . prototype . and = function ( other ) {
801
- return new binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '&&' , true ) ;
801
+ return new binaryExpressionNodeConstructor ( this , this . enforceType ( other ) , '&&' , true ) ;
802
802
}
803
-
803
+
804
804
function branch ( callback ) {
805
805
const branch = new BranchNode ( ) ;
806
806
callback ( ) ;
@@ -847,8 +847,8 @@ function shadergenerator(p5, fn) {
847
847
} ;
848
848
849
849
saveState ( context , usedInSatisfied , dependsOnSatisfied ) {
850
- this . states . push ( {
851
- line : context . declarations . length ,
850
+ this . states . push ( {
851
+ line : context . declarations . length ,
852
852
usedInSatisfied,
853
853
dependsOnSatisfied
854
854
} ) ;
@@ -951,13 +951,13 @@ function shadergenerator(p5, fn) {
951
951
} else {
952
952
result = value . toGLSLBase ( context ) ;
953
953
}
954
-
954
+
955
955
if ( isVariableNode ( node ) || hasTemporaryVariable ( node ) ) {
956
956
statement = `${ node . toGLSLBase ( context ) } = ${ result } ;` ;
957
- }
957
+ }
958
958
else if ( isFloatNode ( node ) && node . name ) {
959
959
statement = `${ node . parent . toGLSLBase ( context ) } .${ node . name } = ${ result } ;` ;
960
- }
960
+ }
961
961
else {
962
962
node . temporaryVariable = `temp_${ context . getNextID ( ) } ` ;
963
963
statement = `${ node . type } ${ node . toGLSLBase ( context ) } = ${ result } ;`
@@ -994,11 +994,11 @@ function shadergenerator(p5, fn) {
994
994
if ( Array . isArray ( values ) ) {
995
995
for ( let val of values ) {
996
996
if ( isVectorType ( val ) ) {
997
- length += parseInt ( val . type . slice ( 3 ) ) ;
997
+ length += parseInt ( val . type . slice ( 3 ) ) ;
998
998
}
999
999
else length += 1 ;
1000
1000
}
1001
- }
1001
+ }
1002
1002
else if ( isVectorType ( values ) ) {
1003
1003
length += parseInt ( val . type . slice ( 3 ) ) ;
1004
1004
}
@@ -1208,7 +1208,7 @@ function shadergenerator(p5, fn) {
1208
1208
} ) ;
1209
1209
codeLines . push ( ...finalVaryingAssignments ) ;
1210
1210
}
1211
-
1211
+
1212
1212
codeLines . push ( ' return finalReturnValue;' , '}' ) ;
1213
1213
this . output [ hookName ] = codeLines . join ( '\n' ) ;
1214
1214
this . resetGLSLContext ( ) ;
@@ -1220,7 +1220,7 @@ function shadergenerator(p5, fn) {
1220
1220
GLOBAL_SHADER [ hookTypes . name ] ( userOverride ) ;
1221
1221
} ;
1222
1222
} ) ;
1223
-
1223
+
1224
1224
1225
1225
this . cleanup = ( ) => {
1226
1226
for ( const key in windowOverrides ) {
@@ -1230,8 +1230,8 @@ function shadergenerator(p5, fn) {
1230
1230
}
1231
1231
1232
1232
registerVarying ( node , value ) {
1233
- if ( ! Array . isArray ( this . context . varyings [ node . name ] ) ) {
1234
- this . context . varyings [ node . name ] = [ ] ;
1233
+ if ( ! Array . isArray ( this . context . varyings [ node . name ] ) ) {
1234
+ this . context . varyings [ node . name ] = [ ] ;
1235
1235
}
1236
1236
this . context . varyings [ node . name ] . push ( { node, value } ) ;
1237
1237
this . output . vertexDeclarations . add ( `OUT ${ node . type } ${ node . name } ;` ) ;
@@ -1303,7 +1303,7 @@ function shadergenerator(p5, fn) {
1303
1303
} ;
1304
1304
}
1305
1305
}
1306
-
1306
+
1307
1307
function makeDependencyArray ( dependencies ) {
1308
1308
return dependencies . map ( dep => makeDependencyObject ( dep ) ) ;
1309
1309
}
@@ -1336,15 +1336,15 @@ function shadergenerator(p5, fn) {
1336
1336
[ 'r' , 'g' , 'b' , 'a' ] ,
1337
1337
[ 's' , 't' , 'p' , 'q' ]
1338
1338
] . map ( s => s . slice ( 0 , size ) ) ;
1339
- return {
1339
+ return {
1340
1340
get ( target , property , receiver ) {
1341
1341
if ( property in target ) {
1342
1342
return Reflect . get ( ...arguments ) ;
1343
1343
} else {
1344
1344
for ( const set of swizzleSets ) {
1345
1345
if ( [ ...property ] . every ( char => set . includes ( char ) ) ) {
1346
1346
if ( property . length === 1 ) {
1347
- return target [ swizzleSets [ 0 ] [ set . indexOf ( property [ 0 ] ) ] ]
1347
+ return target [ swizzleSets [ 0 ] [ set . indexOf ( property [ 0 ] ) ] ]
1348
1348
}
1349
1349
const components = [ ...property ] . map ( char => {
1350
1350
const index = set . indexOf ( char ) ;
@@ -1376,7 +1376,7 @@ function shadergenerator(p5, fn) {
1376
1376
}
1377
1377
}
1378
1378
}
1379
-
1379
+
1380
1380
// User functions
1381
1381
fn . If = function ( condition , branch ) {
1382
1382
return new ConditionalNode ( condition , branch ) ;
@@ -1390,7 +1390,7 @@ function shadergenerator(p5, fn) {
1390
1390
const props = { args : [ 'sampler2D' , 'vec2' ] , returnType : 'vec4' , isp5Function : true } ;
1391
1391
return fnNodeConstructor ( 'getTexture' , userArgs , props ) ;
1392
1392
}
1393
-
1393
+
1394
1394
// Generating uniformFloat, uniformVec, createFloat, etc functions
1395
1395
// Maps a GLSL type to the name suffix for method names
1396
1396
const GLSLTypesToIdentifiers = {
@@ -1407,7 +1407,7 @@ function shadergenerator(p5, fn) {
1407
1407
const size = _size ? _size : parseInt ( node . type . slice ( 3 ) ) ;
1408
1408
node = new Proxy ( node , swizzleTrap ( size ) ) ;
1409
1409
node . addVectorComponents ( ) ;
1410
- }
1410
+ }
1411
1411
return node ;
1412
1412
}
1413
1413
@@ -1480,8 +1480,8 @@ function shadergenerator(p5, fn) {
1480
1480
return GLOBAL_SHADER [ uniformMethodName ] ( ...args ) ;
1481
1481
} ;
1482
1482
1483
-
1484
- // We don't need a createTexture method.
1483
+
1484
+ // We don't need a texture creation method.
1485
1485
if ( glslType === 'sampler2D' ) { continue ; }
1486
1486
1487
1487
const varyingMethodName = `varying${ typeIdentifier } ` ;
@@ -1493,16 +1493,24 @@ function shadergenerator(p5, fn) {
1493
1493
return GLOBAL_SHADER [ varyingMethodName ] ( name ) ;
1494
1494
} ;
1495
1495
1496
- // Generate the create*() Methods for creating variables in shaders
1497
- const createMethodName = `create${ typeIdentifier } ` ;
1498
- fn [ createMethodName ] = function ( ...value ) {
1499
- if ( glslType . startsWith ( 'vec' ) ) {
1500
- value = conformVectorParameters ( value , parseInt ( glslType . slice ( 3 ) ) ) ;
1496
+ // Generate the creation methods for creating variables in shaders
1497
+ const originalFn = fn [ glslType ] ;
1498
+ fn [ glslType ] = function ( ...value ) {
1499
+ if ( GLOBAL_SHADER ?. isGenerating ) {
1500
+ if ( glslType . startsWith ( 'vec' ) ) {
1501
+ value = conformVectorParameters ( value , parseInt ( glslType . slice ( 3 ) ) ) ;
1502
+ } else {
1503
+ value = value [ 0 ] ;
1504
+ }
1505
+ return nodeConstructors [ glslType ] ( value ) ;
1506
+ } else if ( originalFn ) {
1507
+ return originalFn . apply ( this , value ) ;
1501
1508
} else {
1502
- value = value [ 0 ] ;
1509
+ p5 . _friendlyError (
1510
+ `It looks like you've called ${ glslType } outside of a shader's modify() function.`
1511
+ ) ;
1503
1512
}
1504
- return nodeConstructors [ glslType ] ( value ) ;
1505
- }
1513
+ } ;
1506
1514
}
1507
1515
1508
1516
// GLSL Built in functions
@@ -1546,16 +1554,16 @@ function shadergenerator(p5, fn) {
1546
1554
'log' : { args : [ 'genType' ] , returnType : 'genType' , isp5Function : true } ,
1547
1555
'log2' : { args : [ 'genType' ] , returnType : 'genType' , isp5Function : false } ,
1548
1556
'max' : [
1549
- { args : [ 'genType' , 'genType' ] , returnType : 'genType' , isp5Function : true } ,
1550
- { args : [ 'genType' , 'float' ] , returnType : 'genType' , isp5Function : true } ,
1557
+ { args : [ 'genType' , 'genType' ] , returnType : 'genType' , isp5Function : true } ,
1558
+ { args : [ 'genType' , 'float' ] , returnType : 'genType' , isp5Function : true } ,
1551
1559
] ,
1552
1560
'min' : [
1553
- { args : [ 'genType' , 'genType' ] , returnType : 'genType' , isp5Function : true } ,
1554
- { args : [ 'genType' , 'float' ] , returnType : 'genType' , isp5Function : true } ,
1561
+ { args : [ 'genType' , 'genType' ] , returnType : 'genType' , isp5Function : true } ,
1562
+ { args : [ 'genType' , 'float' ] , returnType : 'genType' , isp5Function : true } ,
1555
1563
] ,
1556
1564
'mix' : [
1557
- { args : [ 'genType' , 'genType' , 'genType' ] , returnType : 'genType' , isp5Function : false } ,
1558
- { args : [ 'genType' , 'genType' , 'float' ] , returnType : 'genType' , isp5Function : false } ,
1565
+ { args : [ 'genType' , 'genType' , 'genType' ] , returnType : 'genType' , isp5Function : false } ,
1566
+ { args : [ 'genType' , 'genType' , 'float' ] , returnType : 'genType' , isp5Function : false } ,
1559
1567
] ,
1560
1568
// 'mod': {},
1561
1569
// 'modf': {},
@@ -1582,7 +1590,7 @@ function shadergenerator(p5, fn) {
1582
1590
// 'notEqual': {},
1583
1591
'reflect' : { args : [ 'genType' , 'genType' ] , returnType : 'genType' , isp5Function : false } ,
1584
1592
'refract' : { args : [ 'genType' , 'genType' , 'float' ] , returnType : 'genType' , isp5Function : false } ,
1585
-
1593
+
1586
1594
////////// Texture sampling //////////
1587
1595
'texture' : { args : [ 'sampler2D' , 'vec2' ] , returnType : 'vec4' , isp5Function : true } ,
1588
1596
}
0 commit comments