MathNode.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import TempNode from '../core/TempNode.js';
  2. import { sub, mul, div } from './OperatorNode.js';
  3. import { addNodeClass } from '../core/Node.js';
  4. import { addNodeElement, nodeObject, nodeProxy, float, vec3, vec4 } from '../shadernode/ShaderNode.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 if ( method === MathNode.ALL ) {
  36. return 'bool';
  37. } else if ( method === MathNode.EQUALS ) {
  38. return builder.changeComponentType( this.aNode.getNodeType( builder ), 'bool' );
  39. } else if ( method === MathNode.MOD ) {
  40. return this.aNode.getNodeType( builder );
  41. } else {
  42. return this.getInputType( builder );
  43. }
  44. }
  45. generate( builder, output ) {
  46. const method = this.method;
  47. const type = this.getNodeType( builder );
  48. const inputType = this.getInputType( builder );
  49. const a = this.aNode;
  50. const b = this.bNode;
  51. const c = this.cNode;
  52. const isWebGL = builder.renderer.isWebGLRenderer === true;
  53. if ( method === MathNode.TRANSFORM_DIRECTION ) {
  54. // dir can be either a direction vector or a normal vector
  55. // upper-left 3x3 of matrix is assumed to be orthogonal
  56. let tA = a;
  57. let tB = b;
  58. if ( builder.isMatrix( tA.getNodeType( builder ) ) ) {
  59. tB = vec4( vec3( tB ), 0.0 );
  60. } else {
  61. tA = vec4( vec3( tA ), 0.0 );
  62. }
  63. const mulNode = mul( tA, tB ).xyz;
  64. return normalize( mulNode ).build( builder, output );
  65. } else if ( method === MathNode.NEGATE ) {
  66. return builder.format( '( - ' + a.build( builder, inputType ) + ' )', type, output );
  67. } else if ( method === MathNode.ONE_MINUS ) {
  68. return sub( 1.0, a ).build( builder, output );
  69. } else if ( method === MathNode.RECIPROCAL ) {
  70. return div( 1.0, a ).build( builder, output );
  71. } else if ( method === MathNode.DIFFERENCE ) {
  72. return abs( sub( a, b ) ).build( builder, output );
  73. } else {
  74. const params = [];
  75. if ( method === MathNode.CROSS || method === MathNode.MOD ) {
  76. params.push(
  77. a.build( builder, type ),
  78. b.build( builder, type )
  79. );
  80. } else if ( method === MathNode.STEP ) {
  81. params.push(
  82. a.build( builder, builder.getTypeLength( a.getNodeType( builder ) ) === 1 ? 'float' : inputType ),
  83. b.build( builder, inputType )
  84. );
  85. } else if ( ( isWebGL && ( method === MathNode.MIN || method === MathNode.MAX ) ) || method === MathNode.MOD ) {
  86. params.push(
  87. a.build( builder, inputType ),
  88. b.build( builder, builder.getTypeLength( b.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  89. );
  90. } else if ( method === MathNode.REFRACT ) {
  91. params.push(
  92. a.build( builder, inputType ),
  93. b.build( builder, inputType ),
  94. c.build( builder, 'float' )
  95. );
  96. } else if ( method === MathNode.MIX ) {
  97. params.push(
  98. a.build( builder, inputType ),
  99. b.build( builder, inputType ),
  100. c.build( builder, builder.getTypeLength( c.getNodeType( builder ) ) === 1 ? 'float' : inputType )
  101. );
  102. } else {
  103. params.push( a.build( builder, inputType ) );
  104. if ( b !== null ) params.push( b.build( builder, inputType ) );
  105. if ( c !== null ) params.push( c.build( builder, inputType ) );
  106. }
  107. return builder.format( `${ builder.getMethod( method, type ) }( ${params.join( ', ' )} )`, type, output );
  108. }
  109. }
  110. serialize( data ) {
  111. super.serialize( data );
  112. data.method = this.method;
  113. }
  114. deserialize( data ) {
  115. super.deserialize( data );
  116. this.method = data.method;
  117. }
  118. }
  119. // 1 input
  120. MathNode.ALL = 'all';
  121. MathNode.ANY = 'any';
  122. MathNode.EQUALS = 'equals';
  123. MathNode.RADIANS = 'radians';
  124. MathNode.DEGREES = 'degrees';
  125. MathNode.EXP = 'exp';
  126. MathNode.EXP2 = 'exp2';
  127. MathNode.LOG = 'log';
  128. MathNode.LOG2 = 'log2';
  129. MathNode.SQRT = 'sqrt';
  130. MathNode.INVERSE_SQRT = 'inversesqrt';
  131. MathNode.FLOOR = 'floor';
  132. MathNode.CEIL = 'ceil';
  133. MathNode.NORMALIZE = 'normalize';
  134. MathNode.FRACT = 'fract';
  135. MathNode.SIN = 'sin';
  136. MathNode.COS = 'cos';
  137. MathNode.TAN = 'tan';
  138. MathNode.ASIN = 'asin';
  139. MathNode.ACOS = 'acos';
  140. MathNode.ATAN = 'atan';
  141. MathNode.ABS = 'abs';
  142. MathNode.SIGN = 'sign';
  143. MathNode.LENGTH = 'length';
  144. MathNode.NEGATE = 'negate';
  145. MathNode.ONE_MINUS = 'oneMinus';
  146. MathNode.DFDX = 'dFdx';
  147. MathNode.DFDY = 'dFdy';
  148. MathNode.ROUND = 'round';
  149. MathNode.RECIPROCAL = 'reciprocal';
  150. MathNode.TRUNC = 'trunc';
  151. MathNode.FWIDTH = 'fwidth';
  152. MathNode.BITCAST = 'bitcast';
  153. MathNode.TRANSPOSE = 'transpose';
  154. // 2 inputs
  155. MathNode.ATAN2 = 'atan2';
  156. MathNode.MIN = 'min';
  157. MathNode.MAX = 'max';
  158. MathNode.MOD = 'mod';
  159. MathNode.STEP = 'step';
  160. MathNode.REFLECT = 'reflect';
  161. MathNode.DISTANCE = 'distance';
  162. MathNode.DIFFERENCE = 'difference';
  163. MathNode.DOT = 'dot';
  164. MathNode.CROSS = 'cross';
  165. MathNode.POW = 'pow';
  166. MathNode.TRANSFORM_DIRECTION = 'transformDirection';
  167. // 3 inputs
  168. MathNode.MIX = 'mix';
  169. MathNode.CLAMP = 'clamp';
  170. MathNode.REFRACT = 'refract';
  171. MathNode.SMOOTHSTEP = 'smoothstep';
  172. MathNode.FACEFORWARD = 'faceforward';
  173. export default MathNode;
  174. export const EPSILON = float( 1e-6 );
  175. export const INFINITY = float( 1e6 );
  176. export const PI = float( Math.PI );
  177. export const PI2 = float( Math.PI * 2 );
  178. export const all = nodeProxy( MathNode, MathNode.ALL );
  179. export const any = nodeProxy( MathNode, MathNode.ANY );
  180. export const equals = nodeProxy( MathNode, MathNode.EQUALS );
  181. export const radians = nodeProxy( MathNode, MathNode.RADIANS );
  182. export const degrees = nodeProxy( MathNode, MathNode.DEGREES );
  183. export const exp = nodeProxy( MathNode, MathNode.EXP );
  184. export const exp2 = nodeProxy( MathNode, MathNode.EXP2 );
  185. export const log = nodeProxy( MathNode, MathNode.LOG );
  186. export const log2 = nodeProxy( MathNode, MathNode.LOG2 );
  187. export const sqrt = nodeProxy( MathNode, MathNode.SQRT );
  188. export const inverseSqrt = nodeProxy( MathNode, MathNode.INVERSE_SQRT );
  189. export const floor = nodeProxy( MathNode, MathNode.FLOOR );
  190. export const ceil = nodeProxy( MathNode, MathNode.CEIL );
  191. export const normalize = nodeProxy( MathNode, MathNode.NORMALIZE );
  192. export const fract = nodeProxy( MathNode, MathNode.FRACT );
  193. export const sin = nodeProxy( MathNode, MathNode.SIN );
  194. export const cos = nodeProxy( MathNode, MathNode.COS );
  195. export const tan = nodeProxy( MathNode, MathNode.TAN );
  196. export const asin = nodeProxy( MathNode, MathNode.ASIN );
  197. export const acos = nodeProxy( MathNode, MathNode.ACOS );
  198. export const atan = nodeProxy( MathNode, MathNode.ATAN );
  199. export const abs = nodeProxy( MathNode, MathNode.ABS );
  200. export const sign = nodeProxy( MathNode, MathNode.SIGN );
  201. export const length = nodeProxy( MathNode, MathNode.LENGTH );
  202. export const negate = nodeProxy( MathNode, MathNode.NEGATE );
  203. export const oneMinus = nodeProxy( MathNode, MathNode.ONE_MINUS );
  204. export const dFdx = nodeProxy( MathNode, MathNode.DFDX );
  205. export const dFdy = nodeProxy( MathNode, MathNode.DFDY );
  206. export const round = nodeProxy( MathNode, MathNode.ROUND );
  207. export const reciprocal = nodeProxy( MathNode, MathNode.RECIPROCAL );
  208. export const trunc = nodeProxy( MathNode, MathNode.TRUNC );
  209. export const fwidth = nodeProxy( MathNode, MathNode.FWIDTH );
  210. export const bitcast = nodeProxy( MathNode, MathNode.BITCAST );
  211. export const transpose = nodeProxy( MathNode, MathNode.TRANSPOSE );
  212. export const atan2 = nodeProxy( MathNode, MathNode.ATAN2 );
  213. export const min = nodeProxy( MathNode, MathNode.MIN );
  214. export const max = nodeProxy( MathNode, MathNode.MAX );
  215. export const mod = nodeProxy( MathNode, MathNode.MOD );
  216. export const step = nodeProxy( MathNode, MathNode.STEP );
  217. export const reflect = nodeProxy( MathNode, MathNode.REFLECT );
  218. export const distance = nodeProxy( MathNode, MathNode.DISTANCE );
  219. export const difference = nodeProxy( MathNode, MathNode.DIFFERENCE );
  220. export const dot = nodeProxy( MathNode, MathNode.DOT );
  221. export const cross = nodeProxy( MathNode, MathNode.CROSS );
  222. export const pow = nodeProxy( MathNode, MathNode.POW );
  223. export const pow2 = nodeProxy( MathNode, MathNode.POW, 2 );
  224. export const pow3 = nodeProxy( MathNode, MathNode.POW, 3 );
  225. export const pow4 = nodeProxy( MathNode, MathNode.POW, 4 );
  226. export const transformDirection = nodeProxy( MathNode, MathNode.TRANSFORM_DIRECTION );
  227. export const cbrt = ( a ) => mul( sign( a ), pow( abs( a ), 1.0 / 3.0 ) );
  228. export const lengthSq = ( a ) => dot( a, a );
  229. export const mix = nodeProxy( MathNode, MathNode.MIX );
  230. export const clamp = ( value, low = 0, high = 1 ) => nodeObject( new MathNode( MathNode.CLAMP, nodeObject( value ), nodeObject( low ), nodeObject( high ) ) );
  231. export const saturate = ( value ) => clamp( value );
  232. export const refract = nodeProxy( MathNode, MathNode.REFRACT );
  233. export const smoothstep = nodeProxy( MathNode, MathNode.SMOOTHSTEP );
  234. export const faceForward = nodeProxy( MathNode, MathNode.FACEFORWARD );
  235. export const mixElement = ( t, e1, e2 ) => mix( e1, e2, t );
  236. export const smoothstepElement = ( x, low, high ) => smoothstep( low, high, x );
  237. addNodeElement( 'all', all );
  238. addNodeElement( 'any', any );
  239. addNodeElement( 'equals', equals );
  240. addNodeElement( 'radians', radians );
  241. addNodeElement( 'degrees', degrees );
  242. addNodeElement( 'exp', exp );
  243. addNodeElement( 'exp2', exp2 );
  244. addNodeElement( 'log', log );
  245. addNodeElement( 'log2', log2 );
  246. addNodeElement( 'sqrt', sqrt );
  247. addNodeElement( 'inverseSqrt', inverseSqrt );
  248. addNodeElement( 'floor', floor );
  249. addNodeElement( 'ceil', ceil );
  250. addNodeElement( 'normalize', normalize );
  251. addNodeElement( 'fract', fract );
  252. addNodeElement( 'sin', sin );
  253. addNodeElement( 'cos', cos );
  254. addNodeElement( 'tan', tan );
  255. addNodeElement( 'asin', asin );
  256. addNodeElement( 'acos', acos );
  257. addNodeElement( 'atan', atan );
  258. addNodeElement( 'abs', abs );
  259. addNodeElement( 'sign', sign );
  260. addNodeElement( 'length', length );
  261. addNodeElement( 'lengthSq', lengthSq );
  262. addNodeElement( 'negate', negate );
  263. addNodeElement( 'oneMinus', oneMinus );
  264. addNodeElement( 'dFdx', dFdx );
  265. addNodeElement( 'dFdy', dFdy );
  266. addNodeElement( 'round', round );
  267. addNodeElement( 'reciprocal', reciprocal );
  268. addNodeElement( 'trunc', trunc );
  269. addNodeElement( 'fwidth', fwidth );
  270. addNodeElement( 'atan2', atan2 );
  271. addNodeElement( 'min', min );
  272. addNodeElement( 'max', max );
  273. addNodeElement( 'mod', mod );
  274. addNodeElement( 'step', step );
  275. addNodeElement( 'reflect', reflect );
  276. addNodeElement( 'distance', distance );
  277. addNodeElement( 'dot', dot );
  278. addNodeElement( 'cross', cross );
  279. addNodeElement( 'pow', pow );
  280. addNodeElement( 'pow2', pow2 );
  281. addNodeElement( 'pow3', pow3 );
  282. addNodeElement( 'pow4', pow4 );
  283. addNodeElement( 'transformDirection', transformDirection );
  284. addNodeElement( 'mix', mixElement );
  285. addNodeElement( 'clamp', clamp );
  286. addNodeElement( 'refract', refract );
  287. addNodeElement( 'smoothstep', smoothstepElement );
  288. addNodeElement( 'faceForward', faceForward );
  289. addNodeElement( 'difference', difference );
  290. addNodeElement( 'saturate', saturate );
  291. addNodeElement( 'cbrt', cbrt );
  292. addNodeElement( 'transpose', transpose );
  293. addNodeClass( 'MathNode', MathNode );