MathNode.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import Node from '../core/Node.js';
  2. class MathNode extends Node {
  3. static NORMALIZE = 'normalize';
  4. static INVERSE_TRANSFORM_DIRETION = 'inverseTransformDirection';
  5. constructor( method, a, b = null ) {
  6. super();
  7. this.method = method;
  8. this.a = a;
  9. this.b = b;
  10. }
  11. getType( builder ) {
  12. const method = this.method;
  13. if ( method === MathNode.INVERSE_TRANSFORM_DIRETION ) {
  14. return 'vec3';
  15. } else {
  16. const typeA = this.a.getType( builder );
  17. if ( this.b !== null ) {
  18. if ( builder.getTypeLength( typeB ) > builder.getTypeLength( typeA ) ) {
  19. // anytype x anytype: use the greater length vector
  20. return typeB;
  21. }
  22. }
  23. return typeA;
  24. }
  25. }
  26. generate( builder, output ) {
  27. const method = this.method;
  28. const type = this.getType( builder );
  29. let a = null, b = null;
  30. if ( method === MathNode.INVERSE_TRANSFORM_DIRETION ) {
  31. a = this.a.build( builder, 'vec3' );
  32. b = this.b.build( builder, 'mat4' );
  33. // add in FunctionNode later
  34. return `normalize( ( vec4( ${a}, 0.0 ) * ${b} ).xyz )`;
  35. } else {
  36. a = this.a.build( builder, type );
  37. if ( this.b !== null ) {
  38. b = this.b.build( builder, type );
  39. }
  40. }
  41. if ( b !== null ) {
  42. return builder.format( `${method}( ${a}, ${b} )`, type, output );
  43. } else {
  44. return builder.format( `${method}( ${a} )`, type, output );
  45. }
  46. }
  47. }
  48. export default MathNode;