I have a working solution here, which behaves exactly the way I want.
However I am wondering is there is a more efficient way to do this. Maybe without having to update the camera settings at every frame?
Thanks!
I have a working solution here, which behaves exactly the way I want.
However I am wondering is there is a more efficient way to do this. Maybe without having to update the camera settings at every frame?
Thanks!
cc @PolygonalSun our input and camera expert
Solution looks pretty good but I would change one thing. If you’re changing the offset to a constant value (e.g. to move by your relativeOffsetY of 0.5), you only need to set it once. The reason I’m bringing this up is because it looks like you’re setting the targetOffset Vector on each rendered frame. The offset value will hold until you change it so you could either do something like this instead of adding an observer to scene.onBeforeRenderObservable
...
// 0: the camera's target is at the center of the viewport.
// 1: the camera's target is at the upper edge of the viewport.
const relativeOffsetY = 0.5;
let absoluteOffsetY = relativeOffsetY * camera.radius * Math.tan(camera.fov / 2);
camera.targetScreenOffset = new BABYLON.Vector2(0, absoluteOffsetY);
return scene;
...
You could also use scene.onBeforeRenderObservable.addOnce
instead of add
for a similar result.
Thanks for your suggestions @PolygonalSun, but I need the camera target to keep its screen position even when zooming in / out.
I guess what I was hoping for is a camera feature that I would have missed, and which would provide this behavior natively, possibly with less computations under the hood.
Building on your comment, I refactored the PG to reduce the number of operations executed at every frame. New version here. That should be pretty light on the CPU
May be just one warning using screenoffset of the camera, beware not to use parallax occlusion in your scene because it will break it. I had the ‘chance’ to experience this Else, your custom behavior now looks good. Bookmarked it for a possible later use.
Depending on the perspective effect one wants to obtain, there’s actually a simpler solution: to increase the dimensions of the viewport, and to offset its origin, like so.
This solution is also POM-friendly
See @mawa’s playground, modified here
smart thinking. I luv it. Thx,