Math2Node.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. function Math2Node( a, b, method ) {
  6. TempNode.call( this );
  7. this.a = a;
  8. this.b = b;
  9. this.method = method || Math2Node.DISTANCE;
  10. };
  11. Math2Node.MIN = 'min';
  12. Math2Node.MAX = 'max';
  13. Math2Node.MOD = 'mod';
  14. Math2Node.STEP = 'step';
  15. Math2Node.REFLECT = 'reflect';
  16. Math2Node.DISTANCE = 'distance';
  17. Math2Node.DOT = 'dot';
  18. Math2Node.CROSS = 'cross';
  19. Math2Node.POW = 'pow';
  20. Math2Node.prototype = Object.create( TempNode.prototype );
  21. Math2Node.prototype.constructor = Math2Node;
  22. Math2Node.prototype.nodeType = "Math2";
  23. Math2Node.prototype.getInputType = function ( builder ) {
  24. // use the greater length vector
  25. if ( builder.getTypeLength( this.b.getType( builder ) ) > builder.getTypeLength( this.a.getType( builder ) ) ) {
  26. return this.b.getType( builder );
  27. }
  28. return this.a.getType( builder );
  29. };
  30. Math2Node.prototype.getType = function ( builder ) {
  31. switch ( this.method ) {
  32. case Math2Node.DISTANCE:
  33. case Math2Node.DOT:
  34. return 'f';
  35. case Math2Node.CROSS:
  36. return 'v3';
  37. }
  38. return this.getInputType( builder );
  39. };
  40. Math2Node.prototype.generate = function ( builder, output ) {
  41. var a, b,
  42. type = this.getInputType( builder ),
  43. al = builder.getTypeLength( this.a.getType( builder ) ),
  44. bl = builder.getTypeLength( this.b.getType( builder ) );
  45. // optimzer
  46. switch ( this.method ) {
  47. case Math2Node.CROSS:
  48. a = this.a.build( builder, 'v3' );
  49. b = this.b.build( builder, 'v3' );
  50. break;
  51. case Math2Node.STEP:
  52. a = this.a.build( builder, al === 1 ? 'f' : type );
  53. b = this.b.build( builder, type );
  54. break;
  55. case Math2Node.MIN:
  56. case Math2Node.MAX:
  57. case Math2Node.MOD:
  58. a = this.a.build( builder, type );
  59. b = this.b.build( builder, bl === 1 ? 'f' : type );
  60. break;
  61. default:
  62. a = this.a.build( builder, type );
  63. b = this.b.build( builder, type );
  64. break;
  65. }
  66. return builder.format( this.method + '( ' + a + ', ' + b + ' )', this.getType( builder ), output );
  67. };
  68. Math2Node.prototype.copy = function ( source ) {
  69. TempNode.prototype.copy.call( this, source );
  70. this.a = source.a;
  71. this.b = source.b;
  72. this.method = source.method;
  73. };
  74. Math2Node.prototype.toJSON = function ( meta ) {
  75. var data = this.getJSONNode( meta );
  76. if ( ! data ) {
  77. data = this.createJSONNode( meta );
  78. data.a = this.a.toJSON( meta ).uuid;
  79. data.b = this.b.toJSON( meta ).uuid;
  80. data.method = this.method;
  81. }
  82. return data;
  83. };
  84. export { Math2Node };