NodeMaterial.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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
  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. if ( builder.fogNode ) outputNode = vec4( vec3( builder.fogNode.mix( outputNode ) ), outputNode.w );
  80. // RESULT
  81. builder.addFlow( 'fragment', label( outputNode, 'Output' ) );
  82. return outputNode;
  83. }
  84. setDefaultValues( values ) {
  85. // This approach is to reuse the native refreshUniforms*
  86. // and turn available the use of features like transmission and environment in core
  87. for ( const property in values ) {
  88. const value = values[ property ];
  89. if ( this[ property ] === undefined ) {
  90. this[ property ] = value?.clone?.() || value;
  91. }
  92. }
  93. Object.assign( this.defines, values.defines );
  94. }
  95. toJSON( meta ) {
  96. const isRoot = ( meta === undefined || typeof meta === 'string' );
  97. if ( isRoot ) {
  98. meta = {
  99. textures: {},
  100. images: {},
  101. nodes: {}
  102. };
  103. }
  104. const data = Material.prototype.toJSON.call( this, meta );
  105. const nodeKeys = getNodesKeys( this );
  106. data.inputNodes = {};
  107. for ( const name of nodeKeys ) {
  108. data.inputNodes[ name ] = this[ name ].toJSON( meta ).uuid;
  109. }
  110. // TODO: Copied from Object3D.toJSON
  111. function extractFromCache( cache ) {
  112. const values = [];
  113. for ( const key in cache ) {
  114. const data = cache[ key ];
  115. delete data.metadata;
  116. values.push( data );
  117. }
  118. return values;
  119. }
  120. if ( isRoot ) {
  121. const textures = extractFromCache( meta.textures );
  122. const images = extractFromCache( meta.images );
  123. const nodes = extractFromCache( meta.nodes );
  124. if ( textures.length > 0 ) data.textures = textures;
  125. if ( images.length > 0 ) data.images = images;
  126. if ( nodes.length > 0 ) data.nodes = nodes;
  127. }
  128. return data;
  129. }
  130. static fromMaterial( /*material*/ ) { }
  131. }
  132. export default NodeMaterial;