在threejs中实现DollyZoom(希区柯克变焦)

本文介绍了如何在Three.js中创建DollyOutZoomIn和DollyInZoomOut类,实现摄像机的外退内进效果,通过计算目标点和当前点的距离调整视场和摄像机位置。
摘要由CSDN通过智能技术生成

DollyOutZoomIn

项目参见:

GitHub - DragonsZhou/threejs-DollyZoom

核心代码

import * as THREE from 'three';
export class DollyZoom {
    // nearPoint, farPoint,targetPos: Vector3
    // targetPos为被观察物体的坐标
    nearPoint
    farPoint
    targetPos
    nearFov    // nearFov[0,90]

    constructor(nPoint, fPoint, tPos, nFov) {
        this.nearPoint = nPoint;
        this.farPoint = fPoint;
        this.targetPos = tPos;
        this.nearFov = nFov;
        this._init()
    }

    _line1
    _targetPoint
    _distance2
    _x
    _init() {
        this._line1 = new THREE.Line3(this.nearPoint, this.farPoint);
        let targetPosProgress = this._line1.closestPointToPointParameter(this.targetPos)
        this._targetPoint = this._line1.at(targetPosProgress, new THREE.Vector3())
        let line2 = new THREE.Line3(this.nearPoint, this._targetPoint)
        this._distance2 = line2.distance()

        let radFov = this.nearFov / 2 * Math.PI / 180
        this._x= Math.tan(radFov) * this._distance2
    }

    // 外退内进(Dolly-out & Zoom-in)  curProgress: 0->1
    // 外进内退(Dolly-in & Zoom-out)  curProgress: 1->0
    // curProgress [0,1]
    getCurrent(curProgress) {
        let currentPoint = this._line1.at(curProgress, new THREE.Vector3())
        let line3 = new THREE.Line3(currentPoint, this._targetPoint)
        let distance3 = line3.distance()
        
        let curFov = Math.atan(this._x/distance3) / Math.PI * 180 * 2
        return {
            currentPoint: currentPoint,
            curFov: curFov - 3,
            // -3是为了修正偏差
            lookat: this._targetPoint,
        }
    }
}

export class DollyOutZoomIn extends DollyZoom{
    curPos= 0
    spead = 0.005
    constructor(nPoint, fPoint, tPos, nFov){
        super(nPoint, fPoint, tPos, nFov)
    }
    reset(){
        this.curPos= 0
        this.spead = 0.005
    }
    defaultDollyOutZoomIn(camera) {
        if (this.curPos <= 1) {
            this.curPos += this.spead
            let tmp = this.getCurrent(this.curPos)
            camera.position.set(tmp.currentPoint.x, tmp.currentPoint.y, tmp.currentPoint.z)
            camera.fov = tmp.curFov
            camera.lookAt(tmp.lookat);
            camera.updateProjectionMatrix()
        }
    }
}
export class DollyInZoomOut extends DollyZoom{
    curPos= 1
    spead = 0.005
    constructor(nPoint, fPoint, tPos, nFov){
        super(nPoint, fPoint, tPos, nFov)
    }
    reset(){
        this.curPos= 1
        this.spead = 0.005
    }
    defaultDollyInZoomOut(camera) {
        if (this.curPos >= 0) {
            this.curPos -= this.spead
            let tmp = this.getCurrent(this.curPos)

            camera.position.set(tmp.currentPoint.x, tmp.currentPoint.y, tmp.currentPoint.z)
            camera.fov = tmp.curFov
            camera.lookAt(tmp.lookat);
            camera.updateProjectionMatrix()
        }
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值