MaterialNode.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import { reference } from './ReferenceNode.js';
  3. import { materialReference } from './MaterialReferenceNode.js';
  4. import { normalView } from './NormalNode.js';
  5. import { nodeImmutable, float, vec2, mat2 } from '../shadernode/ShaderNode.js';
  6. import { uniform } from '../core/UniformNode.js';
  7. import { Vector2 } from 'three';
  8. const _propertyCache = new Map();
  9. class MaterialNode extends Node {
  10. constructor( scope ) {
  11. super();
  12. this.scope = scope;
  13. }
  14. getCache( property, type ) {
  15. let node = _propertyCache.get( property );
  16. if ( node === undefined ) {
  17. node = materialReference( property, type );
  18. _propertyCache.set( property, node );
  19. }
  20. return node;
  21. }
  22. getFloat( property ) {
  23. return this.getCache( property, 'float' );
  24. }
  25. getColor( property ) {
  26. return this.getCache( property, 'color' );
  27. }
  28. getTexture( property ) {
  29. return this.getCache( property === 'map' ? 'map' : property + 'Map', 'texture' );
  30. }
  31. setup( builder ) {
  32. const material = builder.context.material;
  33. const scope = this.scope;
  34. let node = null;
  35. if ( scope === MaterialNode.COLOR ) {
  36. const colorNode = this.getColor( scope );
  37. if ( material.map && material.map.isTexture === true ) {
  38. node = colorNode.mul( this.getTexture( 'map' ) );
  39. } else {
  40. node = colorNode;
  41. }
  42. } else if ( scope === MaterialNode.OPACITY ) {
  43. const opacityNode = this.getFloat( scope );
  44. if ( material.alphaMap && material.alphaMap.isTexture === true ) {
  45. node = opacityNode.mul( this.getTexture( 'alpha' ) );
  46. } else {
  47. node = opacityNode;
  48. }
  49. } else if ( scope === MaterialNode.SPECULAR_STRENGTH ) {
  50. if ( material.specularMap && material.specularMap.isTexture === true ) {
  51. node = this.getTexture( 'specular' ).r;
  52. } else {
  53. node = float( 1 );
  54. }
  55. } else if ( scope === MaterialNode.SPECULAR_INTENSITY ) {
  56. const specularIntensity = this.getFloat( scope );
  57. if ( material.specularMap ) {
  58. node = specularIntensity.mul( this.getTexture( scope ).a );
  59. } else {
  60. node = specularIntensity;
  61. }
  62. } else if ( scope === MaterialNode.ROUGHNESS ) { // TODO: cleanup similar branches
  63. const roughnessNode = this.getFloat( scope );
  64. if ( material.roughnessMap && material.roughnessMap.isTexture === true ) {
  65. node = roughnessNode.mul( this.getTexture( scope ).g );
  66. } else {
  67. node = roughnessNode;
  68. }
  69. } else if ( scope === MaterialNode.METALNESS ) {
  70. const metalnessNode = this.getFloat( scope );
  71. if ( material.metalnessMap && material.metalnessMap.isTexture === true ) {
  72. node = metalnessNode.mul( this.getTexture( scope ).b );
  73. } else {
  74. node = metalnessNode;
  75. }
  76. } else if ( scope === MaterialNode.EMISSIVE ) {
  77. const emissiveNode = this.getColor( scope );
  78. if ( material.emissiveMap && material.emissiveMap.isTexture === true ) {
  79. node = emissiveNode.mul( this.getTexture( scope ) );
  80. } else {
  81. node = emissiveNode;
  82. }
  83. } else if ( scope === MaterialNode.NORMAL ) {
  84. if ( material.normalMap ) {
  85. node = this.getTexture( 'normal' ).normalMap( this.getCache( 'normalScale', 'vec2' ) );
  86. } else if ( material.bumpMap ) {
  87. node = this.getTexture( 'bump' ).r.bumpMap( this.getFloat( 'bumpScale' ) );
  88. } else {
  89. node = normalView;
  90. }
  91. } else if ( scope === MaterialNode.CLEARCOAT ) {
  92. const clearcoatNode = this.getFloat( scope );
  93. if ( material.clearcoatMap && material.clearcoatMap.isTexture === true ) {
  94. node = clearcoatNode.mul( this.getTexture( scope ).r );
  95. } else {
  96. node = clearcoatNode;
  97. }
  98. } else if ( scope === MaterialNode.CLEARCOAT_ROUGHNESS ) {
  99. const clearcoatRoughnessNode = this.getFloat( scope );
  100. if ( material.clearcoatRoughnessMap && material.clearcoatRoughnessMap.isTexture === true ) {
  101. node = clearcoatRoughnessNode.mul( this.getTexture( scope ).r );
  102. } else {
  103. node = clearcoatRoughnessNode;
  104. }
  105. } else if ( scope === MaterialNode.CLEARCOAT_NORMAL ) {
  106. if ( material.clearcoatNormalMap ) {
  107. node = this.getTexture( scope ).normalMap( this.getCache( scope + 'Scale', 'vec2' ) );
  108. } else {
  109. node = normalView;
  110. }
  111. } else if ( scope === MaterialNode.SHEEN ) {
  112. const sheenNode = this.getColor( 'sheenColor' ).mul( this.getFloat( 'sheen' ) ); // Move this mul() to CPU
  113. if ( material.sheenColorMap && material.sheenColorMap.isTexture === true ) {
  114. node = sheenNode.mul( this.getTexture( 'sheenColor' ).rgb );
  115. } else {
  116. node = sheenNode;
  117. }
  118. } else if ( scope === MaterialNode.SHEEN_ROUGHNESS ) {
  119. const sheenRoughnessNode = this.getFloat( scope );
  120. if ( material.sheenRoughnessMap && material.sheenRoughnessMap.isTexture === true ) {
  121. node = sheenRoughnessNode.mul( this.getTexture( scope ).a );
  122. } else {
  123. node = sheenRoughnessNode;
  124. }
  125. node = node.clamp( 0.07, 1.0 );
  126. } else if ( scope === MaterialNode.ANISOTROPY ) {
  127. if ( material.anisotropyMap && material.anisotropyMap.isTexture === true ) {
  128. const anisotropyPolar = this.getTexture( scope );
  129. const anisotropyMat = mat2( materialAnisotropyVector.x, materialAnisotropyVector.y, materialAnisotropyVector.y.negate(), materialAnisotropyVector.x );
  130. node = anisotropyMat.mul( anisotropyPolar.rg.mul( 2.0 ).sub( vec2( 1.0 ) ).normalize().mul( anisotropyPolar.b ) );
  131. } else {
  132. node = materialAnisotropyVector;
  133. }
  134. } else if ( scope === MaterialNode.IRIDESCENCE_THICKNESS ) {
  135. const iridescenceThicknessMaximum = reference( '1', 'float', material.iridescenceThicknessRange );
  136. if ( material.iridescenceThicknessMap ) {
  137. const iridescenceThicknessMinimum = reference( '0', 'float', material.iridescenceThicknessRange );
  138. node = iridescenceThicknessMaximum.sub( iridescenceThicknessMinimum ).mul( this.getTexture( scope ).g ).add( iridescenceThicknessMinimum );
  139. } else {
  140. node = iridescenceThicknessMaximum;
  141. }
  142. } else if ( scope === MaterialNode.TRANSMISSION ) {
  143. const transmissionNode = this.getFloat( scope );
  144. if ( material.transmissionMap ) {
  145. node = transmissionNode.mul( this.getTexture( scope ).r );
  146. } else {
  147. node = transmissionNode;
  148. }
  149. } else if ( scope === MaterialNode.THICKNESS ) {
  150. const thicknessNode = this.getFloat( scope );
  151. if ( material.thicknessMap ) {
  152. node = thicknessNode.mul( this.getTexture( scope ).g );
  153. } else {
  154. node = thicknessNode;
  155. }
  156. } else if ( scope === MaterialNode.IOR ) {
  157. node = this.getFloat( scope );
  158. } else {
  159. const outputType = this.getNodeType( builder );
  160. node = this.getCache( scope, outputType );
  161. }
  162. return node;
  163. }
  164. }
  165. MaterialNode.ALPHA_TEST = 'alphaTest';
  166. MaterialNode.COLOR = 'color';
  167. MaterialNode.OPACITY = 'opacity';
  168. MaterialNode.SHININESS = 'shininess';
  169. MaterialNode.SPECULAR_COLOR = 'specular';
  170. MaterialNode.SPECULAR_STRENGTH = 'specularStrength';
  171. MaterialNode.SPECULAR_INTENSITY = 'specularIntensity';
  172. MaterialNode.SPECULAR_COLOR2 = 'specularColor';
  173. MaterialNode.REFLECTIVITY = 'reflectivity';
  174. MaterialNode.ROUGHNESS = 'roughness';
  175. MaterialNode.METALNESS = 'metalness';
  176. MaterialNode.NORMAL = 'normal';
  177. MaterialNode.CLEARCOAT = 'clearcoat';
  178. MaterialNode.CLEARCOAT_ROUGHNESS = 'clearcoatRoughness';
  179. MaterialNode.CLEARCOAT_NORMAL = 'clearcoatNormal';
  180. MaterialNode.EMISSIVE = 'emissive';
  181. MaterialNode.ROTATION = 'rotation';
  182. MaterialNode.SHEEN = 'sheen';
  183. MaterialNode.SHEEN_ROUGHNESS = 'sheenRoughness';
  184. MaterialNode.ANISOTROPY = 'anisotropy';
  185. MaterialNode.IRIDESCENCE = 'iridescence';
  186. MaterialNode.IRIDESCENCE_IOR = 'iridescenceIOR';
  187. MaterialNode.IRIDESCENCE_THICKNESS = 'iridescenceThickness';
  188. MaterialNode.IOR = 'ior';
  189. MaterialNode.TRANSMISSION = 'transmission';
  190. MaterialNode.THICKNESS = 'thickness';
  191. MaterialNode.ATTENUATION_DISTANCE = 'attenuationDistance';
  192. MaterialNode.ATTENUATION_COLOR = 'attenuationColor';
  193. MaterialNode.LINE_SCALE = 'scale';
  194. MaterialNode.LINE_DASH_SIZE = 'dashSize';
  195. MaterialNode.LINE_GAP_SIZE = 'gapSize';
  196. MaterialNode.LINE_WIDTH = 'linewidth';
  197. MaterialNode.LINE_DASH_OFFSET = 'dashOffset';
  198. MaterialNode.POINT_WIDTH = 'pointWidth';
  199. export default MaterialNode;
  200. export const materialAlphaTest = nodeImmutable( MaterialNode, MaterialNode.ALPHA_TEST );
  201. export const materialColor = nodeImmutable( MaterialNode, MaterialNode.COLOR );
  202. export const materialShininess = nodeImmutable( MaterialNode, MaterialNode.SHININESS );
  203. export const materialEmissive = nodeImmutable( MaterialNode, MaterialNode.EMISSIVE );
  204. export const materialOpacity = nodeImmutable( MaterialNode, MaterialNode.OPACITY );
  205. export const materialSpecularColor = nodeImmutable( MaterialNode, MaterialNode.SPECULAR_COLOR );
  206. export const materialSpecularIntensity = nodeImmutable( MaterialNode, MaterialNode.SPECULAR_INTENSITY );
  207. export const materialSpecularColor2 = nodeImmutable( MaterialNode, MaterialNode.SPECULAR_COLOR2 );
  208. export const materialSpecularStrength = nodeImmutable( MaterialNode, MaterialNode.SPECULAR_STRENGTH );
  209. export const materialReflectivity = nodeImmutable( MaterialNode, MaterialNode.REFLECTIVITY );
  210. export const materialRoughness = nodeImmutable( MaterialNode, MaterialNode.ROUGHNESS );
  211. export const materialMetalness = nodeImmutable( MaterialNode, MaterialNode.METALNESS );
  212. export const materialNormal = nodeImmutable( MaterialNode, MaterialNode.NORMAL );
  213. export const materialClearcoat = nodeImmutable( MaterialNode, MaterialNode.CLEARCOAT );
  214. export const materialClearcoatRoughness = nodeImmutable( MaterialNode, MaterialNode.CLEARCOAT_ROUGHNESS );
  215. export const materialClearcoatNormal = nodeImmutable( MaterialNode, MaterialNode.CLEARCOAT_NORMAL );
  216. export const materialRotation = nodeImmutable( MaterialNode, MaterialNode.ROTATION );
  217. export const materialSheen = nodeImmutable( MaterialNode, MaterialNode.SHEEN );
  218. export const materialSheenRoughness = nodeImmutable( MaterialNode, MaterialNode.SHEEN_ROUGHNESS );
  219. export const materialAnisotropy = nodeImmutable( MaterialNode, MaterialNode.ANISOTROPY );
  220. export const materialIridescence = nodeImmutable( MaterialNode, MaterialNode.IRIDESCENCE );
  221. export const materialIridescenceIOR = nodeImmutable( MaterialNode, MaterialNode.IRIDESCENCE_IOR );
  222. export const materialIridescenceThickness = nodeImmutable( MaterialNode, MaterialNode.IRIDESCENCE_THICKNESS );
  223. export const materialTransmission = nodeImmutable( MaterialNode, MaterialNode.TRANSMISSION );
  224. export const materialThickness = nodeImmutable( MaterialNode, MaterialNode.THICKNESS );
  225. export const materialIOR = nodeImmutable( MaterialNode, MaterialNode.IOR );
  226. export const materialAttenuationDistance = nodeImmutable( MaterialNode, MaterialNode.ATTENUATION_DISTANCE );
  227. export const materialAttenuationColor = nodeImmutable( MaterialNode, MaterialNode.ATTENUATION_COLOR );
  228. export const materialLineScale = nodeImmutable( MaterialNode, MaterialNode.LINE_SCALE );
  229. export const materialLineDashSize = nodeImmutable( MaterialNode, MaterialNode.LINE_DASH_SIZE );
  230. export const materialLineGapSize = nodeImmutable( MaterialNode, MaterialNode.LINE_GAP_SIZE );
  231. export const materialLineWidth = nodeImmutable( MaterialNode, MaterialNode.LINE_WIDTH );
  232. export const materialLineDashOffset = nodeImmutable( MaterialNode, MaterialNode.LINE_DASH_OFFSET );
  233. export const materialPointWidth = nodeImmutable( MaterialNode, MaterialNode.POINT_WIDTH );
  234. export const materialAnisotropyVector = uniform( new Vector2() ).onReference( function ( frame ) {
  235. return frame.material;
  236. } ).onRenderUpdate( function ( { material } ) {
  237. this.value.set( material.anisotropy * Math.cos( material.anisotropyRotation ), material.anisotropy * Math.sin( material.anisotropyRotation ) );
  238. } );
  239. addNodeClass( 'MaterialNode', MaterialNode );