MaterialNode.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import { reference } from './ReferenceNode.js';
  3. import { materialReference } from './MaterialReferenceNode.js';
  4. import { nodeImmutable, float } from '../shadernode/ShaderNode.js';
  5. class MaterialNode extends Node {
  6. constructor( scope ) {
  7. super();
  8. this.scope = scope;
  9. }
  10. getFloat( property ) {
  11. //@TODO: Check if it can be cached by property name.
  12. return materialReference( property, 'float' );
  13. }
  14. getColor( property ) {
  15. //@TODO: Check if it can be cached by property name.
  16. return materialReference( property, 'color' );
  17. }
  18. getTexture( property ) {
  19. //@TODO: Check if it can be cached by property name.
  20. const textureRefNode = materialReference( property, 'texture' );
  21. return textureRefNode;
  22. }
  23. construct( builder ) {
  24. const material = builder.context.material;
  25. const scope = this.scope;
  26. let node = null;
  27. if ( scope === MaterialNode.ALPHA_TEST || scope === MaterialNode.SHININESS || scope === MaterialNode.REFLECTIVITY || scope === MaterialNode.ROTATION || scope === MaterialNode.IRIDESCENCE || scope === MaterialNode.IRIDESCENCE_IOR ) {
  28. node = this.getFloat( scope );
  29. } else if ( scope === MaterialNode.SPECULAR_COLOR ) {
  30. node = this.getColor( 'specular' );
  31. } else if ( scope === MaterialNode.COLOR ) {
  32. const colorNode = this.getColor( 'color' );
  33. if ( material.map && material.map.isTexture === true ) {
  34. node = colorNode.mul( this.getTexture( 'map' ) );
  35. } else {
  36. node = colorNode;
  37. }
  38. } else if ( scope === MaterialNode.OPACITY ) {
  39. const opacityNode = this.getFloat( 'opacity' );
  40. if ( material.alphaMap && material.alphaMap.isTexture === true ) {
  41. node = opacityNode.mul( this.getTexture( 'alphaMap' ) );
  42. } else {
  43. node = opacityNode;
  44. }
  45. } else if ( scope === MaterialNode.SPECULAR_STRENGTH ) {
  46. if ( material.specularMap && material.specularMap.isTexture === true ) {
  47. node = this.getTexture( 'specularMap' ).r;
  48. } else {
  49. node = float( 1 );
  50. }
  51. } else if ( scope === MaterialNode.ROUGHNESS ) {
  52. const roughnessNode = this.getFloat( 'roughness' );
  53. if ( material.roughnessMap && material.roughnessMap.isTexture === true ) {
  54. node = roughnessNode.mul( this.getTexture( 'roughnessMap' ).g );
  55. } else {
  56. node = roughnessNode;
  57. }
  58. } else if ( scope === MaterialNode.METALNESS ) {
  59. const metalnessNode = this.getFloat( 'metalness' );
  60. if ( material.metalnessMap && material.metalnessMap.isTexture === true ) {
  61. node = metalnessNode.mul( this.getTexture( 'metalnessMap' ).b );
  62. } else {
  63. node = metalnessNode;
  64. }
  65. } else if ( scope === MaterialNode.EMISSIVE ) {
  66. const emissiveNode = this.getColor( 'emissive' );
  67. if ( material.emissiveMap && material.emissiveMap.isTexture === true ) {
  68. node = emissiveNode.mul( this.getTexture( 'emissiveMap' ) );
  69. } else {
  70. node = emissiveNode;
  71. }
  72. } else if ( scope === MaterialNode.CLEARCOAT ) {
  73. const clearcoatNode = this.getFloat( 'clearcoat' );
  74. if ( material.clearcoatMap && material.clearcoatMap.isTexture === true ) {
  75. node = clearcoatNode.mul( this.getTexture( 'clearcoatMap' ).r );
  76. } else {
  77. node = clearcoatNode;
  78. }
  79. } else if ( scope === MaterialNode.CLEARCOAT_ROUGHNESS ) {
  80. const clearcoatRoughnessNode = this.getFloat( 'clearcoatRoughness' );
  81. if ( material.clearcoatRoughnessMap && material.clearcoatRoughnessMap.isTexture === true ) {
  82. node = clearcoatRoughnessNode.mul( this.getTexture( 'clearcoatRoughnessMap' ).r );
  83. } else {
  84. node = clearcoatRoughnessNode;
  85. }
  86. } else if ( scope === MaterialNode.SHEEN ) {
  87. const sheenNode = this.getColor( 'sheenColor' ).mul( this.getFloat( 'sheen' ) ); // Move this mul() to CPU
  88. if ( material.sheenColorMap && material.sheenColorMap.isTexture === true ) {
  89. node = sheenNode.mul( this.getTexture( 'sheenColorMap' ).rgb );
  90. } else {
  91. node = sheenNode;
  92. }
  93. } else if ( scope === MaterialNode.SHEEN_ROUGHNESS ) {
  94. const sheenRoughnessNode = this.getFloat( 'sheenRoughness' );
  95. if ( material.sheenRoughnessMap && material.sheenRoughnessMap.isTexture === true ) {
  96. node = sheenRoughnessNode.mul( this.getTexture( 'sheenRoughnessMap' ).a );
  97. } else {
  98. node = sheenRoughnessNode;
  99. }
  100. node = node.clamp( 0.07, 1.0 );
  101. } else if ( scope === MaterialNode.IRIDESCENCE_THICKNESS ) {
  102. const iridescenceThicknessMaximum = reference( 1, 'float', material.iridescenceThicknessRange );
  103. if ( material.iridescenceThicknessMap ) {
  104. const iridescenceThicknessMinimum = reference( 0, 'float', material.iridescenceThicknessRange );
  105. node = iridescenceThicknessMaximum.sub( iridescenceThicknessMinimum ).mul( this.getTexture( 'iridescenceThicknessMap' ).g ).add( iridescenceThicknessMinimum );
  106. } else {
  107. node = iridescenceThicknessMaximum;
  108. }
  109. } else {
  110. const outputType = this.getNodeType( builder );
  111. node = materialReference( scope, outputType );
  112. }
  113. return node;
  114. }
  115. }
  116. MaterialNode.ALPHA_TEST = 'alphaTest';
  117. MaterialNode.COLOR = 'color';
  118. MaterialNode.OPACITY = 'opacity';
  119. MaterialNode.SHININESS = 'shininess';
  120. MaterialNode.SPECULAR = 'specular';
  121. MaterialNode.SPECULAR_STRENGTH = 'specularStrength';
  122. MaterialNode.REFLECTIVITY = 'reflectivity';
  123. MaterialNode.ROUGHNESS = 'roughness';
  124. MaterialNode.METALNESS = 'metalness';
  125. MaterialNode.CLEARCOAT = 'clearcoat';
  126. MaterialNode.CLEARCOAT_ROUGHNESS = 'clearcoatRoughness';
  127. MaterialNode.EMISSIVE = 'emissive';
  128. MaterialNode.ROTATION = 'rotation';
  129. MaterialNode.SHEEN = 'sheen';
  130. MaterialNode.SHEEN_ROUGHNESS = 'sheenRoughness';
  131. MaterialNode.IRIDESCENCE = 'iridescence';
  132. MaterialNode.IRIDESCENCE_IOR = 'iridescenceIOR';
  133. MaterialNode.IRIDESCENCE_THICKNESS = 'iridescenceThickness';
  134. export default MaterialNode;
  135. export const materialAlphaTest = nodeImmutable( MaterialNode, MaterialNode.ALPHA_TEST );
  136. export const materialColor = nodeImmutable( MaterialNode, MaterialNode.COLOR );
  137. export const materialShininess = nodeImmutable( MaterialNode, MaterialNode.SHININESS );
  138. export const materialEmissive = nodeImmutable( MaterialNode, MaterialNode.EMISSIVE );
  139. export const materialOpacity = nodeImmutable( MaterialNode, MaterialNode.OPACITY );
  140. export const materialSpecularColor = nodeImmutable( MaterialNode, MaterialNode.SPECULAR_COLOR );
  141. export const materialSpecularStrength = nodeImmutable( MaterialNode, MaterialNode.SPECULAR_STRENGTH );
  142. export const materialReflectivity = nodeImmutable( MaterialNode, MaterialNode.REFLECTIVITY );
  143. export const materialRoughness = nodeImmutable( MaterialNode, MaterialNode.ROUGHNESS );
  144. export const materialMetalness = nodeImmutable( MaterialNode, MaterialNode.METALNESS );
  145. export const materialClearcoat = nodeImmutable( MaterialNode, MaterialNode.CLEARCOAT );
  146. export const materialClearcoatRoughness = nodeImmutable( MaterialNode, MaterialNode.CLEARCOAT_ROUGHNESS );
  147. export const materialRotation = nodeImmutable( MaterialNode, MaterialNode.ROTATION );
  148. export const materialSheen = nodeImmutable( MaterialNode, MaterialNode.SHEEN );
  149. export const materialSheenRoughness = nodeImmutable( MaterialNode, MaterialNode.SHEEN_ROUGHNESS );
  150. export const materialIridescence = nodeImmutable( MaterialNode, MaterialNode.IRIDESCENCE );
  151. export const materialIridescenceIOR = nodeImmutable( MaterialNode, MaterialNode.IRIDESCENCE_IOR );
  152. export const materialIridescenceThickness = nodeImmutable( MaterialNode, MaterialNode.IRIDESCENCE_THICKNESS );
  153. addNodeClass( MaterialNode );