MeshStandardNodeMaterial.js 4.4 KB

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