From 1c445f3098cca0d449b2ba4770ef66ba4abcae22 Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Mon, 7 Jan 2019 16:23:00 +0800 Subject: [PATCH] add 'isNormalized' to GFXInputAttribute --- cocos/camera/CCCamera.js | 2 +- cocos/core/root.ts | 11 +- cocos/gfx/device.ts | 15 + cocos/gfx/input-assembler.ts | 1 + cocos/gfx/pipeline-state.ts | 28 ++ cocos/gfx/shader.ts | 6 + cocos/gfx/webgl/webgl-commands.ts | 5 +- cocos/gfx/webgl/webgl-device.ts | 7 +- cocos/gfx/webgl/webgl-gpu-objects.ts | 1 + cocos/pipeline/forward/forward-pipeline.ts | 19 +- cocos/pipeline/render-pipeline.ts | 131 +++++- cocos/pipeline/render-view.ts | 14 +- cocos/renderer/core/pass.ts | 9 +- cocos/renderer/index.js | 2 +- cocos/renderer/scene/camera.js | 478 --------------------- cocos/renderer/scene/camera.ts | 141 ++++++ cocos/renderer/scene/model.ts | 2 +- cocos/renderer/scene/render-scene.ts | 19 +- 18 files changed, 379 insertions(+), 512 deletions(-) delete mode 100644 cocos/renderer/scene/camera.js create mode 100644 cocos/renderer/scene/camera.ts diff --git a/cocos/camera/CCCamera.js b/cocos/camera/CCCamera.js index d197071376..b08129c519 100644 --- a/cocos/camera/CCCamera.js +++ b/cocos/camera/CCCamera.js @@ -31,7 +31,7 @@ import vec2 from '../core/vmath/vec2'; import vec3 from '../core/vmath/vec3'; import Component from '../components/CCComponent'; // TODO fix import from renderer -import {default as Renderer_Camera} from '../renderer/scene/camera'; +import { Camera as Renderer_Camera} from '../renderer/scene/camera'; import {default as Renderer_View} from '../renderer/core/view'; import {ccclass, property, executeInEditMode, menu, inspector} from '../core/data/class-decorator'; diff --git a/cocos/core/root.ts b/cocos/core/root.ts index 2882840121..26dbfe5c87 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -48,6 +48,8 @@ export class Root { public frameMove(deltaTime: number) { + this._frameTime = deltaTime; + for (let i = 0; i < this._views.length; ++i) { let view = this._views[i]; if (view.isEnable() && view.isAttached) { @@ -88,7 +90,7 @@ export class Root { } public createScene(info: RenderSceneInfo): RenderScene | null { - let scene = new RenderScene; + let scene = new RenderScene(this); if (scene.initialize(info)) { this._scenes.push(scene); return scene; @@ -155,7 +157,7 @@ export class Root { return this._pipeline; } - public get sceneManagers(): RenderScene[] { + public get scenes(): RenderScene[] { return this._scenes; } @@ -163,10 +165,15 @@ export class Root { return this._views; } + public get frameTime(): number { + return this._frameTime; + } + private _device: GFXDevice; private _windows: GFXWindow[] = []; private _mainWindow: GFXWindow | null = null; private _pipeline: RenderPipeline | null = null; private _scenes: RenderScene[] = []; private _views: RenderView[] = []; + private _frameTime: number = 0; }; diff --git a/cocos/gfx/device.ts b/cocos/gfx/device.ts index 9bda923c72..d1e3f93686 100644 --- a/cocos/gfx/device.ts +++ b/cocos/gfx/device.ts @@ -55,6 +55,14 @@ export abstract class GFXDevice { return this._queue; } + public get width(): number { + return this._width; + } + + public get height(): number { + return this._height; + } + public get mainWindow(): GFXWindow | null { return this._mainWindow; } @@ -99,12 +107,18 @@ export abstract class GFXDevice { return this._stencilBits; } + public genShaderId(): number { + return this._shaderIdGen++; + } + protected _canvas: HTMLCanvasElement | null = null; protected _deviceName: string = ""; protected _renderer: string = ""; protected _vendor: string = ""; protected _version: string = ""; protected _queue: GFXQueue | null = null; + protected _width: number = 0; + protected _height: number = 0; protected _mainWindow: GFXWindow | null = null; protected _cmdAllocator: GFXCommandAllocator | null = null; protected _maxVertexAttributes: number = 0; @@ -114,4 +128,5 @@ export abstract class GFXDevice { protected _maxVertexTextureUnits: number = 0; protected _depthBits: number = 0; protected _stencilBits: number = 0; + protected _shaderIdGen: number = 0; }; diff --git a/cocos/gfx/input-assembler.ts b/cocos/gfx/input-assembler.ts index 88be409ef4..fe18052dd4 100644 --- a/cocos/gfx/input-assembler.ts +++ b/cocos/gfx/input-assembler.ts @@ -6,6 +6,7 @@ export interface GFXInputAttribute { binding?: number; name: string; format: GFXFormat; + isNormalized?: boolean; stream?: number; isInstanced?: boolean; }; diff --git a/cocos/gfx/pipeline-state.ts b/cocos/gfx/pipeline-state.ts index e371b7dcb1..8d091141b0 100644 --- a/cocos/gfx/pipeline-state.ts +++ b/cocos/gfx/pipeline-state.ts @@ -128,6 +128,34 @@ export abstract class GFXPipelineState { public abstract initialize(info: GFXPipelineStateInfo): boolean; public abstract destroy(): void; + public get shader() : GFXShader { + return this._shader; + } + + public get primitive() : GFXPrimitiveMode { + return this._primitive; + } + + public get rasterizerState() : GFXRasterizerState { + return this._rs; + } + + public get depthStencilState() : GFXDepthStencilState { + return this._dss; + } + + public get blendState() : GFXBlendState { + return this._bs; + } + + public get pipelineLayout() : GFXPipelineLayout { + return this._layout; + } + + public get renderPass() : GFXRenderPass { + return this._renderPass; + } + protected _device: GFXDevice; protected _shader: GFXShader | null = null; protected _primitive: GFXPrimitiveMode = GFXPrimitiveMode.TRIANGLE_LIST; diff --git a/cocos/gfx/shader.ts b/cocos/gfx/shader.ts index 764ebfb6dc..980413e9ba 100644 --- a/cocos/gfx/shader.ts +++ b/cocos/gfx/shader.ts @@ -45,12 +45,18 @@ export abstract class GFXShader { constructor(device: GFXDevice) { this._device = device; + this._id = device.genShaderId(); } public abstract initialize(info: GFXShaderInfo): boolean; public abstract destroy(); + public get id(): number { + return this._id; + } + protected _device: GFXDevice; + protected _id: number; protected _name: string = ""; protected _stages: GFXShaderStage[] = []; //protected _bindings: GFXBinding[] = []; diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index df5e0e6e85..09748aedb5 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -1075,7 +1075,8 @@ export function WebGLCmdFuncCreateInputAssember(device: WebGLGFXDevice, gpuInput count: GFXFormatInfos[attrib.format].count, stride: gpuBuffer.stride, componentCount: WebGLGetComponentCount(glType), - isInstanced: attrib.isInstanced !== undefined ? attrib.isInstanced : false, + isNormalized: (attrib.isNormalized !== undefined? attrib.isNormalized : false), + isInstanced: (attrib.isInstanced !== undefined ? attrib.isInstanced : false), offset: offsets[stream], }; @@ -1918,7 +1919,7 @@ export function WebGLCmdFuncExecuteCmds(device: WebGLGFXDevice, cmdPackage: WebG } device.stateCache.glCurrentAttribLocs[glLoc] = true; - gl.vertexAttribPointer(glLoc, glAttrib.count, glAttrib.glType, false, glAttrib.stride, attribOffset); + gl.vertexAttribPointer(glLoc, glAttrib.count, glAttrib.glType, glAttrib.isNormalized, glAttrib.stride, attribOffset); } } } // if diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index 1279e9783e..5e28863b76 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -103,7 +103,7 @@ export class WebGLGFXDevice extends GFXDevice { return false; } - console.info('WebGL device initialization successed.'); + console.info('WebGL device initialized.'); this._deviceName = "WebGL"; let gl = this._webGLRC; @@ -126,9 +126,14 @@ export class WebGLGFXDevice extends GFXDevice { this._depthBits = gl.getParameter(WebGLRenderingContext.DEPTH_BITS); this._stencilBits = gl.getParameter(WebGLRenderingContext.STENCIL_BITS); + this._width = this._canvas.width; + this._height = this._canvas.height; + console.info("RENDERER: " + this._renderer); console.info("VENDOR: " + this._vendor); console.info("VERSION: " + this._version); + console.info("SCREEN_SIZE: " + this._width + "x" + this._height); + console.info("MAX_VERTEX_ATTRIBS: " + this._maxVertexAttributes); console.info("MAX_VERTEX_UNIFORM_VECTORS: " + this._maxVertexUniformVectors); console.info("MAX_FRAGMENT_UNIFORM_VECTORS: " + this._maxFragmentUniformVectors); diff --git a/cocos/gfx/webgl/webgl-gpu-objects.ts b/cocos/gfx/webgl/webgl-gpu-objects.ts index 160f9ecce5..9bfd803b83 100644 --- a/cocos/gfx/webgl/webgl-gpu-objects.ts +++ b/cocos/gfx/webgl/webgl-gpu-objects.ts @@ -235,6 +235,7 @@ export class WebGLAttrib { count: number = 0; stride: number = 0; componentCount: number = 1; + isNormalized: boolean = false; isInstanced: boolean = false; offset: number = 0; }; diff --git a/cocos/pipeline/forward/forward-pipeline.ts b/cocos/pipeline/forward/forward-pipeline.ts index ba1f6b932e..ddc813292d 100644 --- a/cocos/pipeline/forward/forward-pipeline.ts +++ b/cocos/pipeline/forward/forward-pipeline.ts @@ -1,7 +1,8 @@ -import { RenderPipeline, RenderPassStage } from "../render-pipeline"; +import { RenderPipeline, RenderPassStage, UBOGlobal } from "../render-pipeline"; import { Root } from "../../core/root"; import { ForwardFlow } from "./forward-flow"; import { GFXRenderPass } from "../../gfx/render-pass"; +import { GFXBufferUsageBit, GFXMemoryUsageBit } from "../../gfx/define"; export enum ForwardFlowPriority { FORWARD = 0, @@ -19,6 +20,10 @@ export class ForwardPipeline extends RenderPipeline { return false; } + if(!this.createUBOs()) { + return false; + } + let mainWindow = this._root.mainWindow; let windowPass: GFXRenderPass | null = null; @@ -33,8 +38,14 @@ export class ForwardPipeline extends RenderPipeline { this.addRenderPass(RenderPassStage.FORWARD, windowPass); - // create flows + // create UBOs + this._globalUBO = this._root.device.createBuffer({ + usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST, + size: UBOGlobal.SIZE, + }); + // create flows this.createFlow(ForwardFlow, { name: "ForwardFlow", priority: ForwardFlowPriority.FORWARD, @@ -43,9 +54,11 @@ export class ForwardPipeline extends RenderPipeline { return true; } - public destroy(): void { + public destroy() { this.destroyFlows(); this.clearRenderPasses(); + this.destroyUBOs(); this.destroyQuadInputAssembler(); } + }; diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index 5c7fd4c6a9..f56eb8f420 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -6,16 +6,45 @@ import { GFXDevice } from "../gfx/device"; import { GFXBuffer } from "../gfx/buffer"; import { GFXInputAssembler, GFXInputAttribute } from "../gfx/input-assembler"; import { GFXBufferUsageBit, GFXMemoryUsageBit, GFXFormat } from "../gfx/define"; +import { Camera } from "../renderer/scene/camera"; +import vec4 from "../core/vmath/vec4"; +import mat4 from "../core/vmath/mat4"; export enum RenderPassStage { - FORWARD = 0, - CUSTOM = 100, + FORWARD = 100, }; -export interface QuadVertex { - position: number[]; - texCoord: number[]; -} +export class UBOGlobal { + static TIME_OFFSET: number = 0; + static SCREEN_SIZE_OFFSET: number = UBOGlobal.TIME_OFFSET + 4; + static SCREEN_SCALE_OFFSET: number = UBOGlobal.SCREEN_SIZE_OFFSET + 4; + static MAT_VIEW_OFFSET: number = UBOGlobal.SCREEN_SCALE_OFFSET + 4; + static MAT_VIEW_INV_OFFSET: number = UBOGlobal.MAT_VIEW_OFFSET + 16; + static MAT_PROJ_OFFSET: number = UBOGlobal.MAT_VIEW_INV_OFFSET + 16; + static MAT_PROJ_INV_OFFSET: number = UBOGlobal.MAT_PROJ_OFFSET + 16; + static MAT_VIEW_PROJ_OFFSET: number = UBOGlobal.MAT_VIEW_INV_OFFSET + 16; + static MAT_VIEW_PROJ_INV_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_OFFSET + 16; + static CAMERA_POS_OFFSET: number = UBOGlobal.MAT_VIEW_PROJ_INV_OFFSET + 16; + static COUNT: number = UBOGlobal.CAMERA_POS_OFFSET + 4; + static SIZE: number = UBOGlobal.COUNT * 4; + + view: Float32Array = new Float32Array(UBOGlobal.COUNT); +}; + +export class UBOLocal { + static MAT_WORLD_OFFSET: number = 0; + static MAT_WORLD_VIEW_OFFSET: number = UBOLocal.MAT_WORLD_OFFSET + 16; + static MAT_WORLD_VIEW_PROJ_OFFSET: number = UBOLocal.MAT_WORLD_VIEW_OFFSET + 16; + static WORLD_POS_OFFSET: number = UBOLocal.MAT_WORLD_VIEW_PROJ_OFFSET + 16; + static COUNT: number = UBOLocal.WORLD_POS_OFFSET + 4; + static SIZE: number = UBOGlobal.COUNT * 4; + + view: Float32Array = new Float32Array(UBOLocal.COUNT); +}; + +let _vec4Array = new Float32Array(4); +let _mat4Array = new Float32Array(16); +let _outMat = new mat4; export abstract class RenderPipeline { @@ -28,6 +57,9 @@ export abstract class RenderPipeline { public abstract destroy(); public render(view: RenderView) { + + this.updateUBOs(view); + for (let i = 0; i < this._flows.length; ++i) { this._flows[i].render(view); } @@ -89,8 +121,16 @@ export abstract class RenderPipeline { return this._flows; } - public get quadIA(): GFXInputAssembler | null { - return this._quadIA; + public get quadIA(): GFXInputAssembler { + return this._quadIA; + } + + public get uboGlobal(): UBOGlobal { + return this._uboGlobal; + } + + public get globalUBO(): GFXBuffer { + return this._globalUBO; } protected createQuadInputAssembler(): boolean { @@ -174,6 +214,79 @@ export abstract class RenderPipeline { } } + protected createUBOs(): boolean { + this._globalUBO = this._root.device.createBuffer({ + usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST, + size: UBOGlobal.SIZE, + }); + + if(!this._globalUBO) { + return false; + } + + return true; + } + + protected destroyUBOs() { + if(this._globalUBO) { + this._globalUBO.destroy(); + this._globalUBO = null; + } + } + + protected updateUBOs(view: RenderView) { + let camera = view.camera; + camera.update(); + + // update UBOGlobal + _vec4Array[0] = this._root.frameTime; + _vec4Array[1] = 0.0; + _vec4Array[2] = 0.0; + _vec4Array[3] = 0.0; + this._uboGlobal.view.set(_vec4Array, UBOGlobal.TIME_OFFSET); + + _vec4Array[0] = this._root.device.width; + _vec4Array[1] = this._root.device.height; + _vec4Array[2] = 1.0/_vec4Array[0]; + _vec4Array[3] = 1.0/_vec4Array[1]; + this._uboGlobal.view.set(_vec4Array, UBOGlobal.SCREEN_SIZE_OFFSET); + + _vec4Array[0] = 1.0; + _vec4Array[1] = 1.0; + _vec4Array[2] = 1.0; + _vec4Array[3] = 1.0; + this._uboGlobal.view.set(_vec4Array, UBOGlobal.SCREEN_SCALE_OFFSET); + + mat4.array(_mat4Array, camera.matView); + this._uboGlobal.view.set(_mat4Array, UBOGlobal.MAT_VIEW_OFFSET); + + mat4.invert(_outMat, camera.matView); + mat4.array(_mat4Array, _outMat); + this._uboGlobal.view.set(_mat4Array, UBOGlobal.MAT_VIEW_INV_OFFSET); + + mat4.array(_mat4Array, camera.matProj); + this._uboGlobal.view.set(_mat4Array, UBOGlobal.MAT_PROJ_OFFSET); + + mat4.invert(_outMat, camera.matProj); + mat4.array(_mat4Array, _outMat); + this._uboGlobal.view.set(_mat4Array, UBOGlobal.MAT_PROJ_INV_OFFSET); + + mat4.array(_mat4Array, camera.matViewProj); + this._uboGlobal.view.set(_mat4Array, UBOGlobal.MAT_VIEW_PROJ_OFFSET); + + mat4.invert(_outMat, camera.matViewProj); + mat4.array(_mat4Array, _outMat); + this._uboGlobal.view.set(_mat4Array, UBOGlobal.MAT_VIEW_PROJ_INV_OFFSET); + + vec4.array(_vec4Array, camera.position); + _vec4Array[3] = 1.0; + this._uboGlobal.view.set(_vec4Array, UBOGlobal.CAMERA_POS_OFFSET); + + // update ubos + (this._globalUBO).update(this._uboGlobal.view); + } + protected _root: Root; protected _device: GFXDevice; protected _renderPasses: Map = new Map; @@ -181,4 +294,6 @@ export abstract class RenderPipeline { protected _quadVB: GFXBuffer | null = null; protected _quadIB: GFXBuffer | null = null; protected _quadIA: GFXInputAssembler | null = null; + protected _uboGlobal: UBOGlobal = new UBOGlobal; + protected _globalUBO: GFXBuffer | null = null; }; diff --git a/cocos/pipeline/render-view.ts b/cocos/pipeline/render-view.ts index aa58ea44d0..32447eaf43 100644 --- a/cocos/pipeline/render-view.ts +++ b/cocos/pipeline/render-view.ts @@ -49,13 +49,13 @@ export class RenderView { } public attach(camera: Camera) { - this._sceneMgr = camera.sceneMgr; + this._scene = camera.scene; this._camera = camera; this._isAttached = true; } public detach() { - this._sceneMgr = null; + this._scene = null; this._camera = null; this._isAttached = false; } @@ -92,12 +92,20 @@ export class RenderView { return this._height; } + public get camera(): Camera | null { + return this._camera; + } + + public get scene(): RenderScene | null { + return this._scene; + } + private _name: string = ""; private _window: GFXWindow | null = null; private _priority: number = 0; private _width: number = 0; private _height: number = 0; - private _sceneMgr: RenderScene | null = null; + private _scene: RenderScene | null = null; private _camera: Camera | null = null; private _isAttached: boolean = false; private _isEnable: boolean = true; diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index 1782923d86..8b7e00f4c1 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -39,8 +39,8 @@ export class Pass { const renderPass = cc.director.root.pipeline.getRenderPass(pass._stage); if (!renderPass) { console.error('create render pass failed'); return pass; } const bs = new GFXBlendState(); - if (info.BlendState) { - const bsInfo = Object.assign({}, info.BlendState); + if (info.blendState) { + const bsInfo = Object.assign({}, info.blendState); if (bsInfo.targets) { bsInfo.targets.forEach((t, i) => Object.assign( bs.targets[i] || (bs.targets[i] = new GFXBlendTarget()), t)); @@ -51,8 +51,8 @@ export class Pass { pass._pipelineState = device.createPipelineState({ primitive: info.primitive || GFXPrimitiveMode.TRIANGLE_LIST, - rs: Object.assign(new GFXRasterizerState(), info.RasterizerState), - dss: Object.assign(new GFXDepthStencilState(), info.DepthStencilState), + rs: Object.assign(new GFXRasterizerState(), info.rasterizerState), + dss: Object.assign(new GFXDepthStencilState(), info.rasterizerState), is: new GFXInputState(), bs, shader, layout, renderPass, }); @@ -60,6 +60,7 @@ export class Pass { } get programName() { return this._programName; } + get pipelineState(): GFXPipelineState { return this._pipelineState; } protected _device: GFXDevice | null = null; protected _programName: string = ''; diff --git a/cocos/renderer/index.js b/cocos/renderer/index.js index 593e989d8e..cd824b1e0d 100644 --- a/cocos/renderer/index.js +++ b/cocos/renderer/index.js @@ -10,7 +10,7 @@ import InputAssembler from './core/input-assembler'; import View from './core/view'; import Light from './scene/light'; -import Camera from './scene/camera'; +import { Camera } from './scene/camera'; import Model from './scene/model'; import Scene from './scene/scene'; diff --git a/cocos/renderer/scene/camera.js b/cocos/renderer/scene/camera.js deleted file mode 100644 index 63dd0323b4..0000000000 --- a/cocos/renderer/scene/camera.js +++ /dev/null @@ -1,478 +0,0 @@ -// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - -import { color4, vec3, mat4, lerp } from '../../core/vmath'; -import { ray } from '../../3d/geom-utils'; -import enums from '../enums'; - -let _matView = mat4.create(); -let _matProj = mat4.create(); -let _matViewProj = mat4.create(); -let _matInvViewProj = mat4.create(); -let _tmp_v3 = vec3.create(); -let _tmp2_v3 = vec3.create(); - -/** - * A representation of a camera instance - */ -export default class Camera { - /** - * Setup a default perspective camera - */ - constructor(scene, name) { - - this._scene = scene; - this._name = name; - this._enable = false; - - this._poolID = -1; - this._node = null; - - // - this._projection = enums.PROJ_PERSPECTIVE; - - // priority. the smaller one will be rendered first - this._priority = 0; - - // clear options - this._color = color4.create(); - this._depth = 1; - this._stencil = 0; - this._clearFlags = enums.CLEAR_COLOR | enums.CLEAR_DEPTH; - this._clearModel = null; - - // stages & framebuffer - this._stages = 0; - this._framebuffer = null; - - // projection properties - this._near = 0.01; - this._far = 1000.0; - this._fov = Math.PI/4.0; // vertical fov - this._rect = { - x: 0, y: 0, w: 1, h: 1 - }; - - // ortho properties - this._orthoHeight = 10; - } - - get scene() { - return this._scene; - } - - get name() { - return this._name; - } - - /** - * Get the hosting node of this camera - * @returns {Node} the hosting node - */ - getNode() { - return this._node; - } - - /** - * Set the hosting node of this camera - * @param {Node} node the hosting node - */ - setNode(node) { - this._node = node; - } - - /** - * Get the projection type of the camera - * @returns {number} camera projection type - */ - getType() { - return this._projection; - } - - /** - * Set the projection type of the camera - * @param {number} type camera projection type - */ - setType(type) { - this._projection = type; - } - - /** - * Get the priority of the camera - * @returns {number} camera priority - */ - getPriority() { - return this._priority; - } - - /** - * Set the priority of the camera - * @param {number} priority camera priority - */ - setPriority(priority) { - this._priority = priority; - } - - /** - * Get the orthogonal height of the camera - * @returns {number} camera height - */ - getOrthoHeight() { - return this._orthoHeight; - } - - /** - * Set the orthogonal height of the camera - * @param {number} val camera height - */ - setOrthoHeight(val) { - this._orthoHeight = val; - } - - /** - * Get the field of view of the camera - * @returns {number} camera field of view - */ - getFov() { - return this._fov; - } - - /** - * Set the field of view of the camera - * @param {number} fov camera field of view - */ - setFov(fov) { - this._fov = fov; - } - - /** - * Get the near clipping distance of the camera - * @returns {number} camera near clipping distance - */ - getNear() { - return this._near; - } - - /** - * Set the near clipping distance of the camera - * @param {number} near camera near clipping distance - */ - setNear(near) { - this._near = near; - } - - /** - * Get the far clipping distance of the camera - * @returns {number} camera far clipping distance - */ - getFar() { - return this._far; - } - - /** - * Set the far clipping distance of the camera - * @param {number} far camera far clipping distance - */ - setFar(far) { - this._far = far; - } - - /** - * Get the clear color of the camera - * @returns {color4} out the receiving color vector - */ - getColor(out) { - return color4.copy(out, this._color); - } - - /** - * Set the clear color of the camera - * @param {number} r red channel of camera clear color - * @param {number} g green channel of camera clear color - * @param {number} b blue channel of camera clear color - * @param {number} a alpha channel of camera clear color - */ - setColor(r, g, b, a) { - color4.set(this._color, r, g, b, a); - } - - /** - * Get the clear depth of the camera - * @returns {number} camera clear depth - */ - getDepth() { - return this._depth; - } - - /** - * Set the clear depth of the camera - * @param {number} depth camera clear depth - */ - setDepth(depth) { - this._depth = depth; - } - - /** - * Get the clearing stencil value of the camera - * @returns {number} camera clearing stencil value - */ - getStencil() { - return this._stencil; - } - - /** - * Set the clearing stencil value of the camera - * @param {number} stencil camera clearing stencil value - */ - setStencil(stencil) { - this._stencil = stencil; - } - - /** - * Get the clearing flags of the camera - * @returns {number} camera clearing flags - */ - getClearFlags() { - return this._clearFlags; - } - - /** - * Set the clearing flags of the camera - * @param {number} flags camera clearing flags - */ - setClearFlags(flags) { - this._clearFlags = flags; - } - - /** - * Get the rect of the camera - * @param {Object} out the receiving object - * @returns {Object} camera rect - */ - getRect(out) { - out.x = this._rect.x; - out.y = this._rect.y; - out.w = this._rect.w; - out.h = this._rect.h; - - return out; - } - - /** - * Set the rect of the camera - * @param {Number} x - [0,1] - * @param {Number} y - [0,1] - * @param {Number} w - [0,1] - * @param {Number} h - [0,1] - */ - setRect(x, y, w, h) { - this._rect.x = x; - this._rect.y = y; - this._rect.w = w; - this._rect.h = h; - } - - /** - * Get the stages of the camera - * @returns {string[]} camera stages - */ - getStages() { - return this._stages; - } - - /** - * Set the stages of the camera - * @param {number} stages camera stages - */ - setStages(stages) { - this._stages = stages; - } - - /** - * Get the framebuffer of the camera - * @returns {FrameBuffer} camera framebuffer - */ - getFramebuffer() { - return this._framebuffer; - } - - /** - * Set the framebuffer of the camera - * @param {FrameBuffer} framebuffer camera framebuffer - */ - setFramebuffer(framebuffer) { - this._framebuffer = framebuffer; - } - - _calcMatrices(width, height) { - // view matrix - this._node.getWorldRT(_matView); - mat4.invert(_matView, _matView); - - // projection matrix - let aspect = width / height; - if (this._projection === enums.PROJ_PERSPECTIVE) { - mat4.perspective(_matProj, - this._fov, - aspect, - this._near, - this._far - ); - } else { - let x = this._orthoHeight * aspect; - let y = this._orthoHeight; - mat4.ortho(_matProj, - -x, x, -y, y, this._near, this._far - ); - } - - // view-projection - mat4.mul(_matViewProj, _matProj, _matView); - // inv view-projection - mat4.invert(_matInvViewProj, _matViewProj); - } - - /** - * extract a view of this camera - * @param {View} out the receiving view - * @param {number} width framebuffer width - * @param {number} height framebuffer height - */ - extractView(out, width, height) { - if (this._framebuffer) { - width = this._framebuffer._width; - height = this._framebuffer._height; - } - - // priority - out._priority = this._priority; - - // rect - out._rect.x = this._rect.x * width; - out._rect.y = this._rect.y * height; - out._rect.w = this._rect.w * width; - out._rect.h = this._rect.h * height; - - // clear opts - out._color = this._color; - out._depth = this._depth; - out._stencil = this._stencil; - out._clearFlags = this._clearFlags; - out._clearModel = this._clearModel; - - // stages & framebuffer - out._stages = this._stages; - out._framebuffer = this._framebuffer; - - this._calcMatrices(width, height); - mat4.copy(out._matView, _matView); - mat4.copy(out._matProj, _matProj); - mat4.copy(out._matViewProj, _matViewProj); - mat4.copy(out._matInvViewProj, _matInvViewProj); - - // update view's frustum - out._frustum.update(_matViewProj, _matInvViewProj); - } - - /** - * transform a screen position to a world space ray - * @param {number} x the screen x position to be transformed - * @param {number} y the screen y position to be transformed - * @param {number} width framebuffer width - * @param {number} height framebuffer height - * @param {ray} out the resulting ray - * @returns {Ray} the resulting ray - */ - screenPointToRay(x, y, width, height, out) { - out = out || ray.create(); - this._calcMatrices(width, height); - - let cx = this._rect.x * width; - let cy = this._rect.y * height; - let cw = this._rect.w * width; - let ch = this._rect.h * height; - - // far plane intersection - vec3.set(_tmp2_v3, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, 1); - vec3.transformMat4(_tmp2_v3, _tmp2_v3, _matInvViewProj); - - if (this._projection === enums.PROJ_PERSPECTIVE) { - // camera origin - this._node.getWorldPosition(_tmp_v3); - } else { - // near plane intersection - vec3.set(_tmp_v3, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, -1); - vec3.transformMat4(_tmp_v3, _tmp_v3, _matInvViewProj); - } - - return ray.fromPoints(out, _tmp_v3, _tmp2_v3); - } - - /** - * transform a screen position to world space - * @param {vec3} out the resulting vector - * @param {vec3} screenPos the screen position to be transformed - * @param {number} width framebuffer width - * @param {number} height framebuffer height - * @returns {vec3} the resulting vector - */ - screenToWorld(out, screenPos, width, height) { - this._calcMatrices(width, height); - - let cx = this._rect.x * width; - let cy = this._rect.y * height; - let cw = this._rect.w * width; - let ch = this._rect.h * height; - - if (this._projection === enums.PROJ_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, _matInvViewProj); - - // lerp to depth z - this._node.getWorldPosition(_tmp_v3); - - vec3.lerp(out, _tmp_v3, out, lerp(this._near / this._far, 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, _matInvViewProj); - } - - return out; - } - - /** - * transform a world space position to screen space - * @param {vec3} out the resulting vector - * @param {vec3} worldPos the world space position to be transformed - * @param {number} width framebuffer width - * @param {number} height framebuffer height - * @returns {vec3} the resulting vector - */ - worldToScreen(out, worldPos, width, height) { - this._calcMatrices(width, height); - - let cx = this._rect.x * width; - let cy = this._rect.y * height; - let cw = this._rect.w * width; - let ch = this._rect.h * height; - - vec3.transformMat4(out, worldPos, _matViewProj); - 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; - } -} diff --git a/cocos/renderer/scene/camera.ts b/cocos/renderer/scene/camera.ts new file mode 100644 index 0000000000..5d1f7a4bdf --- /dev/null +++ b/cocos/renderer/scene/camera.ts @@ -0,0 +1,141 @@ +import { RenderScene } from "./render-scene"; +import Node from "../../scene-graph/node"; +import vec3 from "../../core/vmath/vec3"; +import mat4 from "../../core/vmath/mat4"; + +export enum CameraProjection { + PERSPECTIVE, + ORTHO, +}; + +export class Camera { + + constructor(scene: RenderScene, name: string) { + this._scene = scene; + this._name = name; + this._node = scene.createNode({ name: name + "Node", isStatic: false }); + this._proj = CameraProjection.PERSPECTIVE; + + let window = scene.root.mainWindow; + if (window) { + this._width = window.width; + this._height = window.height; + } else { + this._width = 1; + this._height = 1; + } + + this._aspect = this._width / this._height; + this._fov = Math.PI / 4; + this._nearClip = 1.0; + this._farClip = 10000.0; + } + + public resize(width: number, height: number) { + this._width = width; + this._height = height; + this._aspect = this._width / this._height; + } + + public update() { + // view matrix + this._node.getWorldRT(this._matView); + mat4.invert(this._matView, this._matView); + + // projection matrix + if (this._proj === CameraProjection.PERSPECTIVE) { + mat4.perspective(this._matProj, this._fov, this._aspect, this._nearClip, this._farClip); + } else { + let x = this._width; + let y = this._height; + mat4.ortho(this._matProj, -x, x, -y, y, this._nearClip, this._farClip); + } + + // view-projection + mat4.mul(this._matViewProj, this._matProj, this._matView); + } + + public set fov(fov: number) { + this._fov = fov; + } + + public set nearClip(nearClip: number) { + this._nearClip = nearClip; + } + + public set farClip(farClip: number) { + this._farClip = farClip; + } + + public get scene(): RenderScene { + return this._scene; + } + + public get name(): string { + return this._name; + } + + public get node(): Node { + return this._node; + } + + public get width(): number { + return this._width; + } + + public get height(): number { + return this._height; + } + + public get aspect(): number { + 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(): number { + return this._matView; + } + + public get matProj(): number { + return this._matProj; + } + + public get matViewProj(): number { + return this._matViewProj; + } + + public set position(pos: vec3) { + this._node.setWorldPosition(pos.x, pos.y, pos.z); + } + + public get position(): vec3 { + this._node.getWorldPosition(this._position); + return this._position; + } + + private _scene: RenderScene; + private _name: string; + private _node: Node; + private _proj: CameraProjection; + private _width: number; + private _height: number; + private _aspect: number; + private _fov: number; + private _nearClip: number; + private _farClip: number; + private _matView: mat4 = new mat4; + private _matProj: mat4 = new mat4; + private _matViewProj: mat4 = new mat4; + private _position: vec3 = new vec3; +}; diff --git a/cocos/renderer/scene/model.ts b/cocos/renderer/scene/model.ts index 13cfd68490..74bb8197f9 100644 --- a/cocos/renderer/scene/model.ts +++ b/cocos/renderer/scene/model.ts @@ -68,7 +68,7 @@ export default class Model { } } - get scene(): RenderScene { + get scene(): RenderScene | null { return this._scene; } diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index 56f95eed2c..d8ef313727 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -1,7 +1,8 @@ import Node from "../../scene-graph/node"; -import Camera from "./camera"; import Light from "./light"; import Model from "./model"; +import { Camera } from "./camera"; +import { Root } from "../../core/root"; export interface RenderSceneInfo { name: string; @@ -15,15 +16,13 @@ export interface SceneNodeInfo { export class RenderScene { - constructor() { + constructor(root: Root) { + this._root = root; } public initialize(info: RenderSceneInfo): boolean { this._name = info.name; - - let cameraNode = this.createNode({ name: "mainCamNode", isStatic: false }); this._mainCamera = this.createCamera("mainCamera"); - this._mainCamera.setNode(cameraNode); return true; } @@ -80,9 +79,8 @@ export class RenderScene { public getCamera(name: string): Camera | null { for (let i = 0; i < this._cameras.length; ++i) { - let camera = this._cameras[i]; - if (camera.getName() === name) { - return camera; + if (this._cameras[i].name === name) { + return this._cameras[i]; } } @@ -143,6 +141,10 @@ export class RenderScene { return this._modelId++; } + public get root(): Root { + return this._root; + } + public get name(): string { return this._name; } @@ -163,6 +165,7 @@ export class RenderScene { return this._models; } + private _root: Root; private _name: string = ""; private _nodes: Map = new Map; private _cameras: Camera[] = [];