MeshPhysicalNodeMaterial.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. import { clearcoat, clearcoatRoughness, sheen, sheenRoughness, iridescence, iridescenceIOR, iridescenceThickness, specularColor, specularColorBlended, specularF90, diffuseColor, metalness, roughness, anisotropy, alphaT, anisotropyT, anisotropyB, ior, transmission, thickness, attenuationDistance, attenuationColor, dispersion } from '../../nodes/core/PropertyNode.js';
  2. import { materialClearcoat, materialClearcoatRoughness, materialClearcoatNormal, materialSheen, materialSheenRoughness, materialIridescence, materialIridescenceIOR, materialIridescenceThickness, materialSpecularIntensity, materialSpecularColor, materialAnisotropy, materialIOR, materialTransmission, materialThickness, materialAttenuationDistance, materialAttenuationColor, materialDispersion } from '../../nodes/accessors/MaterialNode.js';
  3. import { float, vec2, vec3, If } from '../../nodes/tsl/TSLBase.js';
  4. import getRoughness from '../../nodes/functions/material/getRoughness.js';
  5. import { TBNViewMatrix } from '../../nodes/accessors/AccessorsUtils.js';
  6. import PhysicalLightingModel from '../../nodes/functions/PhysicalLightingModel.js';
  7. import MeshStandardNodeMaterial from './MeshStandardNodeMaterial.js';
  8. import { mix, pow2, min } from '../../nodes/math/MathNode.js';
  9. import { subBuild } from '../../nodes/core/SubBuildNode.js';
  10. import { MeshPhysicalMaterial } from '../MeshPhysicalMaterial.js';
  11. const _defaultValues = /*@__PURE__*/ new MeshPhysicalMaterial();
  12. /**
  13. * Node material version of {@link MeshPhysicalMaterial}.
  14. *
  15. * @augments MeshStandardNodeMaterial
  16. */
  17. class MeshPhysicalNodeMaterial extends MeshStandardNodeMaterial {
  18. static get type() {
  19. return 'MeshPhysicalNodeMaterial';
  20. }
  21. /**
  22. * Constructs a new mesh physical node material.
  23. *
  24. * @param {Object} [parameters] - The configuration parameter.
  25. */
  26. constructor( parameters ) {
  27. super();
  28. /**
  29. * This flag can be used for type testing.
  30. *
  31. * @type {boolean}
  32. * @readonly
  33. * @default true
  34. */
  35. this.isMeshPhysicalNodeMaterial = true;
  36. /**
  37. * The clearcoat of physical materials is by default inferred from the `clearcoat`
  38. * and `clearcoatMap` properties. This node property allows to overwrite the default
  39. * and define the clearcoat with a node instead.
  40. *
  41. * If you don't want to overwrite the clearcoat but modify the existing
  42. * value instead, use {@link materialClearcoat}.
  43. *
  44. * @type {?Node<float>}
  45. * @default null
  46. */
  47. this.clearcoatNode = null;
  48. /**
  49. * The clearcoat roughness of physical materials is by default inferred from the `clearcoatRoughness`
  50. * and `clearcoatRoughnessMap` properties. This node property allows to overwrite the default
  51. * and define the clearcoat roughness with a node instead.
  52. *
  53. * If you don't want to overwrite the clearcoat roughness but modify the existing
  54. * value instead, use {@link materialClearcoatRoughness}.
  55. *
  56. * @type {?Node<float>}
  57. * @default null
  58. */
  59. this.clearcoatRoughnessNode = null;
  60. /**
  61. * The clearcoat normal of physical materials is by default inferred from the `clearcoatNormalMap`
  62. * property. This node property allows to overwrite the default
  63. * and define the clearcoat normal with a node instead.
  64. *
  65. * If you don't want to overwrite the clearcoat normal but modify the existing
  66. * value instead, use {@link materialClearcoatNormal}.
  67. *
  68. * @type {?Node<vec3>}
  69. * @default null
  70. */
  71. this.clearcoatNormalNode = null;
  72. /**
  73. * The sheen of physical materials is by default inferred from the `sheen`, `sheenColor`
  74. * and `sheenColorMap` properties. This node property allows to overwrite the default
  75. * and define the sheen with a node instead.
  76. *
  77. * If you don't want to overwrite the sheen but modify the existing
  78. * value instead, use {@link materialSheen}.
  79. *
  80. * @type {?Node<vec3>}
  81. * @default null
  82. */
  83. this.sheenNode = null;
  84. /**
  85. * The sheen roughness of physical materials is by default inferred from the `sheenRoughness` and
  86. * `sheenRoughnessMap` properties. This node property allows to overwrite the default
  87. * and define the sheen roughness with a node instead.
  88. *
  89. * If you don't want to overwrite the sheen roughness but modify the existing
  90. * value instead, use {@link materialSheenRoughness}.
  91. *
  92. * @type {?Node<float>}
  93. * @default null
  94. */
  95. this.sheenRoughnessNode = null;
  96. /**
  97. * The iridescence of physical materials is by default inferred from the `iridescence`
  98. * property. This node property allows to overwrite the default
  99. * and define the iridescence with a node instead.
  100. *
  101. * If you don't want to overwrite the iridescence but modify the existing
  102. * value instead, use {@link materialIridescence}.
  103. *
  104. * @type {?Node<float>}
  105. * @default null
  106. */
  107. this.iridescenceNode = null;
  108. /**
  109. * The iridescence IOR of physical materials is by default inferred from the `iridescenceIOR`
  110. * property. This node property allows to overwrite the default
  111. * and define the iridescence IOR with a node instead.
  112. *
  113. * If you don't want to overwrite the iridescence IOR but modify the existing
  114. * value instead, use {@link materialIridescenceIOR}.
  115. *
  116. * @type {?Node<float>}
  117. * @default null
  118. */
  119. this.iridescenceIORNode = null;
  120. /**
  121. * The iridescence thickness of physical materials is by default inferred from the `iridescenceThicknessRange`
  122. * and `iridescenceThicknessMap` properties. This node property allows to overwrite the default
  123. * and define the iridescence thickness with a node instead.
  124. *
  125. * If you don't want to overwrite the iridescence thickness but modify the existing
  126. * value instead, use {@link materialIridescenceThickness}.
  127. *
  128. * @type {?Node<float>}
  129. * @default null
  130. */
  131. this.iridescenceThicknessNode = null;
  132. /**
  133. * The specular intensity of physical materials is by default inferred from the `specularIntensity`
  134. * and `specularIntensityMap` properties. This node property allows to overwrite the default
  135. * and define the specular intensity with a node instead.
  136. *
  137. * If you don't want to overwrite the specular intensity but modify the existing
  138. * value instead, use {@link materialSpecularIntensity}.
  139. *
  140. * @type {?Node<float>}
  141. * @default null
  142. */
  143. this.specularIntensityNode = null;
  144. /**
  145. * The specular color of physical materials is by default inferred from the `specularColor`
  146. * and `specularColorMap` properties. This node property allows to overwrite the default
  147. * and define the specular color with a node instead.
  148. *
  149. * If you don't want to overwrite the specular color but modify the existing
  150. * value instead, use {@link materialSpecularColor}.
  151. *
  152. * @type {?Node<vec3>}
  153. * @default null
  154. */
  155. this.specularColorNode = null;
  156. /**
  157. * The ior of physical materials is by default inferred from the `ior`
  158. * property. This node property allows to overwrite the default
  159. * and define the ior with a node instead.
  160. *
  161. * If you don't want to overwrite the ior but modify the existing
  162. * value instead, use {@link materialIOR}.
  163. *
  164. * @type {?Node<float>}
  165. * @default null
  166. */
  167. this.iorNode = null;
  168. /**
  169. * The transmission of physical materials is by default inferred from the `transmission` and
  170. * `transmissionMap` properties. This node property allows to overwrite the default
  171. * and define the transmission with a node instead.
  172. *
  173. * If you don't want to overwrite the transmission but modify the existing
  174. * value instead, use {@link materialTransmission}.
  175. *
  176. * @type {?Node<float>}
  177. * @default null
  178. */
  179. this.transmissionNode = null;
  180. /**
  181. * The thickness of physical materials is by default inferred from the `thickness` and
  182. * `thicknessMap` properties. This node property allows to overwrite the default
  183. * and define the thickness with a node instead.
  184. *
  185. * If you don't want to overwrite the thickness but modify the existing
  186. * value instead, use {@link materialThickness}.
  187. *
  188. * @type {?Node<float>}
  189. * @default null
  190. */
  191. this.thicknessNode = null;
  192. /**
  193. * The attenuation distance of physical materials is by default inferred from the
  194. * `attenuationDistance` property. This node property allows to overwrite the default
  195. * and define the attenuation distance with a node instead.
  196. *
  197. * If you don't want to overwrite the attenuation distance but modify the existing
  198. * value instead, use {@link materialAttenuationDistance}.
  199. *
  200. * @type {?Node<float>}
  201. * @default null
  202. */
  203. this.attenuationDistanceNode = null;
  204. /**
  205. * The attenuation color of physical materials is by default inferred from the
  206. * `attenuationColor` property. This node property allows to overwrite the default
  207. * and define the attenuation color with a node instead.
  208. *
  209. * If you don't want to overwrite the attenuation color but modify the existing
  210. * value instead, use {@link materialAttenuationColor}.
  211. *
  212. * @type {?Node<vec3>}
  213. * @default null
  214. */
  215. this.attenuationColorNode = null;
  216. /**
  217. * The dispersion of physical materials is by default inferred from the
  218. * `dispersion` property. This node property allows to overwrite the default
  219. * and define the dispersion with a node instead.
  220. *
  221. * If you don't want to overwrite the dispersion but modify the existing
  222. * value instead, use {@link materialDispersion}.
  223. *
  224. * @type {?Node<float>}
  225. * @default null
  226. */
  227. this.dispersionNode = null;
  228. /**
  229. * The anisotropy of physical materials is by default inferred from the
  230. * `anisotropy` property. This node property allows to overwrite the default
  231. * and define the anisotropy with a node instead.
  232. *
  233. * If you don't want to overwrite the anisotropy but modify the existing
  234. * value instead, use {@link materialAnisotropy}.
  235. *
  236. * @type {?Node<float>}
  237. * @default null
  238. */
  239. this.anisotropyNode = null;
  240. this.setDefaultValues( _defaultValues );
  241. this.setValues( parameters );
  242. }
  243. /**
  244. * Whether the lighting model should use clearcoat or not.
  245. *
  246. * @type {boolean}
  247. * @default true
  248. */
  249. get useClearcoat() {
  250. return this.clearcoat > 0 || this.clearcoatNode !== null;
  251. }
  252. /**
  253. * Whether the lighting model should use iridescence or not.
  254. *
  255. * @type {boolean}
  256. * @default true
  257. */
  258. get useIridescence() {
  259. return this.iridescence > 0 || this.iridescenceNode !== null;
  260. }
  261. /**
  262. * Whether the lighting model should use sheen or not.
  263. *
  264. * @type {boolean}
  265. * @default true
  266. */
  267. get useSheen() {
  268. return this.sheen > 0 || this.sheenNode !== null;
  269. }
  270. /**
  271. * Whether the lighting model should use anisotropy or not.
  272. *
  273. * @type {boolean}
  274. * @default true
  275. */
  276. get useAnisotropy() {
  277. return this.anisotropy > 0 || this.anisotropyNode !== null;
  278. }
  279. /**
  280. * Whether the lighting model should use transmission or not.
  281. *
  282. * @type {boolean}
  283. * @default true
  284. */
  285. get useTransmission() {
  286. return this.transmission > 0 || this.transmissionNode !== null;
  287. }
  288. /**
  289. * Whether the lighting model should use dispersion or not.
  290. *
  291. * @type {boolean}
  292. * @default true
  293. */
  294. get useDispersion() {
  295. return this.dispersion > 0 || this.dispersionNode !== null;
  296. }
  297. /**
  298. * Setups the specular related node variables.
  299. */
  300. setupSpecular() {
  301. const iorNode = this.iorNode ? float( this.iorNode ) : materialIOR;
  302. ior.assign( iorNode );
  303. specularColor.assign( min( pow2( ior.sub( 1.0 ).div( ior.add( 1.0 ) ) ).mul( materialSpecularColor ), vec3( 1.0 ) ).mul( materialSpecularIntensity ) );
  304. specularColorBlended.assign( mix( specularColor, diffuseColor.rgb, metalness ) );
  305. specularF90.assign( mix( materialSpecularIntensity, 1.0, metalness ) );
  306. }
  307. /**
  308. * Setups the lighting model.
  309. *
  310. * @return {PhysicalLightingModel} The lighting model.
  311. */
  312. setupLightingModel( /*builder*/ ) {
  313. return new PhysicalLightingModel( this.useClearcoat, this.useSheen, this.useIridescence, this.useAnisotropy, this.useTransmission, this.useDispersion );
  314. }
  315. /**
  316. * Setups the physical specific node variables.
  317. *
  318. * @param {NodeBuilder} builder - The current node builder.
  319. */
  320. setupVariants( builder ) {
  321. super.setupVariants( builder );
  322. // CLEARCOAT
  323. if ( this.useClearcoat ) {
  324. const clearcoatNode = this.clearcoatNode ? float( this.clearcoatNode ) : materialClearcoat;
  325. const clearcoatRoughnessNode = this.clearcoatRoughnessNode ? float( this.clearcoatRoughnessNode ) : materialClearcoatRoughness;
  326. clearcoat.assign( clearcoatNode );
  327. clearcoatRoughness.assign( getRoughness( { roughness: clearcoatRoughnessNode } ) );
  328. }
  329. // SHEEN
  330. if ( this.useSheen ) {
  331. const sheenNode = this.sheenNode ? vec3( this.sheenNode ) : materialSheen;
  332. const sheenRoughnessNode = this.sheenRoughnessNode ? float( this.sheenRoughnessNode ) : materialSheenRoughness;
  333. sheen.assign( sheenNode );
  334. sheenRoughness.assign( sheenRoughnessNode );
  335. }
  336. // IRIDESCENCE
  337. if ( this.useIridescence ) {
  338. const iridescenceNode = this.iridescenceNode ? float( this.iridescenceNode ) : materialIridescence;
  339. const iridescenceIORNode = this.iridescenceIORNode ? float( this.iridescenceIORNode ) : materialIridescenceIOR;
  340. const iridescenceThicknessNode = this.iridescenceThicknessNode ? float( this.iridescenceThicknessNode ) : materialIridescenceThickness;
  341. iridescence.assign( iridescenceNode );
  342. iridescenceIOR.assign( iridescenceIORNode );
  343. iridescenceThickness.assign( iridescenceThicknessNode );
  344. }
  345. // ANISOTROPY
  346. if ( this.useAnisotropy ) {
  347. const anisotropyV = ( this.anisotropyNode ? vec2( this.anisotropyNode ) : materialAnisotropy ).toVar();
  348. anisotropy.assign( anisotropyV.length() );
  349. If( anisotropy.equal( 0.0 ), () => {
  350. anisotropyV.assign( vec2( 1.0, 0.0 ) );
  351. } ).Else( () => {
  352. anisotropyV.divAssign( vec2( anisotropy ) );
  353. anisotropy.assign( anisotropy.saturate() );
  354. } );
  355. // Roughness along the anisotropy bitangent is the material roughness, while the tangent roughness increases with anisotropy.
  356. alphaT.assign( anisotropy.pow2().mix( roughness.pow2(), 1.0 ) );
  357. anisotropyT.assign( TBNViewMatrix[ 0 ].mul( anisotropyV.x ).add( TBNViewMatrix[ 1 ].mul( anisotropyV.y ) ) );
  358. anisotropyB.assign( TBNViewMatrix[ 1 ].mul( anisotropyV.x ).sub( TBNViewMatrix[ 0 ].mul( anisotropyV.y ) ) );
  359. }
  360. // TRANSMISSION
  361. if ( this.useTransmission ) {
  362. const transmissionNode = this.transmissionNode ? float( this.transmissionNode ) : materialTransmission;
  363. const thicknessNode = this.thicknessNode ? float( this.thicknessNode ) : materialThickness;
  364. const attenuationDistanceNode = this.attenuationDistanceNode ? float( this.attenuationDistanceNode ) : materialAttenuationDistance;
  365. const attenuationColorNode = this.attenuationColorNode ? vec3( this.attenuationColorNode ) : materialAttenuationColor;
  366. transmission.assign( transmissionNode );
  367. thickness.assign( thicknessNode );
  368. attenuationDistance.assign( attenuationDistanceNode );
  369. attenuationColor.assign( attenuationColorNode );
  370. if ( this.useDispersion ) {
  371. const dispersionNode = this.dispersionNode ? float( this.dispersionNode ) : materialDispersion;
  372. dispersion.assign( dispersionNode );
  373. }
  374. }
  375. }
  376. /**
  377. * Setups the clearcoat normal node.
  378. *
  379. * @return {Node<vec3>} The clearcoat normal.
  380. */
  381. setupClearcoatNormal() {
  382. return this.clearcoatNormalNode ? vec3( this.clearcoatNormalNode ) : materialClearcoatNormal;
  383. }
  384. setup( builder ) {
  385. builder.context.setupClearcoatNormal = () => subBuild( this.setupClearcoatNormal( builder ), 'NORMAL', 'vec3' );
  386. super.setup( builder );
  387. }
  388. copy( source ) {
  389. this.clearcoatNode = source.clearcoatNode;
  390. this.clearcoatRoughnessNode = source.clearcoatRoughnessNode;
  391. this.clearcoatNormalNode = source.clearcoatNormalNode;
  392. this.sheenNode = source.sheenNode;
  393. this.sheenRoughnessNode = source.sheenRoughnessNode;
  394. this.iridescenceNode = source.iridescenceNode;
  395. this.iridescenceIORNode = source.iridescenceIORNode;
  396. this.iridescenceThicknessNode = source.iridescenceThicknessNode;
  397. this.specularIntensityNode = source.specularIntensityNode;
  398. this.specularColorNode = source.specularColorNode;
  399. this.iorNode = source.iorNode;
  400. this.transmissionNode = source.transmissionNode;
  401. this.thicknessNode = source.thicknessNode;
  402. this.attenuationDistanceNode = source.attenuationDistanceNode;
  403. this.attenuationColorNode = source.attenuationColorNode;
  404. this.dispersionNode = source.dispersionNode;
  405. this.anisotropyNode = source.anisotropyNode;
  406. return super.copy( source );
  407. }
  408. }
  409. export default MeshPhysicalNodeMaterial;
粤ICP备19079148号