2
0

MeshStandardNodeMaterial.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
  2. import { diffuseColor, metalness, roughness, specularColor } from '../core/PropertyNode.js';
  3. import { mix } from '../math/MathNode.js';
  4. import { materialRoughness, materialMetalness } from '../accessors/MaterialNode.js';
  5. import getRoughness from '../functions/material/getRoughness.js';
  6. import physicalLightingModel from '../functions/PhysicalLightingModel.js';
  7. import { float, vec3, vec4 } from '../shadernode/ShaderNode.js';
  8. import { MeshStandardMaterial } from 'three';
  9. const defaultValues = new MeshStandardMaterial();
  10. class MeshStandardNodeMaterial extends NodeMaterial {
  11. constructor( parameters ) {
  12. super();
  13. this.isMeshStandardNodeMaterial = true;
  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.envNode = null;
  22. this.lightsNode = null;
  23. this.positionNode = null;
  24. this.setDefaultValues( defaultValues );
  25. this.setValues( parameters );
  26. }
  27. constructLightingModel( /*builder*/ ) {
  28. return physicalLightingModel;
  29. }
  30. constructVariants( { stack } ) {
  31. // METALNESS
  32. const metalnessNode = this.metalnessNode ? float( this.metalnessNode ) : materialMetalness;
  33. stack.assign( metalness, metalnessNode );
  34. // ROUGHNESS
  35. let roughnessNode = this.roughnessNode ? float( this.roughnessNode ) : materialRoughness;
  36. roughnessNode = getRoughness.call( { roughness: roughnessNode } );
  37. stack.assign( roughness, roughnessNode );
  38. // SPECULAR COLOR
  39. const specularColorNode = mix( vec3( 0.04 ), diffuseColor.rgb, metalnessNode );
  40. stack.assign( specularColor, specularColorNode );
  41. // DIFFUSE COLOR
  42. stack.assign( diffuseColor, vec4( diffuseColor.rgb.mul( metalnessNode.oneMinus() ), diffuseColor.a ) );
  43. }
  44. copy( source ) {
  45. this.colorNode = source.colorNode;
  46. this.opacityNode = source.opacityNode;
  47. this.alphaTestNode = source.alphaTestNode;
  48. this.normalNode = source.normalNode;
  49. this.emissiveNode = source.emissiveNode;
  50. this.metalnessNode = source.metalnessNode;
  51. this.roughnessNode = source.roughnessNode;
  52. this.envNode = source.envNode;
  53. this.lightsNode = source.lightsNode;
  54. this.positionNode = source.positionNode;
  55. return super.copy( source );
  56. }
  57. }
  58. export default MeshStandardNodeMaterial;
  59. addNodeMaterial( MeshStandardNodeMaterial );