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

Transmission not working properly when a Reflector is added to the scene #27846

Closed
LR17 opened this issue Feb 29, 2024 · 8 comments · Fixed by #27886
Closed

Transmission not working properly when a Reflector is added to the scene #27846

LR17 opened this issue Feb 29, 2024 · 8 comments · Fixed by #27886
Labels
Milestone

Comments

@LR17
Copy link
Contributor

LR17 commented Feb 29, 2024

Description

The transmission effect is not correct when a Reflector is added to the scene and facing the camera. It seems that the WebGLRenderer.render() invoked inside Reflector.onBeforeRender() alters the internal state of the renderer while it is rendering the transmissive object.

Reproduction steps

  1. Create a material with transmission
  2. Add a Reflector to the scene

Live example

Screenshots

No response

Version

dev

Device

No response

Browser

No response

OS

No response

@LR17
Copy link
Contributor Author

LR17 commented Mar 7, 2024

I've investigated the issue and found that the problem is the renderer _transmissionRenderTarget being overwritten when rendering the transmission pass triggered by the Reflector's render.

To workaround the problem I added 2 new methods to WEBGLRenderer:

this.setTransmissionRenderTarget = function (renderTarget) {
	_transmissionRenderTarget = renderTarget;
}

this.getTransmissionRenderTarget = function () {
	return _transmissionRenderTarget;
}

Then added a render target for transmission to Reflector class and used it in onBeforeRender and onAfterRender callbacks:

// Reflector: constructor

this.transmissionRenderTarget = new THREE.WebGLRenderTarget(1, 1, {
    generateMipmaps: true,
    type: THREE.HalfFloatType,
    minFilter: THREE.LinearMipmapLinearFilter,
    samples: 4
})


// Reflector: OnBeforeRender

this.transmissionRenderTargetSwap = renderer.getTransmissionRenderTarget();
const size = renderer.getDrawingBufferSize(new THREE.Vector2());
this.transmissionRenderTarget.setSize(size.x, size.y);
renderer.setTransmissionRenderTarget(this.transmissionRenderTarget); 


// Reflector: OnAfterRender

renderer.setTransmissionRenderTarget(this.transmissionRenderTargetSwap);

In this way transmission is renderered correctly. In my particular use case I also use a transparent background and found that this line in WEBGLRenderer is a bit arbitrary because the alpha setting is hardcoded. It would be useful to be able to control the alpha/clear color for transmission.

@Mugen87
Copy link
Collaborator

Mugen87 commented Mar 7, 2024

In my particular use case I also use a transparent background and found that this line in WEBGLRenderer is a bit arbitrary because the alpha setting is hardcoded. It would be useful to be able to control the alpha/clear color for transmission.

You are referring to:

if ( _currentClearAlpha < 1 ) _this.setClearColor( 0xffffff, 0.5 );

Some background information about the mentioned line: #25819 (comment)

@lo-th

This comment was marked as off-topic.

@Mugen87

This comment was marked as off-topic.

@Mugen87

This comment was marked as off-topic.

@Mugen87

This comment was marked as off-topic.

@Mugen87
Copy link
Collaborator

Mugen87 commented Mar 8, 2024

Um, issues like that show how tricky the transmission implementation is^^.

@LR17 Instead of having setTransmissionRenderTarget() and getTransmissionRenderTarget(), I think we can implement this in a way so it works for all logic that uses nested render calls.

The idea is to have a transmission render target for each render state. Can you please give the following commit a try and see if it solves the issue? Mugen87@449b914

@LR17
Copy link
Contributor Author

LR17 commented Mar 8, 2024

Yes, it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants