MaterialNode.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.SPECULAR_STRENGTH ) {
  60. const specularNode = this.getColor( 'specular' );
  61. if ( material.specularMap && material.specularMap.isTexture === true ) {
  62. node = specularNode.mul( this.getTexture( 'specularMap' ).r );
  63. } else {
  64. node = specularNode;
  65. }
  66. } else if ( scope === MaterialNode.REFLECTIVITY ) {
  67. node = this.getFloat( 'reflectivity' );
  68. } else if ( scope === MaterialNode.ROUGHNESS ) {
  69. const roughnessNode = this.getFloat( 'roughness' );
  70. if ( material.roughnessMap && material.roughnessMap.isTexture === true ) {
  71. node = roughnessNode.mul( this.getTexture( 'roughnessMap' ).g );
  72. } else {
  73. node = roughnessNode;
  74. }
  75. } else if ( scope === MaterialNode.METALNESS ) {
  76. const metalnessNode = this.getFloat( 'metalness' );
  77. if ( material.metalnessMap && material.metalnessMap.isTexture === true ) {
  78. node = metalnessNode.mul( this.getTexture( 'metalnessMap' ).b );
  79. } else {
  80. node = metalnessNode;
  81. }
  82. } else if ( scope === MaterialNode.EMISSIVE ) {
  83. const emissiveNode = this.getColor( 'emissive' );
  84. if ( material.emissiveMap && material.emissiveMap.isTexture === true ) {
  85. node = emissiveNode.mul( this.getTexture( 'emissiveMap' ) );
  86. } else {
  87. node = emissiveNode;
  88. }
  89. } else if ( scope === MaterialNode.CLEARCOAT ) {
  90. const clearcoatNode = this.getFloat( 'clearcoat' );
  91. if ( material.clearcoatMap && material.clearcoatMap.isTexture === true ) {
  92. node = clearcoatNode.mul( this.getTexture( 'clearcoatMap' ).r );
  93. } else {
  94. node = clearcoatNode;
  95. }
  96. } else if ( scope === MaterialNode.CLEARCOAT_ROUGHNESS ) {
  97. const clearcoatRoughnessNode = this.getFloat( 'clearcoatRoughness' );
  98. if ( material.clearcoatRoughnessMap && material.clearcoatRoughnessMap.isTexture === true ) {
  99. node = clearcoatRoughnessNode.mul( this.getTexture( 'clearcoatRoughnessMap' ).r );
  100. } else {
  101. node = clearcoatRoughnessNode;
  102. }
  103. } else if ( scope === MaterialNode.SHEEN ) {
  104. const sheenNode = this.getColor( 'sheenColor' ).mul( this.getFloat( 'sheen' ) ); // Move this mul() to CPU
  105. if ( material.sheenColorMap && material.sheenColorMap.isTexture === true ) {
  106. node = sheenNode.mul( this.getTexture( 'sheenColorMap' ).rgb );
  107. } else {
  108. node = sheenNode;
  109. }
  110. } else if ( scope === MaterialNode.SHEEN_ROUGHNESS ) {
  111. const sheenRoughnessNode = this.getFloat( 'sheenRoughness' );
  112. if ( material.sheenRoughnessMap && material.sheenRoughnessMap.isTexture === true ) {
  113. node = sheenRoughnessNode.mul( this.getTexture( 'sheenRoughnessMap' ).a );
  114. } else {
  115. node = sheenRoughnessNode;
  116. }
  117. node = node.clamp( 0.07, 1.0 );
  118. } else if ( scope === MaterialNode.ROTATION ) {
  119. node = this.getFloat( 'rotation' );
  120. } else {
  121. const outputType = this.getNodeType( builder );
  122. node = materialReference( scope, outputType );
  123. }
  124. return node;
  125. }
  126. }
  127. MaterialNode.ALPHA_TEST = 'alphaTest';
  128. MaterialNode.COLOR = 'color';
  129. MaterialNode.OPACITY = 'opacity';
  130. MaterialNode.SHININESS = 'shininess';
  131. MaterialNode.SPECULAR = 'specular';
  132. MaterialNode.SPECULAR_STRENGTH = 'specularStrength';
  133. MaterialNode.REFLECTIVITY = 'reflectivity';
  134. MaterialNode.ROUGHNESS = 'roughness';
  135. MaterialNode.METALNESS = 'metalness';
  136. MaterialNode.CLEARCOAT = 'clearcoat';
  137. MaterialNode.CLEARCOAT_ROUGHNESS = 'clearcoatRoughness';
  138. MaterialNode.EMISSIVE = 'emissive';
  139. MaterialNode.ROTATION = 'rotation';
  140. MaterialNode.SHEEN = 'sheen';
  141. MaterialNode.SHEEN_ROUGHNESS = 'sheenRoughness';
  142. export default MaterialNode;
  143. export const materialAlphaTest = nodeImmutable( MaterialNode, MaterialNode.ALPHA_TEST );
  144. export const materialColor = nodeImmutable( MaterialNode, MaterialNode.COLOR );
  145. export const materialShininess = nodeImmutable( MaterialNode, MaterialNode.SHININESS );
  146. export const materialEmissive = nodeImmutable( MaterialNode, MaterialNode.EMISSIVE );
  147. export const materialOpacity = nodeImmutable( MaterialNode, MaterialNode.OPACITY );
  148. export const materialSpecularColor = nodeImmutable( MaterialNode, MaterialNode.SPECULAR_COLOR );
  149. export const materialSpecularStrength = nodeImmutable( MaterialNode, MaterialNode.SPECULAR_STRENGTH );
  150. export const materialReflectivity = nodeImmutable( MaterialNode, MaterialNode.REFLECTIVITY );
  151. export const materialRoughness = nodeImmutable( MaterialNode, MaterialNode.ROUGHNESS );
  152. export const materialMetalness = nodeImmutable( MaterialNode, MaterialNode.METALNESS );
  153. export const materialClearcoat = nodeImmutable( MaterialNode, MaterialNode.CLEARCOAT );
  154. export const materialClearcoatRoughness = nodeImmutable( MaterialNode, MaterialNode.CLEARCOAT_ROUGHNESS );
  155. export const materialRotation = nodeImmutable( MaterialNode, MaterialNode.ROTATION );
  156. export const materialSheen = nodeImmutable( MaterialNode, MaterialNode.SHEEN );
  157. export const materialSheenRoughness = nodeImmutable( MaterialNode, MaterialNode.SHEEN_ROUGHNESS );
  158. addNodeClass( MaterialNode );