NodeMaterial.js 4.5 KB

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