mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
Merge pull request #2598 from pandamicro/poly3d
Tranform properties API polyfill for 3d
This commit is contained in:
+18
-13
@@ -508,6 +508,12 @@ var Node = cc.Class({
|
||||
cc.error(ERR_INVALID_NUMBER, 'new z');
|
||||
}
|
||||
}
|
||||
|
||||
// fast check event
|
||||
var cache = this._hasListenerCache;
|
||||
if (cache && cache[POSITION_CHANGED]) {
|
||||
this.emit(POSITION_CHANGED);
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
@@ -1113,7 +1119,7 @@ var Node = cc.Class({
|
||||
}
|
||||
},
|
||||
|
||||
// EVENTS
|
||||
// EVENT TARGET
|
||||
|
||||
/**
|
||||
* !#en
|
||||
@@ -1532,7 +1538,6 @@ var Node = cc.Class({
|
||||
if (CC_EDITOR) {
|
||||
var oldPosition = new cc.Vec2(locPosition);
|
||||
}
|
||||
|
||||
if (!CC_EDITOR || isFinite(x)) {
|
||||
locPosition.x = x;
|
||||
}
|
||||
@@ -1542,7 +1547,7 @@ var Node = cc.Class({
|
||||
if (!CC_EDITOR || isFinite(y)) {
|
||||
locPosition.y = y;
|
||||
}
|
||||
else {
|
||||
else {
|
||||
return cc.error(ERR_INVALID_NUMBER, 'y of new position');
|
||||
}
|
||||
this.setLocalDirty(POSITION_DIRTY_FLAG);
|
||||
@@ -1584,19 +1589,19 @@ var Node = cc.Class({
|
||||
* @param {Number} [scaleY]
|
||||
* @example
|
||||
* node.setScale(cc.v2(1, 1));
|
||||
* node.setScale(1, 1);
|
||||
* node.setScale(1);
|
||||
*/
|
||||
setScale (scaleX, scaleY) {
|
||||
if (scaleX && typeof scaleX !== 'number') {
|
||||
scaleY = scaleX.y;
|
||||
scaleX = scaleX.x;
|
||||
setScale (x, y) {
|
||||
if (x && typeof x !== 'number') {
|
||||
y = x.y;
|
||||
x = x.x;
|
||||
}
|
||||
else {
|
||||
scaleY = (scaleY || scaleY === 0) ? scaleY : scaleX;
|
||||
else if (y === undefined) {
|
||||
y = x;
|
||||
}
|
||||
if (this._scale.x !== scaleX || this._scale.y !== scaleY) {
|
||||
this._scale.x = scaleX;
|
||||
this._scale.y = scaleY;
|
||||
if (this._scale.x !== x || this._scale.y !== y) {
|
||||
this._scale.x = x;
|
||||
this._scale.y = y;
|
||||
this.setLocalDirty(SCALE_DIRTY_FLAG);
|
||||
|
||||
var cache = this._hasListenerCache;
|
||||
|
||||
@@ -30,6 +30,13 @@ const math = renderEngine.math;
|
||||
// ====== Node transform polyfills ======
|
||||
const ONE_DEGREE = Math.PI / 180;
|
||||
|
||||
const POSITION_CHANGED = 'position-changed';
|
||||
const ROTATION_CHANGED = 'rotation-changed';
|
||||
const SCALE_CHANGED = 'scale-changed';
|
||||
const POSITION_DIRTY_FLAG = 1 << 0;
|
||||
const SCALE_DIRTY_FLAG = 1 << 1;
|
||||
const ROTATION_DIRTY_FLAG = 1 << 2;
|
||||
|
||||
let _updateLocalMatrix2d = null;
|
||||
let _calculWorldMatrix2d = null;
|
||||
|
||||
@@ -75,16 +82,162 @@ function _calculWorldMatrix3d () {
|
||||
this._worldMatDirty = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* !#en Returns a copy of the position (x, y, z) of the node in its parent's coordinates.
|
||||
* !#zh 获取节点在父节点坐标系中的位置(x, y, z)。
|
||||
* @method getPosition
|
||||
* @return {Vec3} The position (x, y, z) of the node in its parent's coordinates
|
||||
*/
|
||||
function getPosition () {
|
||||
return new cc.Vec3(this._position);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Sets the position (x, y, z) of the node in its parent's coordinates.<br/>
|
||||
* Usually we use cc.v3(x, y, z) to compose cc.Vec3 object.<br/>
|
||||
* and Passing two numbers (x, y, z) is more efficient than passing cc.Vec3 object.
|
||||
* !#zh
|
||||
* 设置节点在父节点坐标系中的位置。<br/>
|
||||
* 可以通过两种方式设置坐标点:<br/>
|
||||
* 1. 传入 3 个数值 x, y, z。<br/>
|
||||
* 2. 传入 cc.v3(x, y, z) 类型为 cc.Vec3 的对象。
|
||||
* @method setPosition
|
||||
* @param {Vec3|Number} newPosOrX - X coordinate for position or the position (x, y, z) of the node in coordinates
|
||||
* @param {Number} [y] - Y coordinate for position
|
||||
* @param {Number} [z] - Z coordinate for position
|
||||
*/
|
||||
function setPosition (newPosOrX, y, z) {
|
||||
var x;
|
||||
if (y === undefined) {
|
||||
x = newPosOrX.x;
|
||||
y = newPosOrX.y;
|
||||
z = newPosOrX.z;
|
||||
}
|
||||
else {
|
||||
x = newPosOrX;
|
||||
}
|
||||
|
||||
var pos = this._position;
|
||||
if (pos.x === x && pos.y === y && pos.z === z) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (CC_EDITOR) {
|
||||
var oldPosition = new cc.Vec3(pos);
|
||||
}
|
||||
|
||||
pos.x = x;
|
||||
pos.y = y;
|
||||
pos.z = z || 0;
|
||||
this.setLocalDirty(POSITION_DIRTY_FLAG);
|
||||
|
||||
// fast check event
|
||||
var cache = this._hasListenerCache;
|
||||
if (cache && cache[POSITION_CHANGED]) {
|
||||
// send event
|
||||
if (CC_EDITOR) {
|
||||
this.emit(POSITION_CHANGED, oldPosition);
|
||||
}
|
||||
else {
|
||||
this.emit(POSITION_CHANGED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Get rotation of node (in quaternion).
|
||||
* !#zh 获取该节点的 quaternion 旋转角度。
|
||||
* @method getQuat
|
||||
* @return {cc.Quat} Quaternion object represents the rotation
|
||||
*/
|
||||
function getQuat () {
|
||||
return math.quat.clone(this._quat);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Set rotation of node (in quaternion).
|
||||
* !#zh 设置该节点的 quaternion 旋转角度。
|
||||
* @method setQuat
|
||||
* @param {cc.Quat} quat Quaternion object represents the rotation
|
||||
*/
|
||||
function setQuat (quat) {
|
||||
if (!this._quat.equals(value)) {
|
||||
math.quat.copy(this._quat, value);
|
||||
this.setLocalDirty(ROTATION_DIRTY_FLAG);
|
||||
|
||||
var cache = this._hasListenerCache;
|
||||
if (cache && cache[ROTATION_CHANGED]) {
|
||||
this.emit(ROTATION_CHANGED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Returns the scale of the node.
|
||||
* !#zh 获取节点的缩放。
|
||||
* @method getScale
|
||||
* @return {cc.Vec3} The scale factor
|
||||
*/
|
||||
function getScale () {
|
||||
return cc.v3(this._scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* !#en Sets the scale of three axis in local coordinates of the node.
|
||||
* !#zh 设置节点在本地坐标系中三个坐标轴上的缩放比例。
|
||||
* @method setScale
|
||||
* @param {Number|Vec3} x - scaleX or scale object
|
||||
* @param {Number} [y]
|
||||
* @param {Number} [z]
|
||||
* @example
|
||||
* node.setScale(cc.v2(2, 2, 2));
|
||||
* node.setScale(2);
|
||||
*/
|
||||
function setScale (x, y, z) {
|
||||
if (x && typeof x !== 'number') {
|
||||
y = x.y;
|
||||
z = x.z || 1;
|
||||
x = x.x;
|
||||
}
|
||||
else if (x !== undefined && y === undefined) {
|
||||
y = x;
|
||||
z = x;
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
if (this._scale.x !== x || this._scale.y !== y) {
|
||||
this._scale.x = x;
|
||||
this._scale.y = y;
|
||||
this.setLocalDirty(SCALE_DIRTY_FLAG);
|
||||
|
||||
var cache = this._hasListenerCache;
|
||||
if (cache && cache[SCALE_CHANGED]) {
|
||||
this.emit(SCALE_CHANGED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
enabled: false,
|
||||
enable () {
|
||||
let proto = cc.Node.prototype;
|
||||
if (!_updateLocalMatrix2d) {
|
||||
_updateLocalMatrix2d = cc.Node.prototype._updateLocalMatrix;
|
||||
_calculWorldMatrix2d = cc.Node.prototype._calculWorldMatrix;
|
||||
_updateLocalMatrix2d = proto._updateLocalMatrix;
|
||||
_calculWorldMatrix2d = proto._calculWorldMatrix;
|
||||
}
|
||||
if (!this.enabled) {
|
||||
cc.Node.prototype._updateLocalMatrix = _updateLocalMatrix3d;
|
||||
cc.Node.prototype._calculWorldMatrix = _calculWorldMatrix3d;
|
||||
proto._updateLocalMatrix = _updateLocalMatrix3d;
|
||||
proto._calculWorldMatrix = _calculWorldMatrix3d;
|
||||
|
||||
proto.getPosition = getPosition;
|
||||
proto.setPosition = setPosition;
|
||||
proto.getScale = getScale;
|
||||
proto.setScale = setScale;
|
||||
this.enabled = true;
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user