NodeMaterial.js 4.9 KB

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