如何使用 Babylonjs 让相机聚焦在物体上并使其保持在中心焦点?

How do I get the camera to focus on the object and keep it in center focus using Babylonjs?

我真正想要的是将网格放在对象上并将相机聚焦在该网格上。我想他们是用 lookAt 函数来做的,但我不知道如何正确使用它。

我从这个页面得到了帮助:https://www.babylonjs-playground.com/#1CSVHO#12

我尝试了一些功能演示。

 setCamera_Mesh = () => {
        let { currentWidth, currentDepth, rowCount } = this.currentConfig;
        let sphere = Mesh.CreateSphere("sphere", 1, this.scene);
        let referenceBox = Mesh.CreateBox("referenceBox", { width: 1, height: 1, depth: 1, updatable: true });


        sphere.scaling = new Vector3(0.1, 0.1, 0.1);
        sphere.position = this.scene.cameras[0].position;
        sphere.parent = this.scene.cameras[0];

        this.referenceBox && this.referenceBox.dispose()

        referenceBox.position = new Vector3(0, 0, 0.08);

        referenceBox.enableEdgesRendering();
        referenceBox.edgesWidth = 1;
        referenceBox.edgesColor = new Color4(0, 0, 1, 0.05);
        referenceBox.visibility = 0.5;
        referenceBox.scaling = new Vector3(currentDepth / 40, rowCount / 3, currentWidth / 100);


        this.referenceBox = referenceBox;
        sphere.lookAt(referenceBox.position);
    }

我必须使用 setParent () 而不是 .parent 来添加相机作为父对象。

当我如下编辑代码时,它工作正常。

setCameraToMesh = () => {
        let { currentWidth, currentDepth, rowCount } = this.currentConfig;
        let sphere = MeshBuilder.CreateSphere("referenceSphere", this.scene);
        let referenceBox = MeshBuilder.CreateBox("referenceBox", { width: 0.1, height: 0.1, depth: 0.1, updatable: true });

        sphere.scaling = new Vector3(0.1, 0.1, 0.1);
        sphere.position = this.scene.cameras[0].position;
        sphere.setParent(this.scene.cameras[0]);
        this.referenceBox && this.referenceBox.dispose()

        referenceBox.position = new Vector3(0, rowCount / 500, 0.08);
        referenceBox.enableEdgesRendering();
        referenceBox.edgesWidth = 1;
        referenceBox.edgesColor = new Color4(0, 0, 1, 0.05);
        referenceBox.visibility = 0.05;
        referenceBox.scaling = new Vector3(currentDepth / 40, rowCount / 3, currentWidth / 100);

        this.referenceBox = referenceBox;

        sphere.lookAt(referenceBox.absolutePosition);    
        this.scene.cameras[0].focusOn([referenceBox], true);
    }