Skip to content

Commit 6cd65b2

Browse files
committed
added static switch input labels
1 parent ccccbf6 commit 6cd65b2

File tree

5 files changed

+52
-24
lines changed

5 files changed

+52
-24
lines changed

examples/graphics/shader-graph-chunks-builder.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@
9696
var emisCalcNode = builder.addNode('mul', [ colConst, {node: texSampNodeA, swizzle: 'rgb'} ], { precision: 'highp' } );
9797

9898
// create a static switch to choose alpha source
99-
var switchValue = 1;
99+
var switchValue = 'B';
100100
var switchName = 'switchAlpha';
101-
var staticSwitchAlpha = builder.addStaticSwitch(switchName, [ { node: texSampNodeA, swizzle: 'gb' }, { node: texSampNodeB, swizzle: 'ar' } ], switchValue);
101+
var staticSwitchAlpha = builder.addStaticSwitch(switchName, [ { node: texSampNodeA, swizzle: 'gb', switchLabel: 'A' }, { node: texSampNodeB, swizzle: 'ar', switchLabel: 'B' } ], switchValue);
102102

103103
// hook up outputs
104104
builder.addOutput( staticSwitchAlpha, 'float', 'sgAlpha' );
@@ -148,7 +148,7 @@
148148
{
149149
flipTime += 4.0;
150150
flipSwitch = true;
151-
switchValue = 1-switchValue;
151+
switchValue = switchValue === 'A' ? 'B' : 'A';
152152
}
153153

154154
shaderGraphMaterials.forEach(function (material) {

examples/graphics/shader-graph-chunks-overrides.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@
125125
var texSampNodeB = builder.addTextureSample( texParamB, builder.addNode('mul', [scrollNodeB, tileParamB]) );
126126

127127
// create a static switch to choose alpha source
128-
var switchValue = 0;
128+
var switchValue = 'A';
129129
var switchName = 'switchAlpha';
130-
var staticSwitch = builder.addStaticSwitch(switchName, [ { node: texSampNodeA, swizzle: 'rgbg' }, { node: texSampNodeB, swizzle: 'rgbg' } ], switchValue);
130+
var staticSwitch = builder.addStaticSwitch(switchName, [ { node: texSampNodeA, swizzle: 'rgbg', switchLabel: 'A' }, { node: texSampNodeB, swizzle: 'rgbg', switchLabel: 'B' } ], switchValue);
131131

132132
var calcNodeA = builder.addNode('mul', [{ node: usefulConsts, swizzle: 'g' }, { node: usefulConsts, swizzle: 'g' }, { node: staticSwitch, swizzle: 'g' }]);
133133

@@ -179,19 +179,19 @@
179179

180180
var t4=time%4.0;
181181
if (t4>=2.0) {
182-
shaderGraphChunk.setSwitchValue(switchName, 0);
182+
shaderGraphChunk.setSwitchValue(switchName, 'A');
183183
} else {
184-
shaderGraphChunk.setSwitchValue(switchName, 1);
184+
shaderGraphChunk.setSwitchValue(switchName, 'B');
185185
}
186186

187187
shaderGraphChunk.update(app.graphicsDevice);
188188

189189
if (entityB && matB) {
190190
var tB=time%1.0;
191191
if (tB>=0.5) {
192-
matB.overrideGraphSwitch(switchName, 1);
192+
matB.overrideGraphSwitch(switchName, 'B');
193193
} else {
194-
matB.overrideGraphSwitch(switchName, 0);
194+
matB.overrideGraphSwitch(switchName, 'A');
195195
}
196196
matB.update();
197197
}

src/scene/materials/shader-graph-builder.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ Object.assign(ShaderGraphBuilder.prototype, {
222222
var texNode = this.addNode('texSample', texArgs, options);
223223

224224
var switchName = 'switch_' + texNode;
225-
var switchValue = 0;
225+
var switchValue = 'linear';
226226
if (texParam.valueTex.type === TEXTURETYPE_RGBM) {
227-
switchValue = 2;
227+
switchValue = 'RGBM';
228228
}
229229

230-
var switchArgs = [{ node: texNode, port: 'rgba' }, { node: texNode, port: 'srgba' }, { node: texNode, port: 'rgbm' }];
230+
var switchArgs = [{ node: texNode, port: 'rgba', switchLabel: 'linear' }, { node: texNode, port: 'srgba', switchLabel: 'sRGB' }, { node: texNode, port: 'rgbm', switchLabel: 'RGBM' }];
231231
var switchNodeId = this.addStaticSwitch(switchName, switchArgs, switchValue);
232232

233233
return switchNodeId;
@@ -248,7 +248,7 @@ Object.assign(ShaderGraphBuilder.prototype, {
248248
console.error("pc.ShaderGraphBuilder#addStaticSwitch: failed to add static switch:" + name);
249249
return undefined;
250250
}
251-
this._graph.addStaticSwitch(name, switchNodeId, args.length - 1, value);
251+
this._graph.addStaticSwitch(name, switchNodeId, args, value);
252252

253253
return switchNodeId;
254254
},

src/scene/materials/shader-graph-node.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,23 @@ Object.assign(ShaderGraphNode.prototype, {
458458
return callString;
459459
},
460460

461-
addStaticSwitch: function (name, id, maxValue, value) {
461+
addStaticSwitch: function (name, id, args, value) {
462462
if (this._switchMap[name]) {
463463
console.error('switch ' + name + ' already added!');
464464
} 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+
466478
this._switchId2Name[id] = name;
467479
}
468480
},
@@ -471,17 +483,34 @@ Object.assign(ShaderGraphNode.prototype, {
471483
if (this._switchMap[name]) {
472484
return ((this._switchOverrides && this._switchOverrides[name] !== undefined) ? this._switchOverrides[name] : this._switchMap[name].value);
473485
}
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!`);
474498
},
475499

476500
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;
481508
this._defaultParamsDirty = true;
482509
this._defaultSwitchesDirty = true;
483510
}
484511
}
512+
} else {
513+
console.error(`switch ${name} is invalid!`);
485514
}
486515
},
487516

@@ -554,7 +583,7 @@ Object.assign(ShaderGraphNode.prototype, {
554583
}
555584
if (ioPort.ptype === PORT_TYPE_SWITCH) {
556585
// 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';
558587
depIoPortList.push(depIoPort);
559588
}
560589
}
@@ -583,7 +612,7 @@ Object.assign(ShaderGraphNode.prototype, {
583612
}
584613
if (ioPort.ptype === PORT_TYPE_SWITCH) {
585614
// 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';
587616
}
588617
}
589618

@@ -819,7 +848,7 @@ Object.assign(ShaderGraphNode.prototype, {
819848

820849
// if (this._switchId2Name[subGraphIndex]) {
821850
// 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]);
823852
// } else {
824853
// generatedCodeString += this.graphData.subGraphs[subGraphIndex]._generateSubGraphCall(dstTmpVarMap[subGraphIndex], srcTmpVarMap[subGraphIndex]);
825854
// }

src/scene/materials/standard-node-material.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ Object.assign(StandardNodeMaterial.prototype, {
5858

5959
overrideGraphSwitch: function (name, value) {
6060
if (this.shaderGraphChunk) {
61-
var currentValue = this._graphSwitchOverrides[name];// this.shaderGraphChunk.getSwitchValue(name);
61+
var currentValue = this._graphSwitchOverrides[name];
6262
if (currentValue !== value) {
6363
this._graphSwitchOverrides[name] = value;
64-
// this.shaderGraphChunk.setSwitchValue(name, value);
6564
this.dirtyShader = true;
6665
}
6766
}

0 commit comments

Comments
 (0)