NodeMaterial.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { Material, ShaderMaterial } from 'three';
  2. import { getNodesKeys } from '../core/NodeUtils.js';
  3. import ExpressionNode from '../core/ExpressionNode.js';
  4. import {
  5. float, vec4,
  6. assign, label, mul, bypass,
  7. positionLocal, skinning, instance, modelViewProjection, lightingContext, colorSpace,
  8. materialAlphaTest, materialColor, materialOpacity
  9. } from '../shadernode/ShaderNodeElements.js';
  10. class NodeMaterial extends ShaderMaterial {
  11. constructor() {
  12. super();
  13. this.type = this.constructor.name;
  14. this.lights = true;
  15. }
  16. build( builder ) {
  17. const { lightsNode } = this;
  18. const { diffuseColorNode } = this.generateMain( builder );
  19. const outgoingLightNode = this.generateLight( builder, { diffuseColorNode, lightsNode } );
  20. this.generateOutput( builder, { diffuseColorNode, outgoingLightNode } );
  21. }
  22. generateMain( builder ) {
  23. const object = builder.object;
  24. // < VERTEX STAGE >
  25. let vertex = positionLocal;
  26. if ( this.positionNode !== null ) {
  27. vertex = bypass( vertex, assign( positionLocal, this.positionNode ) );
  28. }
  29. if ( object.instanceMatrix?.isInstancedBufferAttribute === true && builder.isAvailable( 'instance' ) === true ) {
  30. vertex = bypass( vertex, instance( object ) );
  31. }
  32. if ( object.isSkinnedMesh === true ) {
  33. vertex = bypass( vertex, skinning( object ) );
  34. }
  35. builder.context.vertex = vertex;
  36. builder.addFlow( 'vertex', modelViewProjection() );
  37. // < FRAGMENT STAGE >
  38. let colorNode = vec4( this.colorNode || materialColor );
  39. let opacityNode = this.opacityNode ? float( this.opacityNode ) : materialOpacity;
  40. // COLOR
  41. colorNode = builder.addFlow( 'fragment', label( colorNode, 'Color' ) );
  42. const diffuseColorNode = builder.addFlow( 'fragment', label( colorNode, 'DiffuseColor' ) );
  43. // OPACITY
  44. opacityNode = builder.addFlow( 'fragment', label( opacityNode, 'OPACITY' ) );
  45. builder.addFlow( 'fragment', assign( diffuseColorNode.a, mul( diffuseColorNode.a, opacityNode ) ) );
  46. // ALPHA TEST
  47. if ( this.alphaTestNode || this.alphaTest > 0 ) {
  48. const alphaTestNode = this.alphaTestNode ? float( this.alphaTestNode ) : materialAlphaTest;
  49. builder.addFlow( 'fragment', label( alphaTestNode, 'AlphaTest' ) );
  50. // @TODO: remove ExpressionNode here and then possibly remove it completely
  51. builder.addFlow( 'fragment', new ExpressionNode( 'if ( DiffuseColor.a <= AlphaTest ) { discard; }' ) );
  52. }
  53. return { colorNode, diffuseColorNode };
  54. }
  55. generateLight( builder, { diffuseColorNode, lightingModelNode, lightsNode = builder.lightsNode } ) {
  56. // < ANALYTIC LIGHTS >
  57. // OUTGOING LIGHT
  58. let outgoingLightNode = diffuseColorNode.xyz;
  59. if ( lightsNode && lightsNode.hasLight !== false ) outgoingLightNode = builder.addFlow( 'fragment', label( lightingContext( lightsNode, lightingModelNode ), 'Light' ) );
  60. return outgoingLightNode;
  61. }
  62. generateOutput( builder, { diffuseColorNode, outgoingLightNode } ) {
  63. // OUTPUT
  64. let outputNode = vec4( outgoingLightNode, diffuseColorNode.a );
  65. // ENCODING
  66. outputNode = colorSpace( outputNode, builder.renderer.outputEncoding );
  67. // FOG
  68. if ( builder.fogNode ) outputNode = builder.fogNode.mix( outputNode );
  69. // RESULT
  70. builder.addFlow( 'fragment', label( outputNode, 'Output' ) );
  71. return outputNode;
  72. }
  73. setDefaultValues( values ) {
  74. // This approach is to reuse the native refreshUniforms*
  75. // and turn available the use of features like transmission and environment in core
  76. for ( const property in values ) {
  77. const value = values[ property ];
  78. if ( this[ property ] === undefined ) {
  79. this[ property ] = value?.clone?.() || value;
  80. }
  81. }
  82. Object.assign( this.defines, values.defines );
  83. }
  84. toJSON( meta ) {
  85. const isRoot = ( meta === undefined || typeof meta === 'string' );
  86. if ( isRoot ) {
  87. meta = {
  88. textures: {},
  89. images: {},
  90. nodes: {}
  91. };
  92. }
  93. const data = Material.prototype.toJSON.call( this, meta );
  94. const nodeKeys = getNodesKeys( this );
  95. data.inputNodes = {};
  96. for ( const name of nodeKeys ) {
  97. data.inputNodes[ name ] = this[ name ].toJSON( meta ).uuid;
  98. }
  99. // TODO: Copied from Object3D.toJSON
  100. function extractFromCache( cache ) {
  101. const values = [];
  102. for ( const key in cache ) {
  103. const data = cache[ key ];
  104. delete data.metadata;
  105. values.push( data );
  106. }
  107. return values;
  108. }
  109. if ( isRoot ) {
  110. const textures = extractFromCache( meta.textures );
  111. const images = extractFromCache( meta.images );
  112. const nodes = extractFromCache( meta.nodes );
  113. if ( textures.length > 0 ) data.textures = textures;
  114. if ( images.length > 0 ) data.images = images;
  115. if ( nodes.length > 0 ) data.nodes = nodes;
  116. }
  117. return data;
  118. }
  119. static fromMaterial( /*material*/ ) { }
  120. }
  121. NodeMaterial.prototype.isNodeMaterial = true;
  122. export default NodeMaterial;