diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index cb56e7bd71..68b12edf70 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -680,17 +680,15 @@ var Node = cc.Class({ if (!CC_EDITOR || isFinite(value)) { localPosition.z = value; this.setLocalDirty(LocalDirtyFlag.POSITION); + // fast check event + if (this._eventMask & POSITION_ON) { + this.emit(EventType.POSITION_CHANGED); + } } else { cc.error(ERR_INVALID_NUMBER, 'new z'); } } - - // fast check event - var cache = this._hasListenerCache; - if (cache && cache[POSITION_CHANGED]) { - this.emit(POSITION_CHANGED); - } }, }, @@ -2272,9 +2270,11 @@ var Node = cc.Class({ this._parent.getWorldRot(this._quat); math.quat.conjugate(this._quat, this._quat); math.quat.mul(this._quat, this._quat, quat); - return; } - math.quat.copy(this._quat, quat); + else { + math.quat.copy(this._quat, quat); + } + this.setLocalDirty(LocalDirtyFlag.ROTATION); }, getWorldRT (out) { diff --git a/cocos2d/core/renderer/render-component-walker.js b/cocos2d/core/renderer/render-component-walker.js index 8347f3547b..2eeb2fa396 100644 --- a/cocos2d/core/renderer/render-component-walker.js +++ b/cocos2d/core/renderer/render-component-walker.js @@ -379,12 +379,13 @@ RenderComponentWalker.prototype = { // Transform scene if (scene._worldMatDirty) { - scene._updateWorldMatrix(); - this._batchData.worldMatUpdated = true; + scene._calculWorldMatrix(); } + this._batchData.worldMatUpdated = scene._worldMatUpdated; for (let i = 0, l = scene._children.length; i < l; i++) { this._visitNode(scene._children[i]); } + scene._worldMatUpdated = false; atlasManager.update(); this._flush(); diff --git a/cocos2d/core/utils/polyfill-3d.js b/cocos2d/core/utils/polyfill-3d.js index a369454aff..8f1bf0d3b8 100644 --- a/cocos2d/core/utils/polyfill-3d.js +++ b/cocos2d/core/utils/polyfill-3d.js @@ -79,6 +79,7 @@ function _calculWorldMatrix3d () { math.mat4.copy(this._worldMatrix, this._matrix); } this._worldMatDirty = false; + this._worldMatUpdated = true; } @@ -113,10 +114,11 @@ function setPosition (newPosOrX, y, z) { if (y === undefined) { x = newPosOrX.x; y = newPosOrX.y; - z = newPosOrX.z; + z = newPosOrX.z || 0; } else { x = newPosOrX; + z = z || 0 } var pos = this._position; @@ -130,7 +132,7 @@ function setPosition (newPosOrX, y, z) { pos.x = x; pos.y = y; - pos.z = z || 0; + pos.z = z; this.setLocalDirty(DirtyFlag.POSITION); // fast check event @@ -198,15 +200,15 @@ function setScale (x, y, z) { y = x; z = x; } - else { - return; + else if (z === undefined) { + z = 1; } - if (this._scale.x !== x || this._scale.y !== y) { + if (this._scale.x !== x || this._scale.y !== y || this._scale.z !== z) { this._scale.x = x; this._scale.y = y; + this._scale.z = z; this.setLocalDirty(DirtyFlag.SCALE); - var cache = this._hasListenerCache; if (this._eventMask & SCALE_ON) { this.emit(EventType.SCALE_CHANGED); } diff --git a/cocos2d/core/value-types/vec3.js b/cocos2d/core/value-types/vec3.js index 2571aaa187..335f22e45a 100644 --- a/cocos2d/core/value-types/vec3.js +++ b/cocos2d/core/value-types/vec3.js @@ -27,6 +27,7 @@ var ValueType = require('./value-type'); var js = require('../platform/js'); var CCClass = require('../platform/CCClass'); +var math = require('../renderer/render-engine').math; /** * !#en Representation of 3D vectors and points. @@ -109,6 +110,338 @@ proto.equals = function (other) { return other && this.x === other.x && this.y === other.y && this.z === other.z; }; + +/** + * !#en Check whether two vector equal with some degree of variance. + * !#zh + * 近似判断两个点是否相等。
+ * 判断 2 个向量是否在指定数值的范围之内,如果在则返回 true,反之则返回 false。 + * @method pFuzzyEqual + * @param {Vec3} other + * @param {Number} variance + * @return {Boolean} + */ +proto.fuzzyEquals = function (other, variance) { + if (this.x - variance <= other.x && other.x <= this.x + variance) { + if (this.y - variance <= other.y && other.y <= this.y + variance) { + if (this.z - variance <= other.z && other.z <= this.z + variance) + return true; + } + } + return false; +}; + +/** + * !#en Transform to string with vector informations + * !#zh 转换为方便阅读的字符串。 + * @method toString + * @return {string} + */ +proto.toString = function () { + return "(" + + this.x.toFixed(2) + ", " + + this.y.toFixed(2) + ", " + + this.z.toFixed(2) + ")" + ; +}; + +/** + * !#en Calculate linear interpolation result between this vector and another one with given ratio + * !#zh 线性插值。 + * @method lerp + * @param {Vec3} to + * @param {number} ratio - the interpolation coefficient + * @param {Vec3} [out] - optional, the receiving vector + * @return {Vec3} + */ +proto.lerp = function (to, ratio, out) { + out = out || new Vec3(); + math.vec3.lerp(out, this, to, ratio); + return out; +}; + +/** + * !#en Clamp the vector between from float and to float. + * !#zh + * 返回指定限制区域后的向量。
+ * 向量大于 max_inclusive 则返回 max_inclusive。
+ * 向量小于 min_inclusive 则返回 min_inclusive。
+ * 否则返回自身。 + * @method clampf + * @param {Vec3} min_inclusive + * @param {Vec3} max_inclusive + * @return {Vec3} + */ +proto.clampf = function (min_inclusive, max_inclusive) { + this.x = misc.clampf(this.x, min_inclusive.x, max_inclusive.x); + this.y = misc.clampf(this.y, min_inclusive.y, max_inclusive.y); + this.z = misc.clampf(this.z, min_inclusive.z, max_inclusive.z); + return this; +}; + +/** + * !#en Adds this vector. If you want to save result to another vector, use add() instead. + * !#zh 向量加法。如果你想保存结果到另一个向量,使用 add() 代替。 + * @method addSelf + * @param {Vec3} vector + * @return {Vec3} returns this + * @chainable + */ +proto.addSelf = function (vector) { + this.x += vector.x; + this.y += vector.y; + this.z += vector.z; + return this; +}; + +/** + * !#en Adds two vectors, and returns the new result. + * !#zh 向量加法,并返回新结果。 + * @method add + * @param {Vec3} vector + * @param {Vec3} [out] - optional, the receiving vector + * @return {Vec3} the result + */ +proto.add = function (vector, out) { + out = out || new Vec3(); + out.x = this.x + vector.x; + out.y = this.y + vector.y; + out.z = this.z + vector.z; + return out; +}; + +/** + * !#en Subtracts one vector from this. If you want to save result to another vector, use sub() instead. + * !#zh 向量减法。如果你想保存结果到另一个向量,可使用 sub() 代替。 + * @method subSelf + * @param {Vec3} vector + * @return {Vec3} returns this + * @chainable + */ +proto.subSelf = function (vector) { + this.x -= vector.x; + this.y -= vector.y; + this.z -= vector.z; + return this; +}; + +/** + * !#en Subtracts one vector from this, and returns the new result. + * !#zh 向量减法,并返回新结果。 + * @method sub + * @param {Vec3} vector + * @param {Vec3} [out] - optional, the receiving vector + * @return {Vec3} the result + */ +proto.sub = function (vector, out) { + out = out || new Vec3(); + out.x = this.x - vector.x; + out.y = this.y - vector.y; + out.z = this.z - vector.z; + return out; +}; + +/** + * !#en Multiplies this by a number. If you want to save result to another vector, use mul() instead. + * !#zh 缩放当前向量。如果你想结果保存到另一个向量,可使用 mul() 代替。 + * @method mulSelf + * @param {number} num + * @return {Vec3} returns this + * @chainable + */ +proto.mulSelf = function (num) { + this.x *= num; + this.y *= num; + this.z *= num; + return this; +}; + +/** + * !#en Multiplies by a number, and returns the new result. + * !#zh 缩放向量,并返回新结果。 + * @method mul + * @param {number} num + * @param {Vec3} [out] - optional, the receiving vector + * @return {Vec3} the result + */ +proto.mul = function (num, out) { + out = out || new Vec3(); + out.x = this.x * num; + out.y = this.y * num; + out.z = this.z * num; + return out; +}; + +/** + * !#en Multiplies two vectors. + * !#zh 分量相乘。 + * @method scaleSelf + * @param {Vec3} vector + * @return {Vec3} returns this + * @chainable + */ +proto.scaleSelf = function (vector) { + this.x *= vector.x; + this.y *= vector.y; + this.z *= vector.z; + return this; +}; + +/** + * !#en Multiplies two vectors, and returns the new result. + * !#zh 分量相乘,并返回新的结果。 + * @method scale + * @param {Vec3} vector + * @param {Vec3} [out] - optional, the receiving vector + * @return {Vec3} the result + */ +proto.scale = function (vector, out) { + out = out || new Vec3(); + out.x = this.x * vector.x; + out.y = this.y * vector.y; + out.z = this.z * vector.z; + return out; +}; + +/** + * !#en Divides by a number. If you want to save result to another vector, use div() instead. + * !#zh 向量除法。如果你想结果保存到另一个向量,可使用 div() 代替。 + * @method divSelf + * @param {Vec3} vector + * @return {Vec3} returns this + * @chainable + */ +proto.divSelf = function (num) { + this.x /= num; + this.y /= num; + this.z /= num; + return this; +}; + +/** + * !#en Divides by a number, and returns the new result. + * !#zh 向量除法,并返回新的结果。 + * @method div + * @param {Vec3} vector + * @param {Vec3} [out] - optional, the receiving vector + * @return {Vec3} the result + */ +proto.div = function (num, out) { + out = out || new Vec3(); + out.x = this.x / num; + out.y = this.y / num; + out.z = this.z / num; + return out; +}; + +/** + * !#en Negates the components. If you want to save result to another vector, use neg() instead. + * !#zh 向量取反。如果你想结果保存到另一个向量,可使用 neg() 代替。 + * @method negSelf + * @return {Vec3} returns this + * @chainable + */ +proto.negSelf = function () { + this.x = -this.x; + this.y = -this.y; + this.z = -this.z; + return this; +}; + +/** + * !#en Negates the components, and returns the new result. + * !#zh 返回取反后的新向量。 + * @method neg + * @param {Vec3} [out] - optional, the receiving vector + * @return {Vec3} the result + */ +proto.neg = function (out) { + out = out || new Vec3(); + out.x = -this.x; + out.y = -this.y; + out.z = -this.z; + return out; +}; + +/** + * !#en Dot product + * !#zh 当前向量与指定向量进行点乘。 + * @method dot + * @param {Vec3} [vector] + * @return {number} the result + */ +proto.dot = function (vector) { + return this.x * vector.x + this.y * vector.y + this.z * vector.z; +}; + +/** + * !#en Cross product + * !#zh 当前向量与指定向量进行叉乘。 + * @method cross + * @param {Vec3} vector + * @param {Vec3} [out] + * @return {Vec3} the result + */ +proto.cross = function (vector, out) { + out = out || new Vec3(); + math.vec3.cross(out, this, vector) + return out; +}; + +/** + * !#en Returns the length of this vector. + * !#zh 返回该向量的长度。 + * @method mag + * @return {number} the result + * @example + * var v = cc.v2(10, 10); + * v.mag(); // return 14.142135623730951; + */ +proto.mag = function () { + return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); +}; + +/** + * !#en Returns the squared length of this vector. + * !#zh 返回该向量的长度平方。 + * @method magSqr + * @return {number} the result + */ +proto.magSqr = function () { + return this.x * this.x + this.y * this.y + this.z * this.z; +}; + +/** + * !#en Make the length of this vector to 1. + * !#zh 向量归一化,让这个向量的长度为 1。 + * @method normalizeSelf + * @return {Vec3} returns this + * @chainable + */ +proto.normalizeSelf = function () { + math.vec3.normalize(this, this); + return this; +}; + +/** + * !#en + * Returns this vector with a magnitude of 1.
+ *
+ * Note that the current vector is unchanged and a new normalized vector is returned. If you want to normalize the current vector, use normalizeSelf function. + * !#zh + * 返回归一化后的向量。
+ *
+ * 注意,当前向量不变,并返回一个新的归一化向量。如果你想来归一化当前向量,可使用 normalizeSelf 函数。 + * @method normalize + * @param {Vec3} [out] - optional, the receiving vector + * @return {Vec3} result + */ +proto.normalize = function (out) { + out = out || new Vec3(); + math.vec3.normalize(out, this); + return out; +}; + /** * !#en The convenience method to create a new {{#crossLink "Vec3"}}cc.Vec3{{/crossLink}}. * !#zh 通过该简便的函数进行创建 {{#crossLink "Vec3"}}cc.Vec3{{/crossLink}} 对象。