先凑合一下。。。
`const { ccclass, property } = cc._decorator;
@ccclass
export default class Drag3dObj extends cc.Component {
@property(cc.Node)
target: cc.Node = null;
@property(cc.Camera)
cam: cc.Camera = null;
onEnable() {
this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchMove, this)
this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
let targetpos = cc.v3()
this.target.getWorldPosition(targetpos)
const camPos = cc.v3()
this.cam.node.getWorldPosition(camPos)
const camRot = cc.quat()
this.cam.node.getWorldRotation(camRot)
const f = cc.v3()
cc.Vec3.transformQuat(f, cc.Vec3.FRONT, camRot)
this.dis = targetpos.sub(camPos).project(f).mag()
}
onDisable() {
this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchMove, this)
this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
}
dis: number = 10
onTouchMove(event: cc.Event.EventTouch) {
const epos = event.getLocation()
const size = cc.view.getVisibleSize()
epos.x = epos.x - size.width / 2
epos.y = epos.y - size.height / 2
const camPos = cc.v3()
this.cam.node.getWorldPosition(camPos)
const camRot = cc.quat()
this.cam.node.getWorldRotation(camRot)
const f = cc.v3()
cc.Vec3.transformQuat(f, cc.v3(0, 0, -1), camRot)
const r = cc.v3()
cc.Vec3.transformQuat(r, cc.Vec3.RIGHT, camRot)
const u = cc.v3()
cc.Vec3.transformQuat(u, cc.Vec3.UP, camRot)
const h = this.dis * Math.tan(Math.PI / (180 / this.cam.fov * 2)) * 2
const w = h * size.width / size.height
const targetpos = camPos.add(f.mul(this.dis)).add(u.mul(epos.y / size.height * h)).add(r.mul(epos.x / size.width * w))
this.target.setWorldPosition(targetpos)
}
}
`