Skip to content

Commit 2680e00

Browse files
committed
WIP add shader graph chunk support to standard material
1 parent 0b3adbf commit 2680e00

File tree

7 files changed

+344
-184
lines changed

7 files changed

+344
-184
lines changed

examples/graphics/shader-node-no-shader-code.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@
8383
var model = modelComponent.model;
8484
var diffuseMap = model.getMaterials()[0].diffuseMap;
8585

86-
//start building shader graph
86+
//start building shader graph, register core nodes
8787
pc.shadergraph.start(coreNodesJSONString);
8888

8989
//create a simple PS shader graph - just a 2D texture sample with scrolling UVs
9090
var timeParam = pc.shadergraph.param('float', 'uTime', 0.0);
9191
var scrollNode = pc.shadergraph.add2( pc.shadergraph.joinVec2( timeParam, timeParam), pc.shadergraph.uv0);
92-
var texSampleNode = pc.shadergraph.textureSample2D('diffSamp',diffuseMap, scrollNode);
92+
var texSampleNode = pc.shadergraph.textureSample2D('diffSamp', diffuseMap, scrollNode);
9393

9494
//hook up PS outputs
9595
pc.shadergraph.connectFragOut(texSampleNode);

src/graphics/program-lib/programs/standard.js

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222

2323
import { begin, end, fogCode, gammaCode, precisionCode, skinCode, tonemapCode, versionCode } from './common.js';
2424

25+
import { shadergraph_nodeRegistry } from '../../../scene/materials/shader-graph-registry.js';
26+
2527
var _oldChunkWarn = function (oldName, newName) {
2628
// #ifdef DEBUG
2729
console.warn("Shader chunk " + oldName + " is deprecated - override " + newName + " instead");
@@ -212,6 +214,10 @@ var standard = {
212214
}
213215
}
214216

217+
if (options._shaderGraphChunk) {
218+
key += options._shaderGraphChunk;
219+
}
220+
215221
return hashCode(key);
216222
},
217223

@@ -438,6 +444,18 @@ var standard = {
438444
},
439445

440446
createShaderDefinition: function (device, options) {
447+
var rootShaderGraph = null;
448+
var rootDeclGLSL = '';
449+
var rootCallGLSL = '';
450+
451+
if (options._shaderGraphChunk)
452+
{
453+
rootShaderGraph = shadergraph_nodeRegistry.getNode(options._shaderGraphChunk);
454+
455+
rootDeclGLSL = rootShaderGraph.generateRootDeclGlsl();
456+
rootCallGLSL = rootShaderGraph.generateRootCallGlsl();
457+
}
458+
441459
var i, p;
442460
var lighting = options.lights.length > 0;
443461

@@ -465,6 +483,8 @@ var standard = {
465483
var needsNormal = lighting || reflections || options.ambientSH || options.prefilteredCubemap || options.heightMap || options.enableGGXSpecular;
466484
var shadowPass = options.pass >= SHADER_SHADOW && options.pass <= 17;
467485

486+
needsNormal = needsNormal || options._shaderGraphChunk;
487+
468488
this.options = options;
469489

470490
// GENERATE VERTEX SHADER
@@ -761,10 +781,25 @@ var standard = {
761781

762782
if (needsNormal) code += chunks.normalVS;
763783

784+
if (options._shaderGraphChunk)
785+
{
786+
code += "#define SG_VS\n";
787+
code += rootDeclGLSL;
788+
}
789+
764790
code += "\n";
765791

766792
code += chunks.startVS;
767793
code += codeBody;
794+
if (options._shaderGraphChunk)
795+
{
796+
if (rootShaderGraph.getGraphVarByName('OUT_vertOff'))
797+
{
798+
code += rootCallGLSL;
799+
code += " vPositionW = vPositionW+OUT_vertOff;\n";
800+
code += " gl_Position = matrix_viewProjection*vec4(vPositionW,1);\n";
801+
}
802+
}
768803
code += "}";
769804

770805
var vshader = code;
@@ -1304,6 +1339,13 @@ var standard = {
13041339
code += (options.enableGGXSpecular) ? chunks.reflDirAnisoPS : chunks.reflDirPS;
13051340
}
13061341
}
1342+
1343+
if (options._shaderGraphChunk)
1344+
{
1345+
code += "#define SG_PS\n";
1346+
code += rootDeclGLSL;
1347+
}
1348+
13071349
var hasPointLights = false;
13081350
var usesLinearFalloff = false;
13091351
var usesInvSquaredFalloff = false;
@@ -1341,7 +1383,7 @@ var standard = {
13411383
} else {
13421384
code += " getOpacity();\n"; // calculate opacity first if there's no parallax+opacityMap, to allow early out
13431385
if (options.alphaTest) {
1344-
code += " alphaTest(dAlpha);\n";
1386+
if (!options._shaderGraphChunk) code += " alphaTest(dAlpha);\n";
13451387
}
13461388
}
13471389
}
@@ -1360,7 +1402,7 @@ var standard = {
13601402
if (opacityParallax) {
13611403
code += " getOpacity();\n"; // if there's parallax, calculate opacity after it, to properly distort
13621404
if (options.alphaTest) {
1363-
code += " alphaTest(dAlpha);\n";
1405+
if (!options._shaderGraphChunk) code += " alphaTest(dAlpha);\n";
13641406
}
13651407
}
13661408

@@ -1370,12 +1412,43 @@ var standard = {
13701412
code += " getGlossiness();\n";
13711413
getGlossinessCalled = true;
13721414
}
1373-
code += " getReflDir();\n";
1415+
if (!options._shaderGraphChunk) code += " getReflDir();\n";
13741416
}
13751417
}
13761418

13771419
code += " getAlbedo();\n";
13781420

1421+
if (options._shaderGraphChunk)
1422+
{
1423+
code += rootCallGLSL;
1424+
1425+
if (rootShaderGraph.getGraphVarByName('OUT_dAlpha'))
1426+
{
1427+
code += 'dAlpha=OUT_dAlpha;\n';
1428+
}
1429+
if (options.alphaTest) {
1430+
code += " alphaTest(dAlpha);\n";
1431+
}
1432+
1433+
if (rootShaderGraph.getGraphVarByName('OUT_dNormalW'))
1434+
{
1435+
code += 'dNormalW=OUT_dNormalW;\n';
1436+
}
1437+
1438+
if (rootShaderGraph.getGraphVarByName('OUT_dGlossiness'))
1439+
{
1440+
code += 'dGlossiness=OUT_dGlossiness;\n';
1441+
}
1442+
if (options.useSpecular) {
1443+
code += " getReflDir();\n";
1444+
}
1445+
1446+
if (rootShaderGraph.getGraphVarByName('OUT_dAlbedo'))
1447+
{
1448+
code += 'dAlbedo=OUT_dAlbedo;\n';
1449+
}
1450+
}
1451+
13791452
if ((lighting && options.useSpecular) || reflections) {
13801453
code += " getSpecularity();\n";
13811454
if (!getGlossinessCalled) code += " getGlossiness();\n";
@@ -1555,6 +1628,19 @@ var standard = {
15551628
code += " gl_FragColor = applyMsdf(gl_FragColor);\n";
15561629
}
15571630

1631+
if (options._shaderGraphChunk)
1632+
{
1633+
if (rootShaderGraph.getGraphVarByName('OUT_fragOut'))
1634+
{
1635+
code += 'gl_FragColor = OUT_fragOut;\n';
1636+
}
1637+
1638+
if (rootShaderGraph.getGraphVarByName('OUT_dEmission'))
1639+
{
1640+
code += 'gl_FragColor.rgb += OUT_dEmission;\n';
1641+
}
1642+
}
1643+
15581644
code += "\n";
15591645
code += end();
15601646

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ export { Model } from './scene/model.js';
8787
export { Morph } from './scene/morph.js';
8888
export { MorphInstance } from './scene/morph-instance.js';
8989
export { MorphTarget } from './scene/morph-target.js';
90-
export { NodeMaterial, shadergraph } from './scene/materials/node-material.js';
90+
export { NodeMaterial } from './scene/materials/node-material.js';
9191
export { ParticleEmitter } from './scene/particle-system/particle-emitter.js';
9292
export { Picker } from './scene/pick.js';
9393
export { Scene } from './scene/scene.js';
94+
export { shadergraph } from './scene/materials/shader-graph.js';
9495
export { Skin, SkinInstance } from './scene/skin.js';
9596
export { Sprite } from './scene/sprite.js';
9697
export { StandardMaterial } from './scene/materials/standard-material.js';

0 commit comments

Comments
 (0)