MaterialNode.js 6.9 KB

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