@@ -458,11 +458,23 @@ Object.assign(ShaderGraphNode.prototype, {
458
458
return callString ;
459
459
} ,
460
460
461
- addStaticSwitch : function ( name , id , maxValue , value ) {
461
+ addStaticSwitch : function ( name , id , args , value ) {
462
462
if ( this . _switchMap [ name ] ) {
463
463
console . error ( 'switch ' + name + ' already added!' ) ;
464
464
} else {
465
- this . _switchMap [ name ] = { id : id , maxValue : maxValue , value : value } ;
465
+ var value2Index = { } ;
466
+ for ( var i = 0 ; i < args . length ; i ++ ) {
467
+ var labelValue = args [ i ] . switchLabel || `${ i } ` ;
468
+
469
+ if ( value2Index [ labelValue ] !== undefined ) {
470
+ console . error ( `switch label ${ labelValue } already used in switch: ${ name } !` ) ;
471
+ }
472
+
473
+ value2Index [ labelValue ] = i ;
474
+ }
475
+
476
+ this . _switchMap [ name ] = { id : id , maxValue : args . length - 1 , value : value , index : value2Index [ value ] , value2Index : value2Index } ;
477
+
466
478
this . _switchId2Name [ id ] = name ;
467
479
}
468
480
} ,
@@ -471,17 +483,34 @@ Object.assign(ShaderGraphNode.prototype, {
471
483
if ( this . _switchMap [ name ] ) {
472
484
return ( ( this . _switchOverrides && this . _switchOverrides [ name ] !== undefined ) ? this . _switchOverrides [ name ] : this . _switchMap [ name ] . value ) ;
473
485
}
486
+ console . error ( `switch ${ name } is invalid!` ) ;
487
+ } ,
488
+
489
+ getSwitchIndex : function ( name ) {
490
+ var value = this . getSwitchValue ( name ) ;
491
+ if ( value !== undefined ) {
492
+ var index = this . _switchMap [ name ] . value2Index [ value ] ;
493
+ if ( index !== undefined ) {
494
+ return index ;
495
+ }
496
+ }
497
+ console . error ( `switch ${ name } value or index is invalid!` ) ;
474
498
} ,
475
499
476
500
setSwitchValue : function ( name , value ) {
477
- if ( this . _switchMap [ name ] ) {
478
- if ( value !== undefined && value >= 0 && value <= this . _switchMap [ name ] . maxValue ) {
479
- if ( this . _switchMap [ name ] . value != value ) {
480
- this . _switchMap [ name ] . value = value ;
501
+ var mappedSwitch = this . _switchMap [ name ] ;
502
+ if ( mappedSwitch ) {
503
+ var index = mappedSwitch . value2Index [ value ] ;
504
+ if ( index !== undefined ) {
505
+ if ( mappedSwitch . index != index ) {
506
+ mappedSwitch . index = index ;
507
+ mappedSwitch . value = value ;
481
508
this . _defaultParamsDirty = true ;
482
509
this . _defaultSwitchesDirty = true ;
483
510
}
484
511
}
512
+ } else {
513
+ console . error ( `switch ${ name } is invalid!` ) ;
485
514
}
486
515
} ,
487
516
@@ -554,7 +583,7 @@ Object.assign(ShaderGraphNode.prototype, {
554
583
}
555
584
if ( ioPort . ptype === PORT_TYPE_SWITCH ) {
556
585
// assume it is a dynamic switch in sub graph (if a static is passed as a param, then compiler can still optimize)
557
- depIoPort = 'uniform ' + subGraph . _precision + ' ' + ioPort . type + ' ' + ioPortCached . nameId + ' = float(' + subGraph . getSwitchValue ( ioPort . name ) + ');\n' ;
586
+ depIoPort = 'uniform ' + subGraph . _precision + ' ' + ioPort . type + ' ' + ioPortCached . nameId + ' = float(' + subGraph . getSwitchIndex ( ioPort . name ) + ');\n' ;
558
587
depIoPortList . push ( depIoPort ) ;
559
588
}
560
589
}
@@ -583,7 +612,7 @@ Object.assign(ShaderGraphNode.prototype, {
583
612
}
584
613
if ( ioPort . ptype === PORT_TYPE_SWITCH ) {
585
614
// assume it is a static switch in root
586
- generatedCodeString += 'const ' + this . _precision + ' ' + ioPort . type + ' ' + ioPortCached . nameId + ' = float(' + this . getSwitchValue ( ioPort . name ) + ');\n' ;
615
+ generatedCodeString += 'const ' + this . _precision + ' ' + ioPort . type + ' ' + ioPortCached . nameId + ' = float(' + this . getSwitchIndex ( ioPort . name ) + ');\n' ;
587
616
}
588
617
}
589
618
@@ -819,7 +848,7 @@ Object.assign(ShaderGraphNode.prototype, {
819
848
820
849
// if (this._switchId2Name[subGraphIndex]) {
821
850
// static switch
822
- // generatedCodeString += this.graphData.subGraphs[subGraphIndex]._generateStaticSwitch(this.getSwitchValue (this._switchId2Name[subGraphIndex]), dstTmpVarMap[subGraphIndex], srcTmpVarMap[subGraphIndex]);
851
+ // generatedCodeString += this.graphData.subGraphs[subGraphIndex]._generateStaticSwitch(this.getSwitchIndex (this._switchId2Name[subGraphIndex]), dstTmpVarMap[subGraphIndex], srcTmpVarMap[subGraphIndex]);
823
852
// } else {
824
853
// generatedCodeString += this.graphData.subGraphs[subGraphIndex]._generateSubGraphCall(dstTmpVarMap[subGraphIndex], srcTmpVarMap[subGraphIndex]);
825
854
// }
0 commit comments