MathNode.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 RADIANS = 'radians';
  9. static DEGREES = 'degrees';
  10. static EXP = 'exp';
  11. static EXP2 = 'exp2';
  12. static LOG = 'log';
  13. static LOG2 = 'log2';
  14. static SQRT = 'sqrt';
  15. static INVERSE_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 ATAN2 = 'atan2';
  37. static MIN = 'min';
  38. static MAX = 'max';
  39. static MOD = 'mod';
  40. static STEP = 'step';
  41. static REFLECT = 'reflect';
  42. static DISTANCE = 'distance';
  43. static DOT = 'dot';
  44. static CROSS = 'cross';
  45. static POW = 'pow';
  46. static TRANSFORM_DIRECTION = 'transformDirection';
  47. // 3 inputs
  48. static MIX = 'mix';
  49. static CLAMP = 'clamp';
  50. static REFRACT = 'refract';
  51. static SMOOTHSTEP = 'smoothstep';
  52. static FACEFORWARD = 'faceforward';
  53. constructor( method, aNode, bNode = null, cNode = null ) {
  54. super();
  55. this.method = method;
  56. this.aNode = aNode;
  57. this.bNode = bNode;
  58. this.cNode = cNode;
  59. }
  60. getInputType( builder ) {
  61. const aType = this.aNode.getNodeType( builder );
  62. const bType = this.bNode ? this.bNode.getNodeType( builder ) : null;
  63. const cType = this.cNode ? this.cNode.getNodeType( builder ) : null;
  64. const aLen = builder.isMatrix( aType ) ? 0 : builder.getTypeLength( aType );
  65. const bLen = builder.isMatrix( bType ) ? 0 : builder.getTypeLength( bType );
  66. const cLen = builder.isMatrix( cType ) ? 0 : builder.getTypeLength( cType );
  67. if ( aLen > bLen && aLen > cLen ) {
  68. return aType;
  69. } else if ( bLen > cLen ) {
  70. return bType;
  71. } else if ( cLen > aLen ) {
  72. return cType;
  73. }
  74. return aType;
  75. }
  76. getNodeType( builder ) {
  77. const method = this.method;
  78. if ( method === MathNode.LENGTH || method === MathNode.DISTANCE || method === MathNode.DOT ) {
  79. return 'float';
  80. } else if ( method === MathNode.CROSS ) {
  81. return 'vec3';
  82. } else {
  83. return this.getInputType( builder );
  84. }
  85. }
  86. generate( builder, output ) {
  87. const method = this.method;
  88. const type = this.getNodeType( builder );
  89. const inputType = this.getInputType( builder );
  90. const a = this.aNode;
  91. const b = this.bNode;
  92. const c = this.cNode;
  93. const isWebGL = builder.renderer.isWebGLRenderer === true;
  94. if ( isWebGL && ( method === MathNode.DFDX || method === MathNode.DFDY ) && output === 'vec3' ) {
  95. // Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
  96. return new JoinNode( [
  97. new MathNode( method, new SplitNode( a, 'x' ) ),
  98. new MathNode( method, new SplitNode( a, 'y' ) ),
  99. new MathNode( method, new SplitNode( a, 'z' ) )
  100. ] ).build( builder );
  101. } else if ( method === MathNode.TRANSFORM_DIRECTION ) {
  102. // dir can be either a direction vector or a normal vector
  103. // upper-left 3x3 of matrix is assumed to be orthogonal
  104. let tA = a;
  105. let tB = b;
  106. if ( builder.isMatrix( tA.getNodeType( builder ) ) ) {
  107. tB = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tB.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  108. } else {
  109. tA = new ExpressionNode( `${ builder.getType( 'vec4' ) }( ${ tA.build( builder, 'vec3' ) }, 0.0 )`, 'vec4' );
  110. }
  111. const mulNode = new SplitNode( new OperatorNode( '*', tA, tB ), 'xyz' );
  112. return new MathNode( MathNode.NORMALIZE, mulNode ).build( builder );
  113. } else if ( method === MathNode.SATURATE ) {
  114. return builder.format( `clamp( ${ a.build( builder, inputType ) }, 0.0, 1.0 )`, type, output );
  115. } else if ( method === MathNode.NEGATE ) {
  116. return builder.format( '( -' + a.build( builder, inputType ) + ' )', type, output );
  117. } else if ( method === MathNode.INVERT ) {
  118. return builder.format( '( 1.0 - ' + a.build( builder, inputType ) + ' )', type, output );
  119. } else {
  120. const params = [];
  121. if ( method === MathNode.CROSS ) {
  122. params.push(
  123. a.build( builder, type ),
  124. b.build( builder, type )
  125. );
  126. } else if ( method === MathNode.STEP ) {
  127. params.push(
  128. a.build( builder, builder.getTypeLength( a.getNodeType( builder ) ) === 1 ? 'float' : inputType ),
  129. b.build( builder, inputType )
  130. );
  131. } else if ( ( isWebGL && ( method === MathNode.MIN || method === MathNode.MAX ) ) || method === MathNode.MOD ) {
  132. params.push(
  133. a.build( builder, inputType ),
  134. b.build( builder, builder.getTypeLength( b.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  135. );
  136. } else if ( method === MathNode.REFRACT ) {
  137. params.push(
  138. a.build( builder, inputType ),
  139. b.build( builder, inputType ),
  140. c.build( builder, 'float' )
  141. );
  142. } else if ( method === MathNode.MIX ) {
  143. params.push(
  144. a.build( builder, inputType ),
  145. b.build( builder, inputType ),
  146. c.build( builder, builder.getTypeLength( c.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  147. );
  148. } else {
  149. params.push( a.build( builder, inputType ) );
  150. if ( c !== null ) {
  151. params.push( b.build( builder, inputType ), c.build( builder, inputType ) );
  152. } else if ( b !== null ) {
  153. params.push( b.build( builder, inputType ) );
  154. }
  155. }
  156. return builder.format( `${ builder.getMethod( method ) }( ${params.join( ', ' )} )`, type, output );
  157. }
  158. }
  159. serialize( data ) {
  160. super.serialize( data );
  161. data.method = this.method;
  162. }
  163. deserialize( data ) {
  164. super.deserialize( data );
  165. this.method = data.method;
  166. }
  167. }
  168. export default MathNode;