Math1Node.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. function Math1Node( a, method ) {
  6. TempNode.call( this );
  7. this.a = a;
  8. this.method = method || Math1Node.SIN;
  9. };
  10. Math1Node.RAD = 'radians';
  11. Math1Node.DEG = 'degrees';
  12. Math1Node.EXP = 'exp';
  13. Math1Node.EXP2 = 'exp2';
  14. Math1Node.LOG = 'log';
  15. Math1Node.LOG2 = 'log2';
  16. Math1Node.SQRT = 'sqrt';
  17. Math1Node.INV_SQRT = 'inversesqrt';
  18. Math1Node.FLOOR = 'floor';
  19. Math1Node.CEIL = 'ceil';
  20. Math1Node.NORMALIZE = 'normalize';
  21. Math1Node.FRACT = 'fract';
  22. Math1Node.SAT = 'saturate';
  23. Math1Node.SIN = 'sin';
  24. Math1Node.COS = 'cos';
  25. Math1Node.TAN = 'tan';
  26. Math1Node.ASIN = 'asin';
  27. Math1Node.ACOS = 'acos';
  28. Math1Node.ARCTAN = 'atan';
  29. Math1Node.ABS = 'abs';
  30. Math1Node.SIGN = 'sign';
  31. Math1Node.LENGTH = 'length';
  32. Math1Node.NEGATE = 'negate';
  33. Math1Node.INVERT = 'invert';
  34. Math1Node.prototype = Object.create( TempNode.prototype );
  35. Math1Node.prototype.constructor = Math1Node;
  36. Math1Node.prototype.nodeType = "Math1";
  37. Math1Node.prototype.getType = function ( builder ) {
  38. switch ( this.method ) {
  39. case Math1Node.LENGTH:
  40. return 'f';
  41. }
  42. return this.a.getType( builder );
  43. };
  44. Math1Node.prototype.generate = function ( builder, output ) {
  45. var type = this.getType( builder ),
  46. result = this.a.build( builder, type );
  47. switch ( this.method ) {
  48. case Math1Node.NEGATE:
  49. result = '( -' + result + ' )';
  50. break;
  51. case Math1Node.INVERT:
  52. result = '( 1.0 - ' + result + ' )';
  53. break;
  54. default:
  55. result = this.method + '( ' + result + ' )';
  56. break;
  57. }
  58. return builder.format( result, type, output );
  59. };
  60. Math1Node.prototype.copy = function ( source ) {
  61. TempNode.prototype.copy.call( this, source );
  62. this.a = source.a;
  63. this.method = source.method;
  64. };
  65. Math1Node.prototype.toJSON = function ( meta ) {
  66. var data = this.getJSONNode( meta );
  67. if ( ! data ) {
  68. data = this.createJSONNode( meta );
  69. data.a = this.a.toJSON( meta ).uuid;
  70. data.method = this.method;
  71. }
  72. return data;
  73. };
  74. export { Math1Node };