MathNode.js 1.4 KB

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