MeshStandardNodeMaterial.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import NodeMaterial from './NodeMaterial.js';
  2. import { diffuseColor, metalness, roughness, specularColor, specularF90 } from '../../nodes/core/PropertyNode.js';
  3. import { mix } from '../../nodes/math/MathNode.js';
  4. import { materialRoughness, materialMetalness } from '../../nodes/accessors/MaterialNode.js';
  5. import getRoughness from '../../nodes/functions/material/getRoughness.js';
  6. import PhysicalLightingModel from '../../nodes/functions/PhysicalLightingModel.js';
  7. import EnvironmentNode from '../../nodes/lighting/EnvironmentNode.js';
  8. import { float, vec3, vec4 } from '../../nodes/tsl/TSLBase.js';
  9. import { MeshStandardMaterial } from '../MeshStandardMaterial.js';
  10. const _defaultValues = /*@__PURE__*/ new MeshStandardMaterial();
  11. /**
  12. * Node material version of `MeshStandardMaterial`.
  13. *
  14. * @augments NodeMaterial
  15. */
  16. class MeshStandardNodeMaterial extends NodeMaterial {
  17. static get type() {
  18. return 'MeshStandardNodeMaterial';
  19. }
  20. /**
  21. * Constructs a new mesh standard node material.
  22. *
  23. * @param {?Object} parameters - The configuration parameter.
  24. */
  25. constructor( parameters ) {
  26. super();
  27. /**
  28. * This flag can be used for type testing.
  29. *
  30. * @type {boolean}
  31. * @readonly
  32. * @default true
  33. */
  34. this.isMeshStandardNodeMaterial = true;
  35. /**
  36. * Set to `true` because standard materials react on lights.
  37. *
  38. * @type {boolean}
  39. * @default true
  40. */
  41. this.lights = true;
  42. /**
  43. * The emissive color of standard materials is by default inferred from the `emissive`,
  44. * `emissiveIntensity` and `emissiveMap` properties. This node property allows to
  45. * overwrite the default and define the emissive color with a node instead.
  46. *
  47. * If you don't want to overwrite the emissive color but modify the existing
  48. * value instead, use {@link materialEmissive}.
  49. *
  50. * @type {?Node<vec3>}
  51. * @default null
  52. */
  53. this.emissiveNode = null;
  54. /**
  55. * The metalness of standard materials is by default inferred from the `metalness`,
  56. * and `metalnessMap` properties. This node property allows to
  57. * overwrite the default and define the metalness with a node instead.
  58. *
  59. * If you don't want to overwrite the metalness but modify the existing
  60. * value instead, use {@link materialMetalness}.
  61. *
  62. * @type {?Node<float>}
  63. * @default null
  64. */
  65. this.metalnessNode = null;
  66. /**
  67. * The roughness of standard materials is by default inferred from the `roughness`,
  68. * and `roughnessMap` properties. This node property allows to
  69. * overwrite the default and define the roughness with a node instead.
  70. *
  71. * If you don't want to overwrite the roughness but modify the existing
  72. * value instead, use {@link materialRoughness}.
  73. *
  74. * @type {?Node<float>}
  75. * @default null
  76. */
  77. this.roughnessNode = null;
  78. this.setDefaultValues( _defaultValues );
  79. this.setValues( parameters );
  80. }
  81. /**
  82. * Overwritten since this type of material uses {@link EnvironmentNode}
  83. * to implement the PBR (PMREM based) environment mapping. Besides, the
  84. * method honors `Scene.environment`.
  85. *
  86. * @param {NodeBuilder} builder - The current node builder.
  87. * @return {?EnvironmentNode<vec3>} The environment node.
  88. */
  89. setupEnvironment( builder ) {
  90. let envNode = super.setupEnvironment( builder );
  91. if ( envNode === null && builder.environmentNode ) {
  92. envNode = builder.environmentNode;
  93. }
  94. return envNode ? new EnvironmentNode( envNode ) : null;
  95. }
  96. /**
  97. * Setups the lighting model.
  98. *
  99. * @return {PhysicalLightingModel} The lighting model.
  100. */
  101. setupLightingModel( /*builder*/ ) {
  102. return new PhysicalLightingModel();
  103. }
  104. /**
  105. * Setups the specular related node variables.
  106. */
  107. setupSpecular() {
  108. const specularColorNode = mix( vec3( 0.04 ), diffuseColor.rgb, metalness );
  109. specularColor.assign( specularColorNode );
  110. specularF90.assign( 1.0 );
  111. }
  112. /**
  113. * Setups the standard specific node variables.
  114. *
  115. * @param {NodeBuilder} builder - The current node builder.
  116. */
  117. setupVariants() {
  118. // METALNESS
  119. const metalnessNode = this.metalnessNode ? float( this.metalnessNode ) : materialMetalness;
  120. metalness.assign( metalnessNode );
  121. // ROUGHNESS
  122. let roughnessNode = this.roughnessNode ? float( this.roughnessNode ) : materialRoughness;
  123. roughnessNode = getRoughness( { roughness: roughnessNode } );
  124. roughness.assign( roughnessNode );
  125. // SPECULAR COLOR
  126. this.setupSpecular();
  127. // DIFFUSE COLOR
  128. diffuseColor.assign( vec4( diffuseColor.rgb.mul( metalnessNode.oneMinus() ), diffuseColor.a ) );
  129. }
  130. copy( source ) {
  131. this.emissiveNode = source.emissiveNode;
  132. this.metalnessNode = source.metalnessNode;
  133. this.roughnessNode = source.roughnessNode;
  134. return super.copy( source );
  135. }
  136. }
  137. export default MeshStandardNodeMaterial;
粤ICP备19079148号