diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index 1ef5140250..a177314e50 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -2,18 +2,18 @@ export default [ { "name": "test", "techniques": [{"queue": 0, "lod": 500, "passes": [{"stage": 100, "program": "f4c45e6cc7794309d86202e6ec9036a3871fd1ec167a9e02eb1e1574"}], "priority": 0}], - "properties": {"u_color": {"type": 17, "value": [1, 1, 1, 1]}, "u_sampler": {"type": 29, "value": null}}, + "properties": {"u_color": {"type": 16, "value": [1, 1, 1, 1]}, "u_sampler": {"type": 29, "value": null}}, "shaders": [ { "name": "f4c45e6cc7794309d86202e6ec9036a3871fd1ec167a9e02eb1e1574", "vert": ` attribute vec3 a_position; - attribute vec2 a_texCoord; + attribute vec2 a_uv0; uniform mat4 cc_matWorld; varying vec2 v_texCoord; void main() { gl_Position = cc_matWorld*vec4(a_position, 1.0); - v_texCoord = a_texCoord; + v_texCoord = a_uv0; } `, "frag": ` diff --git a/cocos/3d/framework/camera-component.js b/cocos/3d/framework/camera-component.js index 44059b77f3..b556cb3904 100644 --- a/cocos/3d/framework/camera-component.js +++ b/cocos/3d/framework/camera-component.js @@ -134,7 +134,8 @@ export default class CameraComponent extends Component { if (this._projection === CameraProjection.Ortho) { type = renderer.PROJ_ORTHO; } - this._camera.setType(type); + if (this.enabled) + this._camera.setType(type); } /** @@ -150,7 +151,8 @@ export default class CameraComponent extends Component { set priority(val) { this._priority = val; - this._camera.setPriority(val); + if (this.enabled) + this._camera.setPriority(val); } /** @@ -166,7 +168,8 @@ export default class CameraComponent extends Component { set fov(val) { this._fov = val; - this._camera.setFov(toRadian(val)); + if (this.enabled) + this._camera.setFov(toRadian(val)); } /** @@ -182,7 +185,8 @@ export default class CameraComponent extends Component { set orthoHeight(val) { this._orthoHeight = val; - this._camera.setOrthoHeight(val); + if (this.enabled) + this._camera.setOrthoHeight(val); } /** @@ -198,7 +202,8 @@ export default class CameraComponent extends Component { set near(val) { this._near = val; - this._camera.setNear(val); + if (this.enabled) + this._camera.setNear(val); } /** @@ -214,7 +219,8 @@ export default class CameraComponent extends Component { set far(val) { this._far = val; - this._camera.setFar(val); + if (this.enabled) + this._camera.setFar(val); } /** @@ -230,7 +236,8 @@ export default class CameraComponent extends Component { set color(val) { this._color = val; - this._camera.setColor(val.r / 255, val.g / 255, val.b / 255, val.a / 255); + if (this.enabled) + this._camera.setColor(val.r / 255, val.g / 255, val.b / 255, val.a / 255); } /** @@ -246,7 +253,8 @@ export default class CameraComponent extends Component { set depth(val) { this._depth = val; - this._camera.setDepth(val); + if (this.enabled) + this._camera.setDepth(val); } /** @@ -262,7 +270,8 @@ export default class CameraComponent extends Component { set stencil(val) { this._stencil = val; - this._camera.setStencil(val); + if (this.enabled) + this._camera.setStencil(val); } /** @@ -280,7 +289,8 @@ export default class CameraComponent extends Component { set clearFlags(val) { this._clearFlags = val; - this._camera.setClearFlags(val); + if (this.enabled) + this._camera.setClearFlags(val); } /** @@ -296,7 +306,8 @@ export default class CameraComponent extends Component { set rect(val) { this._rect = val; - this._camera.setRect(val.x, val.y, val.width, val.height); + if (this.enabled) + this._camera.setRect(val.x, val.y, val.width, val.height); } static Projection = CameraProjection; @@ -306,7 +317,11 @@ export default class CameraComponent extends Component { } onLoad() { - this._camera = this.getRenderScene().createCamera(this.name); + + } + + onEnable() { + this._camera = this._getRenderScene().createCamera(this.name); this.projection = this._projection; this.priority = this._priority; this.fov = this._fov; @@ -322,16 +337,12 @@ export default class CameraComponent extends Component { this._camera.setNode(this.node); } - onEnable() { - this._camera._enable = this.enabled; - } - onDisable() { - this._camera._enable = this.enabled; + this._getRenderScene().destroyCamera(this._camera); } onDestroy() { - this.getRenderScene().destroyCamera(this._camera); + } /** diff --git a/cocos/3d/framework/light-component.js b/cocos/3d/framework/light-component.js index 948e12cc8a..01493bba33 100644 --- a/cocos/3d/framework/light-component.js +++ b/cocos/3d/framework/light-component.js @@ -180,7 +180,8 @@ export default class LightComponent extends Component { } else if (this._type === LightType.Spot) { type = renderer.LIGHT_SPOT; } - this._light.setType(type); + if (this.enabled) + this._light.setType(type); } /** @@ -196,7 +197,8 @@ export default class LightComponent extends Component { set color(val) { this._color = val; - this._light.setColor(val.r / 255, val.g / 255, val.b / 255); + if (this.enabled) + this._light.setColor(val.r / 255, val.g / 255, val.b / 255); } /** @@ -212,7 +214,8 @@ export default class LightComponent extends Component { set intensity(val) { this._intensity = val; - this._light.setIntensity(val); + if (this.enabled) + this._light.setIntensity(val); } /** @@ -228,7 +231,8 @@ export default class LightComponent extends Component { set range(val) { this._range = val; - this._light.setRange(val); + if (this.enabled) + this._light.setRange(val); } /** @@ -244,7 +248,8 @@ export default class LightComponent extends Component { set spotAngle(val) { this._spotAngle = val; - this._light.setSpotAngle(toRadian(val)); + if (this.enabled) + this._light.setSpotAngle(toRadian(val)); } /** @@ -260,7 +265,8 @@ export default class LightComponent extends Component { set spotExp(val) { this._spotExp = val; - this._light.setSpotExp(val); + if (this.enabled) + this._light.setSpotExp(val); } /** @@ -285,7 +291,8 @@ export default class LightComponent extends Component { } else if (val === LightShadowType.Soft) { type = renderer.SHADOW_SOFT; } - this._light.setShadowType(type); + if (this.enabled) + this._light.setShadowType(type); } /** @@ -302,7 +309,8 @@ export default class LightComponent extends Component { set shadowResolution(val) { this._shadowResolution = val; - this._light.setShadowResolution(val); + if (this.enabled) + this._light.setShadowResolution(val); } /** @@ -319,7 +327,8 @@ export default class LightComponent extends Component { set shadowDarkness(val) { this._shadowDarkness = val; - this._light.setShadowDarkness(val); + if (this.enabled) + this._light.setShadowDarkness(val); } /** @@ -336,7 +345,8 @@ export default class LightComponent extends Component { set shadowMinDepth(val) { this._shadowMinDepth = val; - this._light.setShadowMinDepth(val); + if (this.enabled) + this._light.setShadowMinDepth(val); } /** @@ -353,7 +363,8 @@ export default class LightComponent extends Component { set shadowMaxDepth(val) { this._shadowMaxDepth = val; - this._light.setShadowMaxDepth(val); + if (this.enabled) + this._light.setShadowMaxDepth(val); } /** @@ -370,7 +381,8 @@ export default class LightComponent extends Component { set shadowDepthScale(val) { this._shadowDepthScale = val; - this._light.setShadowDepthScale(val); + if (this.enabled) + this._light.setShadowDepthScale(val); } /** @@ -387,7 +399,8 @@ export default class LightComponent extends Component { set shadowFrustumSize(val) { this._shadowFrustumSize = val; - this._light.setShadowFrustumSize(val); + if (this.enabled) + this._light.setShadowFrustumSize(val); } /** @@ -404,7 +417,8 @@ export default class LightComponent extends Component { set shadowBias(val) { this._shadowBias = val; - this._light.setShadowBias(val); + if (this.enabled) + this._light.setShadowBias(val); } static Type = LightType; @@ -416,7 +430,11 @@ export default class LightComponent extends Component { } onLoad() { - this._light = this.getRenderScene().createLight(this.name); + + } + + onEnable() { + this._light = this._getRenderScene().createLight(this.name); this._light.setNode(this.node); this.type = this._type; this.color = this._color; @@ -433,15 +451,11 @@ export default class LightComponent extends Component { this.shadowBias = this._shadowBias; } - onEnable() { - this._light._enable = this.enabled; - } - onDisable() { - this._light._enable = this.enabled; + this._getRenderScene().destroyLight(this._light); } onDestroy() { - this.getRenderScene().destroyLight(this._light); + } } diff --git a/cocos/3d/framework/model-component.js b/cocos/3d/framework/model-component.js index e9c0f1c5e7..9b6807e7fa 100644 --- a/cocos/3d/framework/model-component.js +++ b/cocos/3d/framework/model-component.js @@ -171,49 +171,42 @@ export default class ModelComponent extends RenderableComponent { } onLoad() { + + } + + onEnable() { this._updateModels(); this._updateCastShadow(); this._updateReceiveShadow(); } - onEnable() { - for (let i = 0; i < this._models.length; ++i) { - this._models[i]._enable = this.enabled; - } - } - onDisable() { for (let i = 0; i < this._models.length; ++i) { - this._models[i]._enable = this.enabled; + this._getRenderScene().destroyModel(this._models[i]); } + this._models.splice(0); } onDestroy() { - for (let i = 0; i < this._models.length; ++i) { - this.getRenderScene().destroyModel(this._models[i]); - } + } _updateModels() { + if (!this.enabled) + return; let meshCount = this._mesh ? this._mesh.subMeshCount : 0; - let oldModels = this._models; - this._models = new Array(meshCount); for (let i = 0; i < meshCount; ++i) { let model = this._createModel(); model.createBoundingShape(this._mesh.minPosition, this._mesh.maxPosition); - this._models[i] = model; + this._models.push(model); } this._updateModelParams(); - - for (let i = 0; i < oldModels.length; ++i) { - this.getRenderScene().destroyModel(oldModels[i]); - } } _createModel() { - return this.getRenderScene().createModel(Model); + return this._getRenderScene().createModel(Model); } _updateModelParams() { @@ -247,10 +240,14 @@ export default class ModelComponent extends RenderableComponent { * @param {Material} material */ _updateModelMaterial(model, material) { + if (!this.enabled) + return; model.material = material ? material : this._getBuiltinMaterial(); } _updateCastShadow() { + if (!this.enabled) + return; if (this._shadowCastingMode === ModelShadowCastingMode.Off) { for (let i = 0; i < this._models.length; ++i) { let model = this._models[i]; @@ -267,6 +264,8 @@ export default class ModelComponent extends RenderableComponent { } _updateReceiveShadow() { + if (!this.enabled) + return; for (let i = 0; i < this._models.length; ++i) { let model = this._models[i]; if (model._defines['_USE_SHADOW_MAP'] != undefined) { diff --git a/cocos/assets/render-texture.ts b/cocos/assets/render-texture.ts index 72d001bcc6..30e1ba5699 100644 --- a/cocos/assets/render-texture.ts +++ b/cocos/assets/render-texture.ts @@ -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], }); } diff --git a/cocos/components/component.d.ts b/cocos/components/component.d.ts index 7e20bc95d2..1deb53422d 100644 --- a/cocos/components/component.d.ts +++ b/cocos/components/component.d.ts @@ -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[]; diff --git a/cocos/components/component.js b/cocos/components/component.js index a6ba88a08a..6370572336 100644 --- a/cocos/components/component.js +++ b/cocos/components/component.js @@ -195,8 +195,8 @@ class Component extends CCObject { static system = null; - getRenderScene(index = 0) { - return this.node._scene._renderScenes[index]; + _getRenderScene() { + return this.node._scene._renderScene; } constructor () { diff --git a/cocos/pipeline/forward/test-model-stage.ts b/cocos/pipeline/forward/test-model-stage.ts index 6060404f65..c0b167a9a2 100644 --- a/cocos/pipeline/forward/test-model-stage.ts +++ b/cocos/pipeline/forward/test-model-stage.ts @@ -1,22 +1,14 @@ import { Material } from '../../3d/assets/material'; +import ImageAsset from '../../assets/image-asset'; +import Texture2D from '../../assets/texture-2d'; import { GFXCommandBuffer } from '../../gfx/command-buffer'; -import { - GFXBufferTextureCopy, GFXCommandBufferType, GFXFormat, - GFXTextureFlagBit, GFXTextureType, GFXTextureUsageBit, GFXTextureViewType, GFXType -} from '../../gfx/define'; +import { GFXCommandBufferType} from '../../gfx/define'; import { GFXFramebuffer } from '../../gfx/framebuffer'; -import { GFXInputAssembler } from '../../gfx/input-assembler'; -import { GFXTexture } from '../../gfx/texture'; -import { GFXTextureView } from '../../gfx/texture-view'; -import { RenderFlow } from '../render-flow'; -import { RenderStage, IRenderStageInfo } from '../render-stage'; -import { RenderView } from '../render-view'; import Model from '../../renderer/scene/model'; import { Scene } from '../../scene-graph'; -import { EffectAsset } from '../../3d/assets/effect-asset'; -import { UBOLocal } from '../render-pipeline'; -import Texture2D from '../../assets/texture-2d'; -import ImageAsset from '../../assets/image-asset'; +import { RenderFlow } from '../render-flow'; +import { IRenderStageInfo, RenderStage } from '../render-stage'; +import { RenderView } from '../render-view'; export class TestModelStage extends RenderStage { @@ -26,11 +18,11 @@ export class TestModelStage extends RenderStage { private _init: boolean = false; private _textureAsset: Texture2D | null = null; - constructor(flow: RenderFlow) { + constructor (flow: RenderFlow) { super(flow); } - public initialize(info: IRenderStageInfo): boolean { + public initialize (info: IRenderStageInfo): boolean { if (info.name !== undefined) { this._name = info.name; @@ -50,7 +42,7 @@ export class TestModelStage extends RenderStage { // load texture const imgElms = this._device.canvas.getElementsByTagName('img'); if (imgElms.length) { - if (!this.loadTexture(imgElms[0])) { + if (!this.loadTexture( imgElms[0] as HTMLImageElement)) { console.error('Load texture failed.'); return false; } @@ -58,11 +50,10 @@ export class TestModelStage extends RenderStage { console.error('Not found image, load texture failed.'); } - return true; } - public destroy() { + public destroy () { if (this._texView) { this._texView.destroy(); @@ -84,14 +75,15 @@ export class TestModelStage extends RenderStage { if (!this._init) { - this._material.effectName = 'test'; // parsed-effect file is embedded in cocos/3d/builtin/effects.js this._material.setProperty('u_sampler', this._textureAsset); - this._material.setProperty('u_color', cc.color('#ff0000')); + this._material.setProperty('u_color', cc.color('#ff1234')); + this._material.inited = true; + this._material.passes[0].update(); this._scene = new Scene(); this._scene._scene = this._scene; - let modelCom = this._scene.addComponent('cc.ModelComponent'); + const modelCom = this._scene.addComponent('cc.ModelComponent'); modelCom.material = this._material; modelCom.mesh = cc.utils.createMesh(cc.game._renderContext, cc.primitives.box()); this._model = modelCom._models[0]; @@ -100,13 +92,13 @@ export class TestModelStage extends RenderStage { this._init = true; } - const cmdBuff = this._cmdBuff; + const cmdBuff = this._cmdBuff as GFXCommandBuffer; this._renderArea.width = view.width; this._renderArea.height = view.height; this._model._updateTransform(); this._model.updateRenderData(); cmdBuff.begin(); - cmdBuff.beginRenderPass(this._framebuffer, this._renderArea, this._clearColors, + cmdBuff.beginRenderPass( this._framebuffer as GFXFramebuffer, this._renderArea, this._clearColors, this._clearDepth, this._clearStencil); cmdBuff.execute(this._model.commandBuffers); diff --git a/cocos/renderer/core/program-lib.ts b/cocos/renderer/core/program-lib.ts index 470fdb4e33..5541098bd1 100644 --- a/cocos/renderer/core/program-lib.ts +++ b/cocos/renderer/core/program-lib.ts @@ -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 + deps: Record, ) { 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; + protected _templates: Record; protected _cache: Record; - 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 = 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) { diff --git a/cocos/renderer/scene/camera.ts b/cocos/renderer/scene/camera.ts index 7218696242..d78252b389 100644 --- a/cocos/renderer/scene/camera.ts +++ b/cocos/renderer/scene/camera.ts @@ -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) { + 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; + } } diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index 2968a6ce14..bb13775a62 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -164,7 +164,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; } } diff --git a/cocos/renderer/utils.js b/cocos/renderer/utils.js index 8ec7b9c712..a3821a8693 100644 --- a/cocos/renderer/utils.js +++ b/cocos/renderer/utils.js @@ -32,15 +32,15 @@ export function createIA(device, data) { } let vfmt = []; - vfmt.push({ name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 3 }); + vfmt.push({ name: gfx.ATTR_POSITION, format: GFXFormat.RGB32F }); if (data.normals) { - vfmt.push({ name: gfx.ATTR_NORMAL, type: gfx.ATTR_TYPE_FLOAT32, num: 3 }); + vfmt.push({ name: gfx.ATTR_NORMAL, format: GFXFormat.RGB32F }); } if (data.uvs) { - vfmt.push({ name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 }); + vfmt.push({ name: gfx.ATTR_UV0, format: GFXFormat.RG32F }); } if (data.colors) { - vfmt.push({ name: gfx.ATTR_COLOR, type: gfx.ATTR_TYPE_FLOAT32, num: 3 }); + vfmt.push({ name: gfx.ATTR_COLOR, format: GFXFormat.RGB32F }); } let vb = cc.director.root.device.createBuffer({ @@ -61,56 +61,8 @@ export function createIA(device, data) { ib.update(new Uint16Array(data.indices)); - let attributes = []; - - for (let i = 0; i < vfmt.length; i++) { - let gfxAttr = {}; - gfxAttr.name = vfmt[i].name; - let gfxFormat; - switch (vfmt[i].num) { - case 1: - gfxFormat = 'R'; - break; - case 2: - gfxFormat = 'RG'; - break; - case 3: - gfxFormat = 'RGB'; - break; - case 4: - gfxFormat = 'RGBA'; - break; - } - switch (vfmt[i].type) { - case gfx.ATTR_TYPE_INT8: - gfxFormat += '8I'; - break; - case gfx.ATTR_TYPE_UINT8: - gfxFormat += '8UI'; - break; - case gfx.ATTR_TYPE_INT16: - gfxFormat += '16I'; - break; - case gfx.ATTR_TYPE_UINT16: - gfxFormat += '16UI'; - break; - case gfx.ATTR_TYPE_INT32: - gfxFormat += '32I'; - break; - case gfx.ATTR_TYPE_UINT32: - gfxFormat += '32UI'; - break; - case gfx.ATTR_TYPE_FLOAT32: - gfxFormat += '32F'; - break; - } - gfxAttr.format = GFXFormat[gfxFormat]; - gfxAttr.normalize = vfmt[i].normalize; - attributes.push(gfxAttr); - } - return cc.director.root.device.createInputAssembler({ - attributes, + attributes: vfmt, vertexBuffers: [vb], indexBuffer: ib, }); diff --git a/cocos/scene-graph/node.js b/cocos/scene-graph/node.js index 429af2c35b..72461062ad 100644 --- a/cocos/scene-graph/node.js +++ b/cocos/scene-graph/node.js @@ -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); diff --git a/cocos/scene-graph/scene.js b/cocos/scene-graph/scene.js index b3788a9aac..2e508061b5 100644 --- a/cocos/scene-graph/scene.js +++ b/cocos/scene-graph/scene.js @@ -41,8 +41,6 @@ import { RenderScene } from '../renderer/scene/render-scene'; @ccclass("cc.Scene") export default class Scene extends Node { - _renderScenes = new Array(); - constructor(name) { super(name); @@ -56,8 +54,7 @@ export default class Scene extends Node { // cache all depend assets for auto release this.dependAssets = null; - let scene = cc.director.root.createScene({ name: 'mainScene' }); - this._renderScenes.push(scene); + this._renderScene = cc.director.root.createScene({}); } /** @@ -73,6 +70,7 @@ export default class Scene extends Node { destroy () { super.destroy(); + cc.director.root.destroyScene(this._renderScene); this._activeInHierarchy = false; } diff --git a/playground/simple.html b/playground/simple.html index a914784c6d..718538365e 100644 --- a/playground/simple.html +++ b/playground/simple.html @@ -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); @@ -112,4 +112,4 @@ - \ No newline at end of file +