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