fixed switch between 2d/3d rotation (#3207)

This commit is contained in:
yinkaile
2018-09-06 15:27:31 +08:00
committed by pandamicro
parent bd29a6db8c
commit 5de0723e4b
2 changed files with 28 additions and 16 deletions
+9 -4
View File
@@ -246,15 +246,17 @@ function _update3DFunction () {
this._updateLocalMatrix = _updateLocalMatrix3d;
this._calculWorldMatrix = _calculWorldMatrix3d;
this._mulMat = cc.vmath.mat4.mul;
this._syncEulerAngles = _syncEulerAngles;
}
else {
this._updateLocalMatrix = _updateLocalMatrix2d;
this._calculWorldMatrix = _calculWorldMatrix2d;
this._mulMat = _mulMat2d;
this._syncEulerAngles = _syncEulerAngles2d;
}
}
function _apply3DNode () {
function _upgrade_1x_to_2x () {
if (CC_EDITOR && this instanceof cc.Scene) {
this._is3DNode = true;
}
@@ -263,12 +265,15 @@ function _apply3DNode () {
this._update3DFunction();
}
this._syncEulerAngles();
_upgrade_1x_to_2x_2d.call(this);
}
let proto = cc.Node.prototype;
const _updateLocalMatrix2d = proto._updateLocalMatrix;
const _calculWorldMatrix2d = proto._calculWorldMatrix;
const _upgrade_1x_to_2x_2d = proto._upgrade_1x_to_2x;
const _syncEulerAngles2d = proto._syncEulerAngles;
const _mulMat2d = proto._mulMat;
const _onBatchCreated2d = proto._onBatchCreated;
@@ -280,8 +285,7 @@ proto.setPosition = setPosition;
proto.getScale = getScale;
proto.setScale = setScale;
proto._upgrade_1x_to_2x = _apply3DNode;
proto._syncEulerAngles = _syncEulerAngles;
proto._upgrade_1x_to_2x = _upgrade_1x_to_2x;
proto._update3DFunction = _update3DFunction;
cc.js.getset(proto, 'position', getPosition, setPosition, false, true);
@@ -293,6 +297,7 @@ cc.js.getset(proto, 'is3DNode', function () {
if (this._is3DNode === v) return;
this._is3DNode = v;
this._update3DFunction();
this._syncEulerAngles();
});
cc.js.getset(proto, 'quat', getQuat, setQuat);
+19 -12
View File
@@ -1243,6 +1243,19 @@ var Node = cc.Class({
// INTERNAL
_syncEulerAngles () {
let quat = this._quat;
let rotx = quat.getRoll();
let roty = quat.getPitch();
if (rotx === 0 && roty === 0) {
this._rotationX = this._rotationY = -quat.getYaw();
}
else {
this._rotationX = rotx;
this._rotationY = roty;
}
},
_upgrade_1x_to_2x () {
// Upgrade scaleX, scaleY from v1.x
// TODO: remove in future version, 3.0 ?
@@ -1262,25 +1275,19 @@ var Node = cc.Class({
// TODO: remove _rotationX & _rotationY in future version, 3.0 ?
// Update quaternion from rotation, when upgrade from 1.x to 2.0
// If rotation x & y is 0 in old version, then update rotation from default quaternion is ok too
if (this._rotationX !== 0 || this._rotationY !== 0) {
let quat = this._quat;
if ((this._rotationX !== 0 || this._rotationY !== 0) &&
(quat.x === 0 && quat.y === 0 && quat.z === 0 && quat.w === 1)) {
if (this._rotationX === this._rotationY) {
math.quat.fromEuler(this._quat, 0, 0, -this._rotationX);
math.quat.fromEuler(quat, 0, 0, -this._rotationX);
}
else {
math.quat.fromEuler(this._quat, this._rotationX, this._rotationY, 0);
math.quat.fromEuler(quat, this._rotationX, this._rotationY, 0);
}
}
// Update rotation from quaternion
else {
let rotx = this._quat.getRoll();
let roty = this._quat.getPitch();
if (rotx === 0 && roty === 0) {
this._rotationX = this._rotationY = -this._quat.getYaw();
}
else {
this._rotationX = rotx;
this._rotationY = roty;
}
this._syncEulerAngles();
}
// Upgrade from 2.0.0 preview 4 & earlier versions