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