MeshStandardNodeMaterial.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import NodeMaterial from './NodeMaterial.js';
  2. import {
  3. float, vec3, vec4,
  4. assign, label, mul, invert, mix,
  5. normalView,
  6. materialRoughness, materialMetalness
  7. } from '../shadernode/ShaderNodeElements.js';
  8. import { getRoughness } from '../functions/PhysicalMaterialFunctions.js';
  9. import { MeshStandardMaterial } from 'three';
  10. const defaultValues = new MeshStandardMaterial();
  11. export default class MeshStandardNodeMaterial extends NodeMaterial {
  12. constructor( parameters ) {
  13. super();
  14. this.colorNode = null;
  15. this.opacityNode = null;
  16. this.alphaTestNode = null;
  17. this.normalNode = null;
  18. this.emissiveNode = null;
  19. this.metalnessNode = null;
  20. this.roughnessNode = null;
  21. this.clearcoatNode = null;
  22. this.clearcoatRoughnessNode = null;
  23. this.envNode = null;
  24. this.lightNode = null;
  25. this.positionNode = null;
  26. this.setDefaultValues( defaultValues );
  27. this.setValues( parameters );
  28. }
  29. build( builder ) {
  30. const lightNode = this.lightNode || builder.lightNode; // use scene lights
  31. let { colorNode, diffuseColorNode } = this.generateMain( builder );
  32. diffuseColorNode = this.generateStandardMaterial( builder, { colorNode, diffuseColorNode } );
  33. this.generateLight( builder, diffuseColorNode, lightNode );
  34. }
  35. generateStandardMaterial( builder, { colorNode, diffuseColorNode } ) {
  36. // METALNESS
  37. let metalnessNode = this.metalnessNode ? float( this.metalnessNode ) : materialMetalness;
  38. metalnessNode = builder.addFlow( 'fragment', label( metalnessNode, 'Metalness' ) );
  39. builder.addFlow( 'fragment', assign( diffuseColorNode, vec4( mul( diffuseColorNode.rgb, invert( metalnessNode ) ), diffuseColorNode.a ) ) );
  40. // ROUGHNESS
  41. let roughnessNode = this.roughnessNode ? float( this.roughnessNode ) : materialRoughness;
  42. roughnessNode = getRoughness( { roughness: roughnessNode } );
  43. builder.addFlow( 'fragment', label( roughnessNode, 'Roughness' ) );
  44. // SPECULAR COLOR
  45. const specularColorNode = mix( vec3( 0.04 ), colorNode.rgb, metalnessNode );
  46. builder.addFlow( 'fragment', label( specularColorNode, 'SpecularColor' ) );
  47. // NORMAL VIEW
  48. const normalNode = this.normalNode ? vec3( this.normalNode ) : normalView;
  49. builder.addFlow( 'fragment', label( normalNode, 'TransformedNormalView' ) );
  50. return diffuseColorNode;
  51. }
  52. copy( source ) {
  53. this.colorNode = source.colorNode;
  54. this.opacityNode = source.opacityNode;
  55. this.alphaTestNode = source.alphaTestNode;
  56. this.normalNode = source.normalNode;
  57. this.emissiveNode = source.emissiveNode;
  58. this.metalnessNode = source.metalnessNode;
  59. this.roughnessNode = source.roughnessNode;
  60. this.clearcoatNode = source.clearcoatNode;
  61. this.clearcoatRoughnessNode = source.clearcoatRoughnessNode;
  62. this.envNode = source.envNode;
  63. this.lightNode = source.lightNode;
  64. this.positionNode = source.positionNode;
  65. return super.copy( source );
  66. }
  67. }
  68. MeshStandardNodeMaterial.prototype.isMeshStandardNodeMaterial = true;
粤ICP备19079148号