MathNode.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import TempNode from '../core/TempNode.js';
  2. import ExpressionNode from '../core/ExpressionNode.js';
  3. import JoinNode from '../utils/JoinNode.js';
  4. import SplitNode from '../utils/SplitNode.js';
  5. import OperatorNode from './OperatorNode.js';
  6. class MathNode extends TempNode {
  7. // 1 input
  8. static RAD = 'radians';
  9. static DEG = 'degrees';
  10. static EXP = 'exp';
  11. static EXP2 = 'exp2';
  12. static LOG = 'log';
  13. static LOG2 = 'log2';
  14. static SQRT = 'sqrt';
  15. static INV_SQRT = 'inversesqrt';
  16. static FLOOR = 'floor';
  17. static CEIL = 'ceil';
  18. static NORMALIZE = 'normalize';
  19. static FRACT = 'fract';
  20. static SIN = 'sin';
  21. static COS = 'cos';
  22. static TAN = 'tan';
  23. static ASIN = 'asin';
  24. static ACOS = 'acos';
  25. static ATAN = 'atan';
  26. static ABS = 'abs';
  27. static SIGN = 'sign';
  28. static LENGTH = 'length';
  29. static NEGATE = 'negate';
  30. static INVERT = 'invert';
  31. static DFDX = 'dFdx';
  32. static DFDY = 'dFdy';
  33. static SATURATE = 'saturate';
  34. static ROUND = 'round';
  35. // 2 inputs
  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.getTypeLength( aType );
  64. const bLen = builder.getTypeLength( bType );
  65. const cLen = 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 ( isWebGL && ( method === MathNode.DFDX || method === MathNode.DFDY ) && output === 'vec3' ) {
  94. // Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
  95. return new JoinNode( [
  96. new MathNode( method, new SplitNode( a, 'x' ) ),
  97. new MathNode( method, new SplitNode( a, 'y' ) ),
  98. new MathNode( method, new SplitNode( a, 'z' ) )
  99. ] ).build( builder );
  100. } else if ( method === MathNode.TRANSFORM_DIRECTION ) {
  101. // dir can be either a direction vector or a normal vector
  102. // upper-left 3x3 of matrix is assumed to be orthogonal
  103. let tA = a;
  104. let tB = b;
  105. if ( builder.isMatrix( tA.getNodeType( builder ) ) ) {
  106. tB = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tB.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  107. } else {
  108. tA = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tA.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  109. }
  110. const mulNode = new SplitNode( new OperatorNode( '*', tA, tB ), 'xyz' );
  111. return new MathNode( MathNode.NORMALIZE, mulNode ).build( builder );
  112. } else if ( method === MathNode.SATURATE ) {
  113. return `clamp( ${ a.build( builder, inputType ) }, 0.0, 1.0 )`;
  114. } else if ( method === MathNode.NEGATE ) {
  115. return '( -' + a.build( builder, inputType ) + ' )';
  116. } else if ( method === MathNode.INVERT ) {
  117. return '( 1.0 - ' + a.build( builder, inputType ) + ' )';
  118. } else {
  119. const params = [];
  120. if ( method === MathNode.CROSS ) {
  121. params.push(
  122. a.build( builder, type ),
  123. b.build( builder, type )
  124. );
  125. } else if ( method === MathNode.STEP ) {
  126. params.push(
  127. a.build( builder, builder.getTypeLength( a.getNodeType( builder ) ) === 1 ? 'float' : inputType ),
  128. b.build( builder, inputType )
  129. );
  130. } else if ( ( isWebGL && ( method === MathNode.MIN || method === MathNode.MAX ) ) || method === MathNode.MOD ) {
  131. params.push(
  132. a.build( builder, inputType ),
  133. b.build( builder, builder.getTypeLength( b.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  134. );
  135. } else if ( method === MathNode.REFRACT ) {
  136. params.push(
  137. a.build( builder, inputType ),
  138. b.build( builder, inputType ),
  139. c.build( builder, 'float' )
  140. );
  141. } else if ( method === MathNode.MIX ) {
  142. params.push(
  143. a.build( builder, inputType ),
  144. b.build( builder, inputType ),
  145. c.build( builder, builder.getTypeLength( c.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  146. );
  147. } else {
  148. params.push( a.build( builder, inputType ) );
  149. if ( c !== null ) {
  150. params.push( b.build( builder, inputType ), c.build( builder, inputType ) );
  151. } else if ( b !== null ) {
  152. params.push( b.build( builder, inputType ) );
  153. }
  154. }
  155. return `${ builder.getMethod( method ) }( ${params.join( ', ' )} )`;
  156. }
  157. }
  158. serialize( data ) {
  159. super.serialize( data );
  160. data.method = this.method;
  161. }
  162. deserialize( data ) {
  163. super.deserialize( data );
  164. this.method = data.method;
  165. }
  166. }
  167. export default MathNode;