MeshStandardNodeMaterial.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import NodeMaterial, { addNodeMaterial } from './NodeMaterial.js';
  2. import { diffuseColor, metalness, roughness, specularColor, specularF90 } 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.emissiveNode = null;
  15. this.metalnessNode = null;
  16. this.roughnessNode = null;
  17. this.setDefaultValues( defaultValues );
  18. this.setValues( parameters );
  19. }
  20. setupLightingModel( /*builder*/ ) {
  21. return new PhysicalLightingModel();
  22. }
  23. setupSpecular() {
  24. const specularColorNode = mix( vec3( 0.04 ), diffuseColor.rgb, metalness );
  25. specularColor.assign( specularColorNode );
  26. specularF90.assign( 1.0 );
  27. }
  28. setupVariants() {
  29. // METALNESS
  30. const metalnessNode = this.metalnessNode ? float( this.metalnessNode ) : materialMetalness;
  31. metalness.assign( metalnessNode );
  32. // ROUGHNESS
  33. let roughnessNode = this.roughnessNode ? float( this.roughnessNode ) : materialRoughness;
  34. roughnessNode = getRoughness( { roughness: roughnessNode } );
  35. roughness.assign( roughnessNode );
  36. // SPECULAR COLOR
  37. this.setupSpecular();
  38. // DIFFUSE COLOR
  39. diffuseColor.assign( vec4( diffuseColor.rgb.mul( metalnessNode.oneMinus() ), diffuseColor.a ) );
  40. }
  41. copy( source ) {
  42. this.emissiveNode = source.emissiveNode;
  43. this.metalnessNode = source.metalnessNode;
  44. this.roughnessNode = source.roughnessNode;
  45. return super.copy( source );
  46. }
  47. }
  48. export default MeshStandardNodeMaterial;
  49. addNodeMaterial( 'MeshStandardNodeMaterial', MeshStandardNodeMaterial );