From 661c5327e70380347690a2d866e99736df940ba3 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 26 Apr 2018 09:35:48 +0800 Subject: [PATCH 1/3] Tranform properties API polyfill for 3d --- cocos2d/core/CCNode.js | 40 +++----- cocos2d/core/utils/polyfill-3d.js | 155 +++++++++++++++++++++++++++++- 2 files changed, 166 insertions(+), 29 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 101bbaf3e4..fcaf75964b 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1113,7 +1113,7 @@ var Node = cc.Class({ } }, - // EVENTS + // EVENT TARGET /** * !#en @@ -1496,7 +1496,7 @@ var Node = cc.Class({ * cc.log("Node Position: " + node.getPosition()); */ getPosition () { - return new cc.Vec2(this._position); + return new cc.Vec3(this._position); }, /** @@ -1533,18 +1533,8 @@ var Node = cc.Class({ var oldPosition = new cc.Vec2(locPosition); } - if (!CC_EDITOR || isFinite(x)) { - locPosition.x = x; - } - else { - return cc.error(ERR_INVALID_NUMBER, 'x of new position'); - } - if (!CC_EDITOR || isFinite(y)) { - locPosition.y = y; - } - else { - return cc.error(ERR_INVALID_NUMBER, 'y of new position'); - } + locPosition.x = x; + locPosition.y = y; this.setLocalDirty(POSITION_DIRTY_FLAG); // fast check event @@ -1580,23 +1570,23 @@ var Node = cc.Class({ * !#en Sets the scale factor of the node. 1.0 is the default scale factor. This function can modify the X and Y scale at the same time. * !#zh 设置节点的缩放比例,默认值为 1.0。这个函数可以在同一时间修改 X 和 Y 缩放。 * @method setScale - * @param {Number|Vec2} scaleX - scaleX or scale + * @param {Number|Vec3} scaleX - scaleX or scale * @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; diff --git a/cocos2d/core/utils/polyfill-3d.js b/cocos2d/core/utils/polyfill-3d.js index 6120802061..c6d109787d 100644 --- a/cocos2d/core/utils/polyfill-3d.js +++ b/cocos2d/core/utils/polyfill-3d.js @@ -26,6 +26,7 @@ const renderEngine = require('../renderer/render-engine'); const math = renderEngine.math; +const misc = require('../utils/misc'); // ====== Node transform polyfills ====== const ONE_DEGREE = Math.PI / 180; @@ -75,16 +76,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.
+ * Usually we use cc.v3(x, y, z) to compose cc.Vec3 object.
+ * and Passing two numbers (x, y, z) is more efficient than passing cc.Vec3 object. + * !#zh + * 设置节点在父节点坐标系中的位置。
+ * 可以通过两种方式设置坐标点:
+ * 1. 传入 3 个数值 x, y, z。
+ * 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; + y = newPosOrX.y; + } + 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; + this.setLocalDirty(POSITION_DIRTY_FLAG); + + // fast check event + if (this._eventMask & POSITION_ON) { + // 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 setRotation + * @return {cc.Quat} Quaternion object represents the rotation + */ +function getRotation () { + return math.quat.clone(this._quat); +} + +/** + * !#en Set rotation of node (in quaternion). + * !#zh 设置该节点的 quaternion 旋转角度。 + * @method setRotation + * @param {cc.Quat} quat Quaternion object represents the rotation + */ +function setRotation (quat) { + if (!this._quat.equals(value)) { + math.quat.copy(this._quat, value); + this.setLocalDirty(ROTATION_DIRTY_FLAG); + + if (this._eventMask & ROTATION_ON) { + 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} scaleX - scaleX or scale + * @param {Number} [scaleY] + * @param {Number} [scaleZ] + * @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; + 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); + + if (this._eventMask & SCALE_ON) { + 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.getRotation = getRotation; + proto.setRotation = setRotation; + proto.getScale = getScale; + proto.setScale = setScale; + misc.propertyDefine(cc.Node, ['position', 'rotation', 'scale']); this.enabled = true; } }, From 4f1f0a7cfb1b96062ea9a37b3df83d755e5f7e7e Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 26 Apr 2018 10:52:46 +0800 Subject: [PATCH 2/3] Fix issues --- cocos2d/core/CCNode.js | 10 +++++-- cocos2d/core/platform/js.js | 1 + cocos2d/core/utils/polyfill-3d.js | 43 +++++++++++++++++++------------ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index fcaf75964b..f4e2fca326 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -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); + } }, }, @@ -1496,7 +1502,7 @@ var Node = cc.Class({ * cc.log("Node Position: " + node.getPosition()); */ getPosition () { - return new cc.Vec3(this._position); + return new cc.Vec2(this._position); }, /** @@ -1570,7 +1576,7 @@ var Node = cc.Class({ * !#en Sets the scale factor of the node. 1.0 is the default scale factor. This function can modify the X and Y scale at the same time. * !#zh 设置节点的缩放比例,默认值为 1.0。这个函数可以在同一时间修改 X 和 Y 缩放。 * @method setScale - * @param {Number|Vec3} scaleX - scaleX or scale + * @param {Number|Vec2} scaleX - scaleX or scale * @param {Number} [scaleY] * @example * node.setScale(cc.v2(1, 1)); diff --git a/cocos2d/core/platform/js.js b/cocos2d/core/platform/js.js index 1eb448b5a6..44b71dc790 100644 --- a/cocos2d/core/platform/js.js +++ b/cocos2d/core/platform/js.js @@ -261,6 +261,7 @@ var tmpGetSetDesc = { get: null, set: null, enumerable: false, + configurable: true, }; /** diff --git a/cocos2d/core/utils/polyfill-3d.js b/cocos2d/core/utils/polyfill-3d.js index c6d109787d..59bbb91b8c 100644 --- a/cocos2d/core/utils/polyfill-3d.js +++ b/cocos2d/core/utils/polyfill-3d.js @@ -26,11 +26,17 @@ const renderEngine = require('../renderer/render-engine'); const math = renderEngine.math; -const misc = require('../utils/misc'); // ====== 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; @@ -108,7 +114,7 @@ function setPosition (newPosOrX, y, z) { if (y === undefined) { x = newPosOrX.x; y = newPosOrX.y; - y = newPosOrX.y; + z = newPosOrX.z; } else { x = newPosOrX; @@ -125,11 +131,12 @@ function setPosition (newPosOrX, y, z) { pos.x = x; pos.y = y; - pos.z = z; + pos.z = z || 0; this.setLocalDirty(POSITION_DIRTY_FLAG); // fast check event - if (this._eventMask & POSITION_ON) { + var cache = this._hasListenerCache; + if (cache && cache[POSITION_CHANGED]) { // send event if (CC_EDITOR) { this.emit(POSITION_CHANGED, oldPosition); @@ -143,25 +150,26 @@ function setPosition (newPosOrX, y, z) { /** * !#en Get rotation of node (in quaternion). * !#zh 获取该节点的 quaternion 旋转角度。 - * @method setRotation + * @method getQuat * @return {cc.Quat} Quaternion object represents the rotation */ -function getRotation () { +function getQuat () { return math.quat.clone(this._quat); } /** * !#en Set rotation of node (in quaternion). * !#zh 设置该节点的 quaternion 旋转角度。 - * @method setRotation + * @method setQuat * @param {cc.Quat} quat Quaternion object represents the rotation */ -function setRotation (quat) { +function setQuat (quat) { if (!this._quat.equals(value)) { math.quat.copy(this._quat, value); this.setLocalDirty(ROTATION_DIRTY_FLAG); - if (this._eventMask & ROTATION_ON) { + var cache = this._hasListenerCache; + if (cache && cache[ROTATION_CHANGED]) { this.emit(ROTATION_CHANGED); } } @@ -182,9 +190,9 @@ function getScale () { * !#en Sets the scale of three axis in local coordinates of the node. * !#zh 设置节点在本地坐标系中三个坐标轴上的缩放比例。 * @method setScale - * @param {Number|Vec3} scaleX - scaleX or scale - * @param {Number} [scaleY] - * @param {Number} [scaleZ] + * @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); @@ -192,7 +200,7 @@ function getScale () { function setScale (x, y, z) { if (x && typeof x !== 'number') { y = x.y; - z = x.z; + z = x.z || 1; x = x.x; } else if (x !== undefined && y === undefined) { @@ -207,7 +215,8 @@ function setScale (x, y, z) { this._scale.y = y; this.setLocalDirty(SCALE_DIRTY_FLAG); - if (this._eventMask & SCALE_ON) { + var cache = this._hasListenerCache; + if (cache && cache[SCALE_CHANGED]) { this.emit(SCALE_CHANGED); } } @@ -227,11 +236,11 @@ module.exports = { proto.getPosition = getPosition; proto.setPosition = setPosition; - proto.getRotation = getRotation; - proto.setRotation = setRotation; proto.getScale = getScale; proto.setScale = setScale; - misc.propertyDefine(cc.Node, ['position', 'rotation', 'scale']); + cc.js.getset(proto, 'position', getPosition, setPosition); + cc.js.getset(proto, 'quat', getQuat, setQuat); + cc.js.getset(proto, 'scale', getScale, setScale); this.enabled = true; } }, From 6bf14fef51d0a4169b8500e25f05527f0ef6b646 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Thu, 26 Apr 2018 11:32:17 +0800 Subject: [PATCH 3/3] Avoid configurable property and avoid editor issue --- cocos2d/core/CCNode.js | 15 ++++++++++++--- cocos2d/core/platform/js.js | 1 - cocos2d/core/utils/polyfill-3d.js | 3 --- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index f4e2fca326..9fbdb5c64f 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -1538,9 +1538,18 @@ var Node = cc.Class({ if (CC_EDITOR) { var oldPosition = new cc.Vec2(locPosition); } - - locPosition.x = x; - locPosition.y = y; + if (!CC_EDITOR || isFinite(x)) { + locPosition.x = x; + } + else { + return cc.error(ERR_INVALID_NUMBER, 'x of new position'); + } + if (!CC_EDITOR || isFinite(y)) { + locPosition.y = y; + } + else { + return cc.error(ERR_INVALID_NUMBER, 'y of new position'); + } this.setLocalDirty(POSITION_DIRTY_FLAG); // fast check event diff --git a/cocos2d/core/platform/js.js b/cocos2d/core/platform/js.js index 44b71dc790..1eb448b5a6 100644 --- a/cocos2d/core/platform/js.js +++ b/cocos2d/core/platform/js.js @@ -261,7 +261,6 @@ var tmpGetSetDesc = { get: null, set: null, enumerable: false, - configurable: true, }; /** diff --git a/cocos2d/core/utils/polyfill-3d.js b/cocos2d/core/utils/polyfill-3d.js index 59bbb91b8c..cd2dfa54e3 100644 --- a/cocos2d/core/utils/polyfill-3d.js +++ b/cocos2d/core/utils/polyfill-3d.js @@ -238,9 +238,6 @@ module.exports = { proto.setPosition = setPosition; proto.getScale = getScale; proto.setScale = setScale; - cc.js.getset(proto, 'position', getPosition, setPosition); - cc.js.getset(proto, 'quat', getQuat, setQuat); - cc.js.getset(proto, 'scale', getScale, setScale); this.enabled = true; } },