MathNode.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import TempNode from '../core/Node.js';
  2. class MathNode extends TempNode {
  3. // 1 input
  4. static RAD = 'radians';
  5. static DEG = 'degrees';
  6. static EXP = 'exp';
  7. static EXP2 = 'exp2';
  8. static LOG = 'log';
  9. static LOG2 = 'log2';
  10. static SQRT = 'sqrt';
  11. static INV_SQRT = 'inversesqrt';
  12. static FLOOR = 'floor';
  13. static CEIL = 'ceil';
  14. static NORMALIZE = 'normalize';
  15. static FRACT = 'fract';
  16. static SATURATE = 'saturate';
  17. static SIN = 'sin';
  18. static COS = 'cos';
  19. static TAN = 'tan';
  20. static ASIN = 'asin';
  21. static ACOS = 'acos';
  22. static ATAN = 'atan';
  23. static ABS = 'abs';
  24. static SIGN = 'sign';
  25. static LENGTH = 'length';
  26. static NEGATE = 'negate';
  27. static INVERT = 'invert';
  28. // 2 inputs
  29. static MIN = 'min';
  30. static MAX = 'max';
  31. static MOD = 'mod';
  32. static STEP = 'step';
  33. static REFLECT = 'reflect';
  34. static DISTANCE = 'distance';
  35. static DOT = 'dot';
  36. static CROSS = 'cross';
  37. static POW = 'pow';
  38. // 3 inputs
  39. static MIX = 'mix';
  40. static CLAMP = 'clamp';
  41. static REFRACT = 'refract';
  42. static SMOOTHSTEP = 'smoothstep';
  43. static FACEFORWARD = 'faceforward';
  44. constructor( method, a, b = null, c = null ) {
  45. super();
  46. this.method = method;
  47. this.a = a;
  48. this.b = b;
  49. this.c = c;
  50. }
  51. getInputType( builder ) {
  52. const aLen = this.a.getTypeLength( builder );
  53. const bLen = this.b ? this.b.getTypeLength( builder ) : 0;
  54. const cLen = this.c ? this.c.getTypeLength( builder ) : 0;
  55. if ( aLen > bLen && aLen > cLen ) {
  56. return this.a.getType( builder );
  57. } else if ( bLen > cLen ) {
  58. return this.b.getType( builder );
  59. } else if ( cLen > aLen ) {
  60. this.c.getType( builder )
  61. }
  62. return this.a.getType( builder );
  63. }
  64. getType( builder ) {
  65. const method = this.method;
  66. if ( method === MathNode.LENGTH || method === MathNode.DISTANCE || method === MathNode.DOT ) {
  67. return 'float';
  68. } else if (method === MathNode.CROSS) {
  69. return 'vec3';
  70. } else {
  71. return this.getInputType( builder );
  72. }
  73. }
  74. generate( builder, output ) {
  75. const method = this.method;
  76. const type = this.getType( builder );
  77. const inputType = this.getInputType( builder );
  78. if ( method === MathNode.NEGATE ) {
  79. return builder.format( '( -' + this.a.build( builder, inputType ) + ' )', type, output );
  80. } else if ( method === MathNode.INVERT ) {
  81. return builder.format( '( 1.0 - ' + this.a.build( builder, inputType ) + ' )', type, output );
  82. } else {
  83. const params = [];
  84. if ( method === MathNode.CROSS ) {
  85. params.push(
  86. this.a.build( builder, type ),
  87. this.b.build( builder, type )
  88. );
  89. } else if ( method === MathNode.STEP ) {
  90. params.push(
  91. this.b.build( builder, this.a.getTypeLength( builder ) === 1 ? 'float' : inputType ),
  92. this.b.build( builder, inputType )
  93. );
  94. } else if ( method === MathNode.MIN || method === MathNode.MAX || method === MathNode.MOD ) {
  95. params.push(
  96. this.a.build( builder, inputType ),
  97. this.b.build( builder, this.b.getTypeLength( builder ) === 1 ? 'float' : inputType )
  98. );
  99. } else if ( method === MathNode.REFRACT ) {
  100. params.push(
  101. this.a.build( builder, inputType ),
  102. this.b.build( builder, inputType ),
  103. this.c.build( builder, 'float' )
  104. );
  105. } else if ( method === MathNode.MIX ) {
  106. params.push(
  107. this.a.build( builder, inputType ),
  108. this.b.build( builder, inputType ),
  109. this.c.build( builder, this.c.getTypeLength( builder ) === 1 ? 'float' : inputType )
  110. );
  111. } else {
  112. params.push( this.a.build( builder, inputType ) );
  113. if ( this.c !== null ) {
  114. params.push( this.b.build( builder, inputType ), this.c.build( builder, inputType ) );
  115. } else if ( this.b !== null ) {
  116. params.push( this.b.build( builder, inputType ) );
  117. }
  118. }
  119. return builder.format( `${method}( ${params.join(', ')} )`, type, output );
  120. }
  121. }
  122. }
  123. export default MathNode;