MaterialNode.js 2.3 KB

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