|
1 | 1 | import { Color, Vector2, NearestFilter, Matrix4, RendererUtils, PassNode, QuadMesh, NodeMaterial } from 'three/webgpu';
|
2 |
| -import { add, float, If, Loop, int, Fn, min, max, clamp, nodeObject, texture, uniform, uv, vec2, vec4, luminance } from 'three/tsl'; |
| 2 | +import { add, float, If, Loop, int, Fn, min, max, clamp, nodeObject, texture, uniform, uv, vec2, vec4, luminance, output, mrt, textureLoad, screenCoordinate } from 'three/tsl'; |
3 | 3 |
|
4 | 4 | const _quadMesh = /*@__PURE__*/ new QuadMesh();
|
5 | 5 | const _size = /*@__PURE__*/ new Vector2();
|
@@ -105,6 +105,15 @@ class TRAAPassNode extends PassNode {
|
105 | 105 | */
|
106 | 106 | this._historyRenderTarget = null;
|
107 | 107 |
|
| 108 | + /** |
| 109 | + * The MRT for the transfer step. |
| 110 | + * |
| 111 | + * @private |
| 112 | + * @type {?MRTNode} |
| 113 | + * @default null |
| 114 | + */ |
| 115 | + this._transferMRT = null; |
| 116 | + |
108 | 117 | /**
|
109 | 118 | * Material used for the resolve step.
|
110 | 119 | *
|
@@ -204,29 +213,21 @@ class TRAAPassNode extends PassNode {
|
204 | 213 |
|
205 | 214 | // configure velocity
|
206 | 215 |
|
207 |
| - const mrt = this.getMRT(); |
208 |
| - const velocityOutput = mrt.get( 'velocity' ); |
209 |
| - |
210 |
| - if ( velocityOutput !== undefined ) { |
| 216 | + const currentMRT = this.getMRT(); |
| 217 | + const velocityOutput = currentMRT.get( 'velocity' ); |
211 | 218 |
|
212 |
| - velocityOutput.setProjectionMatrix( this._originalProjectionMatrix ); |
213 |
| - |
214 |
| - } else { |
215 |
| - |
216 |
| - throw new Error( 'THREE:TRAAPassNode: Missing velocity output in MRT configuration.' ); |
217 |
| - |
218 |
| - } |
| 219 | + velocityOutput.setProjectionMatrix( this._originalProjectionMatrix ); |
219 | 220 |
|
220 | 221 | // render sample
|
221 | 222 |
|
222 |
| - renderer.setMRT( mrt ); |
| 223 | + renderer.setMRT( currentMRT ); |
223 | 224 |
|
224 | 225 | renderer.setClearColor( this.clearColor, this.clearAlpha );
|
225 | 226 | renderer.setRenderTarget( this._sampleRenderTarget );
|
226 | 227 | renderer.render( scene, camera );
|
227 | 228 |
|
228 | 229 | renderer.setRenderTarget( null );
|
229 |
| - renderer.setMRT( null ); |
| 230 | + renderer.setMRT( this._transferMRT ); |
230 | 231 |
|
231 | 232 | // every time when the dimensions change we need fresh history data. Copy the sample
|
232 | 233 | // into the history and final render target (no AA happens at that point).
|
@@ -312,19 +313,49 @@ class TRAAPassNode extends PassNode {
|
312 | 313 | this._sampleRenderTarget.texture.minFiler = NearestFilter;
|
313 | 314 | this._sampleRenderTarget.texture.magFilter = NearestFilter;
|
314 | 315 |
|
315 |
| - const velocityTarget = this._sampleRenderTarget.texture.clone(); |
316 |
| - velocityTarget.isRenderTargetTexture = true; |
317 |
| - velocityTarget.name = 'velocity'; |
| 316 | + const currentMRT = this.getMRT(); |
| 317 | + |
| 318 | + if ( currentMRT === null ) { |
| 319 | + |
| 320 | + throw new Error( 'THREE:TRAAPassNode: Missing MRT configuration.' ); |
318 | 321 |
|
319 |
| - this._sampleRenderTarget.textures.push( velocityTarget ); // for MRT |
| 322 | + } else if ( currentMRT.has( 'velocity' ) === false ) { |
| 323 | + |
| 324 | + throw new Error( 'THREE:TRAAPassNode: Missing velocity output in MRT configuration.' ); |
| 325 | + |
| 326 | + } |
| 327 | + |
| 328 | + this._texturesIndex = currentMRT.getIndexes( this.renderTarget ); |
| 329 | + |
| 330 | + const transferNodes = {}; |
| 331 | + |
| 332 | + for ( const name in this._texturesIndex ) { |
| 333 | + |
| 334 | + if ( name === 'output' ) { |
| 335 | + |
| 336 | + transferNodes[ name ] = output; |
| 337 | + |
| 338 | + } else { |
| 339 | + |
| 340 | + const index = this._texturesIndex[ name ]; |
| 341 | + |
| 342 | + transferNodes[ name ] = textureLoad( this._sampleRenderTarget.textures[ index ], screenCoordinate ); |
| 343 | + |
| 344 | + } |
| 345 | + |
| 346 | + } |
| 347 | + |
| 348 | + this._transferMRT = mrt( transferNodes ); |
320 | 349 |
|
321 | 350 | }
|
322 | 351 |
|
323 | 352 | // textures
|
324 | 353 |
|
| 354 | + const velocityIndex = this._texturesIndex[ 'velocity' ]; |
| 355 | + |
325 | 356 | const historyTexture = texture( this._historyRenderTarget.texture );
|
326 | 357 | const sampleTexture = texture( this._sampleRenderTarget.textures[ 0 ] );
|
327 |
| - const velocityTexture = texture( this._sampleRenderTarget.textures[ 1 ] ); |
| 358 | + const velocityTexture = texture( this._sampleRenderTarget.textures[ velocityIndex ] ); |
328 | 359 | const depthTexture = texture( this._sampleRenderTarget.depthTexture );
|
329 | 360 |
|
330 | 361 | const resolve = Fn( () => {
|
@@ -395,7 +426,7 @@ class TRAAPassNode extends PassNode {
|
395 | 426 |
|
396 | 427 | // materials
|
397 | 428 |
|
398 |
| - this._resolveMaterial.fragmentNode = resolve(); |
| 429 | + this._resolveMaterial.colorNode = resolve(); |
399 | 430 |
|
400 | 431 | return super.setup( builder );
|
401 | 432 |
|
|
0 commit comments