MaterialNode.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 SHININESS = 'shininess';
  10. constructor( scope = MaterialNode.COLOR ) {
  11. super();
  12. this.scope = scope;
  13. }
  14. getType( builder ) {
  15. const scope = this.scope;
  16. const material = builder.getContextParameter( 'material' );
  17. if ( scope === MaterialNode.COLOR ) {
  18. return material.map !== null ? 'vec4' : 'vec3';
  19. } else if ( scope === MaterialNode.OPACITY ) {
  20. return 'float';
  21. } else if ( scope === MaterialNode.SPECULAR ) {
  22. return 'vec3';
  23. } else if ( scope === MaterialNode.SHININESS ) {
  24. return 'float';
  25. }
  26. }
  27. generate( builder, output ) {
  28. const material = builder.getContextParameter( 'material' );
  29. const scope = this.scope;
  30. let node = null;
  31. if ( scope === MaterialNode.ALPHA_TEST ) {
  32. node = new MaterialReferenceNode( 'alphaTest', 'float' );
  33. } else if ( scope === MaterialNode.COLOR ) {
  34. const colorNode = new MaterialReferenceNode( 'color', 'color' );
  35. if ( material.map !== null && material.map !== undefined && material.map.isTexture === true ) {
  36. node = new OperatorNode( '*', colorNode, new MaterialReferenceNode( 'map', 'texture' ) );
  37. } else {
  38. node = colorNode;
  39. }
  40. } else if ( scope === MaterialNode.OPACITY ) {
  41. const opacityNode = new MaterialReferenceNode( 'opacity', 'float' );
  42. if ( material.alphaMap !== null && material.alphaMap !== undefined && material.alphaMap.isTexture === true ) {
  43. node = new OperatorNode( '*', opacityNode, new MaterialReferenceNode( 'alphaMap', 'texture' ) );
  44. } else {
  45. node = opacityNode;
  46. }
  47. } else if ( scope === MaterialNode.SPECULAR ) {
  48. const specularColorNode = new MaterialReferenceNode( 'specular', 'color' );
  49. if ( material.specularMap !== null && material.specularMap !== undefined && material.specularMap.isTexture === true ) {
  50. node = new OperatorNode( '*', specularColorNode, new MaterialReferenceNode( 'specularMap', 'texture' ) );
  51. } else {
  52. node = specularColorNode;
  53. }
  54. } else if ( scope === MaterialNode.SHININESS ) {
  55. node = new MaterialReferenceNode( 'shininess', 'float' );
  56. }
  57. return node.build( builder, output );
  58. }
  59. }
  60. export default MaterialNode;