TRAANode.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. import { HalfFloatType, Vector2, RenderTarget, RendererUtils, QuadMesh, NodeMaterial, TempNode, NodeUpdateType, Matrix4, DepthTexture } from 'three/webgpu';
  2. import { add, float, If, Fn, max, nodeObject, texture, uniform, uv, vec2, vec4, luminance, convertToTexture, passTexture, velocity, getViewPosition, viewZToPerspectiveDepth, struct, ivec2, mix } from 'three/tsl';
  3. const _quadMesh = /*@__PURE__*/ new QuadMesh();
  4. const _size = /*@__PURE__*/ new Vector2();
  5. let _rendererState;
  6. /**
  7. * A special node that applies TRAA (Temporal Reprojection Anti-Aliasing).
  8. *
  9. * References:
  10. * - {@link https://alextardif.com/TAA.html}
  11. * - {@link https://www.elopezr.com/temporal-aa-and-the-quest-for-the-holy-trail/}
  12. *
  13. * @augments TempNode
  14. * @three_import import { traa } from 'three/addons/tsl/display/TRAANode.js';
  15. */
  16. class TRAANode extends TempNode {
  17. static get type() {
  18. return 'TRAANode';
  19. }
  20. /**
  21. * Constructs a new TRAA node.
  22. *
  23. * @param {TextureNode} beautyNode - The texture node that represents the input of the effect.
  24. * @param {TextureNode} depthNode - A node that represents the scene's depth.
  25. * @param {TextureNode} velocityNode - A node that represents the scene's velocity.
  26. * @param {Camera} camera - The camera the scene is rendered with.
  27. */
  28. constructor( beautyNode, depthNode, velocityNode, camera ) {
  29. super( 'vec4' );
  30. /**
  31. * This flag can be used for type testing.
  32. *
  33. * @type {boolean}
  34. * @readonly
  35. * @default true
  36. */
  37. this.isTRAANode = true;
  38. /**
  39. * The `updateBeforeType` is set to `NodeUpdateType.FRAME` since the node renders
  40. * its effect once per frame in `updateBefore()`.
  41. *
  42. * @type {string}
  43. * @default 'frame'
  44. */
  45. this.updateBeforeType = NodeUpdateType.FRAME;
  46. /**
  47. * The texture node that represents the input of the effect.
  48. *
  49. * @type {TextureNode}
  50. */
  51. this.beautyNode = beautyNode;
  52. /**
  53. * A node that represents the scene's velocity.
  54. *
  55. * @type {TextureNode}
  56. */
  57. this.depthNode = depthNode;
  58. /**
  59. * A node that represents the scene's velocity.
  60. *
  61. * @type {TextureNode}
  62. */
  63. this.velocityNode = velocityNode;
  64. /**
  65. * The camera the scene is rendered with.
  66. *
  67. * @type {Camera}
  68. */
  69. this.camera = camera;
  70. /**
  71. * When the difference between the current and previous depth goes above this threshold,
  72. * the history is considered invalid.
  73. *
  74. * @type {number}
  75. * @default 0.0005
  76. */
  77. this.depthThreshold = 0.0005;
  78. /**
  79. * The depth difference within the 3×3 neighborhood to consider a pixel as an edge.
  80. *
  81. * @type {number}
  82. * @default 0.001
  83. */
  84. this.edgeDepthDiff = 0.001;
  85. /**
  86. * The history becomes invalid as the pixel length of the velocity approaches this value.
  87. *
  88. * @type {number}
  89. * @default 128
  90. */
  91. this.maxVelocityLength = 128;
  92. /**
  93. * Whether to decrease the weight on the current frame when the velocity is more subpixel.
  94. * This reduces blurriness under motion, but can introduce a square pattern artifact.
  95. *
  96. * @type {boolean}
  97. * @default true
  98. */
  99. this.useSubpixelCorrection = true;
  100. /**
  101. * The jitter index selects the current camera offset value.
  102. *
  103. * @private
  104. * @type {number}
  105. * @default 0
  106. */
  107. this._jitterIndex = 0;
  108. /**
  109. * A uniform node holding the inverse resolution value.
  110. *
  111. * @private
  112. * @type {UniformNode<vec2>}
  113. */
  114. this._invSize = uniform( new Vector2() );
  115. /**
  116. * The render target that represents the history of frame data.
  117. *
  118. * @private
  119. * @type {?RenderTarget}
  120. */
  121. this._historyRenderTarget = new RenderTarget( 1, 1, { depthBuffer: false, type: HalfFloatType, depthTexture: new DepthTexture() } );
  122. this._historyRenderTarget.texture.name = 'TRAANode.history';
  123. /**
  124. * The render target for the resolve.
  125. *
  126. * @private
  127. * @type {?RenderTarget}
  128. */
  129. this._resolveRenderTarget = new RenderTarget( 1, 1, { depthBuffer: false, type: HalfFloatType } );
  130. this._resolveRenderTarget.texture.name = 'TRAANode.resolve';
  131. /**
  132. * Material used for the resolve step.
  133. *
  134. * @private
  135. * @type {NodeMaterial}
  136. */
  137. this._resolveMaterial = new NodeMaterial();
  138. this._resolveMaterial.name = 'TRAA.resolve';
  139. /**
  140. * The result of the effect is represented as a separate texture node.
  141. *
  142. * @private
  143. * @type {PassTextureNode}
  144. */
  145. this._textureNode = passTexture( this, this._resolveRenderTarget.texture );
  146. /**
  147. * Used to save the original/unjittered projection matrix.
  148. *
  149. * @private
  150. * @type {Matrix4}
  151. */
  152. this._originalProjectionMatrix = new Matrix4();
  153. /**
  154. * A uniform node holding the camera's near and far.
  155. *
  156. * @private
  157. * @type {UniformNode<vec2>}
  158. */
  159. this._cameraNearFar = uniform( new Vector2() );
  160. /**
  161. * A uniform node holding the camera world matrix.
  162. *
  163. * @private
  164. * @type {UniformNode<mat4>}
  165. */
  166. this._cameraWorldMatrix = uniform( new Matrix4() );
  167. /**
  168. * A uniform node holding the camera world matrix inverse.
  169. *
  170. * @private
  171. * @type {UniformNode<mat4>}
  172. */
  173. this._cameraWorldMatrixInverse = uniform( new Matrix4() );
  174. /**
  175. * A uniform node holding the camera projection matrix inverse.
  176. *
  177. * @private
  178. * @type {UniformNode<mat4>}
  179. */
  180. this._cameraProjectionMatrixInverse = uniform( new Matrix4() );
  181. /**
  182. * A uniform node holding the previous frame's view matrix.
  183. *
  184. * @private
  185. * @type {UniformNode<mat4>}
  186. */
  187. this._previousCameraWorldMatrix = uniform( new Matrix4() );
  188. /**
  189. * A uniform node holding the previous frame's projection matrix inverse.
  190. *
  191. * @private
  192. * @type {UniformNode<mat4>}
  193. */
  194. this._previousCameraProjectionMatrixInverse = uniform( new Matrix4() );
  195. /**
  196. * A texture node for the previous depth buffer.
  197. *
  198. * @private
  199. * @type {TextureNode}
  200. */
  201. this._previousDepthNode = texture( new DepthTexture( 1, 1 ) );
  202. /**
  203. * Sync the post processing stack with the TRAA node.
  204. * @private
  205. * @type {boolean}
  206. */
  207. this._needsPostProcessingSync = false;
  208. }
  209. /**
  210. * Returns the result of the effect as a texture node.
  211. *
  212. * @return {PassTextureNode} A texture node that represents the result of the effect.
  213. */
  214. getTextureNode() {
  215. return this._textureNode;
  216. }
  217. /**
  218. * Sets the size of the effect.
  219. *
  220. * @param {number} width - The width of the effect.
  221. * @param {number} height - The height of the effect.
  222. */
  223. setSize( width, height ) {
  224. this._historyRenderTarget.setSize( width, height );
  225. this._resolveRenderTarget.setSize( width, height );
  226. this._invSize.value.set( 1 / width, 1 / height );
  227. }
  228. /**
  229. * Defines the TRAA's current jitter as a view offset
  230. * to the scene's camera.
  231. *
  232. * @param {number} width - The width of the effect.
  233. * @param {number} height - The height of the effect.
  234. */
  235. setViewOffset( width, height ) {
  236. // save original/unjittered projection matrix for velocity pass
  237. this.camera.updateProjectionMatrix();
  238. this._originalProjectionMatrix.copy( this.camera.projectionMatrix );
  239. velocity.setProjectionMatrix( this._originalProjectionMatrix );
  240. //
  241. const viewOffset = {
  242. fullWidth: width,
  243. fullHeight: height,
  244. offsetX: 0,
  245. offsetY: 0,
  246. width: width,
  247. height: height
  248. };
  249. const jitterOffset = _haltonOffsets[ this._jitterIndex ];
  250. this.camera.setViewOffset(
  251. viewOffset.fullWidth, viewOffset.fullHeight,
  252. viewOffset.offsetX + jitterOffset[ 0 ] - 0.5, viewOffset.offsetY + jitterOffset[ 1 ] - 0.5,
  253. viewOffset.width, viewOffset.height
  254. );
  255. }
  256. /**
  257. * Clears the view offset from the scene's camera.
  258. */
  259. clearViewOffset() {
  260. this.camera.clearViewOffset();
  261. velocity.setProjectionMatrix( null );
  262. // update jitter index
  263. this._jitterIndex ++;
  264. this._jitterIndex = this._jitterIndex % ( _haltonOffsets.length - 1 );
  265. }
  266. /**
  267. * This method is used to render the effect once per frame.
  268. *
  269. * @param {NodeFrame} frame - The current node frame.
  270. */
  271. updateBefore( frame ) {
  272. const { renderer } = frame;
  273. // store previous frame matrices before updating current ones
  274. this._previousCameraWorldMatrix.value.copy( this._cameraWorldMatrix.value );
  275. this._previousCameraProjectionMatrixInverse.value.copy( this._cameraProjectionMatrixInverse.value );
  276. // update camera matrices uniforms
  277. this._cameraNearFar.value.set( this.camera.near, this.camera.far );
  278. this._cameraWorldMatrix.value.copy( this.camera.matrixWorld );
  279. this._cameraWorldMatrixInverse.value.copy( this.camera.matrixWorldInverse );
  280. this._cameraProjectionMatrixInverse.value.copy( this.camera.projectionMatrixInverse );
  281. // keep the TRAA in sync with the dimensions of the beauty node
  282. const beautyRenderTarget = ( this.beautyNode.isRTTNode ) ? this.beautyNode.renderTarget : this.beautyNode.passNode.renderTarget;
  283. const width = beautyRenderTarget.texture.width;
  284. const height = beautyRenderTarget.texture.height;
  285. //
  286. if ( this._needsPostProcessingSync === true ) {
  287. this.setViewOffset( width, height );
  288. this._needsPostProcessingSync = false;
  289. }
  290. _rendererState = RendererUtils.resetRendererState( renderer, _rendererState );
  291. //
  292. const needsRestart = this._historyRenderTarget.width !== width || this._historyRenderTarget.height !== height;
  293. this.setSize( width, height );
  294. // every time when the dimensions change we need fresh history data
  295. if ( needsRestart === true ) {
  296. // bind and clear render target to make sure they are initialized after the resize which triggers a dispose()
  297. renderer.setRenderTarget( this._historyRenderTarget );
  298. renderer.clear();
  299. renderer.setRenderTarget( this._resolveRenderTarget );
  300. renderer.clear();
  301. // make sure to reset the history with the contents of the beauty buffer otherwise subsequent frames after the
  302. // resize will fade from a darker color to the correct one because the history was cleared with black.
  303. renderer.copyTextureToTexture( beautyRenderTarget.texture, this._historyRenderTarget.texture );
  304. }
  305. // resolve
  306. renderer.setRenderTarget( this._resolveRenderTarget );
  307. _quadMesh.material = this._resolveMaterial;
  308. _quadMesh.name = 'TRAA';
  309. _quadMesh.render( renderer );
  310. renderer.setRenderTarget( null );
  311. // update history
  312. renderer.copyTextureToTexture( this._resolveRenderTarget.texture, this._historyRenderTarget.texture );
  313. // Copy current depth to previous depth buffer
  314. const size = renderer.getDrawingBufferSize( _size );
  315. // only allow the depth copy if the dimensions of the history render target match with the drawing
  316. // render buffer and thus the depth texture of the scene. For some reasons, there are timing issues
  317. // with WebGPU resulting in different size of the drawing buffer and the beauty render target when
  318. // resizing the browser window. This does not happen with the WebGL backend
  319. if ( this._historyRenderTarget.height === size.height && this._historyRenderTarget.width === size.width ) {
  320. const currentDepth = this.depthNode.value;
  321. renderer.copyTextureToTexture( currentDepth, this._historyRenderTarget.depthTexture );
  322. this._previousDepthNode.value = this._historyRenderTarget.depthTexture;
  323. }
  324. // restore
  325. RendererUtils.restoreRendererState( renderer, _rendererState );
  326. }
  327. /**
  328. * This method is used to setup the effect's render targets and TSL code.
  329. *
  330. * @param {NodeBuilder} builder - The current node builder.
  331. * @return {PassTextureNode}
  332. */
  333. setup( builder ) {
  334. const postProcessing = builder.context.postProcessing;
  335. if ( postProcessing ) {
  336. this._needsPostProcessingSync = true;
  337. postProcessing.context.onBeforePostProcessing = () => {
  338. const size = builder.renderer.getDrawingBufferSize( _size );
  339. this.setViewOffset( size.width, size.height );
  340. };
  341. postProcessing.context.onAfterPostProcessing = () => {
  342. this.clearViewOffset();
  343. };
  344. }
  345. const currentDepthStruct = struct( {
  346. closestDepth: 'float',
  347. closestPositionTexel: 'vec2',
  348. farthestDepth: 'float',
  349. } );
  350. // Samples 3×3 neighborhood pixels and returns the closest and farthest depths.
  351. const sampleCurrentDepth = Fn( ( [ positionTexel ] ) => {
  352. const closestDepth = float( 2 ).toVar();
  353. const closestPositionTexel = vec2( 0 ).toVar();
  354. const farthestDepth = float( - 1 ).toVar();
  355. for ( let x = - 1; x <= 1; ++ x ) {
  356. for ( let y = - 1; y <= 1; ++ y ) {
  357. const neighbor = positionTexel.add( vec2( x, y ) ).toVar();
  358. const depth = this.depthNode.load( neighbor ).r.toVar();
  359. If( depth.lessThan( closestDepth ), () => {
  360. closestDepth.assign( depth );
  361. closestPositionTexel.assign( neighbor );
  362. } );
  363. If( depth.greaterThan( farthestDepth ), () => {
  364. farthestDepth.assign( depth );
  365. } );
  366. }
  367. }
  368. return currentDepthStruct( closestDepth, closestPositionTexel, farthestDepth );
  369. } );
  370. // Samples a previous depth and reproject it using the current camera matrices.
  371. const samplePreviousDepth = ( uv ) => {
  372. const depth = this._previousDepthNode.sample( uv ).r;
  373. const positionView = getViewPosition( uv, depth, this._previousCameraProjectionMatrixInverse );
  374. const positionWorld = this._previousCameraWorldMatrix.mul( vec4( positionView, 1 ) ).xyz;
  375. const viewZ = this._cameraWorldMatrixInverse.mul( vec4( positionWorld, 1 ) ).z;
  376. return viewZToPerspectiveDepth( viewZ, this._cameraNearFar.x, this._cameraNearFar.y );
  377. };
  378. // Optimized version of AABB clipping.
  379. // Reference: https://github.com/playdeadgames/temporal
  380. const clipAABB = Fn( ( [ currentColor, historyColor, minColor, maxColor ] ) => {
  381. const pClip = maxColor.rgb.add( minColor.rgb ).mul( 0.5 );
  382. const eClip = maxColor.rgb.sub( minColor.rgb ).mul( 0.5 ).add( 1e-7 );
  383. const vClip = historyColor.sub( vec4( pClip, currentColor.a ) );
  384. const vUnit = vClip.xyz.div( eClip );
  385. const absUnit = vUnit.abs();
  386. const maxUnit = max( absUnit.x, absUnit.y, absUnit.z );
  387. return maxUnit.greaterThan( 1 ).select(
  388. vec4( pClip, currentColor.a ).add( vClip.div( maxUnit ) ),
  389. historyColor
  390. );
  391. } ).setLayout( {
  392. name: 'clipAABB',
  393. type: 'vec4',
  394. inputs: [
  395. { name: 'currentColor', type: 'vec4' },
  396. { name: 'historyColor', type: 'vec4' },
  397. { name: 'minColor', type: 'vec4' },
  398. { name: 'maxColor', type: 'vec4' }
  399. ]
  400. } );
  401. // Performs variance clipping.
  402. // See: https://developer.download.nvidia.com/gameworks/events/GDC2016/msalvi_temporal_supersampling.pdf
  403. const varianceClipping = Fn( ( [ positionTexel, currentColor, historyColor, gamma ] ) => {
  404. const offsets = [
  405. [ - 1, - 1 ],
  406. [ - 1, 1 ],
  407. [ 1, - 1 ],
  408. [ 1, 1 ],
  409. [ 1, 0 ],
  410. [ 0, - 1 ],
  411. [ 0, 1 ],
  412. [ - 1, 0 ]
  413. ];
  414. const moment1 = currentColor.toVar();
  415. const moment2 = currentColor.pow2().toVar();
  416. for ( const [ x, y ] of offsets ) {
  417. // Use max() to prevent NaN values from propagating.
  418. const neighbor = this.beautyNode.offset( ivec2( x, y ) ).load( positionTexel ).max( 0 );
  419. moment1.addAssign( neighbor );
  420. moment2.addAssign( neighbor.pow2() );
  421. }
  422. const N = float( offsets.length + 1 );
  423. const mean = moment1.div( N );
  424. const variance = moment2.div( N ).sub( mean.pow2() ).max( 0 ).sqrt().mul( gamma );
  425. const minColor = mean.sub( variance );
  426. const maxColor = mean.add( variance );
  427. return clipAABB( mean.clamp( minColor, maxColor ), historyColor, minColor, maxColor );
  428. } );
  429. // Returns the amount of subpixel (expressed within [0, 1]) in the velocity.
  430. const subpixelCorrection = Fn( ( [ velocityUV, textureSize ] ) => {
  431. const velocityTexel = velocityUV.mul( textureSize );
  432. const phase = velocityTexel.fract().abs();
  433. const weight = max( phase, phase.oneMinus() );
  434. return weight.x.mul( weight.y ).oneMinus().div( 0.75 );
  435. } ).setLayout( {
  436. name: 'subpixelCorrection',
  437. type: 'float',
  438. inputs: [
  439. { name: 'velocityUV', type: 'vec2' },
  440. { name: 'textureSize', type: 'ivec2' }
  441. ]
  442. } );
  443. // Flicker reduction based on luminance weighing.
  444. const flickerReduction = Fn( ( [ currentColor, historyColor, currentWeight ] ) => {
  445. const historyWeight = currentWeight.oneMinus();
  446. const compressedCurrent = currentColor.mul( float( 1 ).div( ( max( currentColor.r, currentColor.g, currentColor.b ).add( 1 ) ) ) );
  447. const compressedHistory = historyColor.mul( float( 1 ).div( ( max( historyColor.r, historyColor.g, historyColor.b ).add( 1 ) ) ) );
  448. const luminanceCurrent = luminance( compressedCurrent.rgb );
  449. const luminanceHistory = luminance( compressedHistory.rgb );
  450. currentWeight.mulAssign( float( 1 ).div( luminanceCurrent.add( 1 ) ) );
  451. historyWeight.mulAssign( float( 1 ).div( luminanceHistory.add( 1 ) ) );
  452. return add( currentColor.mul( currentWeight ), historyColor.mul( historyWeight ) ).div( max( currentWeight.add( historyWeight ), 0.00001 ) ).toVar();
  453. } );
  454. const historyNode = texture( this._historyRenderTarget.texture );
  455. const resolve = Fn( () => {
  456. const uvNode = uv();
  457. const textureSize = this.beautyNode.size(); // Assumes all the buffers share the same size.
  458. const positionTexel = uvNode.mul( textureSize );
  459. // sample the closest and farthest depths in the current buffer
  460. const currentDepth = sampleCurrentDepth( positionTexel );
  461. const closestDepth = currentDepth.get( 'closestDepth' );
  462. const closestPositionTexel = currentDepth.get( 'closestPositionTexel' );
  463. const farthestDepth = currentDepth.get( 'farthestDepth' );
  464. // convert the NDC offset to UV offset
  465. const offsetUV = this.velocityNode.load( closestPositionTexel ).xy.mul( vec2( 0.5, - 0.5 ) );
  466. // sample the previous depth
  467. const historyUV = uvNode.sub( offsetUV );
  468. const previousDepth = samplePreviousDepth( historyUV );
  469. // history is considered valid when the UV is in range and there's no disocclusion except on edges
  470. const isValidUV = historyUV.greaterThanEqual( 0 ).all().and( historyUV.lessThanEqual( 1 ).all() );
  471. const isEdge = farthestDepth.sub( closestDepth ).greaterThan( this.edgeDepthDiff );
  472. const isDisocclusion = closestDepth.sub( previousDepth ).greaterThan( this.depthThreshold );
  473. const hasValidHistory = isValidUV.and( isEdge.or( isDisocclusion.not() ) );
  474. // sample the current and previous colors
  475. const currentColor = this.beautyNode.sample( uvNode );
  476. const historyColor = historyNode.sample( uvNode.sub( offsetUV ) );
  477. // increase the weight towards the current frame under motion
  478. const motionFactor = uvNode.sub( historyUV ).mul( textureSize ).length().div( this.maxVelocityLength ).saturate();
  479. const currentWeight = float( 0.05 ).toVar(); // A minimum weight
  480. if ( this.useSubpixelCorrection ) {
  481. // Increase the minimum weight towards the current frame when the velocity is more subpixel.
  482. currentWeight.addAssign( subpixelCorrection( offsetUV, textureSize ).mul( 0.25 ) );
  483. }
  484. currentWeight.assign( hasValidHistory.select( currentWeight.add( motionFactor ).saturate(), 1 ) );
  485. // Perform neighborhood clipping/clamping. We use variance clipping here.
  486. const varianceGamma = mix( 0.5, 1, motionFactor.oneMinus().pow2() ); // Reasonable gamma range is [0.75, 2]
  487. const clippedHistoryColor = varianceClipping( positionTexel, currentColor, historyColor, varianceGamma );
  488. // flicker reduction based on luminance weighing
  489. const output = flickerReduction( currentColor, clippedHistoryColor, currentWeight );
  490. return output;
  491. } );
  492. // materials
  493. this._resolveMaterial.colorNode = resolve();
  494. return this._textureNode;
  495. }
  496. /**
  497. * Frees internal resources. This method should be called
  498. * when the effect is no longer required.
  499. */
  500. dispose() {
  501. this._historyRenderTarget.dispose();
  502. this._resolveRenderTarget.dispose();
  503. this._resolveMaterial.dispose();
  504. }
  505. }
  506. export default TRAANode;
  507. function _halton( index, base ) {
  508. let fraction = 1;
  509. let result = 0;
  510. while ( index > 0 ) {
  511. fraction /= base;
  512. result += fraction * ( index % base );
  513. index = Math.floor( index / base );
  514. }
  515. return result;
  516. }
  517. const _haltonOffsets = /*@__PURE__*/ Array.from(
  518. { length: 32 },
  519. ( _, index ) => [ _halton( index + 1, 2 ), _halton( index + 1, 3 ) ]
  520. );
  521. /**
  522. * TSL function for creating a TRAA node for Temporal Reprojection Anti-Aliasing.
  523. *
  524. * @tsl
  525. * @function
  526. * @param {TextureNode} beautyNode - The texture node that represents the input of the effect.
  527. * @param {TextureNode} depthNode - A node that represents the scene's depth.
  528. * @param {TextureNode} velocityNode - A node that represents the scene's velocity.
  529. * @param {Camera} camera - The camera the scene is rendered with.
  530. * @returns {TRAANode}
  531. */
  532. export const traa = ( beautyNode, depthNode, velocityNode, camera ) => nodeObject( new TRAANode( convertToTexture( beautyNode ), depthNode, velocityNode, camera ) );
粤ICP备19079148号