Radius-independent screen offset of ArcRotateCamera target

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?


1 Like

cc @PolygonalSun our input and camera expert

1 Like

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.

1 Like

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 :slight_smile:


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 :wink: Else, your custom behavior now looks good. Bookmarked it for a possible later use. :smiley:

1 Like

Oh good to know @mawa, thanks for the info!

Adding a reference to your post on this issue, here.

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 :slight_smile:
See @mawa’s playground, modified here

1 Like

smart thinking. I luv it. Thx, :smiley: