这个是hello word 模板,我改动了一下,我直接贴代码把
import { _decorator, Component, Node, Vec3, Quat, EventTouch, log, CCLoader, Camera, EventMouse } from ‘cc’;
const { ccclass, property } = _decorator;
@ccclass('Camera3dMgr')
export class Camera3dMgr extends Component {
@property(Camera)
private camera3d: Camera = null;
@property(Node)
private targetNode: Node = null;
onEnable() {
console.log(this)
this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
this.node.on(Node.EventType.MOUSE_WHEEL, this.onMousewheel, this);
}
onDisable() {
this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
this.node.off(Node.EventType.MOUSE_WHEEL, this.onMousewheel, this);
}
private _curPos = new Vec3;
private _curRot = new Quat;
private onTouchMove(event: EventTouch) {
this.camera3d.node.getWorldPosition(this._curPos);
this.camera3d.node.getWorldRotation(this._curRot);
const delta = event.getDelta();
const dx = delta.x / 300;
const dy = delta.y / 300;
console.log(delta);
const rot = new Quat(this._curRot);
const euler = new Vec3();
Quat.rotateX(rot, rot, dy);
Quat.rotateAround(rot, rot, Vec3.UNIT_Y, -dx);
Quat.toEuler(euler, rot);
Quat.fromEuler(rot, euler.x, euler.y, 0);
this._rot = rot;
this.setVisualAngle(this._rot, this._depth);
}
private _rot: Quat = new Quat;
private _depth: number = 15;
private onMousewheel(event: EventMouse) {
let scrolly = event.getScrollY();
if (scrolly > 0) {
this._depth += 1
} else {
this._depth -= 1
}
this.setVisualAngle(this._rot, this._depth);
console.log(scrolly)
}
private setVisualAngle(rot: Quat, depth: number) {
const offset = new Vec3(0, 0, 1);
Vec3.transformQuat(offset, offset, rot);
Vec3.normalize(offset, offset);
Vec3.multiplyScalar(offset, offset, depth);
Vec3.add(this._curPos, this.targetNode.worldPosition, offset);
this.camera3d.node.setWorldPosition(this._curPos);
const up = new Vec3(0, 1, 0);
Vec3.transformQuat(up, up, rot);
Vec3.normalize(up, up);
this.camera3d.node.lookAt(this.targetNode.worldPosition, up);
}
}