MathNode.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import TempNode from '../core/TempNode.js';
  2. import ExpressionNode from '../core/ExpressionNode.js';
  3. import SplitNode from '../utils/SplitNode.js';
  4. import OperatorNode from './OperatorNode.js';
  5. class MathNode extends TempNode {
  6. // 1 input
  7. static RADIANS = 'radians';
  8. static DEGREES = 'degrees';
  9. static EXP = 'exp';
  10. static EXP2 = 'exp2';
  11. static LOG = 'log';
  12. static LOG2 = 'log2';
  13. static SQRT = 'sqrt';
  14. static INVERSE_SQRT = 'inversesqrt';
  15. static FLOOR = 'floor';
  16. static CEIL = 'ceil';
  17. static NORMALIZE = 'normalize';
  18. static FRACT = 'fract';
  19. static SIN = 'sin';
  20. static COS = 'cos';
  21. static TAN = 'tan';
  22. static ASIN = 'asin';
  23. static ACOS = 'acos';
  24. static ATAN = 'atan';
  25. static ABS = 'abs';
  26. static SIGN = 'sign';
  27. static LENGTH = 'length';
  28. static NEGATE = 'negate';
  29. static INVERT = 'invert';
  30. static DFDX = 'dFdx';
  31. static DFDY = 'dFdy';
  32. static SATURATE = 'saturate';
  33. static ROUND = 'round';
  34. // 2 inputs
  35. static ATAN2 = 'atan2';
  36. static MIN = 'min';
  37. static MAX = 'max';
  38. static MOD = 'mod';
  39. static STEP = 'step';
  40. static REFLECT = 'reflect';
  41. static DISTANCE = 'distance';
  42. static DOT = 'dot';
  43. static CROSS = 'cross';
  44. static POW = 'pow';
  45. static TRANSFORM_DIRECTION = 'transformDirection';
  46. // 3 inputs
  47. static MIX = 'mix';
  48. static CLAMP = 'clamp';
  49. static REFRACT = 'refract';
  50. static SMOOTHSTEP = 'smoothstep';
  51. static FACEFORWARD = 'faceforward';
  52. constructor( method, aNode, bNode = null, cNode = null ) {
  53. super();
  54. this.method = method;
  55. this.aNode = aNode;
  56. this.bNode = bNode;
  57. this.cNode = cNode;
  58. }
  59. getInputType( builder ) {
  60. const aType = this.aNode.getNodeType( builder );
  61. const bType = this.bNode ? this.bNode.getNodeType( builder ) : null;
  62. const cType = this.cNode ? this.cNode.getNodeType( builder ) : null;
  63. const aLen = builder.isMatrix( aType ) ? 0 : builder.getTypeLength( aType );
  64. const bLen = builder.isMatrix( bType ) ? 0 : builder.getTypeLength( bType );
  65. const cLen = builder.isMatrix( cType ) ? 0 : builder.getTypeLength( cType );
  66. if ( aLen > bLen && aLen > cLen ) {
  67. return aType;
  68. } else if ( bLen > cLen ) {
  69. return bType;
  70. } else if ( cLen > aLen ) {
  71. return cType;
  72. }
  73. return aType;
  74. }
  75. getNodeType( builder ) {
  76. const method = this.method;
  77. if ( method === MathNode.LENGTH || method === MathNode.DISTANCE || method === MathNode.DOT ) {
  78. return 'float';
  79. } else if ( method === MathNode.CROSS ) {
  80. return 'vec3';
  81. } else {
  82. return this.getInputType( builder );
  83. }
  84. }
  85. generate( builder, output ) {
  86. const method = this.method;
  87. const type = this.getNodeType( builder );
  88. const inputType = this.getInputType( builder );
  89. const a = this.aNode;
  90. const b = this.bNode;
  91. const c = this.cNode;
  92. const isWebGL = builder.renderer.isWebGLRenderer === true;
  93. if ( method === MathNode.TRANSFORM_DIRECTION ) {
  94. // dir can be either a direction vector or a normal vector
  95. // upper-left 3x3 of matrix is assumed to be orthogonal
  96. let tA = a;
  97. let tB = b;
  98. if ( builder.isMatrix( tA.getNodeType( builder ) ) ) {
  99. tB = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tB.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  100. } else {
  101. tA = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tA.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  102. }
  103. const mulNode = new SplitNode( new OperatorNode( '*', tA, tB ), 'xyz' );
  104. return new MathNode( MathNode.NORMALIZE, mulNode ).build( builder );
  105. } else if ( method === MathNode.SATURATE ) {
  106. return builder.format( `clamp( ${ a.build( builder, inputType ) }, 0.0, 1.0 )`, type, output );
  107. } else if ( method === MathNode.NEGATE ) {
  108. return builder.format( '( -' + a.build( builder, inputType ) + ' )', type, output );
  109. } else if ( method === MathNode.INVERT ) {
  110. return builder.format( '( 1.0 - ' + a.build( builder, inputType ) + ' )', type, output );
  111. } else {
  112. const params = [];
  113. if ( method === MathNode.CROSS ) {
  114. params.push(
  115. a.build( builder, type ),
  116. b.build( builder, type )
  117. );
  118. } else if ( method === MathNode.STEP ) {
  119. params.push(
  120. a.build( builder, builder.getTypeLength( a.getNodeType( builder ) ) === 1 ? 'float' : inputType ),
  121. b.build( builder, inputType )
  122. );
  123. } else if ( ( isWebGL && ( method === MathNode.MIN || method === MathNode.MAX ) ) || method === MathNode.MOD ) {
  124. params.push(
  125. a.build( builder, inputType ),
  126. b.build( builder, builder.getTypeLength( b.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  127. );
  128. } else if ( method === MathNode.REFRACT ) {
  129. params.push(
  130. a.build( builder, inputType ),
  131. b.build( builder, inputType ),
  132. c.build( builder, 'float' )
  133. );
  134. } else if ( method === MathNode.MIX ) {
  135. params.push(
  136. a.build( builder, inputType ),
  137. b.build( builder, inputType ),
  138. c.build( builder, builder.getTypeLength( c.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  139. );
  140. } else {
  141. params.push( a.build( builder, inputType ) );
  142. if ( c !== null ) {
  143. params.push( b.build( builder, inputType ), c.build( builder, inputType ) );
  144. } else if ( b !== null ) {
  145. params.push( b.build( builder, inputType ) );
  146. }
  147. }
  148. return builder.format( `${ builder.getMethod( method ) }( ${params.join( ', ' )} )`, type, output );
  149. }
  150. }
  151. serialize( data ) {
  152. super.serialize( data );
  153. data.method = this.method;
  154. }
  155. deserialize( data ) {
  156. super.deserialize( data );
  157. this.method = data.method;
  158. }
  159. }
  160. export default MathNode;