MirrorTexture not updated when using screenshot with custom sized renderTargetTexture and doNotChangeAspectRatio = false

Hello !

I am using a MirrorTexture and I want to export a screenshot of custom size, using a custom render target texture.

What I do is that I set the width and height of the render target texture, and I set doNotChangeAspectRatio to false to keep a correct aspect ratio. However, when size is different than current render size, it looks like the mirror texture is not updated, thus leading to a wrong position of the reflections …

Here is a simple playground to test it : https://playground.babylonjs.com/#1YAIO7#807

If you uncomment line 50, it’s working fine as it’s the same render size. However, if you set a custom size (line 53), mirror texture is not updated.

For information, I tried a lot of things like manually set an engine resize + a manual mirror texture render in renderTarget.onAfterRenderObservable (or renderTarget.onBeforeRenderObservable), as well as trying to call scene.updateTransformMatrix(true)

Do someone could have any solution to this ?

Thanks a lot!

I do not repro with your code, I get this:

The aspect ratio issue is expected though.
What do you see on your side?

Can you also share your console log when you see the issue?

The only console log I have is the one is the code that only outputs the width and height variable.

I get also the same output you have, but I expected the reflection to be at the same position as the 3D view. As you can see in your image or in the downloaded screenshot, the reflection is distorted. I expected it to be at the same position at the original 3D view, even if the aspect ratio is modified


The first image is the 3D view and the second is the 1920,1080 exported screenshot

I would say it is expected: the image is mapped with a different size than the viewport so the image will be distorted.

Indeed, the whole image will be distorted, but I expected the reflection to be rendered again with the desired aspect ratio (caused by doNotChangeAspectRatio to false).

For a better example, here is a new PG : https://playground.babylonjs.com/#1YAIO7#814

This one explains better my issue : a box is placed on top of a mirror. The exported screenshot have broken reflections as it looks like the mirror texture is not rendered using the new aspect ratio


So I wanted to know if there is any chance we can still use doNotChangeAspectRatio to false while keeping the correct reflection (maybe by rendering mirror texture again using a new aspect ratio?)

I see. I unfortunately cannot immediately but le me ask @Evgeni_Popov or @sebavan to see if they can check

The mirror texture is generated only a single time during a frame, and will use the settings of the camera / render target currently active. The render target currently active when it renders is the default framebuffer, and because it’s the same target than when the plane/cube are rendered, the mirror texture matches correctly these objects.

But when you create a new RTT, the mirror texture is not regenerated, so it will use the same texture, which won’t work because the aspect ratio of this RTT is not the same than the aspect ratio of the default framebuffer.

You can overcome this problem by using a projection matrix that matches your RTT when the mirror transformation matrix is calculated. The code in MirrorTexture is as follows:

scene!.setTransformMatrix(this._transformMatrix, scene!.getProjectionMatrix());

We want to change the scene!.getProjectionMatrix() part. Fortunately, this code is executed in onBeforeRenderObservable of the mirror texture, so if we add our own observer to this observable, it will be executed after this code and will let us overwrite the texture passed to setTransformMatrix.

Here’s the fixed PG:

1 Like

Many, many thanks to both of you ! It’s working flawlessly !