@@ -37,7 +37,6 @@ function shadergenerator(p5, fn) {
37
37
}
38
38
const generator = new ShaderGenerator ( generatorFunction , this , options . srcLocations ) ;
39
39
const generatedModifyArgument = generator . generate ( ) ;
40
- console . log ( generatedModifyArgument [ 'Vertex getCameraInputs' ] )
41
40
return oldModify . call ( this , generatedModifyArgument ) ;
42
41
}
43
42
else {
@@ -307,33 +306,19 @@ function shadergenerator(p5, fn) {
307
306
return this . usedInConditional ;
308
307
}
309
308
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 ) {
309
+ checkConditionalDependencies ( context ) {
320
310
context . ifs . forEach ( ( statement ) => {
321
- if ( statement . insertionPoint > - 1 ) return ;
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
- ) ;
311
+ const isUsedSatisfied = ( ) => statement . usedInSatisfied . length >= 1 ;
312
+ const isDepsSatisfied = ( ) => statement . dependsOn . length === statement . dependsOnSatisfied . length ;
313
+ if ( statement . insertionPoint > - 1 || ! statement . usedIn . length ) return ;
314
+ if ( statement . dependsOn . includes ( this ) && ! statement . dependsOnSatisfied . includes ( this ) ) {
315
+ statement . dependsOnSatisfied . push ( this ) ;
330
316
}
331
- console . log ( dependentOnConditional )
332
- if ( dependentOnConditional . includes ( this ) ) {
333
- console . log ( context . declarations . join ( '\n' ) )
334
- }
335
- if ( statement . dependsOn . length === statement . dependeciesFulfilled ) {
336
- statement . saveState ( context ) ;
317
+ if ( statement . usedIn . includes ( this ) && ! statement . usedInSatisfied . includes ( this ) ) {
318
+ statement . usedInSatisfied . push ( this ) ;
319
+ }
320
+ if ( isDepsSatisfied ( ) && isUsedSatisfied ( ) ) {
321
+ statement . saveState ( context , isDepsSatisfied ( ) , isUsedSatisfied ( ) ) ;
337
322
}
338
323
} ) ;
339
324
}
@@ -364,10 +349,7 @@ function shadergenerator(p5, fn) {
364
349
} else {
365
350
result = this . toGLSL ( context ) ;
366
351
}
367
- const depsUsedInConditional = this . dependentsUsedInConditional ( ) ;
368
- if ( this . isUsedInConditional ( ) || depsUsedInConditional . length > 0 ) {
369
- this . checkConditionalDependencies ( context , depsUsedInConditional )
370
- } ;
352
+ this . checkConditionalDependencies ( context )
371
353
return result ;
372
354
}
373
355
@@ -845,8 +827,11 @@ function shadergenerator(p5, fn) {
845
827
class ConditionalNode {
846
828
constructor ( condition , branchCallback ) {
847
829
this . dependsOn = [ ] ;
830
+ this . usedIn = [ ] ;
831
+ this . dependsOnSatisfied = [ ] ;
832
+ this . usedInSatisfied = [ ] ;
833
+ this . states = [ ] ;
848
834
this . if ( condition , branchCallback ) ;
849
- this . dependeciesFulfilled = [ ] ;
850
835
this . insertionPoint = - 1 ;
851
836
this . elseIfs = [ ] ;
852
837
this . elseBranch = null ;
@@ -877,10 +862,13 @@ function shadergenerator(p5, fn) {
877
862
return new ConditionalDiscard ( this . condition ) ;
878
863
} ;
879
864
880
- saveState ( context ) {
881
- if ( this . insertionPoint = - 1 ) {
882
- this . insertionPoint = context . declarations . length ;
883
- }
865
+ saveState ( context , usedInSatisfied , dependsOnSatisfied ) {
866
+ this . states . push ( {
867
+ line : context . declarations . length ,
868
+ usedInSatisfied,
869
+ dependsOnSatisfied
870
+ } ) ;
871
+ this . insertionPoint = context . declarations . length - 1 ;
884
872
}
885
873
886
874
toGLSL ( context ) {
@@ -1207,6 +1195,7 @@ function shadergenerator(p5, fn) {
1207
1195
}
1208
1196
1209
1197
this . context . ifs . forEach ( ( statement ) => {
1198
+ if ( statement . usedIn . length === 0 ) { return ; }
1210
1199
const lines = statement . toGLSL ( this . context ) ;
1211
1200
this . context . declarations . splice ( statement . insertionPoint , 0 , lines ) ;
1212
1201
} )
@@ -1432,12 +1421,14 @@ function shadergenerator(p5, fn) {
1432
1421
}
1433
1422
1434
1423
function fnNodeConstructor ( name , userArgs , properties , isInternal ) {
1435
- const node = new FunctionCallNode ( name , userArgs , properties , isInternal )
1436
- if ( node . type . startsWith ( 'vec' ) ) {
1437
- return dynamicAddSwizzleTrap ( node ) ;
1438
- } else {
1439
- return node ;
1424
+ let node = new FunctionCallNode ( name , userArgs , properties , isInternal ) ;
1425
+ node = dynamicAddSwizzleTrap ( node ) ;
1426
+ if ( node . args . some ( arg => arg . isUsedInConditional ( ) ) ) {
1427
+ GLOBAL_SHADER . context . ifs . forEach ( statement => {
1428
+ statement . usedIn . push ( node ) ;
1429
+ } ) ;
1440
1430
}
1431
+ return node ;
1441
1432
}
1442
1433
1443
1434
const nodeConstructors = {
0 commit comments