@@ -22,6 +22,8 @@ import {
22
22
23
23
import { begin , end , fogCode , gammaCode , precisionCode , skinCode , tonemapCode , versionCode } from './common.js' ;
24
24
25
+ import { shadergraph_nodeRegistry } from '../../../scene/materials/shader-graph-registry.js' ;
26
+
25
27
var _oldChunkWarn = function ( oldName , newName ) {
26
28
// #ifdef DEBUG
27
29
console . warn ( "Shader chunk " + oldName + " is deprecated - override " + newName + " instead" ) ;
@@ -212,6 +214,10 @@ var standard = {
212
214
}
213
215
}
214
216
217
+ if ( options . _shaderGraphChunk ) {
218
+ key += options . _shaderGraphChunk ;
219
+ }
220
+
215
221
return hashCode ( key ) ;
216
222
} ,
217
223
@@ -438,6 +444,18 @@ var standard = {
438
444
} ,
439
445
440
446
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
+
441
459
var i , p ;
442
460
var lighting = options . lights . length > 0 ;
443
461
@@ -465,6 +483,8 @@ var standard = {
465
483
var needsNormal = lighting || reflections || options . ambientSH || options . prefilteredCubemap || options . heightMap || options . enableGGXSpecular ;
466
484
var shadowPass = options . pass >= SHADER_SHADOW && options . pass <= 17 ;
467
485
486
+ needsNormal = needsNormal || options . _shaderGraphChunk ;
487
+
468
488
this . options = options ;
469
489
470
490
// GENERATE VERTEX SHADER
@@ -761,10 +781,25 @@ var standard = {
761
781
762
782
if ( needsNormal ) code += chunks . normalVS ;
763
783
784
+ if ( options . _shaderGraphChunk )
785
+ {
786
+ code += "#define SG_VS\n" ;
787
+ code += rootDeclGLSL ;
788
+ }
789
+
764
790
code += "\n" ;
765
791
766
792
code += chunks . startVS ;
767
793
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
+ }
768
803
code += "}" ;
769
804
770
805
var vshader = code ;
@@ -1304,6 +1339,13 @@ var standard = {
1304
1339
code += ( options . enableGGXSpecular ) ? chunks . reflDirAnisoPS : chunks . reflDirPS ;
1305
1340
}
1306
1341
}
1342
+
1343
+ if ( options . _shaderGraphChunk )
1344
+ {
1345
+ code += "#define SG_PS\n" ;
1346
+ code += rootDeclGLSL ;
1347
+ }
1348
+
1307
1349
var hasPointLights = false ;
1308
1350
var usesLinearFalloff = false ;
1309
1351
var usesInvSquaredFalloff = false ;
@@ -1341,7 +1383,7 @@ var standard = {
1341
1383
} else {
1342
1384
code += " getOpacity();\n" ; // calculate opacity first if there's no parallax+opacityMap, to allow early out
1343
1385
if ( options . alphaTest ) {
1344
- code += " alphaTest(dAlpha);\n" ;
1386
+ if ( ! options . _shaderGraphChunk ) code += " alphaTest(dAlpha);\n" ;
1345
1387
}
1346
1388
}
1347
1389
}
@@ -1360,7 +1402,7 @@ var standard = {
1360
1402
if ( opacityParallax ) {
1361
1403
code += " getOpacity();\n" ; // if there's parallax, calculate opacity after it, to properly distort
1362
1404
if ( options . alphaTest ) {
1363
- code += " alphaTest(dAlpha);\n" ;
1405
+ if ( ! options . _shaderGraphChunk ) code += " alphaTest(dAlpha);\n" ;
1364
1406
}
1365
1407
}
1366
1408
@@ -1370,12 +1412,43 @@ var standard = {
1370
1412
code += " getGlossiness();\n" ;
1371
1413
getGlossinessCalled = true ;
1372
1414
}
1373
- code += " getReflDir();\n" ;
1415
+ if ( ! options . _shaderGraphChunk ) code += " getReflDir();\n" ;
1374
1416
}
1375
1417
}
1376
1418
1377
1419
code += " getAlbedo();\n" ;
1378
1420
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
+
1379
1452
if ( ( lighting && options . useSpecular ) || reflections ) {
1380
1453
code += " getSpecularity();\n" ;
1381
1454
if ( ! getGlossinessCalled ) code += " getGlossiness();\n" ;
@@ -1555,6 +1628,19 @@ var standard = {
1555
1628
code += " gl_FragColor = applyMsdf(gl_FragColor);\n" ;
1556
1629
}
1557
1630
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
+
1558
1644
code += "\n" ;
1559
1645
code += end ( ) ;
1560
1646
0 commit comments