cocos creator第三人称视角下查看模型
编译器版本 3.5.2
监听键盘和滚轮事件(使用wasd转动模型,滚轮缩放)
static selfAction(node:Node) {
input.on(Input.EventType.KEY_DOWN, (event: EventKeyboard)=>{
if (commonUtil.actionState) {
viewUtil.viewMode(node,event,this.rotax,this.rotay)
}
}, this);
input.on(Input.EventType.KEY_PRESSING, (event: EventKeyboard)=>{
if (commonUtil.actionState) {
viewUtil.viewMode(node,event,this.rotax,this.rotay)
}
}, this);
input.on(Input.EventType.MOUSE_WHEEL, (event: EventMouse)=>{
if (commonUtil.actionState) {
scaleUtil.wheelToModelSize(node,event.getScrollY()>0?true:false,1.2)
}
}, this);
}
static viewMode(node:Node,event: EventKeyboard,rotax:number,rotay: number){
let rota_target=new Vec3();
let rota_move = new Quat();
let rota_cur = new Quat();
node.getRotation(rota_cur);
//四元数转欧拉角
Quat.toEuler(rota_move, rota_cur);
switch(event.keyCode) {
case KeyCode.KEY_W:
if(rota_move.x<=0&&rota_move.x>=-180){
if(rota_move.x-rotax<-180){
rota_target = new Vec3(rota_move.x-rotax+360,rota_move.y, 0);
}else{
rota_target = new Vec3(rota_move.x-rotax, rota_move.y, 0);
}
}else{
rota_target = new Vec3(rota_move.x-rotax, rota_move.y, 0);
}
break;
case KeyCode.KEY_S:
if(rota_move.x>=0&&rota_move.x<=180){
if(rota_move.x+rotax>180){
rota_target = new Vec3(rotax+rota_move.x-360,rota_move.y, 0);
}else{
rota_target = new Vec3(rota_move.x+rotax, rota_move.y, 0);
}
}else{
rota_target = new Vec3(rota_move.x+rotax, rota_move.y, 0);
}
break;
case KeyCode.KEY_A:
if(rota_move.y<=0&&rota_move.y>=-180){
if(rota_move.y-rotay>-180){
rota_target = new Vec3(rota_move.x,rota_move.y-rotay+360, 0);
}else{
rota_target = new Vec3(rota_move.x,rota_move.y-rotay, 0);
}
}else{
rota_target = new Vec3(rota_move.x,rota_move.y-rotay, 0);
}
break;
case KeyCode.KEY_D:
if(rota_move.y<=0&&rota_move.y<=180){
if(rota_move.y+rotay>180){
rota_target = new Vec3(rota_move.x, rota_move.y+rotay-360, 0);
}else{
rota_target = new Vec3(rota_move.x, rota_move.y+rotay, 0);
}
}else{
rota_target = new Vec3(rota_move.x, rota_move.y+rotay, 0);
}
break;
}
node.setRotationFromEuler(rota_target);
}
由于获取到的模型角度有负值,所以需要判断,进行计算。
static wheelToModelSize(node:Node,wheel:boolean,level:number){
let scale=new Vec3();
node.getScale(scale);
if(wheel){
node.setScale(scale.x*level,scale.y*level,scale.z*level)
}else{
node.setScale(scale.x/level,scale.y/level,scale.z/level)
}
}
根据比例进行缩放。