From 571f7b09476c20cb0fc82670f8d20fcd748aac25 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 10 Jan 2019 21:03:03 +0800 Subject: [PATCH] camera component test scene --- cocos/pipeline/render-pipeline.ts | 2 +- cocos/renderer/scene/camera.ts | 5 ++--- cocos/scene-graph/node.ts | 18 +++++++++--------- playground/simple.html | 21 ++++++++++++--------- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index c77795a958..20d27a0cd2 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -328,7 +328,7 @@ export abstract class RenderPipeline { mat4.array(_mat4Array, camera.matViewProjInv); this._uboGlobal.view.set(_mat4Array, UBOGlobal.MAT_VIEW_PROJ_INV_OFFSET); - vec3.array(_vec4Array, camera.position); + vec3.array(_vec4Array, camera.node && camera.node.getPosition()); _vec4Array[3] = 1.0; this._uboGlobal.view.set(_vec4Array, UBOGlobal.CAMERA_POS_OFFSET); diff --git a/cocos/renderer/scene/camera.ts b/cocos/renderer/scene/camera.ts index 9f01b5b666..acbeb0a130 100644 --- a/cocos/renderer/scene/camera.ts +++ b/cocos/renderer/scene/camera.ts @@ -223,7 +223,6 @@ export class Camera { */ 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; @@ -236,7 +235,7 @@ export class Camera { if (this._proj === CameraProjection.PERSPECTIVE) { // camera origin - this._node.getWorldPosition(v_b); + if (this._node) { this._node.getWorldPosition(v_b); } } else { // near plane intersection vec3.set(v_b, (x - cx) / cw * 2 - 1, (y - cy) / ch * 2 - 1, -1); @@ -267,7 +266,7 @@ export class Camera { vec3.transformMat4(out, out, this._matViewProjInv); // lerp to depth z - this._node.getWorldPosition(v_a); + if (this._node) { this._node.getWorldPosition(v_a); } vec3.lerp(out, v_a, out, lerp(this._nearClip / this._farClip, 1, screenPos.z)); } else { diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts index 5b051de5ef..94ee9641a8 100644 --- a/cocos/scene-graph/node.ts +++ b/cocos/scene-graph/node.ts @@ -297,7 +297,7 @@ class Node extends BaseNode { * @param out the receiving vector * @returns the resulting vector */ - public getPosition (out: Vec3): Vec3 { + public getPosition (out?: Vec3): Vec3 { if (out) { return vec3.set(out, this._lpos.x, this._lpos.y, this._lpos.z); } else { @@ -346,7 +346,7 @@ class Node extends BaseNode { * @param out - the receiving quaternion * @returns the resulting quaternion */ - public getRotation (out: Quat): Quat { + public getRotation (out?: Quat): Quat { if (out) { return quat.set(out, this._lrot.x, this._lrot.y, this._lrot.z, this._lrot.w); } else { @@ -377,7 +377,7 @@ class Node extends BaseNode { * @param out - the receiving vector * @returns the resulting vector */ - public getScale (out: Vec3): Vec3 { + public getScale (out?: Vec3): Vec3 { if (out) { return vec3.set(out, this._lscale.x, this._lscale.y, this._lscale.z); } else { @@ -417,7 +417,7 @@ class Node extends BaseNode { * @param out - the receiving vector * @returns the resulting vector */ - public getWorldPosition (out: Vec3): Vec3 { + public getWorldPosition (out?: Vec3): Vec3 { this.updateWorldTransform(); if (out) { return vec3.copy(out, this._pos); @@ -477,7 +477,7 @@ class Node extends BaseNode { * @param out - the receiving quaternion * @returns the resulting quaternion */ - public getWorldRotation (out: Quat): Quat { + public getWorldRotation (out?: Quat): Quat { this.updateWorldTransform(); if (out) { return quat.copy(out, this._rot); @@ -514,7 +514,7 @@ class Node extends BaseNode { * @param out - the receiving vector * @returns the resulting vector */ - public getWorldScale (out: Vec3): Vec3 { + public getWorldScale (out?: Vec3): Vec3 { this.updateWorldTransform(); if (out) { return vec3.copy(out, this._scale); @@ -528,7 +528,7 @@ class Node extends BaseNode { * @param out - the receiving matrix * @returns the - resulting matrix */ - public getWorldMatrix (out: Mat4): Mat4 { + public getWorldMatrix (out?: Mat4): Mat4 { this.updateWorldTransformFull(); if (out) { return mat4.copy(out, this._mat); @@ -542,7 +542,7 @@ class Node extends BaseNode { * @param out - the receiving matrix * @returns the - resulting matrix */ - public getWorldRS (out: Mat4): Mat4 { + public getWorldRS (out?: Mat4): Mat4 { this.updateWorldTransformFull(); if (out) { mat4.copy(out, this._mat); @@ -558,7 +558,7 @@ class Node extends BaseNode { * @param out - the receiving matrix * @returns the - resulting matrix */ - public getWorldRT (out: Mat4): Mat4 { + public getWorldRT (out?: Mat4): Mat4 { this.updateWorldTransform(); if (!out) { out = new Mat4(); diff --git a/playground/simple.html b/playground/simple.html index 718538365e..d54ca1a78b 100644 --- a/playground/simple.html +++ b/playground/simple.html @@ -55,11 +55,13 @@ */ let root = cc.director.root; - let scene = root.createScene({ - name: "testScene", - }); + let scene = new cc.Scene(); + let cameraNode = new cc.Node(); + cameraNode.parent = scene; + let camComp = cameraNode.addComponent(cc.CameraComponent); let mainWindow = root.mainWindow; + cc.director.runSceneImmediate(scene); let view = root.createView({ name: "defaultView", @@ -69,12 +71,14 @@ width: mainWindow.width, height: mainWindow.height, }); - view.attach(scene.mainCamera); + view.attach(camComp._camera); - let camera = scene.mainCamera; - camera.position = { x: 0.0, y: 0.0, z: 3.0 }; - camera.direction = { x: 0.0, y: 0.0, z: -1.0 }; - camera.update(); + + + // let camera = scene.mainCamera; + // camera.position = { x: 0.0, y: 0.0, z: 3.0 }; + // camera.direction = { x: 0.0, y: 0.0, z: -1.0 }; + // camera.update(); //todo initialization /* @@ -106,7 +110,6 @@ mtl.setProperty('emissive', color); /**/ // model.material = mtl; - //cc.director.runSceneImmediate(scene); } cc.game.run({ id: 'canvas' }, onStart);