mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
camera utils
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { frustum } from '../../3d/geom-utils/frustum';
|
||||
import { mat4, quat, vec3 } from '../../core/vmath';
|
||||
import { frustum, ray } from '../../3d/geom-utils';
|
||||
import { lerp, mat4, quat, vec3 } from '../../core/vmath';
|
||||
import Node from '../../scene-graph/node';
|
||||
import { RenderScene } from './render-scene';
|
||||
|
||||
@@ -14,12 +14,24 @@ export enum CameraProjection {
|
||||
ORTHO,
|
||||
}
|
||||
|
||||
const q_a = cc.quat();
|
||||
const v_a = cc.v3();
|
||||
const v_b = cc.v3();
|
||||
|
||||
export interface INormalizedViewPort {
|
||||
x: number;
|
||||
y: number;
|
||||
w: number;
|
||||
h: number;
|
||||
}
|
||||
|
||||
export class Camera {
|
||||
|
||||
private _scene: RenderScene;
|
||||
private _name: string;
|
||||
private _node: Node;
|
||||
private _proj: CameraProjection;
|
||||
private _viewport: INormalizedViewPort;
|
||||
private _width: number;
|
||||
private _height: number;
|
||||
private _aspect: number;
|
||||
@@ -40,6 +52,7 @@ export class Camera {
|
||||
this._name = name;
|
||||
this._node = scene.createNode({ name: name + 'Node', isStatic: false });
|
||||
this._proj = CameraProjection.PERSPECTIVE;
|
||||
this._viewport = { x: 0, y: 0, w: 1, h: 1 };
|
||||
|
||||
const window = scene.root.mainWindow;
|
||||
if (window) {
|
||||
@@ -83,6 +96,17 @@ export class Camera {
|
||||
this._frustum.update(this._matViewProj, this._matViewProjInv);
|
||||
}
|
||||
|
||||
public set viewport (v: Partial<INormalizedViewPort>) {
|
||||
this._viewport.x = v.x || 0;
|
||||
this._viewport.y = v.y || 0;
|
||||
this._viewport.w = v.w || 1;
|
||||
this._viewport.h = v.h || 1;
|
||||
}
|
||||
|
||||
public get viewport () {
|
||||
return this._viewport;
|
||||
}
|
||||
|
||||
public set fov (fov: number) {
|
||||
this._fov = fov;
|
||||
}
|
||||
@@ -154,9 +178,11 @@ export class Camera {
|
||||
public rotate (rot: quat, ns?: NodeSpace) {
|
||||
const space = (ns !== undefined ? ns : NodeSpace.LOCAL);
|
||||
if (space === NodeSpace.LOCAL || space === NodeSpace.PARENT) {
|
||||
this._node.rotate(rot);
|
||||
this._node.getRotation(q_a);
|
||||
this._node.setRotation(quat.multiply(q_a, q_a, rot));
|
||||
} else if (space === NodeSpace.WORLD) {
|
||||
this._node.rotateWorld(rot);
|
||||
this._node.getWorldRotation(q_a);
|
||||
this._node.setWorldRotation(quat.multiply(q_a, rot, q_a));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,9 +190,11 @@ export class Camera {
|
||||
quat.fromAxisAngle(this._rotation, axis, rad);
|
||||
const space = (ns !== undefined ? ns : NodeSpace.LOCAL);
|
||||
if (space === NodeSpace.LOCAL || space === NodeSpace.PARENT) {
|
||||
this._node.rotate(this._rotation);
|
||||
this._node.getRotation(q_a);
|
||||
this._node.setRotation(quat.multiply(q_a, q_a, this._rotation));
|
||||
} else if (space === NodeSpace.WORLD) {
|
||||
this._node.rotateWorld(this._rotation);
|
||||
this._node.getWorldRotation(q_a);
|
||||
this._node.setWorldRotation(quat.multiply(q_a, this._rotation, q_a));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,4 +270,86 @@ export class Camera {
|
||||
this._node.setRotation(this._rotation);
|
||||
}
|
||||
|
||||
/**
|
||||
* transform a screen position to a world space ray
|
||||
*/
|
||||
public screenPointToRay (x: number, y: number, out: ray): ray {
|
||||
out = out || ray.create();
|
||||
this.update();
|
||||
|
||||
const cx = this._viewport.x * this._width;
|
||||
const cy = this._viewport.y * this._height;
|
||||
const cw = this._viewport.w * this._width;
|
||||
const ch = this._viewport.h * this._height;
|
||||
|
||||
// far plane intersection
|
||||
vec3.set(v_a, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, 1);
|
||||
vec3.transformMat4(v_a, v_a, this._matViewProjInv);
|
||||
|
||||
if (this._proj === CameraProjection.PERSPECTIVE) {
|
||||
// camera origin
|
||||
this._node.getWorldPosition(v_b);
|
||||
} else {
|
||||
// near plane intersection
|
||||
vec3.set(v_b, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, -1);
|
||||
vec3.transformMat4(v_b, v_b, this._matViewProjInv);
|
||||
}
|
||||
|
||||
return ray.fromPoints(out, v_b, v_a);
|
||||
}
|
||||
|
||||
/**
|
||||
* transform a screen position to world space
|
||||
*/
|
||||
public screenToWorld (out: vec3, screenPos: vec3): vec3 {
|
||||
const cx = this._viewport.x * this._width;
|
||||
const cy = this._viewport.y * this._height;
|
||||
const cw = this._viewport.w * this._width;
|
||||
const ch = this._viewport.h * this._height;
|
||||
|
||||
if (this._proj === CameraProjection.PERSPECTIVE) {
|
||||
// calculate screen pos in far clip plane
|
||||
vec3.set(out,
|
||||
(screenPos.x - cx) / cw * 2 - 1,
|
||||
(screenPos.y - cy) / ch * 2 - 1,
|
||||
1.0,
|
||||
);
|
||||
|
||||
// transform to world
|
||||
vec3.transformMat4(out, out, this._matViewProjInv);
|
||||
|
||||
// lerp to depth z
|
||||
this._node.getWorldPosition(v_a);
|
||||
|
||||
vec3.lerp(out, v_a, out, lerp(this._nearClip / this._farClip, 1, screenPos.z));
|
||||
} else {
|
||||
vec3.set(out,
|
||||
(screenPos.x - cx) / cw * 2 - 1,
|
||||
(screenPos.y - cy) / ch * 2 - 1,
|
||||
screenPos.z * 2 - 1,
|
||||
);
|
||||
|
||||
// transform to world
|
||||
vec3.transformMat4(out, out, this.matViewProjInv);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* transform a world space position to screen space
|
||||
*/
|
||||
public worldToScreen (out: vec3, worldPos: vec3): vec3 {
|
||||
const cx = this._viewport.x * this._width;
|
||||
const cy = this._viewport.y * this._height;
|
||||
const cw = this._viewport.w * this._width;
|
||||
const ch = this._viewport.h * this._height;
|
||||
|
||||
vec3.transformMat4(out, worldPos, this.matViewProjInv);
|
||||
out.x = cx + (out.x + 1) * 0.5 * cw;
|
||||
out.y = cy + (out.y + 1) * 0.5 * ch;
|
||||
out.z = out.z * 0.5 + 0.5;
|
||||
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
+12
-58
@@ -137,15 +137,15 @@ class Node extends BaseNode {
|
||||
invalidateChildren() {
|
||||
if (this._dirty && this._hasChanged) return;
|
||||
this._dirty = this._hasChanged = true;
|
||||
|
||||
let len = this._children.length;
|
||||
for (let i = 0; i < len; ++i) {
|
||||
this._children[i].invalidateChildren();
|
||||
for (const child of this._children) {
|
||||
child.invalidateChildren();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* update the world transform information if outdated
|
||||
* here we assume all nodes are children of a scene node,
|
||||
* which is always not dirty, has an identity transform and no parent.
|
||||
*/
|
||||
updateWorldTransform() {
|
||||
if (!this._dirty) return;
|
||||
@@ -154,7 +154,10 @@ class Node extends BaseNode {
|
||||
// top level node
|
||||
array_a[i++] = cur;
|
||||
cur = cur._parent;
|
||||
if (!cur) break;
|
||||
if (!cur || !cur._parent) {
|
||||
cur = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (i) {
|
||||
child = array_a[--i];
|
||||
@@ -194,6 +197,7 @@ class Node extends BaseNode {
|
||||
} else if (arguments.length === 3) {
|
||||
vec3.set(this._lpos, val, y, z);
|
||||
}
|
||||
vec3.copy(this._pos, this._lpos);
|
||||
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART);
|
||||
@@ -212,59 +216,6 @@ class Node extends BaseNode {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* rotate in local space
|
||||
*
|
||||
* @param {quat|number} val the new local rotation, or the x component of it
|
||||
* @param {number} [y] the y component of the new local rotation
|
||||
* @param {number} [z] the z component of the new local rotation
|
||||
* @param {number} [w] the w component of the new local rotation
|
||||
*/
|
||||
rotate(val, y, z, w) {
|
||||
|
||||
if (arguments.length === 1) {
|
||||
quat.normalize(q_a, val);
|
||||
} else if (arguments.length === 4) {
|
||||
quat.set(q_a, val, y, z, w);
|
||||
quat.normalize(q_a, q_a);
|
||||
}
|
||||
|
||||
quat.multiply(this._lrot, this._lrot, q_a);
|
||||
|
||||
this._eulerDirty = true;
|
||||
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
|
||||
}
|
||||
|
||||
/**
|
||||
* rotate in world space
|
||||
*
|
||||
* @param {quat|number} val the new local rotation, or the x component of it
|
||||
* @param {number} [y] the y component of the new local rotation
|
||||
* @param {number} [z] the z component of the new local rotation
|
||||
* @param {number} [w] the w component of the new local rotation
|
||||
*/
|
||||
rotateWorld(val, y, z, w) {
|
||||
|
||||
if (arguments.length === 1) {
|
||||
quat.normalize(q_a, val);
|
||||
} else if (arguments.length === 4) {
|
||||
quat.set(q_a, val, y, z, w);
|
||||
quat.normalize(q_a, q_a);
|
||||
}
|
||||
|
||||
getWorldRotation(q_b);
|
||||
quat.invert(q_b);
|
||||
quat.multiply(q_a, q_a, q_b);
|
||||
quat.multiply(this._lrot, this._lrot, q_a);
|
||||
|
||||
this._eulerDirty = true;
|
||||
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
|
||||
}
|
||||
|
||||
/**
|
||||
* set local rotation
|
||||
* @param {quat|number} val the new local rotation, or the x component of it
|
||||
@@ -278,6 +229,7 @@ class Node extends BaseNode {
|
||||
} else if (arguments.length === 4) {
|
||||
quat.set(this._lrot, val, y, z, w);
|
||||
}
|
||||
quat.copy(this._rot, this._lrot);
|
||||
this._eulerDirty = true;
|
||||
|
||||
this.invalidateChildren();
|
||||
@@ -294,6 +246,7 @@ class Node extends BaseNode {
|
||||
vec3.set(this._euler, x, y, z);
|
||||
this._eulerDirty = false;
|
||||
quat.fromEuler(this._lrot, x, y, z);
|
||||
quat.copy(this._rot, this._lrot);
|
||||
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART);
|
||||
@@ -324,6 +277,7 @@ class Node extends BaseNode {
|
||||
} else if (arguments.length === 3) {
|
||||
vec3.set(this._lscale, val, y, z);
|
||||
}
|
||||
vec3.copy(this._scale, this._lscale);
|
||||
|
||||
this.invalidateChildren();
|
||||
this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART);
|
||||
|
||||
Reference in New Issue
Block a user