MeshPhysicalNodeMaterial.js 7.4 KB

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