MeshStandardNodeMaterial.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import NodeMaterial from './NodeMaterial.js';
  2. import {
  3. float, vec3, vec4, mix,
  4. materialRoughness, materialMetalness, materialColor, diffuseColor,
  5. metalness, roughness, specularColor
  6. } from '../shadernode/ShaderNodeElements.js';
  7. import getRoughness from '../functions/material/getRoughness.js';
  8. import physicalLightingModel from '../functions/PhysicalLightingModel.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.isMeshStandardNodeMaterial = true;
  15. this.colorNode = null;
  16. this.opacityNode = null;
  17. this.alphaTestNode = null;
  18. this.normalNode = null;
  19. this.emissiveNode = null;
  20. this.metalnessNode = null;
  21. this.roughnessNode = null;
  22. this.envNode = null;
  23. this.lightsNode = null;
  24. this.positionNode = null;
  25. this.setDefaultValues( defaultValues );
  26. this.setValues( parameters );
  27. }
  28. constructLightingModel( /*builder*/ ) {
  29. return physicalLightingModel;
  30. }
  31. constructVariants( builder, stack ) {
  32. // METALNESS
  33. const metalnessNode = this.metalnessNode ? float( this.metalnessNode ) : materialMetalness;
  34. stack.assign( metalness, metalnessNode );
  35. stack.assign( diffuseColor, vec4( diffuseColor.rgb.mul( metalnessNode.invert() ), diffuseColor.a ) );
  36. // ROUGHNESS
  37. let roughnessNode = this.roughnessNode ? float( this.roughnessNode ) : materialRoughness;
  38. roughnessNode = getRoughness.call( { roughness: roughnessNode } );
  39. stack.assign( roughness, roughnessNode );
  40. // SPECULAR COLOR
  41. const specularColorNode = mix( vec3( 0.04 ), materialColor.rgb, metalnessNode );
  42. stack.assign( specularColor, specularColorNode );
  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. }