MathNode.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import Node from '../core/Node.js';
  2. class MathNode extends Node {
  3. static NORMALIZE = 'normalize';
  4. static NEGATE = 'negate';
  5. static LENGTH = 'length';
  6. constructor( method, a, b = null ) {
  7. super();
  8. this.method = method;
  9. this.a = a;
  10. this.b = b;
  11. }
  12. getInputType( builder ) {
  13. const typeA = this.a.getType( builder );
  14. if ( this.b !== null ) {
  15. const typeB = this.b.getType( builder );
  16. if ( builder.getTypeLength( typeB ) > builder.getTypeLength( typeA ) ) {
  17. // anytype x anytype: use the greater length vector
  18. return typeB;
  19. }
  20. }
  21. return typeA;
  22. }
  23. getType( builder ) {
  24. const method = this.method;
  25. if ( method === MathNode.LENGTH ) {
  26. return 'float';
  27. } else if (
  28. method === MathNode.TRANSFORM_DIRETION ||
  29. method === MathNode.INVERSE_TRANSFORM_DIRETION
  30. ) {
  31. return 'vec3';
  32. } else {
  33. return this.getInputType( builder );
  34. }
  35. }
  36. generate( builder, output ) {
  37. const method = this.method;
  38. const type = this.getInputType( builder );
  39. const a = this.a.build( builder, type );
  40. let b = null;
  41. if ( this.b !== null ) {
  42. b = this.b.build( builder, type );
  43. }
  44. if ( b !== null ) {
  45. return builder.format( `${method}( ${a}, ${b} )`, type, output );
  46. } else {
  47. if ( method === MathNode.NEGATE ) {
  48. return builder.format( `( -${a} )`, type, output );
  49. } else {
  50. return builder.format( `${method}( ${a} )`, type, output );
  51. }
  52. }
  53. }
  54. }
  55. export default MathNode;