MaterialNode.js 3.2 KB

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