如何让相机聚焦在物体上并使用 Babylonjs 将其保持在中心焦点?
Posted
技术标签:
【中文标题】如何让相机聚焦在物体上并使用 Babylonjs 将其保持在中心焦点?【英文标题】:How do I get the camera to focus on the object and keep it in center focus using Babylonjs? 【发布时间】:2021-02-27 11:54:57 【问题描述】:我真正想要的是把网格放在物体上,然后让相机聚焦在那个网格上。我认为他们使用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);
【问题讨论】:
【参考方案1】:我必须使用 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);
【讨论】:
以上是关于如何让相机聚焦在物体上并使用 Babylonjs 将其保持在中心焦点?的主要内容,如果未能解决你的问题,请参考以下文章