MathNode.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. static DFDX = 'dFdx';
  29. static DFDY = 'dFdy';
  30. // 2 inputs
  31. static MIN = 'min';
  32. static MAX = 'max';
  33. static MOD = 'mod';
  34. static STEP = 'step';
  35. static REFLECT = 'reflect';
  36. static DISTANCE = 'distance';
  37. static DOT = 'dot';
  38. static CROSS = 'cross';
  39. static POW = 'pow';
  40. // 3 inputs
  41. static MIX = 'mix';
  42. static CLAMP = 'clamp';
  43. static REFRACT = 'refract';
  44. static SMOOTHSTEP = 'smoothstep';
  45. static FACEFORWARD = 'faceforward';
  46. constructor( method, a, b = null, c = null ) {
  47. super();
  48. this.method = method;
  49. this.a = a;
  50. this.b = b;
  51. this.c = c;
  52. }
  53. getInputType( builder ) {
  54. const aLen = this.a.getTypeLength( builder );
  55. const bLen = this.b ? this.b.getTypeLength( builder ) : 0;
  56. const cLen = this.c ? this.c.getTypeLength( builder ) : 0;
  57. if ( aLen > bLen && aLen > cLen ) {
  58. return this.a.getNodeType( builder );
  59. } else if ( bLen > cLen ) {
  60. return this.b.getNodeType( builder );
  61. } else if ( cLen > aLen ) {
  62. this.c.getNodeType( builder )
  63. }
  64. return this.a.getNodeType( builder );
  65. }
  66. getNodeType( builder ) {
  67. const method = this.method;
  68. if ( method === MathNode.LENGTH || method === MathNode.DISTANCE || method === MathNode.DOT ) {
  69. return 'float';
  70. } else if (method === MathNode.CROSS) {
  71. return 'vec3';
  72. } else {
  73. return this.getInputType( builder );
  74. }
  75. }
  76. generate( builder ) {
  77. const method = this.method;
  78. const type = this.getNodeType( builder );
  79. const inputType = this.getInputType( builder );
  80. if ( method === MathNode.NEGATE ) {
  81. return '( -' + this.a.build( builder, inputType ) + ' )';
  82. } else if ( method === MathNode.INVERT ) {
  83. return '( 1.0 - ' + this.a.build( builder, inputType ) + ' )';
  84. } else {
  85. const params = [];
  86. if ( method === MathNode.CROSS ) {
  87. params.push(
  88. this.a.build( builder, type ),
  89. this.b.build( builder, type )
  90. );
  91. } else if ( method === MathNode.STEP ) {
  92. params.push(
  93. this.b.build( builder, this.a.getTypeLength( builder ) === 1 ? 'float' : inputType ),
  94. this.b.build( builder, inputType )
  95. );
  96. } else if ( method === MathNode.MIN || method === MathNode.MAX || method === MathNode.MOD ) {
  97. params.push(
  98. this.a.build( builder, inputType ),
  99. this.b.build( builder, this.b.getTypeLength( builder ) === 1 ? 'float' : inputType )
  100. );
  101. } else if ( method === MathNode.REFRACT ) {
  102. params.push(
  103. this.a.build( builder, inputType ),
  104. this.b.build( builder, inputType ),
  105. this.c.build( builder, 'float' )
  106. );
  107. } else if ( method === MathNode.MIX ) {
  108. params.push(
  109. this.a.build( builder, inputType ),
  110. this.b.build( builder, inputType ),
  111. this.c.build( builder, this.c.getTypeLength( builder ) === 1 ? 'float' : inputType )
  112. );
  113. } else {
  114. params.push( this.a.build( builder, inputType ) );
  115. if ( this.c !== null ) {
  116. params.push( this.b.build( builder, inputType ), this.c.build( builder, inputType ) );
  117. } else if ( this.b !== null ) {
  118. params.push( this.b.build( builder, inputType ) );
  119. }
  120. }
  121. return `${method}( ${params.join(', ')} )`;
  122. }
  123. }
  124. }
  125. export default MathNode;