I am working on a scene using the render target texture and I noticed a bug (or I am missing something?). If I set the refresh rate of my rtt to render once an call rtt.render() the rtt renders the active camera of the scene and not the camera attached to the rtt.
I created a pg. If you move the camera to a different perspective and press “r” the rtt generates an image of the scene. But as I said it uses the wrong camera.
Hi!
You can achieve the effect you want by setting layerMasks for the objects and the cameras. Add both cameras as active.
Look at this PG. The RTT will be refreshed every second. It will always render the box from above and the camera will get closer to the box each refresh. The forceRender variable can be left out and you can render the RTT directly in the setInterval method or anywhere else. I’ve just introduced it to better control when to render the RTT.
You are welcome!
I’m sorry, I don’t know the correct answer to your question, but I believe this is the correct approach for multiple camera rendering. Maybe someone more experienced will enlighten us🤗
Weird! I adjusted my code exactly following your pg but it always renders the object with the perspective of the second camera above the normal scene with the first camera. Event though setting the layer mask of camera2 to 0 it is shown. I assumed that .layerMask = 0 doesn’t render it at all. Any ideas what the problem could be? Setting layerMask of meshes to 0 makes them invisible as expected. But anyhow it won’t work for cameras.
I am using this approach all the time and I never had this problem. I suppose, something must be tweaked in your code to make it working. The best way to help you would be to see all your code, but I know it’s quite a hard decision to share private code. However there is still a chance that you have discovered another bug.
As far as I know freezing meshes makes sense at the startup, when you already crated your static meshes you are not going to manipulate anymore. So you freeze them. The engine will not update the frozen ones so you gain some performance boost on the cpu side.
If you haven’t read this yet, I highly encourage you to do so:
Have a nice day!
EDIT: just realized that you have issues rendering a frozen mesh into the RTT. I bet @sebavan 's trick will solve this issue.
Exactly. I am fine with unfreezing meshes temporarily but it seems that freezing meshes leads to this overlap of the layer masks in general. The layer mask of camera2 should just exist to create a snapshot of the scene from this perspective but the output should be invisible.
It would be too good to be true if it just worked straight away.
Thanks again for your approach, @roland. Is it okay to freeze and unfreeze on every frame? I am afraid that the performance gains get decreased due to this method.
A second scene should be possible. I hoped I can render the RTT without that, but it doesn’t seem so obvious to achieve it.
But I got an idea to render the RTT without a second scene and even without layer masks. Am I not able to change the camera perspective right before rendering the RTT and resetting it afterwards? https://playground.babylonjs.com/#N2FNND#17
Do you need to freeze and unfreeze every frame? I thought you need to render the RTT on demand. In that case you don’t need this: rtt.refreshRate = BABYLON.RenderTargetTexture.REFRESHRATE_RENDER_ONCE
Anyway, cool trick how you solved the problem! The most basic solutions are always the best! LOL!
Therefore I was conscious if this is the most performant way if you call freeze/unfreeze on every render.
Since my whole scene only renders on demand the short manipulation of the camera perspective is not visible to the user. So changing the camera perspective right before rendering the RTT works for my case