MeshPhysicalNodeMaterial.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { addNodeMaterial } from './NodeMaterial.js';
  2. import { transformedClearcoatNormalView } from '../accessors/NormalNode.js';
  3. import { roughness, clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness, anisotropy, alphaT, anisotropyT, anisotropyB } from '../core/PropertyNode.js';
  4. import { materialAnisotropy, materialClearcoat, materialClearcoatRoughness, materialClearcoatNormal, materialSheen, materialSheenRoughness, materialIridescence, materialIridescenceIOR, materialIridescenceThickness } from '../accessors/MaterialNode.js';
  5. import { float, vec2, vec3, If } from '../shadernode/ShaderNode.js';
  6. import { TBNViewMatrix } from '../accessors/AccessorsUtils.js';
  7. import PhysicalLightingModel from '../functions/PhysicalLightingModel.js';
  8. import MeshStandardNodeMaterial from './MeshStandardNodeMaterial.js';
  9. import { MeshPhysicalMaterial } from 'three';
  10. const defaultValues = new MeshPhysicalMaterial();
  11. class MeshPhysicalNodeMaterial extends MeshStandardNodeMaterial {
  12. constructor( parameters ) {
  13. super();
  14. this.isMeshPhysicalNodeMaterial = true;
  15. this.clearcoatNode = null;
  16. this.clearcoatRoughnessNode = null;
  17. this.clearcoatNormalNode = null;
  18. this.sheenNode = null;
  19. this.sheenRoughnessNode = null;
  20. this.iridescenceNode = null;
  21. this.iridescenceIORNode = null;
  22. this.iridescenceThicknessNode = null;
  23. this.specularIntensityNode = null;
  24. this.specularColorNode = null;
  25. this.transmissionNode = null;
  26. this.thicknessNode = null;
  27. this.attenuationDistanceNode = null;
  28. this.attenuationColorNode = null;
  29. this.anisotropyNode = null;
  30. this.setDefaultValues( defaultValues );
  31. this.setValues( parameters );
  32. }
  33. get useClearcoat() {
  34. return this.clearcoat > 0 || this.clearcoatNode !== null;
  35. }
  36. get useIridescence() {
  37. return this.iridescence > 0 || this.iridescenceNode !== null;
  38. }
  39. get useSheen() {
  40. return this.sheen > 0 || this.sheenNode !== null;
  41. }
  42. get useAnisotropy() {
  43. return this.anisotropy > 0 || this.anisotropyNode !== null;
  44. }
  45. setupLightingModel( /*builder*/ ) {
  46. return new PhysicalLightingModel( this.useClearcoat, this.useSheen, this.useIridescence, this.useAnisotropy );
  47. }
  48. setupVariants( builder ) {
  49. super.setupVariants( builder );
  50. // CLEARCOAT
  51. if ( this.useClearcoat ) {
  52. const clearcoatNode = this.clearcoatNode ? float( this.clearcoatNode ) : materialClearcoat;
  53. const clearcoatRoughnessNode = this.clearcoatRoughnessNode ? float( this.clearcoatRoughnessNode ) : materialClearcoatRoughness;
  54. clearcoat.assign( clearcoatNode );
  55. clearcoatRoughness.assign( clearcoatRoughnessNode );
  56. }
  57. // SHEEN
  58. if ( this.useSheen ) {
  59. const sheenNode = this.sheenNode ? vec3( this.sheenNode ) : materialSheen;
  60. const sheenRoughnessNode = this.sheenRoughnessNode ? float( this.sheenRoughnessNode ) : materialSheenRoughness;
  61. sheen.assign( sheenNode );
  62. sheenRoughness.assign( sheenRoughnessNode );
  63. }
  64. // IRIDESCENCE
  65. if ( this.useIridescence ) {
  66. const iridescenceNode = this.iridescenceNode ? float( this.iridescenceNode ) : materialIridescence;
  67. const iridescenceIORNode = this.iridescenceIORNode ? float( this.iridescenceIORNode ) : materialIridescenceIOR;
  68. const iridescenceThicknessNode = this.iridescenceThicknessNode ? float( this.iridescenceThicknessNode ) : materialIridescenceThickness;
  69. iridescence.assign( iridescenceNode );
  70. iridescenceIOR.assign( iridescenceIORNode );
  71. iridescenceThickness.assign( iridescenceThicknessNode );
  72. }
  73. // ANISOTROPY
  74. if ( this.useAnisotropy ) {
  75. const anisotropyV = ( this.anisotropyNode ? vec2( this.anisotropyNode ) : materialAnisotropy ).toVar();
  76. anisotropy.assign( anisotropyV.length() );
  77. If( anisotropy.equal( 0.0 ), () => {
  78. anisotropyV.assign( vec2( 1.0, 0.0 ) );
  79. } ).else( () => {
  80. anisotropyV.divAssign( anisotropy );
  81. anisotropy.assign( anisotropy.saturate() );
  82. } );
  83. // Roughness along the anisotropy bitangent is the material roughness, while the tangent roughness increases with anisotropy.
  84. alphaT.assign( anisotropy.pow2().mix( roughness.pow2(), 1.0 ) );
  85. anisotropyT.assign( TBNViewMatrix[ 0 ].mul( anisotropyV.x ).add( TBNViewMatrix[ 1 ].mul( anisotropyV.y ) ) );
  86. anisotropyB.assign( TBNViewMatrix[ 1 ].mul( anisotropyV.x ).sub( TBNViewMatrix[ 0 ].mul( anisotropyV.y ) ) );
  87. }
  88. }
  89. setupNormal( builder ) {
  90. super.setupNormal( builder );
  91. // CLEARCOAT NORMAL
  92. const clearcoatNormalNode = this.clearcoatNormalNode ? vec3( this.clearcoatNormalNode ) : materialClearcoatNormal;
  93. transformedClearcoatNormalView.assign( clearcoatNormalNode );
  94. }
  95. copy( source ) {
  96. this.clearcoatNode = source.clearcoatNode;
  97. this.clearcoatRoughnessNode = source.clearcoatRoughnessNode;
  98. this.clearcoatNormalNode = source.clearcoatNormalNode;
  99. this.sheenNode = source.sheenNode;
  100. this.sheenRoughnessNode = source.sheenRoughnessNode;
  101. this.iridescenceNode = source.iridescenceNode;
  102. this.iridescenceIORNode = source.iridescenceIORNode;
  103. this.iridescenceThicknessNode = source.iridescenceThicknessNode;
  104. this.specularIntensityNode = source.specularIntensityNode;
  105. this.specularColorNode = source.specularColorNode;
  106. this.transmissionNode = source.transmissionNode;
  107. this.thicknessNode = source.thicknessNode;
  108. this.attenuationDistanceNode = source.attenuationDistanceNode;
  109. this.attenuationColorNode = source.attenuationColorNode;
  110. this.anisotropyNode = source.anisotropyNode;
  111. return super.copy( source );
  112. }
  113. }
  114. export default MeshPhysicalNodeMaterial;
  115. addNodeMaterial( 'MeshPhysicalNodeMaterial', MeshPhysicalNodeMaterial );