微信小程序中实现对3D模型的单指旋转双指缩放移动

<canvas id="webgl" canvas-id="webgl" class="webgl" type="webgl" disable-scroll="{{true}}" bindtouchmove='ontouchmove' bindtouchend="ontouchend"></canvas>
let first = true,
    isTwoTouch = false,
    twoFirst = true,
    lastPositon = {},
    lastDistance,
    lastPoint = {},
    steps = 1;
ontouchmove(e) {
        // console.log("e", e);
        let touches = e.touches;
        if (touches.length === 1) {
            if (isTwoTouch) {
                return;
            }
            if (first) {
                lastPositon.x = touches[0].x;
                lastPositon.y = touches[0].y;
                first = false;
            } else {
                let deltaY = (touches[0].y - lastPositon.y) / 100;
                let deltaX = (touches[0].x - lastPositon.x) / 100;
                lastPositon.x = touches[0].x;
                lastPositon.y = touches[0].y;
                currModel.rotation.x += deltaY;
                currModel.rotation.y += deltaX;
            }
        } else if (touches.length === 2) {
            isTwoTouch = true;
            let t1 = touches[0],
                t2 = touches[1];
            if (twoFirst) {
                lastDistance = this.calDistance(t1, t2);
                lastPoint = t1;
                twoFirst = false;
            } else {
                let currDistance = this.calDistance(t1, t2);
                let factor = 0.001 * (currDistance - lastDistance);
                if (Math.abs(factor) < 0.001) {
                    // 移动
                    var disX = (touches[0].x - lastPoint.x) * 0.01;
                    var disY = -(touches[0].y - lastPoint.y) * 0.01;
                    currModel.position.x += disX;
                    currModel.position.y += disY;
                } else {
                    currModel.scale.x += factor;
                    currModel.scale.y += factor;
                    currModel.scale.z += factor;
                }
                lastDistance = currDistance;
                lastPoint.x = touches[0].x;
                lastPoint.y = touches[0].y;
            }
        }
    },
    calDistance(t1, t2) {
        let v1 = new THREE.Vector2(t1.x, t1.y);
        let v2 = new THREE.Vector2(t2.x, t2.y);
        return v1.distanceTo(v2);
    },
    ontouchend() {
        first = true;
        twoFirst = true;
        lastPositon.x = 0;
        lastPositon.y = 0;
        lastPoint.x = 0;
        lastPoint.y = 0;
        isTwoTouch = false;
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值