Skip to content

Commit b3e609b

Browse files
committed
More refactoring based on feedback - shader node registry now owned by app
1 parent 9480f20 commit b3e609b

File tree

9 files changed

+67
-55
lines changed

9 files changed

+67
-55
lines changed

examples/examples.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ var categories = [
4848
"post-effects",
4949
"render-to-texture",
5050
"shader-burn",
51-
"shader-graph-chunks-builder",
5251
"shader-toon",
5352
"shader-wobble",
5453
"texture-basis",

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@
7878
var coreNodeList = asset.resource;
7979

8080
//register loaded core nodes
81-
pc.shaderNodes.register(coreNodeList);
81+
app.shaderNodes.register(coreNodeList);
8282

8383
//create a shader graph builder
84-
var builder = new pc.ShaderGraphBuilder();
84+
var builder = new pc.ShaderGraphBuilder(app);
8585

8686
//add input parameters
8787
var timeParam = builder.addParam('float', 'uTime', 0.0);
@@ -126,7 +126,7 @@
126126
app.on("update", function (dt) {
127127
time += dt;
128128
shaderGraphMaterials.forEach(function (material) {
129-
material.setShaderGraphParameter('uTime', time);
129+
material.setParameter('uTime', time);
130130
material.alphaTest = 1.0 - ((Math.sin(time * 4.0) + 1.7) * 0.5);
131131
});
132132
});

src/framework/application.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ import { BundleRegistry } from '../bundles/bundle-registry.js';
8080

8181
import { ScriptRegistry } from '../script/script-registry.js';
8282

83+
import { ShaderNodeRegistry } from '../graphics/shader-node-registry.js';
84+
8385
import { I18n } from '../i18n/i18n.js';
8486

8587
import { VrManager } from '../vr/vr-manager.js';
@@ -443,6 +445,8 @@ function Application(canvas, options) {
443445
this.scriptsOrder = options.scriptsOrder || [];
444446
this.scripts = new ScriptRegistry(this);
445447

448+
this.shaderNodes = new ShaderNodeRegistry(this);
449+
446450
this.i18n = new I18n(this);
447451

448452
this.scenes = new SceneRegistry(this);

src/graphics/program-lib/nodes/nodes.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/graphics/program-lib/utils.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import { shaderChunks } from './chunks/chunks.js';
99

1010
import { dummyFragmentCode, precisionCode, versionCode } from './programs/common.js';
1111

12-
import { shaderNodes } from './nodes/nodes.js';
13-
import { ShaderGraphNode } from '../../scene/materials/shader-graph-node.js';
14-
1512
var attrib2Semantic = {
1613
vertex_position: SEMANTIC_POSITION,
1714
vertex_normal: SEMANTIC_NORMAL,
@@ -97,19 +94,4 @@ shaderChunks.collectAttribs = collectAttribs;
9794
shaderChunks.createShader = createShader;
9895
shaderChunks.createShaderFromCode = createShaderFromCode;
9996

100-
function registerCoreShaderGraphNodes(coreNodeList) {
101-
var coreNodes = {};
102-
if (coreNodeList) {
103-
if (typeof coreNodeList === 'string') {
104-
coreNodeList = JSON.parse(coreNodeList);
105-
}
106-
Object.keys(coreNodeList).forEach(function (key) {
107-
coreNodes[key] = new ShaderGraphNode(coreNodeList[key].code);
108-
});
109-
}
110-
shaderNodes._nodes = coreNodes;
111-
}
112-
113-
shaderNodes.register = registerCoreShaderGraphNodes;
114-
115-
export { collectAttribs, createShader, createShaderFromCode, registerCoreShaderGraphNodes };
97+
export { collectAttribs, createShader, createShaderFromCode };

src/graphics/shader-node-registry.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { ShaderGraphNode } from '../scene/materials/shader-graph-node.js';
2+
3+
/**
4+
* @private
5+
* @class
6+
* @name pc.ShaderNodeRegistry
7+
* @classdesc Container for all Shader Graph Core Nodes available to this application.
8+
* @description Create an instance of a pc.ShaderNodeRegistry.
9+
* @param {pc.Application} app - Application to attach registry to.
10+
*/
11+
function ShaderNodeRegistry(app) {
12+
this.app = app;
13+
this._nodes = { };
14+
this._list = [];
15+
}
16+
17+
ShaderNodeRegistry.prototype.constructor = ShaderNodeRegistry;
18+
19+
ShaderNodeRegistry.prototype.destroy = function () {
20+
this.app = null;
21+
};
22+
23+
/**
24+
* @private
25+
* @function
26+
* @name pc.ShaderNodeRegistry#register
27+
* @description Register core nodes with registry.
28+
* @param {object|string} coreNodeList - core node list object or JSON string
29+
*/
30+
ShaderNodeRegistry.prototype.register = function (coreNodeList) {
31+
if (coreNodeList) {
32+
if (typeof coreNodeList === 'string') {
33+
coreNodeList = JSON.parse(coreNodeList);
34+
}
35+
var self = this;
36+
Object.keys(coreNodeList).forEach(function (key) {
37+
self._nodes[key] = new ShaderGraphNode(coreNodeList[key].code);
38+
self._list.push(self._nodes[key]);
39+
});
40+
}
41+
};
42+
43+
export { ShaderNodeRegistry };

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export { prefilterCubemap, shFromCubemap } from './graphics/prefilter-cubemap.js
5353
export { reprojectTexture } from './graphics/reproject-texture.js';
5454
export { programlib } from './graphics/program-lib/program-lib.js';
5555
export { shaderChunks } from './graphics/program-lib/chunks/chunks.js';
56-
export { shaderNodes } from './graphics/program-lib/nodes/nodes.js';
5756
export { GraphicsDevice } from './graphics/device.js';
5857
export { IndexBuffer } from './graphics/index-buffer.js';
5958
export { PostEffect, drawFullscreenQuad } from './graphics/post-effect.js';
@@ -62,6 +61,7 @@ export { RenderTarget } from './graphics/render-target.js';
6261
export { ScopeId } from './graphics/scope-id.js';
6362
export { ScopeSpace } from './graphics/scope-space.js';
6463
export { Shader } from './graphics/shader.js';
64+
export { ShaderNodeRegistry } from './graphics/shader-node-registry.js';
6565
export { Texture } from './graphics/texture.js';
6666
export { TransformFeedback } from './graphics/transform-feedback.js';
6767
export { VertexBuffer } from './graphics/vertex-buffer.js';

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import { shaderNodes } from '../../graphics/program-lib/nodes/nodes.js';
2-
31
import { ShaderGraphNode } from './shader-graph-node.js';
42

53
var id = 0;
64

75
/**
6+
* @private
87
* @class
98
* @name pc.ShaderGraphBuilder
109
* @classdesc A Shader Graph Builder class
10+
* @param {pc.Application} app - Application with shader graph core nodes registered that will be used to build the shader graph
1111
*/
12-
var ShaderGraphBuilder = function () {
12+
var ShaderGraphBuilder = function (app) {
1313
id++;
14-
this._nodes = shaderNodes._nodes;
14+
15+
this.app = app;
16+
this._nodes = this.app.shaderNodes._nodes;
1517

1618
this._graph = new ShaderGraphNode();
1719
this._graph.name = 'graphRoot_' + id;
@@ -21,6 +23,7 @@ ShaderGraphBuilder.prototype.constructor = ShaderGraphBuilder;
2123

2224
Object.assign(ShaderGraphBuilder.prototype, {
2325
/**
26+
* @private
2427
* @function
2528
* @name pc.ShaderGraphBuilder#addParam
2629
* @description adds a parameter input to graph
@@ -34,6 +37,7 @@ Object.assign(ShaderGraphBuilder.prototype, {
3437
return ioPort;
3538
},
3639
/**
40+
* @private
3741
* @function
3842
* @name pc.ShaderGraphBuilder#addNode
3943
* @description creates and adds a core node to the shader graph and connects up inputs
@@ -76,6 +80,7 @@ Object.assign(ShaderGraphBuilder.prototype, {
7680
return sgIndex;
7781
},
7882
/**
83+
* @private
7984
* @function
8085
* @name pc.ShaderGraphBuilder#addOutput
8186
* @description connect a node port to graph material output
@@ -109,6 +114,7 @@ Object.assign(ShaderGraphBuilder.prototype, {
109114
}
110115
},
111116
/**
117+
* @private
112118
* @function
113119
* @name pc.ShaderGraphBuilder#getShaderGraphChunk
114120
* @description get shader graph chunk

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

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { StandardMaterial } from './standard-material.js';
88

99
/**
10+
* @private
1011
* @class
1112
* @name pc.StandardNodeMaterial
1213
* @augments pc.StandardMaterial
@@ -31,12 +32,6 @@ StandardNodeMaterial.prototype.constructor = StandardNodeMaterial;
3132

3233
Object.assign(StandardNodeMaterial.prototype, {
3334

34-
/**
35-
* @function
36-
* @name pc.StandardNodeMaterial#clone
37-
* @description Duplicates a Standard node material.
38-
* @returns {pc.StandardNodeMaterial} A cloned Standard node material.
39-
*/
4035
clone: function () {
4136
var clone = new StandardNodeMaterial();
4237
StandardMaterial.prototype._cloneInternal.call(this, clone);
@@ -46,20 +41,15 @@ Object.assign(StandardNodeMaterial.prototype, {
4641
return clone;
4742
},
4843

49-
/**
50-
* @function
51-
* @name pc.StandardNodeMaterial#setShaderGraphParameter
52-
* @description sets a shader graph parameter
53-
* @param {string} name - name of the parameter
54-
* @param {any} value - value of the parameter
55-
*/
56-
setShaderGraphParameter: function (name, value) {
44+
setParameter: function (name, data) {
45+
StandardMaterial.prototype.setParameter.call(this, name, data);
46+
5747
if (this._shaderGraphChunk) {
5848
var rootShaderGraph = this._shaderGraphChunk;
5949

6050
var portName = 'IN_' + name + '_' + rootShaderGraph.id;
6151

62-
this.setParameter(portName, value);
52+
StandardMaterial.prototype.setParameter.call(this, portName, data);
6353
}
6454
},
6555

0 commit comments

Comments
 (0)