mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
304 lines
8.9 KiB
JavaScript
304 lines
8.9 KiB
JavaScript
/****************************************************************************
|
|
Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
http://www.cocos.com
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated engine source code (the "Software"), a limited,
|
|
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
|
to use Cocos Creator solely to develop games on your target platforms. You shall
|
|
not use Cocos Creator software for developing other software or tools that's
|
|
used for developing games. You are not granted to publish, distribute,
|
|
sublicense, and/or sell copies of Cocos Creator.
|
|
|
|
The software or tools in this License Agreement are licensed, not sold.
|
|
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
****************************************************************************/
|
|
|
|
|
|
const Node = require('../CCNode');
|
|
const EventType = Node.EventType;
|
|
const DirtyFlag = Node._LocalDirtyFlag;
|
|
const math = require('../renderer/render-engine').math;
|
|
const RenderFlow = require('../renderer/render-flow');
|
|
const misc = require('../utils/misc');
|
|
|
|
// ====== Node transform polyfills ======
|
|
const ONE_DEGREE = Math.PI / 180;
|
|
|
|
const POSITION_ON = 1 << 0;
|
|
const SCALE_ON = 1 << 1;
|
|
const ROTATION_ON = 1 << 2;
|
|
|
|
function _updateLocalMatrix3d () {
|
|
if (this._localMatDirty) {
|
|
// Update transform
|
|
let t = this._matrix;
|
|
math.mat4.fromRTS(t, this._quat, this._position, this._scale);
|
|
|
|
// skew
|
|
if (this._skewX || this._skewY) {
|
|
let a = t.m00, b = t.m01, c = t.m04, d = t.m05;
|
|
let skx = Math.tan(this._skewX * ONE_DEGREE);
|
|
let sky = Math.tan(this._skewY * ONE_DEGREE);
|
|
if (skx === Infinity)
|
|
skx = 99999999;
|
|
if (sky === Infinity)
|
|
sky = 99999999;
|
|
t.m00 = a + c * sky;
|
|
t.m01 = b + d * sky;
|
|
t.m04 = c + a * skx;
|
|
t.m05 = d + b * skx;
|
|
}
|
|
this._localMatDirty = 0;
|
|
// Register dirty status of world matrix so that it can be recalculated
|
|
this._worldMatDirty = true;
|
|
}
|
|
}
|
|
|
|
function _calculWorldMatrix3d () {
|
|
// Avoid as much function call as possible
|
|
if (this._localMatDirty) {
|
|
this._updateLocalMatrix();
|
|
}
|
|
|
|
if (this._parent) {
|
|
let parentMat = this._parent._worldMatrix;
|
|
math.mat4.mul(this._worldMatrix, parentMat, this._matrix);
|
|
}
|
|
else {
|
|
math.mat4.copy(this._worldMatrix, this._matrix);
|
|
}
|
|
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) {
|
|
let x;
|
|
if (y === undefined) {
|
|
x = newPosOrX.x;
|
|
y = newPosOrX.y;
|
|
z = newPosOrX.z || 0;
|
|
}
|
|
else {
|
|
x = newPosOrX;
|
|
z = z || 0
|
|
}
|
|
|
|
let pos = this._position;
|
|
if (pos.x === x && pos.y === y && pos.z === z) {
|
|
return;
|
|
}
|
|
|
|
if (CC_EDITOR) {
|
|
let oldPosition = new cc.Vec3(pos);
|
|
}
|
|
|
|
pos.x = x;
|
|
pos.y = y;
|
|
pos.z = z;
|
|
this.setLocalDirty(DirtyFlag.POSITION);
|
|
this._renderFlag |= RenderFlow.FLAG_WORLD_TRANSFORM;
|
|
|
|
// fast check event
|
|
if (this._eventMask & POSITION_ON) {
|
|
this.emit(EventType.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|Number} quat Quaternion object represents the rotation or the x value of quaternion
|
|
* @param {Number} y y value of quternion
|
|
* @param {Number} z z value of quternion
|
|
* @param {Number} w w value of quternion
|
|
*/
|
|
function setQuat (quat, y, z, w) {
|
|
let x = quat;
|
|
if (y === undefined) {
|
|
x = quat.x;
|
|
y = quat.y;
|
|
z = quat.z;
|
|
w = quat.w;
|
|
}
|
|
|
|
let old = this._quat;
|
|
if (old.x !== x || old.y !== y || old.z !== z || old.w !== w) {
|
|
old.x = x;
|
|
old.y = y;
|
|
old.z = z;
|
|
old.w = w;
|
|
this.setLocalDirty(DirtyFlag.ROTATION);
|
|
this._renderFlag |= RenderFlow.FLAG_TRANSFORM;
|
|
|
|
if (this._eventMask & ROTATION_ON) {
|
|
this.emit(EventType.ROTATION_CHANGED);
|
|
}
|
|
}
|
|
|
|
if (CC_EDITOR) {
|
|
this._syncEulerAngles();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* !#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 if (z === undefined) {
|
|
z = 1;
|
|
}
|
|
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);
|
|
this._renderFlag |= RenderFlow.FLAG_TRANSFORM;
|
|
|
|
if (this._eventMask & SCALE_ON) {
|
|
this.emit(EventType.SCALE_CHANGED);
|
|
}
|
|
}
|
|
}
|
|
|
|
function _syncEulerAngles () {
|
|
this._rotationX = this._quat.getRoll();
|
|
this._rotationY = this._quat.getPitch();
|
|
this._rotationZ = this._quat.getYaw();
|
|
}
|
|
|
|
function _update3DFunction () {
|
|
if (this._is3DNode) {
|
|
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 _upgrade_1x_to_2x () {
|
|
if (CC_EDITOR && this instanceof cc.Scene) {
|
|
this._is3DNode = true;
|
|
}
|
|
|
|
if (this._is3DNode) {
|
|
this._update3DFunction();
|
|
}
|
|
|
|
_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;
|
|
|
|
proto.getQuat = getQuat;
|
|
proto.setQuat = setQuat;
|
|
|
|
proto.getPosition = getPosition;
|
|
proto.setPosition = setPosition;
|
|
proto.getScale = getScale;
|
|
proto.setScale = setScale;
|
|
|
|
proto._upgrade_1x_to_2x = _upgrade_1x_to_2x;
|
|
proto._update3DFunction = _update3DFunction;
|
|
|
|
cc.js.getset(proto, 'position', getPosition, setPosition, false, true);
|
|
cc.js.getset(proto, 'scale', getScale, setScale, false, true);
|
|
|
|
cc.js.getset(proto, 'is3DNode', function () {
|
|
return this._is3DNode;
|
|
}, function (v) {
|
|
if (this._is3DNode === v) return;
|
|
this._is3DNode = v;
|
|
this._update3DFunction();
|
|
this._syncEulerAngles();
|
|
});
|
|
|
|
cc.js.getset(proto, 'quat', getQuat, setQuat);
|