Skip to content

Commit 288d0f3

Browse files
committed
Added constant input support (and validation) to builder + moved where constants are declared + small jsdoc fixes
1 parent 1eb570c commit 288d0f3

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@
8686
// add input parameters
8787
var timeParam = builder.addParam('float', 'uTime', 0.0);
8888
var emissiveTexParam = builder.addParam('sampler2D', 'uEmissiveMap', emissiveMap);
89-
var emissiveColParam = builder.addParam('vec3', 'uEmmisiveColorParam', new pc.Vec3(0.2, 0.3, 0.3));
89+
var emissiveColConst = builder.addConst('vec3', 'emissiveColConst', new pc.Vec3(0.2, 0.3, 0.3));
9090

9191
// create and hook up graph nodes
9292
var scrollNode = builder.addNode('add2', builder.addNode('joinVec2', timeParam, timeParam), builder.addNode('uv0'));
9393
var emissiveSampleNode = builder.addNode('texSample', emissiveTexParam, scrollNode);
94-
var emissiveCalcNode = builder.addNode('mul3', emissiveColParam, {node:emissiveSampleNode, port:'rgb'});
94+
var emissiveCalcNode = builder.addNode('mul3', emissiveColConst, {node:emissiveSampleNode, port:'rgb'});
9595

9696
// hook up outputs
9797
builder.addOutput({node:emissiveSampleNode, port:'g'}, 'float', 'sgAlpha');

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ var id = 0;
66
* @private
77
* @class
88
* @name pc.ShaderGraphBuilder
9-
* @classdesc A Shader Graph Builder class
9+
* @classdesc A Shader Graph Builder class.
1010
* @param {pc.Application} app - Application with shader graph core nodes registered that will be used to
11-
* build the shader graph
11+
* build the shader graph.
1212
*/
1313
var ShaderGraphBuilder = function (app) {
1414
id++;
@@ -20,7 +20,7 @@ var ShaderGraphBuilder = function (app) {
2020
this._graph.name = 'graphRoot_' + id;
2121

2222
// for validation
23-
this._addedParams = {};
23+
this._addedNamedInputs = {};
2424
this._addedNodes = [];
2525
this._addedOutputs = {};
2626
};
@@ -39,20 +39,47 @@ Object.assign(ShaderGraphBuilder.prototype, {
3939
* @returns {any} Returns the created input port.
4040
*/
4141
addParam: function (type, name, value) {
42-
if (this._addedParams[name]) {
42+
if (this._addedNamedInputs[name]) {
4343
console.error('pc.ShaderGraphBuilder#addOutput: param ' + name + ' already added!');
4444
return null;
4545
}
4646

4747
var ioPort = this._graph.addInput(type, name, value);
4848

49-
this._addedParams[name] = ioPort;
49+
this._addedNamedInputs[name] = ioPort;
50+
51+
return ioPort;
52+
},
53+
/**
54+
* @private
55+
* @function
56+
* @name pc.ShaderGraphBuilder#addConst
57+
* @description Adds a constant input to graph.
58+
* @param {string} type - Type of constant.
59+
* @param {string} userId - Optional userId of constant.
60+
* @param {any} value - Value of constant.
61+
* @returns {any} Returns the created constant port.
62+
*/
63+
addConst: function (type, userId, value) {
64+
var name = userId ? 'const_' + type + '_' + userId : '';
65+
if (userId && this._addedNamedInputs[name]) {
66+
console.error('pc.ShaderGraphBuilder#addConst: userId ' + userId + ' already used!');
67+
return null;
68+
}
69+
if (!value) {
70+
console.error('pc.ShaderGraphBuilder#addConst:' + (userId ? '' : type) + ' constant ' + (userId || '') + ' has no value!');
71+
return null;
72+
}
73+
74+
var ioPort = this._graph.addConstant(type, userId, value);
75+
76+
if (userId) this._addedNamedInputs[name] = ioPort;
5077

5178
return ioPort;
5279
},
5380
_validateArg: function (arg, type, name, node, err) {
5481
if (arg.type) {
55-
if (!this._addedParams[arg.name]) {
82+
if (!this._addedNamedInputs[arg.name] && !arg.name.startsWith('const_') ) {
5683
console.error(err + ": invalid input param: " + arg.name);
5784
return false;
5885
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ Object.assign(ShaderGraphNode.prototype, {
249249
},
250250

251251
addConstant: function (type, userId, value) {
252-
var name = 'CONST_' + type + '_' + userId ? userId : this.graphData.ioPorts.length;
252+
var name = 'const_' + type + '_' + (userId ? userId : this.graphData.ioPorts.length);
253253
return this._addIoPort(type, name, value, PORT_TYPE_CONST);
254254
},
255255

@@ -406,24 +406,18 @@ Object.assign(ShaderGraphNode.prototype, {
406406
generateRootDeclCodeString: function () {
407407
var i;
408408
var ioPort;
409+
var ioPortCached;
409410
var generatedCodeString = '';
410411

411412
// run through inputs (and const sampler2Ds) to declare uniforms - (default) values are set elsewhere
412413
for (i = 0; i < this.graphData.ioPorts.length; i++) {
413414
ioPort = this.graphData.ioPorts[i];
414-
var ioPortCached = this._getCachedPort(i); // use port cache (minor optimization)
415+
ioPortCached = this._getCachedPort(i); // use port cache (minor optimization)
415416

416417
if (ioPortCached.isUniform) {
417418
generatedCodeString += 'uniform ' + ioPort.type + ' ' + ioPortCached.nameId + ';\n';
418419
}
419420
}
420-
// run through constants - values are set here (except for textures - which have to be uniforms)
421-
for (i = 0; i < this.graphData.ioPorts.length; i++) {
422-
ioPort = this.graphData.ioPorts[i];
423-
if (ioPort.ptype === PORT_TYPE_CONST && (ioPort.type !== 'sampler2D' )) {
424-
generatedCodeString += ioPort.type + ' ' + ioPort.name + ' = ' + this._getIoPortValueString(ioPort) + ';\n';
425-
}
426-
}
427421

428422
// get all sub graph function definitions (including functions in sub graphs' sub graphs ...)
429423
// assumes names are unique - maybe should be id or key?
@@ -540,6 +534,14 @@ Object.assign(ShaderGraphNode.prototype, {
540534

541535
generatedCodeString += ' ) {\n';
542536

537+
// input constants (not including samplers)
538+
for (i = 0; i < this.graphData.ioPorts.length; i++) {
539+
ioPort = this.graphData.ioPorts[i];
540+
if (ioPort.ptype === PORT_TYPE_CONST && (ioPort.type !== 'sampler2D' )) {
541+
generatedCodeString += ioPort.type + ' ' + ioPort.name + ' = ' + this._getIoPortValueString(ioPort) + ';\n';
542+
}
543+
}
544+
543545
// temporary structures - with temp scope only in parsing function
544546
var tmpVarCounter = 0;
545547
var srcTmpVarMap = [];

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { StandardMaterial } from './standard-material.js';
1111
* @class
1212
* @name pc.StandardNodeMaterial
1313
* @augments pc.StandardMaterial
14-
* @classdesc StandardNodeMaterial is sub class of the StandardMaterial, and adds shader graph interop functionality
14+
* @classdesc StandardNodeMaterial is sub class of the StandardMaterial, and adds shader graph
15+
* interop functionality.
1516
* @param {pc.StandardMaterial} mat - Optional material which is cloned.
1617
* @param {any} chunk - Optional shader graph node chunk to be used.
1718
*/

0 commit comments

Comments
 (0)