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