From 852d4ec1a63dc92f5d91a071133127cfa0dc475f Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 10 Jan 2019 20:37:37 +0800 Subject: [PATCH] node from compoennt --- ...amera-component.js => camera-component.ts} | 148 ++++++++---------- cocos/3d/framework/index.js | 2 +- cocos/3d/framework/model-component.js | 7 - cocos/components/component.d.ts | 20 +-- cocos/core/data/instantiate.js | 4 +- cocos/core/value-types/color.js | 2 +- cocos/core/value-types/index.js | 4 +- cocos/core/value-types/mat4.js | 2 +- cocos/core/value-types/quat.js | 2 +- cocos/core/value-types/rect.js | 4 +- cocos/core/value-types/size.js | 2 +- .../{value-type.js => value-type.ts} | 31 ++-- cocos/core/value-types/vec2.js | 4 +- cocos/core/value-types/vec3.js | 4 +- cocos/core/value-types/vec4.js | 4 +- cocos/renderer/scene/camera.ts | 4 +- cocos/scene-graph/node.ts | 26 +-- 17 files changed, 122 insertions(+), 148 deletions(-) rename cocos/3d/framework/{camera-component.js => camera-component.ts} (71%) rename cocos/core/value-types/{value-type.js => value-type.ts} (84%) diff --git a/cocos/3d/framework/camera-component.js b/cocos/3d/framework/camera-component.ts similarity index 71% rename from cocos/3d/framework/camera-component.js rename to cocos/3d/framework/camera-component.ts index cf58a3a0c4..e23e1c7d11 100644 --- a/cocos/3d/framework/camera-component.js +++ b/cocos/3d/framework/camera-component.ts @@ -22,17 +22,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -// @ts-check -import { toRadian, color4 } from '../../core/vmath'; -import { ccclass, menu, property, executeInEditMode } from "../../core/data/class-decorator"; -import { Enum, Rect } from '../../core/value-types'; import { Component } from '../../components/component'; -import { CameraProjection } from '../../renderer/scene/camera'; +import { ccclass, executeInEditMode, menu, property } from '../../core/data/class-decorator'; +import { Enum, Rect } from '../../core/value-types'; +import { color4, toRadian } from '../../core/vmath'; import { GFXClearFlag } from '../../gfx/define'; - -/** - * @typedef {import('../../core/value-types/index').Color} Color - */ +import { Camera, CameraProjection } from '../../renderer/scene/camera'; /** * !#en The light source type @@ -41,7 +36,7 @@ import { GFXClearFlag } from '../../gfx/define'; * @static * @enum CameraComponent.Projection */ -let ProjectionType = Enum({ +const ProjectionType = Enum({ /** * !#en The orthogonal camera * @@ -59,14 +54,14 @@ let ProjectionType = Enum({ * @readonly * @type {Number} */ - PERSPECTIVE: 1 + PERSPECTIVE: 1, }); -let CameraClearFlag = Enum({ +const CameraClearFlag = Enum({ SKYBOX: GFXClearFlag.SKYBOX | GFXClearFlag.DEPTH | GFXClearFlag.STENCIL, SOLID_COLOR: GFXClearFlag.COLOR | GFXClearFlag.DEPTH | GFXClearFlag.STENCIL, DEPTH_ONLY: GFXClearFlag.DEPTH | GFXClearFlag.STENCIL, - DONT_CLEAR: 0 + DONT_CLEAR: 0, }); /** @@ -79,39 +74,37 @@ let CameraClearFlag = Enum({ @ccclass('cc.CameraComponent') @menu('Components/CameraComponent') @executeInEditMode -export default class CameraComponent extends Component { - @property - _projection = ProjectionType.PERSPECTIVE; +export class CameraComponent extends Component { + public static ProjectionType = ProjectionType; @property - _priority = 0; - + protected _projection = ProjectionType.PERSPECTIVE; @property - _fov = 45; - + protected _priority = 0; @property - _orthoHeight = 10; - + protected _fov = 45; @property - _near = 0.01; - + protected _orthoHeight = 10; @property - _far = 1000.0; - + protected _near = 0.01; @property - _color = cc.color('#334C78'); - + protected _far = 1000.0; @property - _depth = 1; - + protected _color = cc.color('#334C78'); @property - _stencil = 0; - + protected _depth = 1; @property - _clearFlags = CameraClearFlag.SOLID_COLOR; - + protected _stencil = 0; @property - _rect = new Rect(0, 0, 1, 1); + protected _clearFlags = CameraClearFlag.SOLID_COLOR; + @property + protected _rect = new Rect(0, 0, 1, 1); + + protected _camera: Camera | null = null; + + constructor () { + super(); + } /** * !#en The projection type of the camera @@ -120,20 +113,20 @@ export default class CameraComponent extends Component { * @type {Number} */ @property({ - type: ProjectionType + type: ProjectionType, }) - get projection() { + get projection () { return this._projection; } - set projection(val) { + set projection (val) { this._projection = val; let type = CameraProjection.PERSPECTIVE; if (this._projection === ProjectionType.ORTHO) { type = CameraProjection.ORTHO; } - if (this.enabled) this._camera.projectionType = type; + if (this._camera) { this._camera.projectionType = type; } } /** @@ -143,13 +136,13 @@ export default class CameraComponent extends Component { * @type {Number} */ @property - get fov() { + get fov () { return this._fov; } - set fov(val) { + set fov (val) { this._fov = val; - if (this.enabled) this._camera.fov = toRadian(val); + if (this._camera) { this._camera.fov = toRadian(val); } } /** @@ -159,13 +152,13 @@ export default class CameraComponent extends Component { * @type {Number} */ @property - get orthoHeight() { + get orthoHeight () { return this._orthoHeight; } - set orthoHeight(val) { + set orthoHeight (val) { this._orthoHeight = val; - if (this.enabled) this._camera.orthoHeight = val; + if (this._camera) { this._camera.orthoHeight = val; } } /** @@ -175,13 +168,13 @@ export default class CameraComponent extends Component { * @type {Number} */ @property - get near() { + get near () { return this._near; } - set near(val) { + set near (val) { this._near = val; - if (this.enabled) this._camera.nearClip =val; + if (this._camera) { this._camera.nearClip = val; } } /** @@ -191,13 +184,13 @@ export default class CameraComponent extends Component { * @type {Number} */ @property - get far() { + get far () { return this._far; } - set far(val) { + set far (val) { this._far = val; - if (this.enabled) this._camera.farClip = val; + if (this._camera) { this._camera.farClip = val; } } /** @@ -207,14 +200,15 @@ export default class CameraComponent extends Component { * @type {Color} */ @property - get color() { + get color () { return this._color; } - set color(val) { + set color (val) { this._color = val; - if (this.enabled) this._camera.clearColor = + if (this._camera) { this._camera.clearColor = color4.create(val.r / 255, val.g / 255, val.b / 255, val.a / 255); + } } /** @@ -224,14 +218,15 @@ export default class CameraComponent extends Component { * @type {Number} */ @property - get depth() { + get depth () { return this._depth; } - set depth(val) { + set depth (val) { this._depth = val; - if (this.enabled) + if (this._camera) { this._camera.clearDepth = val; + } } /** @@ -241,13 +236,13 @@ export default class CameraComponent extends Component { * @type {Number} */ @property - get stencil() { + get stencil () { return this._stencil; } - set stencil(val) { + set stencil (val) { this._stencil = val; - if (this.enabled) this._camera.clearStencil = val; + if (this._camera) { this._camera.clearStencil = val; } } /** @@ -257,15 +252,15 @@ export default class CameraComponent extends Component { * @type {Number} */ @property({ - type: CameraClearFlag + type: CameraClearFlag, }) - get clearFlags() { + get clearFlags () { return this._clearFlags; } - set clearFlags(val) { + set clearFlags (val) { this._clearFlags = val; - if (this.enabled) this._camera.clearFlag = val; + if (this._camera) { this._camera.clearFlag = val; } } /** @@ -275,29 +270,23 @@ export default class CameraComponent extends Component { * @type {Rect} */ @property - get rect() { + get rect () { return this._rect; } - set rect(val) { + set rect (val) { this._rect = val; - if (this.enabled) this._camera.viewport = val; + if (this._camera) { this._camera.viewport = val; } } - static Projection = CameraProjection; - - constructor() { - super(); - } - - onLoad() { + public onLoad () { } - onEnable() { + public onEnable () { this._camera = this._getRenderScene().createCamera(this.name); + this._camera.node = this.node; this.projection = this._projection; - this.priority = this._priority; this.fov = this._fov; this.orthoHeight = this._orthoHeight; this.near = this._near; @@ -307,14 +296,13 @@ export default class CameraComponent extends Component { this.stencil = this._stencil; this.clearFlags = this._clearFlags; this.rect = this._rect; - this._camera.node = this.node; } - onDisable() { - this._getRenderScene().destroyCamera(this._camera); + public onDisable () { + if (this._camera) { this._getRenderScene().destroyCamera(this._camera); } } - onDestroy() { + public onDestroy () { } } diff --git a/cocos/3d/framework/index.js b/cocos/3d/framework/index.js index eeaeffaafe..c0750bd999 100644 --- a/cocos/3d/framework/index.js +++ b/cocos/3d/framework/index.js @@ -24,7 +24,7 @@ THE SOFTWARE. ****************************************************************************/ -import CameraComponent from './camera-component'; +import { CameraComponent } from './camera-component'; import AnimationComponent from './animation-component'; import LightComponent from './light-component'; import ModelComponent from './model-component'; diff --git a/cocos/3d/framework/model-component.js b/cocos/3d/framework/model-component.js index 02ddc06c4e..c55f399e76 100644 --- a/cocos/3d/framework/model-component.js +++ b/cocos/3d/framework/model-component.js @@ -22,15 +22,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -// @ts-check import Model from '../../renderer/scene/model'; import { ccclass, property, menu, executionOrder, executeInEditMode } from '../../core/data/class-decorator'; import { Mesh } from '../assets/mesh'; import Enum from '../../core/value-types/enum'; import RenderableComponent from './renderable-component'; -/** - * @typedef {import('../assets/material').default} Material - */ /** * !#en Shadow projection mode @@ -103,9 +99,6 @@ export default class ModelComponent extends RenderableComponent { @property _receiveShadows = false; - /** - * @type {Material} - */ static _builtinMaterial = null; /** diff --git a/cocos/components/component.d.ts b/cocos/components/component.d.ts index 873270c9e4..29cff6644f 100644 --- a/cocos/components/component.d.ts +++ b/cocos/components/component.d.ts @@ -39,14 +39,14 @@ export class Component extends CCObject { public constructor(); - _getRenderScene(index?: number): RenderScene | undefined; - addComponent(typeOrClassName: Function | string): Component; - getComponent(typeOrClassName: Function | string): Component; - getComponents(typeOrClassName: Function | string): Component[]; - getComponentInChildren(typeOrClassName: Function | string): Component; - getComponentsInChildren(typeOrClassName: Function | string): Component[]; - schedule(callback: Function, interval: number, repeat: number, delay: number): void; - scheduleOnce(callback: Function, delay: number): void; - unschedule(callback: Function): void; - unscheduleAllCallbacks(): void; + protected _getRenderScene(index?: number): RenderScene; + public addComponent(typeOrClassName: Function | string): Component; + public getComponent(typeOrClassName: Function | string): Component; + public getComponents(typeOrClassName: Function | string): Component[]; + public getComponentInChildren(typeOrClassName: Function | string): Component; + public getComponentsInChildren(typeOrClassName: Function | string): Component[]; + public schedule(callback: Function, interval: number, repeat: number, delay: number): void; + public scheduleOnce(callback: Function, delay: number): void; + public unschedule(callback: Function): void; + public unscheduleAllCallbacks(): void; } diff --git a/cocos/core/data/instantiate.js b/cocos/core/data/instantiate.js index 77d1bbde6a..f86904a04e 100644 --- a/cocos/core/data/instantiate.js +++ b/cocos/core/data/instantiate.js @@ -25,8 +25,8 @@ ****************************************************************************/ import { CCObject } from './object'; -import ValueType from '../value-types/value-type'; -import {isDomNode} from '../utils/misc'; +import { ValueType } from '../value-types/value-type'; +import { isDomNode } from '../utils/misc'; const Destroyed = CCObject.Flags.Destroyed; const PersistentMask = CCObject.Flags.PersistentMask; diff --git a/cocos/core/value-types/color.js b/cocos/core/value-types/color.js index 76b09bb909..4f7909fe36 100644 --- a/cocos/core/value-types/color.js +++ b/cocos/core/value-types/color.js @@ -24,7 +24,7 @@ THE SOFTWARE. ****************************************************************************/ -import ValueType from './value-type'; +import { ValueType } from './value-type'; import CCClass from '../data/class'; /** diff --git a/cocos/core/value-types/index.js b/cocos/core/value-types/index.js index ad1de90e43..01e52b3897 100644 --- a/cocos/core/value-types/index.js +++ b/cocos/core/value-types/index.js @@ -25,7 +25,7 @@ ****************************************************************************/ export { default as Enum } from './enum'; -export { default as ValueType } from './value-type'; +export { ValueType } from './value-type'; export { default as Vec2} from './vec2'; export { default as Vec3} from './vec3'; export { default as Vec4} from './vec4'; @@ -34,4 +34,4 @@ export { default as Mat4} from './mat4'; export { default as AffineTransform} from './affine-transform'; export { default as Size} from './size'; export { default as Rect} from './rect'; -export { default as Color} from './color'; \ No newline at end of file +export { default as Color} from './color'; diff --git a/cocos/core/value-types/mat4.js b/cocos/core/value-types/mat4.js index 3342642a80..a3261a6f78 100644 --- a/cocos/core/value-types/mat4.js +++ b/cocos/core/value-types/mat4.js @@ -23,7 +23,7 @@ THE SOFTWARE. ****************************************************************************/ -import ValueType from './value-type'; +import { ValueType } from './value-type'; import CCClass from '../data/class'; import {mat4} from '../vmath'; diff --git a/cocos/core/value-types/quat.js b/cocos/core/value-types/quat.js index 30dcfc5f36..8e2199b427 100644 --- a/cocos/core/value-types/quat.js +++ b/cocos/core/value-types/quat.js @@ -23,7 +23,7 @@ THE SOFTWARE. ****************************************************************************/ -import ValueType from './value-type'; +import { ValueType } from './value-type'; import CCClass from '../data/class'; import quat from '../vmath/quat'; diff --git a/cocos/core/value-types/rect.js b/cocos/core/value-types/rect.js index dd8250d77e..1d55185d74 100644 --- a/cocos/core/value-types/rect.js +++ b/cocos/core/value-types/rect.js @@ -24,7 +24,7 @@ THE SOFTWARE. ****************************************************************************/ -import ValueType from './value-type'; +import { ValueType } from './value-type'; import CCClass from '../data/class'; /** @@ -420,4 +420,4 @@ cc.Rect = Rect; */ cc.rect = function rect (x, y, w, h) { return new Rect(x, y, w, h); -}; \ No newline at end of file +}; diff --git a/cocos/core/value-types/size.js b/cocos/core/value-types/size.js index d35ed6d8fe..7d09fb68ce 100644 --- a/cocos/core/value-types/size.js +++ b/cocos/core/value-types/size.js @@ -24,7 +24,7 @@ THE SOFTWARE. ****************************************************************************/ -import ValueType from './value-type'; +import { ValueType } from './value-type'; import CCClass from '../data/class'; /** diff --git a/cocos/core/value-types/value-type.js b/cocos/core/value-types/value-type.ts similarity index 84% rename from cocos/core/value-types/value-type.js rename to cocos/core/value-types/value-type.ts index 00efa08496..b1c16446a0 100644 --- a/cocos/core/value-types/value-type.js +++ b/cocos/core/value-types/value-type.ts @@ -32,26 +32,23 @@ import * as js from '../utils/js'; * @class ValueType * */ -export default class ValueType { +export class ValueType { /** * !#en This method returns an exact copy of current value. * !#zh 克隆当前值,该方法返回一个新对象,新对象的值和原对象相等。 * @method clone - * @return {ValueType} */ - clone () { + public clone (): this { cc.errorID('0100', js.getClassName(this) + '.clone'); - return null; + return this; } /** * !#en Compares this object with the other one. * !#zh 当前对象是否等于指定对象。 - * @method equals - * @param {ValueType} other - * @return {Boolean} + * @param other */ - equals (other) { + public equals (other: this) { cc.errorID('0100', js.getClassName(this) + '.equals'); return false; } @@ -63,12 +60,11 @@ export default class ValueType { * !#zh * 线性插值。
* 当 ratio = 0 时返回自身,ratio = 1 时返回目标,ratio = 0.5 返回自身和目标的平均值。。 - * @method lerp - * @param {ValueType} to - the to value - * @param {number} ratio - the interpolation coefficient - * @return {ValueType} + * @param to - the to value + * @param ratio - the interpolation coefficient + * @returns */ - lerp (to, ratio) { + public lerp (to: this, ratio: this) { cc.errorID('0100', js.getClassName(this) + '.lerp'); return this.clone(); } @@ -78,20 +74,17 @@ export default class ValueType { * Copys all the properties from another given object to this value. * !#zh * 从其它对象把所有属性复制到当前对象。 - * @method set - * @param {ValueType} source - the source to copy + * @param source - the source to copy */ - set (source) { + public set (source: this) { cc.errorID('0100', js.getClassName(this) + '.set'); } /** * !#en TODO * !#zh 转换为方便阅读的字符串。 - * @method toString - * @return {string} */ - toString () { + public toString () { return '' + {}; } } diff --git a/cocos/core/value-types/vec2.js b/cocos/core/value-types/vec2.js index f577484673..07f4cce5f0 100644 --- a/cocos/core/value-types/vec2.js +++ b/cocos/core/value-types/vec2.js @@ -24,7 +24,7 @@ THE SOFTWARE. ****************************************************************************/ -import ValueType from './value-type'; + import { ValueType } from './value-type'; import CCClass from '../data/class'; import {vec2} from '../vmath'; import {clampf} from '../utils/misc'; @@ -664,4 +664,4 @@ cc.Vec2 = Vec2; */ cc.v2 = function v2 (x, y) { return new Vec2(x, y); -}; \ No newline at end of file +}; diff --git a/cocos/core/value-types/vec3.js b/cocos/core/value-types/vec3.js index b11afc79e7..5c0a11c981 100644 --- a/cocos/core/value-types/vec3.js +++ b/cocos/core/value-types/vec3.js @@ -24,7 +24,7 @@ THE SOFTWARE. ****************************************************************************/ -import ValueType from './value-type'; +import { ValueType } from './value-type'; import CCClass from '../data/class'; import {vec3} from '../vmath'; import {clampf} from '../utils/misc'; @@ -469,4 +469,4 @@ cc.Vec3 = Vec3; */ cc.v3 = function v3 (x, y, z) { return new Vec3(x, y, z); -}; \ No newline at end of file +}; diff --git a/cocos/core/value-types/vec4.js b/cocos/core/value-types/vec4.js index 71de353c27..3402a7ba87 100644 --- a/cocos/core/value-types/vec4.js +++ b/cocos/core/value-types/vec4.js @@ -24,7 +24,7 @@ THE SOFTWARE. ****************************************************************************/ -import ValueType from './value-type'; +import { ValueType } from './value-type'; import CCClass from '../data/class'; import {vec4} from '../vmath'; import {clampf} from '../utils/misc'; @@ -500,4 +500,4 @@ cc.Vec4 = Vec4; */ cc.v4 = function v4 (x, y, z, w) { return new Vec4(x, y, z, w); -}; \ No newline at end of file +}; diff --git a/cocos/renderer/scene/camera.ts b/cocos/renderer/scene/camera.ts index 6e72fb3e4f..9f01b5b666 100644 --- a/cocos/renderer/scene/camera.ts +++ b/cocos/renderer/scene/camera.ts @@ -17,7 +17,6 @@ export class Camera { private _scene: RenderScene; private _name: string; - private _node: Node; private _proj: CameraProjection; private _width: number; private _height: number; @@ -36,11 +35,11 @@ export class Camera { private _matViewProj: mat4 = new Mat4(); private _matViewProjInv: mat4 = new Mat4(); private _frustum: frustum = new frustum(); + private _node: Node | null = null; constructor (scene: RenderScene, name: string) { this._scene = scene; this._name = name; - this._node = scene.createNode({ name: name + 'Node', isStatic: false }); this._proj = CameraProjection.PERSPECTIVE; const win = scene.root.mainWindow; @@ -70,6 +69,7 @@ export class Camera { } public update () { + if (!this._node) { return; } // view matrix this._node.getWorldRT(this._matView); mat4.invert(this._matView, this._matView); diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts index 2915cc3ed2..5b051de5ef 100644 --- a/cocos/scene-graph/node.ts +++ b/cocos/scene-graph/node.ts @@ -33,28 +33,28 @@ class Node extends BaseNode { } // local transform @property - public _lpos = new Vec3(); + protected _lpos = new Vec3(); @property - public _lrot = new Quat(); + protected _lrot = new Quat(); @property - public _lscale = new Vec3(1, 1, 1); + protected _lscale = new Vec3(1, 1, 1); @property - public _layer = Layers.Default; // the layer this node belongs to + protected _layer = Layers.Default; // the layer this node belongs to @property - public _euler = new Vec3(); // local rotation in euler angles, maintained here so that rotation angles could be greater than 360 degree. + protected _euler = new Vec3(); // local rotation in euler angles, maintained here so that rotation angles could be greater than 360 degree. // world transform - public _pos = new Vec3(); - public _rot = new Quat(); - public _scale = new Vec3(1, 1, 1); - public _mat = new Mat4(); + protected _pos = new Vec3(); + protected _rot = new Quat(); + protected _scale = new Vec3(1, 1, 1); + protected _mat = new Mat4(); - public _dirty = false; // does the world transform need to update? - public _hasChanged = false; // has the transform changed in this frame? + protected _dirty = false; // does the world transform need to update? + protected _hasChanged = false; // has the transform changed in this frame? - public _matDirty = false; - public _eulerDirty = false; + protected _matDirty = false; + protected _eulerDirty = false; @property({ type: Vec3,