Merge pull request #27 from YunHsiao/gfx

camera utils
This commit is contained in:
Weiliang Wang
2019-01-10 14:51:41 +08:00
committed by GitHub
7 changed files with 179 additions and 117 deletions
+11 -16
View File
@@ -8,17 +8,12 @@ import { RenderPassStage } from '../pipeline/render-pipeline';
import ImageAsset from './image-asset';
import Texture2D from './texture-2d';
enum DepthStencilFormat {
D24S8 = GFXFormat.D24S8,
S8 = GFXFormat.R8UI,
D16 = GFXFormat.D16,
}
function toGfxDepthStencilFormat (depthStencilFormat: DepthStencilFormat): GFXFormat {
return depthStencilFormat as unknown as GFXFormat;
}
type DSFormat = 'D24S8' | 'S8' | 'D16';
const _formatMap = {
D24S8: GFXFormat.D24S8,
S8: GFXFormat.R8UI,
D16: GFXFormat.D16,
};
/**
* Render textures are textures that can be rendered to.
@@ -44,10 +39,10 @@ export default class RenderTexture extends Texture2D {
* 初始化 render texture
* @param [width]
* @param [height]
* @param [depthStencilFormat]
* @param [string]
* @method initWithSize
*/
public initWithSize (width?: number, height?: number, depthStencilFormat?: DepthStencilFormat) {
public initWithSize (width?: number, height?: number, format?: DSFormat) {
this.destroy();
const gfxDevice = this._getGlobalDevice();
@@ -81,11 +76,11 @@ export default class RenderTexture extends Texture2D {
return;
}
if (depthStencilFormat !== undefined) {
if (format !== undefined) {
this._depthStencilTexture = gfxDevice.createTexture({
type: GFXTextureType.TEX2D,
usage: GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT,
format: toGfxDepthStencilFormat(depthStencilFormat),
format: _formatMap[format],
width: w,
height: h,
});
@@ -95,7 +90,7 @@ export default class RenderTexture extends Texture2D {
this._depthStencilTextureView = gfxDevice.createTextureView({
texture: this._depthStencilTexture,
type: GFXTextureViewType.TV2D,
format: toGfxDepthStencilFormat(depthStencilFormat),
format: _formatMap[format],
});
}
+2
View File
@@ -29,6 +29,7 @@ import { getClassName, value } from '../core/utils/js';
import IDGenerator from '../core/utils/id-generator';
import { ccclass, property } from '../core/data/class-decorator';
import Node from '../scene-graph/node';
import { RenderScene } from '../renderer/scene/render-scene';
export class Component extends CCObject {
node: Node;
@@ -36,6 +37,7 @@ export class Component extends CCObject {
enabled: boolean;
enabledInHierarchy: boolean;
getRenderScene(index?: number): RenderScene | undefined;
addComponent(typeOrClassName: Function | string): Component;
getComponent(typeOrClassName: Function | string): Component;
getComponents(typeOrClassName: Function | string): Component[];
+13 -13
View File
@@ -7,11 +7,11 @@ import { GFXShader, GFXUniformBlock, GFXUniform } from '../../gfx/shader';
import { IDefineMap } from './effect';
import { UBOGlobal, UBOLocal } from '../../pipeline/render-pipeline';
function _generateDefines(
function _generateDefines (
device: GFXDevice,
defs: IDefineMap,
tDefs: IDefineInfo[],
deps: Record<string, string>
deps: Record<string, string>,
) {
const defines: string[] = [];
for (const { name } of tDefs) {
@@ -28,21 +28,21 @@ function _generateDefines(
}
let _shdID = 0;
interface DefineRecord extends IDefineInfo {
interface IDefineRecord extends IDefineInfo {
_map: (value: any) => number;
_offset: number;
}
interface ProgramInfo extends IShaderInfo {
interface IProgramInfo extends IShaderInfo {
id: number;
defines: DefineRecord[];
defines: IDefineRecord[];
}
export class ProgramLib {
protected _device: GFXDevice;
protected _precision: string;
protected _templates: Record<string, ProgramInfo>;
protected _templates: Record<string, IProgramInfo>;
protected _cache: Record<string, GFXShader | null>;
constructor(device: GFXDevice) {
constructor (device: GFXDevice) {
this._device = device;
this._precision = `precision highp float;\n`;
this._templates = {};
@@ -65,9 +65,9 @@ export class ProgramLib {
* };
* programLib.define(program);
*/
public define(prog: IShaderInfo) {
public define (prog: IShaderInfo) {
if (this._templates[prog.name]) { return; }
const tmpl = <ProgramInfo>Object.assign({ id: ++_shdID }, prog);
const tmpl = Object.assign({ id: ++_shdID }, prog) as IProgramInfo;
tmpl.vert = this._precision + prog.vert;
tmpl.frag = this._precision + prog.frag;
@@ -89,18 +89,18 @@ export class ProgramLib {
this._templates[prog.name] = tmpl;
}
public getTemplate(name: string) {
public getTemplate (name: string) {
return this._templates[name];
}
/**
* Does this library has the specified program?
*/
public hasProgram(name: string) {
public hasProgram (name: string) {
return this._templates[name] !== undefined;
}
public getKey(name: string, defines: IDefineMap) {
public getKey (name: string, defines: IDefineMap) {
const tmpl = this._templates[name];
let key = 0;
for (const tmplDef of tmpl.defines) {
@@ -113,7 +113,7 @@ export class ProgramLib {
return key << 8 | (tmpl.id & 0xff);
}
public getGFXShader(name: string, defines: IDefineMap = {}) {
public getGFXShader (name: string, defines: IDefineMap = {}) {
const key = this.getKey(name, defines);
let program = this._cache[key];
if (program !== undefined) {
+138 -27
View File
@@ -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,18 +96,41 @@ 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;
}
public get fov (): number {
return this._fov;
}
public set nearClip (nearClip: number) {
this._nearClip = nearClip;
}
public get nearClip (): number {
return this._nearClip;
}
public set farClip (farClip: number) {
this._farClip = farClip;
}
public get farClip (): number {
return this._farClip;
}
public get scene (): RenderScene {
return this._scene;
}
@@ -119,18 +155,6 @@ export class Camera {
return this._aspect;
}
public get fov (): number {
return this._fov;
}
public get nearClip (): number {
return this._nearClip;
}
public get farClip (): number {
return this._farClip;
}
public get matView (): mat4 {
return this._matView;
}
@@ -151,17 +175,14 @@ export class Camera {
return this._frustum;
}
public get position (): vec3 {
this._node.getPosition(this._position);
return this._position;
}
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));
}
}
@@ -169,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));
}
}
@@ -227,18 +250,106 @@ export class Camera {
this._node.lookAt(target);
}
public get direction (): vec3 {
this._node.getRotation(this._rotation);
vec3.transformQuat(this._direction, vec3.UNIT_Z, this._rotation);
return this._direction;
public get position (): vec3 {
this._node.getPosition(this._position);
return this._position;
}
public set position (pos: vec3) {
this._node.setWorldPosition(pos.x, pos.y, pos.z);
}
public get direction (): vec3 {
this._node.getRotation(this._rotation);
vec3.transformQuat(this._direction, vec3.UNIT_Z, this._rotation);
return this._direction;
}
public set direction (dir: vec3) {
quat.rotationTo(this._rotation, vec3.UNIT_Z, dir);
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;
}
}
+1 -1
View File
@@ -128,7 +128,7 @@ export class RenderScene {
public destroyModel(model: Model) {
for (let i = 0; i < this._models.length; ++i) {
if (this._models[i] === model) {
this._models.slice(i);
this._models.splice(i, 1);
return;
}
}
+12 -58
View File
@@ -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);
+2 -2
View File
@@ -33,7 +33,7 @@
function onStart() {
const $body = document.body;
const bcr = $body.getBoundingClientRect();
// cc.view.setCanvasSize(bcr.width, bcr.height);
cc.view.setCanvasSize(bcr.width, bcr.height);
window.addEventListener('resize', () => {
const bcr = $body.getBoundingClientRect();
cc.view.setCanvasSize(bcr.width, bcr.height);
@@ -98,4 +98,4 @@
</script>
</body>
</html>
</html>