MaterialNode.js 7.1 KB

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