MeshPhysicalNodeMaterial.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { addNodeMaterial } from './NodeMaterial.js';
  2. import { transformedClearcoatNormalView } from '../accessors/NormalNode.js';
  3. import { clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness, specularColor, specularF90, diffuseColor, metalness, roughness, anisotropy, alphaT, anisotropyT, anisotropyB, ior, transmission, thickness, attenuationDistance, attenuationColor } from '../core/PropertyNode.js';
  4. import { materialClearcoat, materialClearcoatRoughness, materialClearcoatNormal, materialSheen, materialSheenRoughness, materialIridescence, materialIridescenceIOR, materialIridescenceThickness, materialSpecularIntensity, materialSpecularColor, materialAnisotropy, materialIOR, materialTransmission, materialThickness, materialAttenuationDistance, materialAttenuationColor } 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 { mix, pow2, min } from '../math/MathNode.js';
  10. import { MeshPhysicalMaterial } from 'three';
  11. const defaultValues = new MeshPhysicalMaterial();
  12. class MeshPhysicalNodeMaterial extends MeshStandardNodeMaterial {
  13. constructor( parameters ) {
  14. super();
  15. this.isMeshPhysicalNodeMaterial = true;
  16. this.clearcoatNode = null;
  17. this.clearcoatRoughnessNode = null;
  18. this.clearcoatNormalNode = null;
  19. this.sheenNode = null;
  20. this.sheenRoughnessNode = null;
  21. this.iridescenceNode = null;
  22. this.iridescenceIORNode = null;
  23. this.iridescenceThicknessNode = null;
  24. this.specularIntensityNode = null;
  25. this.specularColorNode = null;
  26. this.iorNode = null;
  27. this.transmissionNode = null;
  28. this.thicknessNode = null;
  29. this.attenuationDistanceNode = null;
  30. this.attenuationColorNode = null;
  31. this.anisotropyNode = null;
  32. this.setDefaultValues( defaultValues );
  33. this.setValues( parameters );
  34. }
  35. get useClearcoat() {
  36. return this.clearcoat > 0 || this.clearcoatNode !== null;
  37. }
  38. get useIridescence() {
  39. return this.iridescence > 0 || this.iridescenceNode !== null;
  40. }
  41. get useSheen() {
  42. return this.sheen > 0 || this.sheenNode !== null;
  43. }
  44. get useAnisotropy() {
  45. return this.anisotropy > 0 || this.anisotropyNode !== null;
  46. }
  47. get useTransmission() {
  48. return this.transmission > 0 || this.transmissionNode !== null;
  49. }
  50. setupSpecular() {
  51. const iorNode = this.iorNode ? float( this.iorNode ) : materialIOR;
  52. ior.assign( iorNode );
  53. specularColor.assign( mix( min( pow2( ior.sub( 1.0 ).div( ior.add( 1.0 ) ) ).mul( materialSpecularColor ), vec3( 1.0 ) ).mul( materialSpecularIntensity ), diffuseColor.rgb, metalness ) );
  54. specularF90.assign( mix( materialSpecularIntensity, 1.0, metalness ) );
  55. }
  56. setupLightingModel( /*builder*/ ) {
  57. return new PhysicalLightingModel( this.useClearcoat, this.useSheen, this.useIridescence, this.useAnisotropy, this.useTransmission );
  58. }
  59. setupVariants( builder ) {
  60. super.setupVariants( builder );
  61. // CLEARCOAT
  62. if ( this.useClearcoat ) {
  63. const clearcoatNode = this.clearcoatNode ? float( this.clearcoatNode ) : materialClearcoat;
  64. const clearcoatRoughnessNode = this.clearcoatRoughnessNode ? float( this.clearcoatRoughnessNode ) : materialClearcoatRoughness;
  65. clearcoat.assign( clearcoatNode );
  66. clearcoatRoughness.assign( clearcoatRoughnessNode );
  67. }
  68. // SHEEN
  69. if ( this.useSheen ) {
  70. const sheenNode = this.sheenNode ? vec3( this.sheenNode ) : materialSheen;
  71. const sheenRoughnessNode = this.sheenRoughnessNode ? float( this.sheenRoughnessNode ) : materialSheenRoughness;
  72. sheen.assign( sheenNode );
  73. sheenRoughness.assign( sheenRoughnessNode );
  74. }
  75. // IRIDESCENCE
  76. if ( this.useIridescence ) {
  77. const iridescenceNode = this.iridescenceNode ? float( this.iridescenceNode ) : materialIridescence;
  78. const iridescenceIORNode = this.iridescenceIORNode ? float( this.iridescenceIORNode ) : materialIridescenceIOR;
  79. const iridescenceThicknessNode = this.iridescenceThicknessNode ? float( this.iridescenceThicknessNode ) : materialIridescenceThickness;
  80. iridescence.assign( iridescenceNode );
  81. iridescenceIOR.assign( iridescenceIORNode );
  82. iridescenceThickness.assign( iridescenceThicknessNode );
  83. }
  84. // ANISOTROPY
  85. if ( this.useAnisotropy ) {
  86. const anisotropyV = ( this.anisotropyNode ? vec2( this.anisotropyNode ) : materialAnisotropy ).toVar();
  87. anisotropy.assign( anisotropyV.length() );
  88. If( anisotropy.equal( 0.0 ), () => {
  89. anisotropyV.assign( vec2( 1.0, 0.0 ) );
  90. } ).else( () => {
  91. anisotropyV.divAssign( anisotropy );
  92. anisotropy.assign( anisotropy.saturate() );
  93. } );
  94. // Roughness along the anisotropy bitangent is the material roughness, while the tangent roughness increases with anisotropy.
  95. alphaT.assign( anisotropy.pow2().mix( roughness.pow2(), 1.0 ) );
  96. anisotropyT.assign( TBNViewMatrix[ 0 ].mul( anisotropyV.x ).add( TBNViewMatrix[ 1 ].mul( anisotropyV.y ) ) );
  97. anisotropyB.assign( TBNViewMatrix[ 1 ].mul( anisotropyV.x ).sub( TBNViewMatrix[ 0 ].mul( anisotropyV.y ) ) );
  98. }
  99. // TRANSMISSION
  100. if ( this.useTransmission ) {
  101. const transmissionNode = this.transmissionNode ? float( this.transmissionNode ) : materialTransmission;
  102. const thicknessNode = this.thicknessNode ? float( this.thicknessNode ) : materialThickness;
  103. const attenuationDistanceNode = this.attenuationDistanceNode ? float( this.attenuationDistanceNode ) : materialAttenuationDistance;
  104. const attenuationColorNode = this.attenuationColorNode ? vec3( this.attenuationColorNode ) : materialAttenuationColor;
  105. transmission.assign( transmissionNode );
  106. thickness.assign( thicknessNode );
  107. attenuationDistance.assign( attenuationDistanceNode );
  108. attenuationColor.assign( attenuationColorNode );
  109. }
  110. }
  111. setupNormal( builder ) {
  112. super.setupNormal( builder );
  113. // CLEARCOAT NORMAL
  114. const clearcoatNormalNode = this.clearcoatNormalNode ? vec3( this.clearcoatNormalNode ) : materialClearcoatNormal;
  115. transformedClearcoatNormalView.assign( clearcoatNormalNode );
  116. }
  117. copy( source ) {
  118. this.clearcoatNode = source.clearcoatNode;
  119. this.clearcoatRoughnessNode = source.clearcoatRoughnessNode;
  120. this.clearcoatNormalNode = source.clearcoatNormalNode;
  121. this.sheenNode = source.sheenNode;
  122. this.sheenRoughnessNode = source.sheenRoughnessNode;
  123. this.iridescenceNode = source.iridescenceNode;
  124. this.iridescenceIORNode = source.iridescenceIORNode;
  125. this.iridescenceThicknessNode = source.iridescenceThicknessNode;
  126. this.specularIntensityNode = source.specularIntensityNode;
  127. this.specularColorNode = source.specularColorNode;
  128. this.transmissionNode = source.transmissionNode;
  129. this.thicknessNode = source.thicknessNode;
  130. this.attenuationDistanceNode = source.attenuationDistanceNode;
  131. this.attenuationColorNode = source.attenuationColorNode;
  132. this.anisotropyNode = source.anisotropyNode;
  133. return super.copy( source );
  134. }
  135. }
  136. export default MeshPhysicalNodeMaterial;
  137. addNodeMaterial( 'MeshPhysicalNodeMaterial', MeshPhysicalNodeMaterial );