Skip to content

Commit

Permalink
Enable post-processing in VR (#20)
Browse files Browse the repository at this point in the history
* Update WebXRManager.js

* Update WebGLRenderer.js

* Update WebGLRenderer.js

deleted useless blank lines

* Update WebXRManager.js

- Deleted useless code and blank lines
- refectored _getRenderTarget to getRenderTarget

* Update WebGLRenderer.js

Reflect the refactoring of getRenderTarget function

Improve compatibility of Post-Processing with VR (#23)

* Update EffectComposer.js for better compatibility with VR

* Update Pass.js for better compatibility with VR
  • Loading branch information
enzofrancescaHM authored and dmarcos committed Feb 3, 2025
1 parent 1123347 commit 3bc7bc0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
25 changes: 23 additions & 2 deletions examples/jsm/postprocessing/EffectComposer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { CopyShader } from '../shaders/CopyShader.js';
import { ShaderPass } from './ShaderPass.js';
import { ClearMaskPass, MaskPass } from './MaskPass.js';

const size = /* @__PURE__ */ new Vector2();

class EffectComposer {

constructor( renderer, renderTarget ) {
Expand All @@ -19,7 +21,7 @@ class EffectComposer {

if ( renderTarget === undefined ) {

const size = renderer.getSize( new Vector2() );
renderer.getSize( size );
this._width = size.width;
this._height = size.height;

Expand Down Expand Up @@ -48,6 +50,22 @@ class EffectComposer {
this.copyPass.material.blending = NoBlending;

this.clock = new Clock();

this.onSessionStateChange = this.onSessionStateChange.bind( this );
this.renderer.xr.addEventListener( 'sessionstart', this.onSessionStateChange );
this.renderer.xr.addEventListener( 'sessionend', this.onSessionStateChange );

}

onSessionStateChange() {

this.renderer.getSize( size );
this._width = size.width;
this._height = size.height;

this._pixelRatio = this.renderer.xr.isPresenting ? 1 : this.renderer.getPixelRatio();

this.setSize( this._width, this._height );

}

Expand Down Expand Up @@ -169,7 +187,7 @@ class EffectComposer {

if ( renderTarget === undefined ) {

const size = this.renderer.getSize( new Vector2() );
this.renderer.getSize( size );
this._pixelRatio = this.renderer.getPixelRatio();
this._width = size.width;
this._height = size.height;
Expand Down Expand Up @@ -223,6 +241,9 @@ class EffectComposer {

this.copyPass.dispose();

this.renderer.xr.removeEventListener( 'sessionstart', this.onSessionStateChange );
this.renderer.xr.removeEventListener( 'sessionend', this.onSessionStateChange );

}

}
Expand Down
7 changes: 6 additions & 1 deletion examples/jsm/postprocessing/Pass.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,13 @@ class FullScreenQuad {

render( renderer ) {

renderer.render( this._mesh, _camera );
// Disable XR projection for fullscreen effects
// https://github.com/mrdoob/three.js/pull/18846
const xrEnabled = renderer.xr.enabled;

renderer.xr.enabled = false;
renderer.render( this._mesh, _camera );
renderer.xr.enabled = xrEnabled;
}

get material() {
Expand Down
7 changes: 7 additions & 0 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2335,6 +2335,13 @@ class WebGLRenderer {
const _scratchFrameBuffer = _gl.createFramebuffer();
this.setRenderTarget = function ( renderTarget, activeCubeFace = 0, activeMipmapLevel = 0 ) {

// Render to base layer instead of canvas in WebXR
if ( renderTarget === null && this.xr.isPresenting ) {

renderTarget = this.xr.getRenderTarget();

}

_currentRenderTarget = renderTarget;
_currentActiveCubeFace = activeCubeFace;
_currentActiveMipmapLevel = activeMipmapLevel;
Expand Down
6 changes: 6 additions & 0 deletions src/renderers/webxr/WebXRManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ class WebXRManager extends EventDispatcher {

};

this.getRenderTarget = function () {

return newRenderTarget;

};

this.getFrame = function () {

return xrFrame;
Expand Down

0 comments on commit 3bc7bc0

Please sign in to comment.