@@ -299,23 +299,39 @@ function shadergenerator(p5, fn) {
299
299
}
300
300
301
301
assertUsedInConditional ( ) {
302
- this . usedInConditional = true ;
303
- this . forceTemporaryVariable ( ) ;
302
+ this . usedInConditional = true ;
303
+ this . forceTemporaryVariable ( ) ;
304
304
}
305
-
306
- checkConditionalDependencies ( context ) {
307
- // if (!this.usedInConditional) return;
305
+
306
+ isUsedInConditional ( ) {
307
+ return this . usedInConditional ;
308
+ }
309
+
310
+ dependentsUsedInConditional ( ) {
311
+ let thisDeps = Array . isArray ( this . dependsOn ) ? this . dependsOn : this . dependsOn . nodesArray
312
+ thisDeps = thisDeps . map ( ( d ) => {
313
+ return Array . isArray ( d ) ? d [ 0 ] . parent : d ;
314
+ } ) ;
315
+ let depsUsed = thisDeps . filter ( d => d . isUsedInConditional ( ) ) ;
316
+ return depsUsed ;
317
+ }
318
+
319
+ checkConditionalDependencies ( context , dependentOnConditional ) {
308
320
context . ifs . forEach ( ( statement ) => {
309
321
if ( statement . insertionPoint > - 1 ) return ;
310
- statement . dependeciesFulfilled += statement . dependsOn
311
- . filter ( ( dependecy ) => dependecy === this )
312
- . length ;
313
- // console.log("CHECKING: ");
314
- // console.log("DEPENDS ON, this: ", statement.dependsOn, this);
315
- // console.log("this === any dep:", statement.dependsOn.filter(d => d == this).length)
316
- // console.log("FULFILLED / TOTAL: ", statement.dependeciesFulfilled ,'/', statement.dependsOn.length);
317
- // console.log(context.declarations.join('\n'));
318
- // console.log('\n')
322
+ if ( statement . dependsOn . includes ( this )
323
+ && ! statement . dependeciesFulfilled . includes ( this ) ) {
324
+ statement . dependeciesFulfilled . push ( this ) ;
325
+ this . oldName = this . toGLSLBase ( context ) ;
326
+ this . temporaryVariable = `temp_${ context . getNextID ( ) } ` ;
327
+ context . declarations . push (
328
+ ` ${ this . type } ${ this . toGLSLBase ( context ) } = ${ this . oldName } ;`
329
+ ) ;
330
+ }
331
+ console . log ( dependentOnConditional )
332
+ if ( dependentOnConditional . includes ( this ) ) {
333
+ console . log ( context . declarations . join ( '\n' ) )
334
+ }
319
335
if ( statement . dependsOn . length === statement . dependeciesFulfilled ) {
320
336
statement . saveState ( context ) ;
321
337
}
@@ -330,28 +346,36 @@ function shadergenerator(p5, fn) {
330
346
result = this . getTemporaryVariable ( context ) ;
331
347
let diff = context . declarations . length - 1 - oldLength ;
332
348
if ( Array . isArray ( this . dependsOn ) ) {
333
- this . dependsOn . forEach ( d => context . updateComponents ( d , diff > 0 ? diff : undefined ) ) ;
349
+ this . dependsOn . forEach ( d => {
350
+ context . updateComponents ( d , diff > 0 ? diff : undefined ) ;
351
+ } ) ;
334
352
} else {
335
- this . dependsOn . nodesArray . forEach ( ( node , i ) => {
336
- const originalComponents = this . dependsOn [ i ] ;
337
- const currentComponents = node . componentNames . map ( name => node [ name ] ) ;
338
- if ( ! originalComponents ) return ;
339
- const dependencies = originalComponents . map ( ( component , i ) =>
340
- component === currentComponents [ i ]
341
- )
342
- context . updateComponents ( node , diff > 0 ? diff : undefined , dependencies ) ;
343
- } )
353
+ // Update the incorrect components
354
+ this . dependsOn . nodesArray . forEach ( ( nodeDependency , i ) => {
355
+ const originalComponents = this . dependsOn [ i ] ;
356
+ const currentComponents = nodeDependency . componentNames . map ( name => nodeDependency [ name ] ) ;
357
+ if ( ! originalComponents ) return ;
358
+ const dependencies = originalComponents . map ( ( component , i ) =>
359
+ component === currentComponents [ i ]
360
+ )
361
+ context . updateComponents ( nodeDependency , diff > 0 ? diff : undefined , dependencies ) ;
362
+ } )
344
363
}
345
364
} else {
346
365
result = this . toGLSL ( context ) ;
347
366
}
348
- this . checkConditionalDependencies ( context ) ;
367
+ const depsUsedInConditional = this . dependentsUsedInConditional ( ) ;
368
+ if ( this . isUsedInConditional ( ) || depsUsedInConditional . length > 0 ) {
369
+ this . checkConditionalDependencies ( context , depsUsedInConditional )
370
+ } ;
349
371
return result ;
350
372
}
351
373
352
374
shouldUseTemporaryVariable ( ) {
353
375
if ( this . componentsChanged || hasTemporaryVariable ( this ) || this . useTemp ) { return true ; }
354
376
if ( this . isInternal || isVariableNode ( this ) || isConditionalNode ( this ) || this . type === 'sampler2D' ) { return false ; }
377
+
378
+ // return false;
355
379
// Swizzles must use temporary variables as otherwise they will not be registered
356
380
let score = 0 ;
357
381
score += isFunctionCallNode ( this ) * 2 ;
@@ -504,7 +528,7 @@ function shadergenerator(p5, fn) {
504
528
}
505
529
506
530
toGLSL ( context ) {
507
- if ( ! this . componentsChanged || ! this . defined ) {
531
+ if ( ( ! this . componentsChanged || ! this . defined ) && ! this . oldName ) {
508
532
let glslArgs = this . componentNames . map ( ( _name , i ) => this . originalValues [ i ] . toGLSLBase ( context ) ) . join ( ', ' ) ;
509
533
this . defined = true ;
510
534
return `${ this . type } (${ glslArgs } )` ;
@@ -616,6 +640,7 @@ function shadergenerator(p5, fn) {
616
640
if ( isIntType ( argNode ) && this . argumentTypes [ i ] != 'float' ) {
617
641
argNode = argNode . toFloat ( ) ;
618
642
}
643
+ argNode . toGLSLBase ( context ) ;
619
644
return argNode . toGLSLBase ( context ) ;
620
645
} ) . join ( ', ' ) ;
621
646
return argsString ;
@@ -821,7 +846,7 @@ function shadergenerator(p5, fn) {
821
846
constructor ( condition , branchCallback ) {
822
847
this . dependsOn = [ ] ;
823
848
this . if ( condition , branchCallback ) ;
824
- this . dependeciesFulfilled = 0 ;
849
+ this . dependeciesFulfilled = [ ] ;
825
850
this . insertionPoint = - 1 ;
826
851
this . elseIfs = [ ] ;
827
852
this . elseBranch = null ;
@@ -854,9 +879,7 @@ function shadergenerator(p5, fn) {
854
879
855
880
saveState ( context ) {
856
881
if ( this . insertionPoint = - 1 ) {
857
- context . declarations . join ( '\n' ) ;
858
882
this . insertionPoint = context . declarations . length ;
859
- console . log ( this . insertionPoint ) ;
860
883
}
861
884
}
862
885
@@ -927,9 +950,12 @@ function shadergenerator(p5, fn) {
927
950
this . assignments . push ( { node, value } ) ;
928
951
}
929
952
node = node . parent ? node . parent : node ;
953
+ value = value . parent ? value . parent : value ;
954
+ if ( [ node , value ] . some ( n => this . dependsOn . includes ( n ) ) ) {
955
+ return ;
956
+ }
930
957
node . assertUsedInConditional ( ) ;
931
958
this . dependsOn . push ( node )
932
- value = value . parent ? value . parent : value ;
933
959
if ( value . shouldUseTemporaryVariable ( ) ) {
934
960
value . assertUsedInConditional ( ) ;
935
961
this . dependsOn . push ( value ) ;
@@ -1235,8 +1261,8 @@ function shadergenerator(p5, fn) {
1235
1261
this . context . varyings [ node . name ] = [ ] ;
1236
1262
}
1237
1263
this . context . varyings [ node . name ] . push ( { node, value } ) ;
1238
- this . output . vertexDeclarations . add ( `out ${ node . type } ${ node . name } ;` ) ;
1239
- this . output . fragmentDeclarations . add ( `in ${ node . type } ${ node . name } ;` ) ;
1264
+ this . output . vertexDeclarations . add ( `OUT ${ node . type } ${ node . name } ;` ) ;
1265
+ this . output . fragmentDeclarations . add ( `IN ${ node . type } ${ node . name } ;` ) ;
1240
1266
}
1241
1267
1242
1268
resetGLSLContext ( ) {
@@ -1364,6 +1390,11 @@ function shadergenerator(p5, fn) {
1364
1390
fn . instanceID = function ( ) {
1365
1391
return variableConstructor ( 'gl_InstanceID' , 'int' ) ;
1366
1392
}
1393
+
1394
+ fn . getTexture = function ( ...userArgs ) {
1395
+ const props = { args : [ 'sampler2D' , 'vec2' ] , returnType : 'vec4' , isp5Function : true } ;
1396
+ return fnNodeConstructor ( 'getTexture' , userArgs , props ) ;
1397
+ }
1367
1398
1368
1399
// Generating uniformFloat, uniformVec, createFloat, etc functions
1369
1400
// Maps a GLSL type to the name suffix for method names
0 commit comments