开源改变世界

使相机适合 3D 可视化器中的对象 #9

推推 grbl 3年前 (2023-02-01) 281次浏览
关闭
cheton 打开了这个问题 2015 年 11 月 18 日 · 3条评论
关闭

使相机适合 3D 可视化器中的对象#9

cheton 打开了这个问题 2015 年 11 月 18 日 · 3条评论

注释

使相机适合 3D 可视化器中的对象 #9 奇顿 添加了 增强 标签 2015 年 11 月 18 日
使相机适合 3D 可视化器中的对象 #9 cheton 自己分配了这个 2015 年 11 月 18 日
使相机适合 3D 可视化器中的对象 #9
合作者作者

更新:
v0.8.1:在渲染 G 代码文件后,将 3D 可视化器的旋转轴心点设置为对象的中心位置。

使相机适合 3D 可视化器中的对象 #9
合作者作者

Todo:将 3D 可视化器中的旋转轴心点设置为铣刀的当前位置。

更新:在 0.9.x 中修复

cheton 添加了引用此问题的提交 2015 年 12 月 15 日

使相机适合 3D 可视化器中的对象 #9
合作者作者

来源:http ://stackoverflow.com/questions/14614252/how-to-fit-camera-to-object

要使对象高度与可见高度相匹配:

fov = 2 * Math.atan(height / (2 * dist)) * ( 180 / Math.PI); // in degrees

要使对象宽度与可见宽度匹配:

fov = 2 * Math.atan((width / aspect) / (2 * dist)) * (180 / Math.PI); // in degrees

该算法已在v0.10.4中实现。

const CAMERA_DEFAULT_FOV = 50;

// Fits camera to object
// @param {number} width The object width
// @param {number} height The object height
// @param {THREE.Vector3} [lookTarget] The object's top position which is nearest to the camera
fitCameraToObject(width, height, lookTarget) {
    console.assert(_.isNumber(width));
    console.assert(_.isNumber(height));
    console.assert(lookTarget instanceof THREE.Vector3);

    let v1 = (lookTarget instanceof THREE.Vector3) ? lookTarget : new THREE.Vector3(0, 0, 0);
    let v2 = new THREE.Vector3(
        this.camera.position.x,
        this.camera.position.y,
        this.camera.position.z
    );
    let dist = v1.distanceTo(v2); // the distance from the camera to the closest face of the object
    let aspect = this.camera.aspect; // the aspect ratio of the canvas (width / height)

    width = Number(width) || 0;
    height = Number(height) || 0;

    // Pick the largest camera field-of-view
    let fov = Math.max(
        2 * Math.atan(height / (2 * dist)) * (180 / Math.PI),
        2 * Math.atan((width / aspect) / (2 * dist)) * (180 / Math.PI)
    );
    fov = fov || CAMERA_DEFAULT_FOV;

    this.camera.fov = fov;
    this.camera.updateProjectionMatrix();
}

使用示例可能如下所示:

let objectWidth = dimension.delta.x;
let objectHeight = dimension.delta.y;
let lookTarget = new THREE.Vector3(0, 0, dimension.max.z);

this.fitCameraToObject(objectWidth, objectHeight, lookTarget);