MaterialNode.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import Node from '../core/Node.js';
  2. import OperatorNode from '../math/OperatorNode.js';
  3. import MaterialReferenceNode from './MaterialReferenceNode.js';
  4. import TextureNode from './TextureNode.js';
  5. import SplitNode from '../utils/SplitNode.js';
  6. class MaterialNode extends Node {
  7. static ALPHA_TEST = 'alphaTest';
  8. static COLOR = 'color';
  9. static OPACITY = 'opacity';
  10. static ROUGHNESS = 'roughness';
  11. static METALNESS = 'metalness';
  12. static EMISSIVE = 'emissive';
  13. constructor( scope = MaterialNode.COLOR ) {
  14. super();
  15. this.scope = scope;
  16. }
  17. getNodeType( builder ) {
  18. const scope = this.scope;
  19. const material = builder.context.material;
  20. if ( scope === MaterialNode.COLOR ) {
  21. return material.map !== null ? 'vec4' : 'vec3';
  22. } else if ( scope === MaterialNode.OPACITY ) {
  23. return 'float';
  24. } else if ( scope === MaterialNode.EMISSIVE ) {
  25. return 'vec3';
  26. } else if ( scope === MaterialNode.ROUGHNESS || scope === MaterialNode.METALNESS ) {
  27. return 'float';
  28. }
  29. }
  30. generate( builder, output ) {
  31. const material = builder.context.material;
  32. const scope = this.scope;
  33. let node = null;
  34. if ( scope === MaterialNode.ALPHA_TEST ) {
  35. node = new MaterialReferenceNode( 'alphaTest', 'float' );
  36. } else if ( scope === MaterialNode.COLOR ) {
  37. const colorNode = new MaterialReferenceNode( 'color', 'color' );
  38. if ( material.map?.isTexture === true ) {
  39. //new MaterialReferenceNode( 'map', 'texture' )
  40. const map = new TextureNode( material.map );
  41. node = new OperatorNode( '*', colorNode, map );
  42. } else {
  43. node = colorNode;
  44. }
  45. } else if ( scope === MaterialNode.OPACITY ) {
  46. const opacityNode = new MaterialReferenceNode( 'opacity', 'float' );
  47. if ( material.alphaMap?.isTexture === true ) {
  48. node = new OperatorNode( '*', opacityNode, new MaterialReferenceNode( 'alphaMap', 'texture' ) );
  49. } else {
  50. node = opacityNode;
  51. }
  52. } else if ( scope === MaterialNode.ROUGHNESS ) {
  53. const roughnessNode = new MaterialReferenceNode( 'roughness', 'float' );
  54. if ( material.roughnessMap?.isTexture === true ) {
  55. node = new OperatorNode( '*', roughnessNode, new SplitNode( new TextureNode( material.roughnessMap ), 'g' ) );
  56. } else {
  57. node = roughnessNode;
  58. }
  59. } else if ( scope === MaterialNode.METALNESS ) {
  60. const metalnessNode = new MaterialReferenceNode( 'metalness', 'float' );
  61. if ( material.metalnessMap?.isTexture === true ) {
  62. node = new OperatorNode( '*', metalnessNode, new SplitNode( new TextureNode( material.metalnessMap ), 'b' ) );
  63. } else {
  64. node = metalnessNode;
  65. }
  66. } else if ( scope === MaterialNode.EMISSIVE ) {
  67. const emissiveNode = new MaterialReferenceNode( 'emissive', 'color' );
  68. if ( material.emissiveMap?.isTexture === true ) {
  69. node = new OperatorNode( '*', emissiveNode, new TextureNode( material.emissiveMap ) );
  70. } else {
  71. node = emissiveNode;
  72. }
  73. } else {
  74. const outputType = this.getNodeType( builder );
  75. node = new MaterialReferenceNode( scope, outputType );
  76. }
  77. return node.build( builder, output );
  78. }
  79. }
  80. export default MaterialNode;