Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodes: StereoCompositePassNode.js #29201

Merged
merged 6 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified examples/screenshots/webgpu_backdrop_water.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion examples/webgpu_display_stereo.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
const position = new THREE.Vector3();

const params = {
effect: 'stereo'
effect: 'stereo',
eyeSep: 0.064,
};

const effects = { Stereo: 'stereo', Anaglyph: 'anaglyph', ParallaxBarrier: 'parallaxBarrier' };
Expand Down Expand Up @@ -106,6 +107,14 @@

const gui = new GUI();
gui.add( params, 'effect', effects ).onChange( update );
gui.add( params, 'eyeSep', 0.001, 0.15, 0.001 ).onChange( function ( value ) {

stereo.eyeSep = value;

anaglyph.eyeSep = value;
parallaxBarrier.eyeSep = value;

} );

//

Expand Down
100 changes: 9 additions & 91 deletions src/nodes/display/AnaglyphPassNode.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
import { Fn, nodeObject, vec4 } from '../shadernode/ShaderNode.js';
import PassNode from './PassNode.js';
import { Vector2 } from '../../math/Vector2.js';
import { StereoCamera } from '../../cameras/StereoCamera.js';
import { HalfFloatType, LinearFilter, NearestFilter } from '../../constants.js';
import { RenderTarget } from '../../core/RenderTarget.js';
import { Matrix3 } from '../../math/Matrix3.js';
import { uniform } from '../core/UniformNode.js';
import QuadMesh from '../../renderers/common/QuadMesh.js';
import { uv } from '../accessors/UVNode.js';
import { clamp, max } from '../math/MathNode.js';
import { texture } from '../accessors/TextureNode.js';
import StereoCompositePassNode from './StereoCompositePassNode.js';
import { addNodeClass } from '../core/Node.js';

const _size = /*@__PURE__*/ new Vector2();
const _quadMesh = /*@__PURE__*/ new QuadMesh();
class AnaglyphPassNode extends StereoCompositePassNode {

class AnaglyphPassNode extends PassNode {
constructor( scene, camera, eyeSep ) {
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved

constructor( scene, camera ) {

super( PassNode.COLOR, scene, camera );
super( scene, camera, eyeSep );

this.isAnaglyphPassNode = true;

this.stereo = new StereoCamera();

const _params = { minFilter: LinearFilter, magFilter: NearestFilter, type: HalfFloatType };

this._renderTargetL = new RenderTarget( 1, 1, _params );
this._renderTargetR = new RenderTarget( 1, 1, _params );

// Dubois matrices from https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.7.6968&rep=rep1&type=pdf#page=4

this._colorMatrixLeft = uniform( new Matrix3().fromArray( [
Expand All @@ -43,60 +28,6 @@ class AnaglyphPassNode extends PassNode {
- 0.00155529, - 0.0184503, 1.2264
] ) );

this._mapLeft = texture( this._renderTargetL.texture );
this._mapRight = texture( this._renderTargetR.texture );

//

this._material = null;

}

setSize( width, height ) {

super.setSize( width, height );

this._renderTargetL.setSize( this.renderTarget.width, this.renderTarget.height );
this._renderTargetR.setSize( this.renderTarget.width, this.renderTarget.height );

}

updateBefore( frame ) {

const { renderer } = frame;
const { scene, camera, stereo, renderTarget } = this;

this._pixelRatio = renderer.getPixelRatio();

stereo.cameraL.coordinateSystem = renderer.coordinateSystem;
stereo.cameraR.coordinateSystem = renderer.coordinateSystem;
stereo.update( camera );

const size = renderer.getSize( _size );
this.setSize( size.width, size.height );

const currentRenderTarget = renderer.getRenderTarget();

// left

renderer.setRenderTarget( this._renderTargetL );
renderer.render( scene, stereo.cameraL );

// right

renderer.setRenderTarget( this._renderTargetR );
renderer.render( scene, stereo.cameraR );

// composite

renderer.setRenderTarget( renderTarget );
_quadMesh.material = this._material;
_quadMesh.render( renderer );

// restore

renderer.setRenderTarget( currentRenderTarget );

}

setup( builder ) {
Expand All @@ -122,23 +53,10 @@ class AnaglyphPassNode extends PassNode {

}

dispose() {

super.dispose();

this._renderTargetL.dispose();
this._renderTargetR.dispose();

if ( this._material !== null ) {

this._material.dispose();

}

}

}

export const anaglyphPass = ( scene, camera ) => nodeObject( new AnaglyphPassNode( scene, camera ) );

export default AnaglyphPassNode;

export const anaglyphPass = ( scene, camera, eyeSep ) => nodeObject( new AnaglyphPassNode( scene, camera, eyeSep ) );
Fixed Show fixed Hide fixed

addNodeClass( 'AnaglyphPassNode', AnaglyphPassNode );
100 changes: 9 additions & 91 deletions src/nodes/display/ParallaxBarrierPassNode.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,18 @@
import { Fn, If, nodeObject, vec4 } from '../shadernode/ShaderNode.js';
import PassNode from './PassNode.js';
import { Vector2 } from '../../math/Vector2.js';
import { StereoCamera } from '../../cameras/StereoCamera.js';
import { HalfFloatType, LinearFilter, NearestFilter } from '../../constants.js';
import { RenderTarget } from '../../core/RenderTarget.js';
import QuadMesh from '../../renderers/common/QuadMesh.js';
import { uv } from '../accessors/UVNode.js';
import { mod } from '../math/MathNode.js';
import { texture } from '../accessors/TextureNode.js';
import { viewportCoordinate } from './ViewportNode.js';
import StereoCompositePassNode from './StereoCompositePassNode.js';
import { addNodeClass } from '../core/Node.js';

const _size = /*@__PURE__*/ new Vector2();
const _quadMesh = /*@__PURE__*/ new QuadMesh();
class ParallaxBarrierPassNode extends StereoCompositePassNode {

class ParallaxBarrierPassNode extends PassNode {
constructor( scene, camera, eyeSep ) {

constructor( scene, camera ) {

super( PassNode.COLOR, scene, camera );
super( scene, camera, eyeSep );

this.isParallaxBarrierPassNode = true;

this.stereo = new StereoCamera();

const _params = { minFilter: LinearFilter, magFilter: NearestFilter, type: HalfFloatType };

this._renderTargetL = new RenderTarget( 1, 1, _params );
this._renderTargetR = new RenderTarget( 1, 1, _params );

this._mapLeft = texture( this._renderTargetL.texture );
this._mapRight = texture( this._renderTargetR.texture );

//

this._material = null;

}

setSize( width, height ) {

super.setSize( width, height );

this._renderTargetL.setSize( this.renderTarget.width, this.renderTarget.height );
this._renderTargetR.setSize( this.renderTarget.width, this.renderTarget.height );

}

updateBefore( frame ) {

const { renderer } = frame;
const { scene, camera, stereo, renderTarget } = this;

this._pixelRatio = renderer.getPixelRatio();

stereo.cameraL.coordinateSystem = renderer.coordinateSystem;
stereo.cameraR.coordinateSystem = renderer.coordinateSystem;
stereo.update( camera );

const size = renderer.getSize( _size );
this.setSize( size.width, size.height );

const currentRenderTarget = renderer.getRenderTarget();

// left

renderer.setRenderTarget( this._renderTargetL );
renderer.render( scene, stereo.cameraL );

// right

renderer.setRenderTarget( this._renderTargetR );
renderer.render( scene, stereo.cameraR );

// composite

renderer.setRenderTarget( renderTarget );
_quadMesh.material = this._material;
_quadMesh.render( renderer );

// restore

renderer.setRenderTarget( currentRenderTarget );

}

setup( builder ) {
Expand Down Expand Up @@ -114,23 +45,10 @@ class ParallaxBarrierPassNode extends PassNode {

}

dispose() {

super.dispose();

this._renderTargetL.dispose();
this._renderTargetR.dispose();

if ( this._material !== null ) {

this._material.dispose();

}

}

}

export const parallaxBarrierPass = ( scene, camera ) => nodeObject( new ParallaxBarrierPassNode( scene, camera ) );

export default ParallaxBarrierPassNode;

export const parallaxBarrierPass = ( scene, camera, eyeSep ) => nodeObject( new ParallaxBarrierPassNode( scene, camera, eyeSep ) );
Fixed Show fixed Hide fixed

addNodeClass( 'ParallaxBarrierPassNode', ParallaxBarrierPassNode );
106 changes: 106 additions & 0 deletions src/nodes/display/StereoCompositePassNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

import PassNode from './PassNode.js';
import { StereoCamera } from '../../cameras/StereoCamera.js';
import { HalfFloatType, LinearFilter, NearestFilter } from '../../constants.js';
import { RenderTarget } from '../../core/RenderTarget.js';
import { texture } from '../accessors/TextureNode.js';
import { Vector2 } from '../../math/Vector2.js';
import QuadMesh from '../../renderers/common/QuadMesh.js';

const _size = /*@__PURE__*/ new Vector2();
const _quadMesh = /*@__PURE__*/ new QuadMesh();

class StereoCompositePassNode extends PassNode {

constructor( scene, camera, eyeSep = 0.064 ) {

super( PassNode.COLOR, scene, camera );

this.isStereoCompositePassNode = true;

this.stereo = new StereoCamera();
this.eyeSep = eyeSep;
const _params = { minFilter: LinearFilter, magFilter: NearestFilter, type: HalfFloatType };

this._renderTargetL = new RenderTarget( 1, 1, _params );
this._renderTargetR = new RenderTarget( 1, 1, _params );

this._mapLeft = texture( this._renderTargetL.texture );
this._mapRight = texture( this._renderTargetR.texture );

this._material = null;

}

updateStereoCamera( coordinateSystem ) {

this.stereo.cameraL.coordinateSystem = coordinateSystem;
this.stereo.cameraR.coordinateSystem = coordinateSystem;
this.stereo.eyeSep = this.eyeSep;
this.stereo.update( this.camera );

}

setSize( width, height ) {

super.setSize( width, height );

this._renderTargetL.setSize( this.renderTarget.width, this.renderTarget.height );
this._renderTargetR.setSize( this.renderTarget.width, this.renderTarget.height );

}

updateBefore( frame ) {

const { renderer } = frame;
const { scene, stereo, renderTarget } = this;

this._pixelRatio = renderer.getPixelRatio();

this.updateStereoCamera( renderer.coordinateSystem );

const size = renderer.getSize( _size );
this.setSize( size.width, size.height );

const currentRenderTarget = renderer.getRenderTarget();

// left

renderer.setRenderTarget( this._renderTargetL );
renderer.render( scene, stereo.cameraL );

// right

renderer.setRenderTarget( this._renderTargetR );
renderer.render( scene, stereo.cameraR );

// composite

renderer.setRenderTarget( renderTarget );
_quadMesh.material = this._material;
_quadMesh.render( renderer );

// restore

renderer.setRenderTarget( currentRenderTarget );

}

dispose() {

super.dispose();

this._renderTargetL.dispose();
this._renderTargetR.dispose();

if ( this._material !== null ) {

this._material.dispose();

}

}

}

export default StereoCompositePassNode;
Loading