NodeMaterial.js 4.8 KB

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