MeshStandardNodeMaterial.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import NodeMaterial from './NodeMaterial.js';
  2. import {
  3. float, vec3, vec4, normalView, add, context,
  4. assign, label, mul, invert, mix, texture, uniform,
  5. materialRoughness, materialMetalness, materialEmissive
  6. } from '../shadernode/ShaderNodeElements.js';
  7. import LightsNode from '../lighting/LightsNode.js';
  8. import EnvironmentLightNode from '../lighting/EnvironmentLightNode.js';
  9. import AONode from '../lighting/AONode.js';
  10. import getRoughness from '../functions/material/getRoughness.js';
  11. import PhysicalLightingModel from '../functions/PhysicalLightingModel.js';
  12. import NormalMapNode from '../display/NormalMapNode.js';
  13. import { MeshStandardMaterial } from 'three';
  14. const defaultValues = new MeshStandardMaterial();
  15. export default class MeshStandardNodeMaterial extends NodeMaterial {
  16. constructor( parameters ) {
  17. super();
  18. this.colorNode = null;
  19. this.opacityNode = null;
  20. this.alphaTestNode = null;
  21. this.normalNode = null;
  22. this.emissiveNode = null;
  23. this.metalnessNode = null;
  24. this.roughnessNode = null;
  25. this.clearcoatNode = null;
  26. this.clearcoatRoughnessNode = null;
  27. this.envNode = null;
  28. this.lightsNode = null;
  29. this.positionNode = null;
  30. this.setDefaultValues( defaultValues );
  31. this.setValues( parameters );
  32. }
  33. build( builder ) {
  34. let { colorNode, diffuseColorNode } = this.generateMain( builder );
  35. const envNode = this.envNode || builder.scene.environmentNode;
  36. diffuseColorNode = this.generateStandardMaterial( builder, { colorNode, diffuseColorNode } );
  37. if ( this.lightsNode ) builder.lightsNode = this.lightsNode;
  38. let materialLightsNode = [];
  39. if ( envNode ) {
  40. materialLightsNode.push( new EnvironmentLightNode( envNode ) );
  41. }
  42. if ( builder.material.aoMap ) {
  43. materialLightsNode.push( new AONode( texture( builder.material.aoMap ) ) );
  44. }
  45. if ( materialLightsNode.length > 0 ) {
  46. builder.lightsNode = new LightsNode( [ ...builder.lightsNode.lightNodes, ...materialLightsNode ] );
  47. }
  48. const outgoingLightNode = this.generateLight( builder, { diffuseColorNode, lightingModelNode: PhysicalLightingModel } );
  49. this.generateOutput( builder, { diffuseColorNode, outgoingLightNode } );
  50. }
  51. generateStandardMaterial( builder, { colorNode, diffuseColorNode } ) {
  52. const { material } = builder;
  53. // METALNESS
  54. let metalnessNode = this.metalnessNode ? float( this.metalnessNode ) : materialMetalness;
  55. metalnessNode = builder.addFlow( 'fragment', label( metalnessNode, 'Metalness' ) );
  56. builder.addFlow( 'fragment', assign( diffuseColorNode, vec4( mul( diffuseColorNode.rgb, invert( metalnessNode ) ), diffuseColorNode.a ) ) );
  57. // ROUGHNESS
  58. let roughnessNode = this.roughnessNode ? float( this.roughnessNode ) : materialRoughness;
  59. roughnessNode = getRoughness.call( { roughness: roughnessNode } );
  60. builder.addFlow( 'fragment', label( roughnessNode, 'Roughness' ) );
  61. // SPECULAR COLOR
  62. const specularColorNode = mix( vec3( 0.04 ), colorNode.rgb, metalnessNode );
  63. builder.addFlow( 'fragment', label( specularColorNode, 'SpecularColor' ) );
  64. // NORMAL VIEW
  65. const normalNode = this.normalNode ? vec3( this.normalNode ) : ( material.normalMap ? new NormalMapNode( texture( material.normalMap ), uniform( material.normalScale ) ) : normalView );
  66. builder.addFlow( 'fragment', label( normalNode, 'TransformedNormalView' ) );
  67. return diffuseColorNode;
  68. }
  69. generateLight( builder, { diffuseColorNode, lightingModelNode, lightsNode = builder.lightsNode } ) {
  70. const renderer = builder.renderer;
  71. // OUTGOING LIGHT
  72. let outgoingLightNode = super.generateLight( builder, { diffuseColorNode, lightingModelNode, lightsNode } );
  73. // EMISSIVE
  74. outgoingLightNode = add( vec3( this.emissiveNode || materialEmissive ), outgoingLightNode );
  75. // TONE MAPPING
  76. if ( renderer.toneMappingNode ) outgoingLightNode = context( renderer.toneMappingNode, { color: outgoingLightNode } );
  77. return outgoingLightNode;
  78. }
  79. copy( source ) {
  80. this.colorNode = source.colorNode;
  81. this.opacityNode = source.opacityNode;
  82. this.alphaTestNode = source.alphaTestNode;
  83. this.normalNode = source.normalNode;
  84. this.emissiveNode = source.emissiveNode;
  85. this.metalnessNode = source.metalnessNode;
  86. this.roughnessNode = source.roughnessNode;
  87. this.clearcoatNode = source.clearcoatNode;
  88. this.clearcoatRoughnessNode = source.clearcoatRoughnessNode;
  89. this.envNode = source.envNode;
  90. this.lightsNode = source.lightsNode;
  91. this.positionNode = source.positionNode;
  92. return super.copy( source );
  93. }
  94. }
  95. MeshStandardNodeMaterial.prototype.isMeshStandardNodeMaterial = true;