mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Merge pull request #2318 from pandamicro/next
Use 3d camera instead of old camera
This commit is contained in:
+120
-39
@@ -52,8 +52,7 @@ var ActionManagerExist = !!cc.ActionManager;
|
||||
var emptyFunc = function () {};
|
||||
var _mat4_temp = math.mat4.create();
|
||||
var _vec3_temp = math.vec3.create();
|
||||
// quaternion for rotation
|
||||
var _quat = math.quat.new(0, 0, 0, 1);
|
||||
var _quat_temp = math.quat.create();
|
||||
var _globalOrderOfArrival = 1;
|
||||
|
||||
/**
|
||||
@@ -379,15 +378,8 @@ var Node = cc.Class({
|
||||
type: cc.Float
|
||||
},
|
||||
_scale: cc.Vec3,
|
||||
_rotationX: {
|
||||
default: undefined,
|
||||
type: cc.Float
|
||||
},
|
||||
_rotationY: {
|
||||
default: undefined,
|
||||
type: cc.Float
|
||||
},
|
||||
_rot: cc.Vec3,
|
||||
_rotationX: 0.0,
|
||||
_rotationY: 0.0,
|
||||
_skewX: 0.0,
|
||||
_skewY: 0.0,
|
||||
_localZOrder: 0,
|
||||
@@ -532,7 +524,19 @@ var Node = cc.Class({
|
||||
z: {
|
||||
get () {
|
||||
return this._position.z;
|
||||
}
|
||||
},
|
||||
set (value) {
|
||||
var localPosition = this._position;
|
||||
if (value !== localPosition.z) {
|
||||
if (!CC_EDITOR || isFinite(value)) {
|
||||
localPosition.z = value;
|
||||
this._localMatDirty = true;
|
||||
}
|
||||
else {
|
||||
cc.error(ERR_INVALID_NUMBER, 'new z');
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -546,13 +550,15 @@ var Node = cc.Class({
|
||||
*/
|
||||
rotation: {
|
||||
get () {
|
||||
if (this._rot.x !== this._rot.y)
|
||||
if (this._rotationX !== this._rotationY)
|
||||
cc.logID(1602);
|
||||
return this._rot.z;
|
||||
return this._rotationX;
|
||||
},
|
||||
set (value) {
|
||||
if (this._rot.z !== value) {
|
||||
this._rot.z = value;
|
||||
if (this._rotationX !== value || this._rotationY !== value) {
|
||||
this._rotationX = this._rotationY = value;
|
||||
// Update quaternion from rotation
|
||||
math.quat.fromEuler(this._quat, 0, 0, this._rotationX);
|
||||
this._localMatDirty = true;
|
||||
|
||||
var cache = this._hasListenerCache;
|
||||
@@ -574,11 +580,18 @@ var Node = cc.Class({
|
||||
*/
|
||||
rotationX: {
|
||||
get () {
|
||||
return this._rot.x;
|
||||
return this._rotationX;
|
||||
},
|
||||
set (value) {
|
||||
if (this._rot.x !== value) {
|
||||
this._rot.x = value;
|
||||
if (this._rotationX !== value) {
|
||||
this._rotationX = value;
|
||||
// Update quaternion from rotation
|
||||
if (this._rotationX === this._rotationY) {
|
||||
math.quat.fromEuler(this._quat, 0, 0, this._rotationX);
|
||||
}
|
||||
else {
|
||||
math.quat.fromEuler(this._quat, this._rotationX, this._rotationY, 0);
|
||||
}
|
||||
this._localMatDirty = true;
|
||||
|
||||
var cache = this._hasListenerCache;
|
||||
@@ -600,11 +613,18 @@ var Node = cc.Class({
|
||||
*/
|
||||
rotationY: {
|
||||
get () {
|
||||
return this._rot.y;
|
||||
return this._rotationY;
|
||||
},
|
||||
set (value) {
|
||||
if (this._rot.y !== value) {
|
||||
this._rot.y = value;
|
||||
if (this._rotationY !== value) {
|
||||
this._rotationY = value;
|
||||
// Update quaternion from rotation
|
||||
if (this._rotationX === this._rotationY) {
|
||||
math.quat.fromEuler(this._quat, 0, 0, this._rotationX);
|
||||
}
|
||||
else {
|
||||
math.quat.fromEuler(this._quat, this._rotationX, this._rotationY, 0);
|
||||
}
|
||||
this._localMatDirty = true;
|
||||
|
||||
var cache = this._hasListenerCache;
|
||||
@@ -913,6 +933,8 @@ var Node = cc.Class({
|
||||
this._scale.x = 1;
|
||||
this._scale.y = 1;
|
||||
this._scale.z = 1;
|
||||
// Quaternion for rotation
|
||||
this._quat = mathPools.quat.get();
|
||||
|
||||
this._matrix = mathPools.mat4.get();
|
||||
this._worldMatrix = mathPools.mat4.get();
|
||||
@@ -967,6 +989,7 @@ var Node = cc.Class({
|
||||
}
|
||||
|
||||
// Recycle math objects
|
||||
mathPools.quat.put(this._quat);
|
||||
mathPools.mat4.put(this._matrix);
|
||||
mathPools.mat4.put(this._worldMatrix);
|
||||
|
||||
@@ -1031,20 +1054,7 @@ var Node = cc.Class({
|
||||
}
|
||||
// Upgrade rotationX, rotationY from v1.x
|
||||
// TODO: remove in future version, 3.0 ?
|
||||
if (this._rotationX !== undefined) {
|
||||
if (this._rotationX === this._rotationY) {
|
||||
this._rot.z = this._rotationX;
|
||||
this._rotationX = undefined;
|
||||
this._rotationY = undefined;
|
||||
}
|
||||
else {
|
||||
this._rot.x = this._rotationX;
|
||||
this._rotationX = undefined;
|
||||
this._rot.y = this._rotationY || 0;
|
||||
this._rotationY = undefined;
|
||||
this._rot.z = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
var prefabInfo = this._prefab;
|
||||
@@ -1617,7 +1627,80 @@ var Node = cc.Class({
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
getWorldPos (out) {
|
||||
math.vec3.copy(out, this._position);
|
||||
let curr = this._parent;
|
||||
while (curr) {
|
||||
// out = parent_scale * pos
|
||||
math.vec3.mul(out, out, curr._scale);
|
||||
// out = parent_quat * out
|
||||
math.vec3.transformQuat(out, out, curr._quat);
|
||||
// out = out + pos
|
||||
math.vec3.add(out, out, curr._position);
|
||||
curr = curr._parent;
|
||||
}
|
||||
return out;
|
||||
},
|
||||
|
||||
getWorldRot (out) {
|
||||
math.quat.copy(out, this._quat);
|
||||
let curr = this._parent;
|
||||
while (curr) {
|
||||
math.quat.mul(out, curr._quat, out);
|
||||
curr = curr._parent;
|
||||
}
|
||||
return out;
|
||||
},
|
||||
|
||||
setWorldRot (quat) {
|
||||
if (this._parent) {
|
||||
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);
|
||||
},
|
||||
|
||||
getWorldRT (out) {
|
||||
let opos = _vec3_temp;
|
||||
let orot = _quat_temp;
|
||||
math.vec3.copy(opos, this._position);
|
||||
math.quat.copy(orot, this._quat);
|
||||
|
||||
let curr = this._parent;
|
||||
while (curr) {
|
||||
// opos = parent_lscale * lpos
|
||||
math.vec3.mul(opos, opos, curr._scale);
|
||||
// opos = parent_lrot * opos
|
||||
math.vec3.transformQuat(opos, opos, curr._quat);
|
||||
// opos = opos + lpos
|
||||
math.vec3.add(opos, opos, curr._position);
|
||||
// orot = lrot * orot
|
||||
math.quat.mul(orot, curr._quat, orot);
|
||||
curr = curr._parent;
|
||||
}
|
||||
math.mat4.fromRT(out, orot, opos);
|
||||
return out;
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en Set rotation by lookAt target point, normally used by Camera Node
|
||||
* !#zh 通过观察目标来设置 rotation,一般用于 Camera Node 上
|
||||
* @method lookAt
|
||||
* @param {vec3} pos
|
||||
* @param {vec3} [up] - default is (0,1,0)
|
||||
*/
|
||||
lookAt (pos, up) {
|
||||
this.getWorldPos(_vec3_temp);
|
||||
math.vec3.sub(_vec3_temp, _vec3_temp, pos); // NOTE: we use -z for view-dir
|
||||
math.vec3.normalize(_vec3_temp, _vec3_temp);
|
||||
math.quat.fromViewUp(_quat_temp, _vec3_temp, up);
|
||||
|
||||
this.setWorldRot(_quat_temp);
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en
|
||||
* Returns a copy the untransformed size of the node. <br/>
|
||||
@@ -1850,11 +1933,9 @@ var Node = cc.Class({
|
||||
|
||||
_updateLocalMatrix () {
|
||||
if (this._localMatDirty) {
|
||||
// Update quaternion from rotation
|
||||
math.quat.fromEuler(_quat, this._rot.x, this._rot.y, this._rot.z);
|
||||
// Update transform
|
||||
let t = this._matrix;
|
||||
math.mat4.fromRTS(t, _quat, this._position, this._scale);
|
||||
math.mat4.fromRTS(t, this._quat, this._position, this._scale);
|
||||
|
||||
// skew
|
||||
if (this._skewX || this._skewY) {
|
||||
|
||||
@@ -25,7 +25,9 @@
|
||||
|
||||
const renderEngine = require('./render-engine');
|
||||
const RenderComponentWalker = require('./render-component-walker');
|
||||
require('./RendererWebGL');
|
||||
const math = renderEngine.math;
|
||||
|
||||
let _pos = math.vec3.create();
|
||||
|
||||
function _initBuiltins(device) {
|
||||
let canvas = document.createElement('canvas');
|
||||
@@ -76,9 +78,13 @@ module.exports = {
|
||||
this._walker = new RenderComponentWalker(this.device, this.scene);
|
||||
|
||||
this._cameraNode = new cc.Node();
|
||||
this._camera = new renderEngine.Camera({
|
||||
x: 0, y: 0, w: canvas.width, h: canvas.height
|
||||
});
|
||||
this._camera = new renderEngine.Camera();
|
||||
this._camera.setFov(Math.PI * 60 / 180);
|
||||
this._camera.setNear(0.1);
|
||||
this._camera.setFar(1024);
|
||||
this._camera.setNode(this._cameraNode);
|
||||
this.updateCameraViewport();
|
||||
|
||||
if (CC_EDITOR) {
|
||||
this._camera.setColor(0, 0, 0, 0);
|
||||
}
|
||||
@@ -96,12 +102,16 @@ module.exports = {
|
||||
var ecScene = cc.director.getScene();
|
||||
ecScene.scaleX = ecScene.scaleY = 1;
|
||||
}
|
||||
this._cameraNode.scaleX = 1 / cc.view.getScaleX();
|
||||
this._cameraNode.scaleY = 1 / cc.view.getScaleY();
|
||||
this._camera._rect.w = this.canvas.width;
|
||||
this._camera._rect.h = this.canvas.height;
|
||||
this._camera.setViewport();
|
||||
this._camera.setNode(this._cameraNode);
|
||||
let node = this._cameraNode;
|
||||
let canvas = this.canvas;
|
||||
let zeye = canvas.height / 1.1566;
|
||||
node.x = canvas.width / 2;
|
||||
node.y = canvas.height / 2;
|
||||
node.z = zeye;
|
||||
_pos.x = canvas.width / 2;
|
||||
_pos.y = canvas.height / 2;
|
||||
_pos.z = 0;
|
||||
node.lookAt(_pos);
|
||||
},
|
||||
|
||||
render () {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -5,7 +5,7 @@ const Vec2 = require('../value-types/CCVec2');
|
||||
const Vec3 = require('../value-types/CCVec3');
|
||||
const math = renderEngine.math;
|
||||
|
||||
var mat4Pool = new JS.Pool(512);
|
||||
var mat4Pool = new JS.Pool(128);
|
||||
mat4Pool.get = function () {
|
||||
var matrix = this._get();
|
||||
if (matrix) {
|
||||
@@ -17,32 +17,45 @@ mat4Pool.get = function () {
|
||||
return matrix;
|
||||
};
|
||||
|
||||
var vec2Pool = new JS.Pool(1024);
|
||||
vec2Pool.get = function () {
|
||||
var vec2 = this._get();
|
||||
if (vec2) {
|
||||
vec2.x = vec2.y = 0;
|
||||
}
|
||||
else {
|
||||
vec2 = new Vec2();
|
||||
}
|
||||
return vec2;
|
||||
};
|
||||
// var vec2Pool = new JS.Pool(128);
|
||||
// vec2Pool.get = function () {
|
||||
// var vec2 = this._get();
|
||||
// if (vec2) {
|
||||
// vec2.x = vec2.y = 0;
|
||||
// }
|
||||
// else {
|
||||
// vec2 = new Vec2();
|
||||
// }
|
||||
// return vec2;
|
||||
// };
|
||||
|
||||
var vec3Pool = new JS.Pool(512);
|
||||
vec3Pool.get = function () {
|
||||
var vec3 = this._get();
|
||||
if (vec3) {
|
||||
vec3.x = vec3.y = vec3.z = 0;
|
||||
// var vec3Pool = new JS.Pool(128);
|
||||
// vec3Pool.get = function () {
|
||||
// var vec3 = this._get();
|
||||
// if (vec3) {
|
||||
// vec3.x = vec3.y = vec3.z = 0;
|
||||
// }
|
||||
// else {
|
||||
// vec3 = new Vec3();
|
||||
// }
|
||||
// return vec3;
|
||||
// };
|
||||
|
||||
var quatPool = new JS.Pool(64);
|
||||
quatPool.get = function () {
|
||||
var quat = this._get();
|
||||
if (quat) {
|
||||
math.quat.identity(quat);
|
||||
}
|
||||
else {
|
||||
vec3 = new Vec3();
|
||||
quat = math.quat.create();
|
||||
}
|
||||
return vec3;
|
||||
return quat;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
mat4: mat4Pool,
|
||||
vec2: vec2Pool,
|
||||
vec3: vec3Pool
|
||||
// vec2: vec2Pool,
|
||||
// vec3: vec3Pool,
|
||||
quat: quatPool
|
||||
};
|
||||
Reference in New Issue
Block a user