MeshPhysicalNodeMaterial.js 7.5 KB

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