How to parent/sync two same cameras

Hi everyone,

Two arc cameras. One should always see exactly what the other one sees. Parenting was advised but I cannot get it work.

Please see: https://playground.babylonjs.com/#JU1DZP#122

Best wishes
Joe

This is how I did it at https://glb.babylonpress.org/
You may not need 2 viewports, of course.

        camera.viewport = new Viewport(0, 0, 0.5, 1.0);

        const camera2 = camera.clone("camera2");

        this._scene.activeCameras!.push(camera);
        this._scene.activeCameras!.push(camera2);

        camera2.viewport = new Viewport(0.5, 0, 0.5, 1.0);
        camera2.attachControl();

        camera2.layerMask = 0x20000000;

        this._scene.onBeforeCameraRenderObservable.add(function () {
            (camera2 as any).alpha = (camera as any).alpha;
            (camera2 as any).beta = (camera as any).beta;
            (camera2 as any).radius = (camera as any).radius;
            (camera2 as any).target = (camera as any).target;
        });
    }

    this._scene.activeCamera!.attachControl();
5 Likes

I agree with @labris’s method.

When copying a specific user action in a large-scale multi-game, it seems cheapest to copy the value calculated on the server and ultimately given to the user.

As a result, how about using only one camera per user and copying only the value in certain situations (the type of camera is the same as the camera of the observing user, but cannot be controlled).

Or are you thinking of a case like “It Takes Two”?

Ohh wow. I have just gotten some weird glitches. The goal was to exclude some meshes from postprocesses like here Sharing depth buffer between two cameras - #8 by Evgeni_Popov. But when I parent the camera it glitches out. So parenting is off the table.

So, just do what @labris said :+1:

An alternative (havent tried yet) might be using one of these: camera.onAfterCheckInputsObservable or camera.onProjectionMatrixChangedObservable. This way you would only update the camera if it actually changed. But it is more code (listener tacking/cleanup).

2 Likes