MaterialNode.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import Node from '../core/Node.js';
  2. import OperatorNode from '../math/OperatorNode.js';
  3. import MaterialReferenceNode from './MaterialReferenceNode.js';
  4. class MaterialNode extends Node {
  5. static ALPHA_TEST = 'alphaTest';
  6. static COLOR = 'color';
  7. static OPACITY = 'opacity';
  8. static SPECULAR = 'specular';
  9. static ROUGHNESS = 'roughness';
  10. static METALNESS = 'metalness';
  11. constructor( scope = MaterialNode.COLOR ) {
  12. super();
  13. this.scope = scope;
  14. }
  15. getNodeType( builder ) {
  16. const scope = this.scope;
  17. const material = builder.context.material;
  18. if ( scope === MaterialNode.COLOR ) {
  19. return material.map !== null ? 'vec4' : 'vec3';
  20. } else if ( scope === MaterialNode.OPACITY ) {
  21. return 'float';
  22. } else if ( scope === MaterialNode.SPECULAR ) {
  23. return 'vec3';
  24. } else if ( scope === MaterialNode.ROUGHNESS || scope === MaterialNode.METALNESS ) {
  25. return 'float';
  26. }
  27. }
  28. generate( builder, output ) {
  29. const material = builder.context.material;
  30. const scope = this.scope;
  31. let node = null;
  32. if ( scope === MaterialNode.ALPHA_TEST ) {
  33. node = new MaterialReferenceNode( 'alphaTest', 'float' );
  34. } else if ( scope === MaterialNode.COLOR ) {
  35. const colorNode = new MaterialReferenceNode( 'color', 'color' );
  36. if ( material.map !== null && material.map !== undefined && material.map.isTexture === true ) {
  37. node = new OperatorNode( '*', colorNode, new MaterialReferenceNode( 'map', 'texture' ) );
  38. } else {
  39. node = colorNode;
  40. }
  41. } else if ( scope === MaterialNode.OPACITY ) {
  42. const opacityNode = new MaterialReferenceNode( 'opacity', 'float' );
  43. if ( material.alphaMap !== null && material.alphaMap !== undefined && material.alphaMap.isTexture === true ) {
  44. node = new OperatorNode( '*', opacityNode, new MaterialReferenceNode( 'alphaMap', 'texture' ) );
  45. } else {
  46. node = opacityNode;
  47. }
  48. } else if ( scope === MaterialNode.SPECULAR ) {
  49. const specularColorNode = new MaterialReferenceNode( 'specularColor', 'color' );
  50. if ( material.specularColorMap !== null && material.specularColorMap !== undefined && material.specularColorMap.isTexture === true ) {
  51. node = new OperatorNode( '*', specularColorNode, new MaterialReferenceNode( 'specularColorMap', 'texture' ) );
  52. } else {
  53. node = specularColorNode;
  54. }
  55. } else {
  56. const outputType = this.getNodeType( builder );
  57. node = new MaterialReferenceNode( scope, outputType );
  58. }
  59. return node.build( builder, output );
  60. }
  61. }
  62. export default MaterialNode;