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 …
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)
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
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).
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?)
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:
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.