NodeMaterial.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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, attribute,
  7. positionLocal, skinning, instance, modelViewProjection, lightingContext, colorSpace,
  8. materialAlphaTest, materialColor, materialOpacity, reference, rangeFog, exp2Fog
  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. // VERTEX COLORS
  48. if ( this.vertexColors === true && builder.geometry.hasAttribute( 'color' ) ) {
  49. colorNode = vec4( mul( colorNode.xyz, attribute( 'color' ) ), colorNode.a );
  50. }
  51. // COLOR
  52. colorNode = builder.addFlow( 'fragment', label( colorNode, 'Color' ) );
  53. const diffuseColorNode = builder.addFlow( 'fragment', label( colorNode, 'DiffuseColor' ) );
  54. // OPACITY
  55. opacityNode = builder.addFlow( 'fragment', label( opacityNode, 'OPACITY' ) );
  56. builder.addFlow( 'fragment', assign( diffuseColorNode.a, mul( diffuseColorNode.a, opacityNode ) ) );
  57. // ALPHA TEST
  58. if ( this.alphaTestNode || this.alphaTest > 0 ) {
  59. const alphaTestNode = this.alphaTestNode ? float( this.alphaTestNode ) : materialAlphaTest;
  60. builder.addFlow( 'fragment', label( alphaTestNode, 'AlphaTest' ) );
  61. // @TODO: remove ExpressionNode here and then possibly remove it completely
  62. builder.addFlow( 'fragment', new ExpressionNode( 'if ( DiffuseColor.a <= AlphaTest ) { discard; }' ) );
  63. }
  64. return { colorNode, diffuseColorNode };
  65. }
  66. generateLight( builder, { diffuseColorNode, lightingModelNode, lightsNode = builder.lightsNode } ) {
  67. // < ANALYTIC LIGHTS >
  68. // OUTGOING LIGHT
  69. let outgoingLightNode = diffuseColorNode.xyz;
  70. if ( lightsNode && lightsNode.hasLight !== false ) outgoingLightNode = builder.addFlow( 'fragment', label( lightingContext( lightsNode, lightingModelNode ), 'Light' ) );
  71. return outgoingLightNode;
  72. }
  73. generateOutput( builder, { diffuseColorNode, outgoingLightNode } ) {
  74. // OUTPUT
  75. let outputNode = vec4( outgoingLightNode, diffuseColorNode.a );
  76. // ENCODING
  77. outputNode = colorSpace( outputNode, builder.renderer.outputEncoding );
  78. // FOG
  79. let fogNode = builder.fogNode;
  80. if ( fogNode?.isNode !== true && builder.scene.fog ) {
  81. const fog = builder.scene.fog;
  82. if ( fog.isFogExp2 ) {
  83. fogNode = exp2Fog( reference( 'color', 'color', fog ), reference( 'density', 'float', fog ) );
  84. } else if ( fog.isFog ) {
  85. fogNode = rangeFog( reference( 'color', 'color', fog ), reference( 'near', 'float', fog ), reference( 'far', 'float', fog ) );
  86. } else {
  87. console.error( 'NodeMaterial: Unsupported fog configuration.', fog );
  88. }
  89. }
  90. if ( fogNode ) outputNode = vec4( vec3( fogNode.mix( outputNode ) ), outputNode.w );
  91. // RESULT
  92. builder.addFlow( 'fragment', label( outputNode, 'Output' ) );
  93. return outputNode;
  94. }
  95. setDefaultValues( values ) {
  96. // This approach is to reuse the native refreshUniforms*
  97. // and turn available the use of features like transmission and environment in core
  98. for ( const property in values ) {
  99. const value = values[ property ];
  100. if ( this[ property ] === undefined ) {
  101. this[ property ] = value?.clone?.() || value;
  102. }
  103. }
  104. Object.assign( this.defines, values.defines );
  105. }
  106. toJSON( meta ) {
  107. const isRoot = ( meta === undefined || typeof meta === 'string' );
  108. if ( isRoot ) {
  109. meta = {
  110. textures: {},
  111. images: {},
  112. nodes: {}
  113. };
  114. }
  115. const data = Material.prototype.toJSON.call( this, meta );
  116. const nodeKeys = getNodesKeys( this );
  117. data.inputNodes = {};
  118. for ( const name of nodeKeys ) {
  119. data.inputNodes[ name ] = this[ name ].toJSON( meta ).uuid;
  120. }
  121. // TODO: Copied from Object3D.toJSON
  122. function extractFromCache( cache ) {
  123. const values = [];
  124. for ( const key in cache ) {
  125. const data = cache[ key ];
  126. delete data.metadata;
  127. values.push( data );
  128. }
  129. return values;
  130. }
  131. if ( isRoot ) {
  132. const textures = extractFromCache( meta.textures );
  133. const images = extractFromCache( meta.images );
  134. const nodes = extractFromCache( meta.nodes );
  135. if ( textures.length > 0 ) data.textures = textures;
  136. if ( images.length > 0 ) data.images = images;
  137. if ( nodes.length > 0 ) data.nodes = nodes;
  138. }
  139. return data;
  140. }
  141. static fromMaterial( /*material*/ ) { }
  142. }
  143. export default NodeMaterial;