NodeMaterial.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { Material, ShaderMaterial } from 'three';
  2. import { getNodesKeys } from '../core/NodeUtils.js';
  3. import ExpressionNode from '../core/ExpressionNode.js';
  4. import {
  5. float, vec3, vec4,
  6. assign, label, mul, add, bypass,
  7. positionLocal, skinning, instance, modelViewProjection, lightContext, 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 { lightNode } = this;
  18. const { diffuseColorNode } = this.generateMain( builder );
  19. const outgoingLightNode = this.generateLight( builder, { diffuseColorNode, lightNode } );
  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, lightNode, lightingModelNode } ) {
  56. // < ANALYTIC LIGHTS >
  57. // OUTGOING LIGHT
  58. let outgoingLightNode = diffuseColorNode.xyz;
  59. if ( lightNode && lightNode.hasLight !== false ) outgoingLightNode = builder.addFlow( 'fragment', label( lightContext( lightNode, lightingModelNode ), 'Light' ) );
  60. // EMISSIVE
  61. if ( this.emissiveNode ) outgoingLightNode = add( vec3( this.emissiveNode ), outgoingLightNode );
  62. return outgoingLightNode;
  63. }
  64. generateOutput( builder, { diffuseColorNode, outgoingLightNode } ) {
  65. // OUTPUT
  66. let outputNode = vec4( outgoingLightNode, diffuseColorNode.a );
  67. // ENCODING
  68. outputNode = colorSpace( outputNode, builder.renderer.outputEncoding );
  69. // FOG
  70. if ( builder.fogNode ) outputNode = builder.fogNode.mix( outputNode );
  71. // RESULT
  72. builder.addFlow( 'fragment', label( outputNode, 'Output' ) );
  73. return outputNode;
  74. }
  75. setDefaultValues( values ) {
  76. // This approach is to reuse the native refreshUniforms*
  77. // and turn available the use of features like transmission and environment in core
  78. for ( const property in values ) {
  79. const value = values[ property ];
  80. if ( this[ property ] === undefined ) {
  81. this[ property ] = value?.clone?.() || value;
  82. }
  83. }
  84. Object.assign( this.defines, values.defines );
  85. }
  86. toJSON( meta ) {
  87. const isRoot = ( meta === undefined || typeof meta === 'string' );
  88. if ( isRoot ) {
  89. meta = {
  90. textures: {},
  91. images: {},
  92. nodes: {}
  93. };
  94. }
  95. const data = Material.prototype.toJSON.call( this, meta );
  96. const nodeKeys = getNodesKeys( this );
  97. data.inputNodes = {};
  98. for ( const name of nodeKeys ) {
  99. data.inputNodes[ name ] = this[ name ].toJSON( meta ).uuid;
  100. }
  101. // TODO: Copied from Object3D.toJSON
  102. function extractFromCache( cache ) {
  103. const values = [];
  104. for ( const key in cache ) {
  105. const data = cache[ key ];
  106. delete data.metadata;
  107. values.push( data );
  108. }
  109. return values;
  110. }
  111. if ( isRoot ) {
  112. const textures = extractFromCache( meta.textures );
  113. const images = extractFromCache( meta.images );
  114. const nodes = extractFromCache( meta.nodes );
  115. if ( textures.length > 0 ) data.textures = textures;
  116. if ( images.length > 0 ) data.images = images;
  117. if ( nodes.length > 0 ) data.nodes = nodes;
  118. }
  119. return data;
  120. }
  121. static fromMaterial( /*material*/ ) { }
  122. }
  123. NodeMaterial.prototype.isNodeMaterial = true;
  124. export default NodeMaterial;