2
0

MathNode.js 12 KB

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