diff --git a/.eslintrc.json b/.eslintrc.json index 705297c9a8..f53f8b7076 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -23,19 +23,20 @@ "globals": { "cc": false, "wx": false, - "CC_EDITOR": false, - "CC_DEV": false, - "CC_JSB": false, - "CC_PREVIEW": false, - "CC_DEBUG": false, "CC_BUILD": false, + "CC_TEST": false, + "CC_EDITOR": false, + "CC_PREVIEW": false, + "CC_DEV": false, + "CC_DEBUG": false, + "CC_JSB": false, + "CC_WECHATGAME_SUB": false, "CC_WECHATGAME": false, "CC_QQPLAY": false, "CC_RUNTIME": false, "CC_SUPPORT_JIT": false, - "CC_PREVIEW": false, - "CC_PREVIEW": false, - "CC_PREVIEW": false, + "Editor": false, + "_Scene": false, "_ccsg": false } } diff --git a/cocos/3d/assets/index.js b/cocos/3d/assets/index.js index 6df8f5d3d3..4cc366051f 100644 --- a/cocos/3d/assets/index.js +++ b/cocos/3d/assets/index.js @@ -29,7 +29,7 @@ export { DOMAudioClip } from "./dom-audio-clip"; export { BitmapFont } from "./bitmap-font"; export { EffectAsset } from "./effect-asset"; export { Material } from "./material"; -export { default as Mesh } from "./mesh"; +export { Mesh } from "./mesh"; export { default as OpentypeFont } from "./opentype-font"; export { default as Skeleton } from "./skeleton"; export { default as Sprite } from "./sprite"; diff --git a/cocos/3d/assets/mesh.ts b/cocos/3d/assets/mesh.ts index 972ef75f88..5ba28687e9 100644 --- a/cocos/3d/assets/mesh.ts +++ b/cocos/3d/assets/mesh.ts @@ -305,7 +305,7 @@ export class RenderingMesh { } @ccclass('cc.Mesh') -export default class Mesh extends Asset { +export class Mesh extends Asset { get _nativeAsset () { return this._data; diff --git a/cocos/3d/framework/camera-component.js b/cocos/3d/framework/camera-component.js index b556cb3904..cf58a3a0c4 100644 --- a/cocos/3d/framework/camera-component.js +++ b/cocos/3d/framework/camera-component.js @@ -23,13 +23,12 @@ THE SOFTWARE. ****************************************************************************/ // @ts-check -import RenderSystemActor from './renderSystemActor'; -import { renderer } from '../../renderer/index'; -import { toRadian } from '../../core/vmath'; +import { toRadian, color4 } from '../../core/vmath'; import { ccclass, menu, property, executeInEditMode } from "../../core/data/class-decorator"; -import { Color, Enum, Rect } from '../../core/value-types'; -import enums from '../../renderer/enums'; +import { Enum, Rect } from '../../core/value-types'; import { Component } from '../../components/component'; +import { CameraProjection } from '../../renderer/scene/camera'; +import { GFXClearFlag } from '../../gfx/define'; /** * @typedef {import('../../core/value-types/index').Color} Color @@ -42,7 +41,7 @@ import { Component } from '../../components/component'; * @static * @enum CameraComponent.Projection */ -let CameraProjection = Enum({ +let ProjectionType = Enum({ /** * !#en The orthogonal camera * @@ -64,9 +63,9 @@ let CameraProjection = Enum({ }); let CameraClearFlag = Enum({ - SKYBOX: enums.CLEAR_SKYBOX | enums.CLEAR_DEPTH | enums.CLEAR_STENCIL, - SOLID_COLOR: enums.CLEAR_COLOR | enums.CLEAR_DEPTH | enums.CLEAR_STENCIL, - DEPTH_ONLY: enums.CLEAR_DEPTH | enums.CLEAR_STENCIL, + SKYBOX: GFXClearFlag.SKYBOX | GFXClearFlag.DEPTH | GFXClearFlag.STENCIL, + SOLID_COLOR: GFXClearFlag.COLOR | GFXClearFlag.DEPTH | GFXClearFlag.STENCIL, + DEPTH_ONLY: GFXClearFlag.DEPTH | GFXClearFlag.STENCIL, DONT_CLEAR: 0 }); @@ -82,7 +81,7 @@ let CameraClearFlag = Enum({ @executeInEditMode export default class CameraComponent extends Component { @property - _projection = CameraProjection.PERSPECTIVE; + _projection = ProjectionType.PERSPECTIVE; @property _priority = 0; @@ -121,7 +120,7 @@ export default class CameraComponent extends Component { * @type {Number} */ @property({ - type: CameraProjection + type: ProjectionType }) get projection() { return this._projection; @@ -130,29 +129,11 @@ export default class CameraComponent extends Component { set projection(val) { this._projection = val; - let type = renderer.PROJ_PERSPECTIVE; - if (this._projection === CameraProjection.Ortho) { - type = renderer.PROJ_ORTHO; + let type = CameraProjection.PERSPECTIVE; + if (this._projection === ProjectionType.ORTHO) { + type = CameraProjection.ORTHO; } - if (this.enabled) - this._camera.setType(type); - } - - /** - * !#en The camera priority - * - * !#ch 相机的优先级,优先级低的优先渲染 - * @type {Number} - */ - @property - get priority() { - return this._priority; - } - - set priority(val) { - this._priority = val; - if (this.enabled) - this._camera.setPriority(val); + if (this.enabled) this._camera.projectionType = type; } /** @@ -168,8 +149,7 @@ export default class CameraComponent extends Component { set fov(val) { this._fov = val; - if (this.enabled) - this._camera.setFov(toRadian(val)); + if (this.enabled) this._camera.fov = toRadian(val); } /** @@ -185,8 +165,7 @@ export default class CameraComponent extends Component { set orthoHeight(val) { this._orthoHeight = val; - if (this.enabled) - this._camera.setOrthoHeight(val); + if (this.enabled) this._camera.orthoHeight = val; } /** @@ -202,8 +181,7 @@ export default class CameraComponent extends Component { set near(val) { this._near = val; - if (this.enabled) - this._camera.setNear(val); + if (this.enabled) this._camera.nearClip =val; } /** @@ -219,8 +197,7 @@ export default class CameraComponent extends Component { set far(val) { this._far = val; - if (this.enabled) - this._camera.setFar(val); + if (this.enabled) this._camera.farClip = val; } /** @@ -236,8 +213,8 @@ export default class CameraComponent extends Component { set color(val) { this._color = val; - if (this.enabled) - this._camera.setColor(val.r / 255, val.g / 255, val.b / 255, val.a / 255); + if (this.enabled) this._camera.clearColor = + color4.create(val.r / 255, val.g / 255, val.b / 255, val.a / 255); } /** @@ -254,7 +231,7 @@ export default class CameraComponent extends Component { set depth(val) { this._depth = val; if (this.enabled) - this._camera.setDepth(val); + this._camera.clearDepth = val; } /** @@ -270,8 +247,7 @@ export default class CameraComponent extends Component { set stencil(val) { this._stencil = val; - if (this.enabled) - this._camera.setStencil(val); + if (this.enabled) this._camera.clearStencil = val; } /** @@ -289,8 +265,7 @@ export default class CameraComponent extends Component { set clearFlags(val) { this._clearFlags = val; - if (this.enabled) - this._camera.setClearFlags(val); + if (this.enabled) this._camera.clearFlag = val; } /** @@ -306,8 +281,7 @@ export default class CameraComponent extends Component { set rect(val) { this._rect = val; - if (this.enabled) - this._camera.setRect(val.x, val.y, val.width, val.height); + if (this.enabled) this._camera.viewport = val; } static Projection = CameraProjection; @@ -333,8 +307,7 @@ export default class CameraComponent extends Component { this.stencil = this._stencil; this.clearFlags = this._clearFlags; this.rect = this._rect; - this._camera.setStages(renderer.PassStage.DEFAULT | renderer.PassStage.FORWARD); - this._camera.setNode(this.node); + this._camera.node = this.node; } onDisable() { @@ -344,16 +317,4 @@ export default class CameraComponent extends Component { onDestroy() { } - - /** - * transform a screen position to world space - * @param out the resulting vector - * @param screenPos the screen position to be transformed - * @param width framebuffer width - * @param height framebuffer height - * @returns the resulting vector - */ - screenToWorld(out, screenPos, width, height) { - return this._camera.screenToWorld(out, screenPos, width, height); - } } diff --git a/cocos/3d/framework/model-component.js b/cocos/3d/framework/model-component.js index b461facd97..02ddc06c4e 100644 --- a/cocos/3d/framework/model-component.js +++ b/cocos/3d/framework/model-component.js @@ -25,7 +25,7 @@ // @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 { Mesh } from '../assets/mesh'; import Enum from '../../core/value-types/enum'; import RenderableComponent from './renderable-component'; /** diff --git a/cocos/3d/framework/physics/body.ts b/cocos/3d/framework/physics/body.ts index f183d680f9..1f9a8c63aa 100644 --- a/cocos/3d/framework/physics/body.ts +++ b/cocos/3d/framework/physics/body.ts @@ -1,7 +1,7 @@ import CANNON from 'cannon'; import Vec3 from '../../../core/value-types/vec3'; import { vec3 } from '../../../core/vmath'; -import Node from '../../../scene-graph/node'; +import { Node } from '../../../scene-graph/node'; import { PhysicsMaterial as PhysicsMaterial } from '../../assets/physics/material'; import { setWrap, getWrap } from './util'; import { Quat } from '../../../core/value-types'; diff --git a/cocos/3d/framework/skinning-model-component.js b/cocos/3d/framework/skinning-model-component.js index c3c9349a69..c170eb5617 100644 --- a/cocos/3d/framework/skinning-model-component.js +++ b/cocos/3d/framework/skinning-model-component.js @@ -30,7 +30,7 @@ import { renderer } from '../../renderer/index'; import { ccclass, property, executionOrder, menu,executeInEditMode } from '../../core/data/class-decorator'; import utils from '../misc/utils'; import Skeleton from '../assets/skeleton'; -import Node from '../../scene-graph/node'; +import { Node } from '../../scene-graph/node'; /** * @typedef {import("../framework/skeleton-instance").default} SkeletonInstance diff --git a/cocos/3d/geom-utils/frustum.ts b/cocos/3d/geom-utils/frustum.ts index 9f7caa9f8a..26330d9e37 100644 --- a/cocos/3d/geom-utils/frustum.ts +++ b/cocos/3d/geom-utils/frustum.ts @@ -1,8 +1,8 @@ +import { mat4, vec3 } from '../../core/vmath'; import enums from './enums'; import plane from './plane'; -import { vec3, mat4 } from '../../core/vmath'; -let _v = new Array(8); +const _v = new Array(8); _v[0] = vec3.create(1, 1, 1); _v[1] = vec3.create(-1, 1, 1); _v[2] = vec3.create(-1, -1, 1); @@ -12,13 +12,51 @@ _v[5] = vec3.create(-1, 1, -1); _v[6] = vec3.create(-1, -1, -1); _v[7] = vec3.create(1, -1, -1); +// tslint:disable-next-line: class-name export class frustum { + /** + * Set whether to use accurate intersection testing function on this frustum + */ + set accurate (b: boolean) { + this._type = b ? enums.SHAPE_FRUSTUM_ACCURATE : enums.SHAPE_FRUSTUM; + } + + /** + * create a new frustum + * + * @return {frustum} + */ + public static create () { + return new frustum(); + } + + /** + * Clone a frustum + */ + public static clone (f: frustum): frustum { + return frustum.copy(new frustum(), f); + } + + /** + * Copy the values from one frustum to another + */ + public static copy (out: frustum, f: frustum): frustum { + out._type = f._type; + for (let i = 0; i < 6; ++i) { + plane.copy(out.planes[i], f.planes[i]); + } + for (let i = 0; i < 8; ++i) { + vec3.copy(out.vertices[i], f.vertices[i]); + } + return out; + } + public planes: plane[]; public vertices: vec3[]; private _type: number; - constructor() { + constructor () { this._type = enums.SHAPE_FRUSTUM; this.planes = new Array(6); for (let i = 0; i < 6; ++i) { @@ -30,52 +68,6 @@ export class frustum { } } - /** - * create a new frustum - * - * @return {frustum} - */ - static create() { - return new frustum(); - } - - /** - * Clone a frustum - * - * @param {frustum} f frustum to clone - * @return {frustum} the new frustum - */ - static clone(f: frustum) { - return frustum.copy(new frustum(), f); - } - - /** - * Copy the values from one frustum to another - * - * @param {frustum} out the receiving frustum - * @param {frustum} f the source frustum - * @return {frustum} - */ - static copy(out, f) { - out._type = f._type; - for (let i = 0; i < 6; ++i) { - plane.copy(out.planes[i], f.planes[i]); - } - for (let i = 0; i < 8; ++i) { - vec3.copy(out.vertices[i], f.vertices[i]); - } - return out; - } - - /** - * Set whether to use accurate intersection testing function on this frustum - * - * @param {boolean} b - */ - set accurate(b) { - this._type = b ? enums.SHAPE_FRUSTUM_ACCURATE : enums.SHAPE_FRUSTUM; - } - /** * Update the frustum information according to the given transform matrix. * Note that the resulting planes are not normalized under normal mode. @@ -83,7 +75,7 @@ export class frustum { * @param {mat4} m the view-projection matrix * @param {mat4} inv the inverse view-projection matrix */ - update(m: mat4, inv: mat4) { + public update (m: mat4, inv: mat4) { // RTR4, ch. 22.14.1, p. 983 // extract frustum planes from view-proj matrix. @@ -106,14 +98,14 @@ export class frustum { vec3.set(this.planes[5].n, m.m03 - m.m02, m.m07 - m.m06, m.m11 - m.m10); this.planes[5].d = -(m.m15 - m.m14); - if (this._type !== enums.SHAPE_FRUSTUM_ACCURATE) return; + if (this._type !== enums.SHAPE_FRUSTUM_ACCURATE) { return; } // normalize planes for (let i = 0; i < 6; i++) { - let plane = this.planes[i]; - let invDist = 1 / vec3.magnitude(plane.n); - vec3.scale(plane.n, plane.n, invDist); - plane.d *= invDist; + const pl = this.planes[i]; + const invDist = 1 / vec3.magnitude(pl.n); + vec3.scale(pl.n, pl.n, invDist); + pl.d *= invDist; } // update frustum vertices diff --git a/cocos/3d/misc/utils.js b/cocos/3d/misc/utils.js index b9b9f948d5..d74cafab42 100644 --- a/cocos/3d/misc/utils.js +++ b/cocos/3d/misc/utils.js @@ -1,6 +1,6 @@ import { renderer } from '../../renderer'; import find from '../../scene-graph/find'; -import Mesh, { RenderingMesh } from '../assets/mesh'; +import { Mesh, RenderingMesh } from '../assets/mesh'; import gfx from '../../renderer/gfx/index'; import { GFXPrimitiveMode } from '../../gfx/define'; diff --git a/cocos/components/component.d.ts b/cocos/components/component.d.ts index 1deb53422d..873270c9e4 100644 --- a/cocos/components/component.d.ts +++ b/cocos/components/component.d.ts @@ -28,7 +28,7 @@ import { CCObject } from '../core/data/object'; 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 { Node } from '../scene-graph/node'; import { RenderScene } from '../renderer/scene/render-scene'; export class Component extends CCObject { @@ -37,7 +37,9 @@ export class Component extends CCObject { enabled: boolean; enabledInHierarchy: boolean; - getRenderScene(index?: number): RenderScene | undefined; + public constructor(); + + _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 6370572336..a4cbd179ee 100644 --- a/cocos/components/component.js +++ b/cocos/components/component.js @@ -105,7 +105,7 @@ class Component extends CCObject { type: cc._Script, tooltip: CC_DEV && 'i18n:INSPECTOR.component.script' }) - get __scriptAsset () {} + get __scriptAsset () { return null; } //set __scriptAsset (value) { // if (this.__scriptUuid !== value) { // if (value && Editor.Utils.UuidUtils.isUuid(value._uuid)) { diff --git a/cocos/core/data/object.d.ts b/cocos/core/data/object.d.ts index 5d70237897..463c47b429 100644 --- a/cocos/core/data/object.d.ts +++ b/cocos/core/data/object.d.ts @@ -24,10 +24,11 @@ ****************************************************************************/ export class CCObject { + protected static _deferredDestroy(): void; protected _name: string; protected _objFlags: number; - static _deferredDestroy(): void; + public constructor(name: string); public name: string; public isValid: boolean; diff --git a/cocos/core/value-types/color.js b/cocos/core/value-types/color.js index 3594466bf7..76b09bb909 100644 --- a/cocos/core/value-types/color.js +++ b/cocos/core/value-types/color.js @@ -570,10 +570,10 @@ CCClass.fastDefine('cc.Color', Color, {r: 0, g: 0, b: 0, a: 255}); * Alpha 通道是可选的。默认值是 255。 * * @method color - * @param {Number} [r=0] - * @param {Number} [g=0] - * @param {Number} [b=0] - * @param {Number} [a=255] + * @param {number|string|object} [r=0] + * @param {number} [g=0] + * @param {number} [b=0] + * @param {number} [a=255] * @return {Color} * @example {@link utils/api/engine/docs/cocos2d/core/value-types/CCColor/color.js} */ @@ -588,4 +588,4 @@ cc.color = function color (r, g, b, a) { return new Color(r, g, b, a); }; -cc.Color = Color; \ No newline at end of file +cc.Color = Color; diff --git a/cocos/core/vmath/color4.ts b/cocos/core/vmath/color4.ts index 957ca79df7..9438446489 100644 --- a/cocos/core/vmath/color4.ts +++ b/cocos/core/vmath/color4.ts @@ -1,5 +1,8 @@ import { EPSILON } from './utils'; +// tslint:disable: one-variable-per-declaration +// tslint:disable: class-name + /** * Represents a color with red(r), green(g), blue(b) component of that color and * and an extra alpha(a) component indicating how opaque this color is. @@ -25,7 +28,7 @@ class color4 { * @param a - Color to clone. * @return The newly created color. */ - public static clone (a) { + public static clone (a: color4) { return new color4(a.r, a.g, a.b, a.a); } @@ -36,7 +39,7 @@ class color4 { * @param a - The specified color. * @return out. */ - public static copy (out, a) { + public static copy (out: color4, a: color4) { out.r = a.r; out.g = a.g; out.b = a.b; @@ -54,7 +57,7 @@ class color4 { * @param a - Value assigned to a component. * @return out. */ - public static set (out, r, g, b, a) { + public static set (out: color4, r: number, g: number, b: number, a: number) { out.r = r; out.g = g; out.b = b; @@ -70,7 +73,7 @@ class color4 { * @return out. * @function */ - public static fromHex (out, hex) { + public static fromHex (out: color4, hex: number) { const r = ((hex >> 24)) / 255.0; const g = ((hex >> 16) & 0xff) / 255.0; const b = ((hex >> 8) & 0xff) / 255.0; @@ -91,7 +94,7 @@ class color4 { * @param b - The second operand. * @return out. */ - public static add (out, a, b) { + public static add (out: color4, a: color4, b: color4) { out.r = a.r + b.r; out.g = a.g + b.g; out.b = a.b + b.b; @@ -107,7 +110,7 @@ class color4 { * @param b - The b. * @return out. */ - public static subtract (out, a, b) { + public static subtract (out: color4, a: color4, b: color4) { out.r = a.r - b.r; out.g = a.g - b.g; out.b = a.b - b.b; @@ -118,7 +121,7 @@ class color4 { /** * Alias of {@link color4.subtract}. */ - public static sub (out, a, b) { + public static sub (out: color4, a: color4, b: color4) { return color4.subtract(out, a, b); } @@ -130,7 +133,7 @@ class color4 { * @param b - The second operand. * @return out. */ - public static multiply (out, a, b) { + public static multiply (out: color4, a: color4, b: color4) { out.r = a.r * b.r; out.g = a.g * b.g; out.b = a.b * b.b; @@ -141,7 +144,7 @@ class color4 { /** * Alias of {@link color4.multiply}. */ - public static mul (out, a, b) { + public static mul (out: color4, a: color4, b: color4) { return color4.multiply(out, a, b); } @@ -153,7 +156,7 @@ class color4 { * @param b - The second operand. * @return out. */ - public static divide (out, a, b) { + public static divide (out: color4, a: color4, b: color4) { out.r = a.r / b.r; out.g = a.g / b.g; out.b = a.b / b.b; @@ -164,7 +167,7 @@ class color4 { /** * Alias of {@link color4.divide}. */ - public static div (out, a, b) { + public static div (out: color4, a: color4, b: color4) { return color4.divide(out, a, b); } @@ -176,7 +179,7 @@ class color4 { * @param b - The scale number. * @return out. */ - public static scale (out, a, b) { + public static scale (out: color4, a: color4, b: number) { out.r = a.r * b; out.g = a.g * b; out.b = a.b * b; @@ -193,11 +196,8 @@ class color4 { * @param t - The interpolation coefficient. * @return out. */ - public static lerp (out, a, b, t) { - const ar = a.r, - ag = a.g, - ab = a.b, - aa = a.a; + public static lerp (out: color4, a: color4, b: color4, t: number) { + const ar = a.r, ag = a.g, ab = a.b, aa = a.a; out.r = ar + t * (b.r - ar); out.g = ag + t * (b.g - ag); out.b = ab + t * (b.b - ab); @@ -211,7 +211,7 @@ class color4 { * @param a - The color. * @return - String representation of this color. */ - public static str (a) { + public static str (a: color4) { return `color4(${a.r}, ${a.g}, ${a.b}, ${a.a})`; } @@ -222,7 +222,7 @@ class color4 { * @param a - The color. * @return out. */ - public static array (out, a) { + public static array (out: number[], a: color4) { const scale = (a instanceof cc.Color || a.a > 1) ? 1 / 255 : 1; out[0] = a.r * scale; out[1] = a.g * scale; @@ -239,7 +239,7 @@ class color4 { * @param b - The second color. * @return True if the colors are equal, false otherwise. */ - public static exactEquals (a, b) { + public static exactEquals (a: color4, b: color4) { return a.r === b.r && a.g === b.g && a.b === b.b && a.a === b.a; } @@ -248,9 +248,9 @@ class color4 { * * @param a - The first color. * @param b - The second color. - * @return True if the colors are approximately equal, false otherwise. + * @return True if the colors are equal, false otherwise. */ - public static equals (a, b) { + public static equals (a: color4, b: color4) { const a0 = a.r, a1 = a.g, a2 = a.b, a3 = a.a; const b0 = b.r, b1 = b.g, b2 = b.b, b3 = b.a; return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && @@ -261,44 +261,23 @@ class color4 { /** * Converts a color's rgb formal into the hexadecimal one. - * - * @param a - The color. - * @return - The color's hexadecimal formal. */ - public static hex (a) { + public static hex (a: color4) { return ((a.r * 255) << 24 | (a.g * 255) << 16 | (a.b * 255) << 8 | a.a * 255) >>> 0; } + + public r: number; + public g: number; + public b: number; + public a: number; + /** * Creates a color, with components specified separately. - * - * @param r - Value assigned to r component. - * @param g - Value assigned to g component. - * @param b - Value assigned to b component. - * @param a - Value assigned to a component. */ constructor (r = 1, g = 1, b = 1, a = 1) { - /** - * The r component. - * @type {number} - * */ this.r = r; - - /** - * The g component. - * @type {number} - * */ this.g = g; - - /** - * The b component. - * @type {number} - * */ this.b = b; - - /** - * The a component. - * @type {number} - * */ this.a = a; } } diff --git a/cocos/core/vmath/quat.ts b/cocos/core/vmath/quat.ts index cdc51b966b..aef11c1c83 100644 --- a/cocos/core/vmath/quat.ts +++ b/cocos/core/vmath/quat.ts @@ -14,6 +14,7 @@ const halfToRad = 0.5 * Math.PI / 180.0; * x, y, z and w are real numbers(called here its components), and i, j, and k are the fundamental quaternion units. */ export default class quat { + public static IDENTITY = new quat(); /** * Creates a quaternion, with components specified separately. @@ -67,20 +68,6 @@ export default class quat { return out; } - /** - * Sets a quaternion as identity quaternion. - * - * @param out - Quaternion to set. - * @return out. - */ - public static identity(out: Out) { - out.x = 0; - out.y = 0; - out.z = 0; - out.w = 1; - return out; - } - /** * Sets a quaternion to represent the shortest rotation from one * vector to another. diff --git a/cocos/core/vmath/vec2.ts b/cocos/core/vmath/vec2.ts index 4069876766..80b54bc8e7 100644 --- a/cocos/core/vmath/vec2.ts +++ b/cocos/core/vmath/vec2.ts @@ -8,6 +8,9 @@ import { EPSILON, random } from './utils'; * x, y is alias of the first, second component of vector, respectively. */ export default class vec2 { + public static ZERO = new vec2(0, 0); + public static ONE = new vec2(1, 1); + public static NEG_ONE = new vec2(-1, -1); /** * Creates a vector, with components specified separately. diff --git a/cocos/core/vmath/vec3.ts b/cocos/core/vmath/vec3.ts index 690d6fb21a..310aeccad8 100644 --- a/cocos/core/vmath/vec3.ts +++ b/cocos/core/vmath/vec3.ts @@ -8,12 +8,12 @@ import { EPSILON, random } from './utils'; * x, y, z is alias of the first, second, third component of vector, respectively. */ export default class vec3 { - public static UNIT_X = { x: 1, y: 0, z: 0 }; - public static UNIT_Y = { x: 0, y: 1, z: 0 }; - public static UNIT_Z = { x: 0, y: 0, z: 1 }; - public static ZERO = { x: 0, y: 0, z: 0 }; - public static ONE = { x: 1, y: 1, z: 1 }; - public static NEG_ONE = { x: -1, y: -1, z: -1 }; + public static UNIT_X = new vec3(1, 0, 0); + public static UNIT_Y = new vec3(0, 1, 0); + public static UNIT_Z = new vec3(0, 0, 1); + public static ZERO = new vec3(0, 0, 0); + public static ONE = new vec3(1, 1, 1); + public static NEG_ONE = new vec3(-1, -1, -1); /** * Creates a vector, with components specified separately. diff --git a/cocos/core/vmath/vec4.ts b/cocos/core/vmath/vec4.ts index f69969bd2b..7eeb1c22f5 100644 --- a/cocos/core/vmath/vec4.ts +++ b/cocos/core/vmath/vec4.ts @@ -8,6 +8,9 @@ import { EPSILON, random } from './utils'; * x, y, z, w is alias of the first, second, third, fourth component of vector, respectively. */ export default class vec4 { + public static ZERO = new vec4(0, 0, 0, 0); + public static ONE = new vec4(1, 1, 1, 1); + public static NEG_ONE = new vec4(-1, -1, -1, -1); /** * Create a vector, with components specified separately. diff --git a/cocos/gfx/define.ts b/cocos/gfx/define.ts index d1b0317d0e..329b970485 100644 --- a/cocos/gfx/define.ts +++ b/cocos/gfx/define.ts @@ -480,6 +480,13 @@ export interface IGFXColor { a: number; } +export enum GFXClearFlag { + COLOR = 1, + DEPTH = 2, + STENCIL = 4, + SKYBOX = 8, +} + export interface IGFXOffset { x: number; y: number; diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index aa1c6f8d96..c77795a958 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -34,7 +34,7 @@ export class UBOGlobal { public static SIZE: number = UBOGlobal.COUNT * 4; public static BLOCK: GFXUniformBlock = { - binding: 30, name: 'Global', members: [ + binding: 30, name: 'CCGlobal', members: [ { name: 'u_time', type: GFXType.FLOAT4, count: 1 }, { name: 'u_screenSize', type: GFXType.FLOAT4, count: 1 }, { name: 'u_screenScale', type: GFXType.FLOAT4, count: 1 }, diff --git a/cocos/renderer/scene/camera.ts b/cocos/renderer/scene/camera.ts index d78252b389..6e72fb3e4f 100644 --- a/cocos/renderer/scene/camera.ts +++ b/cocos/renderer/scene/camera.ts @@ -1,50 +1,40 @@ import { frustum, ray } from '../../3d/geom-utils'; -import { lerp, mat4, quat, vec3 } from '../../core/vmath'; -import Node from '../../scene-graph/node'; +import { Mat4, Rect } from '../../core/value-types'; +import { color4, lerp, mat4, vec3 } from '../../core/vmath'; +import { GFXClearFlag, IGFXColor } from '../../gfx/define'; +import { Node } from '../../scene-graph/node'; import { RenderScene } from './render-scene'; -export enum NodeSpace { - LOCAL, - PARENT, - WORLD, -} - export enum CameraProjection { PERSPECTIVE, 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; + private _orthoHeight: 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 _matViewProjInv: mat4 = new mat4(); - private _position: vec3 = new vec3(); - private _rotation: quat = new quat(); - private _direction: vec3 = new vec3(); + private _clearStencil: number; + private _clearDepth: number; + private _clearFlag: GFXClearFlag; + private _clearColor: IGFXColor = new color4(); + private _viewport: Rect = new Rect(0, 0, 1, 1); + private _matView: mat4 = new Mat4(); + private _matProj: mat4 = new Mat4(); + private _matViewProj: mat4 = new Mat4(); + private _matViewProjInv: mat4 = new Mat4(); private _frustum: frustum = new frustum(); constructor (scene: RenderScene, name: string) { @@ -52,21 +42,25 @@ 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) { - this._width = window.width; - this._height = window.height; + const win = scene.root.mainWindow; + if (win) { + this._width = win.width; + this._height = win.height; } else { this._width = 1; this._height = 1; } this._aspect = this._width / this._height; + this._orthoHeight = 10; this._fov = Math.PI / 4; this._nearClip = 1.0; this._farClip = 10000.0; + + this._clearDepth = 1; + this._clearStencil = 0; + this._clearFlag = GFXClearFlag.COLOR | GFXClearFlag.DEPTH | GFXClearFlag.STENCIL; } public resize (width: number, height: number) { @@ -84,8 +78,8 @@ export class Camera { if (this._proj === CameraProjection.PERSPECTIVE) { mat4.perspective(this._matProj, this._fov, this._aspect, this._nearClip, this._farClip); } else { - const x = this._width; - const y = this._height; + const x = this._orthoHeight * this._aspect; + const y = this._orthoHeight; mat4.ortho(this._matProj, -x, x, -y, y, this._nearClip, this._farClip); } @@ -96,180 +90,134 @@ 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; + set node (val) { + this._node = val; } - 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; - } - - public get name (): string { - return this._name; - } - - public get node (): Node { + get node () { return this._node; } - public get width (): number { + set orthoHeight (val) { + this._orthoHeight = val; + } + + get orthoHeight () { + return this._orthoHeight; + } + + set projectionType (val) { + this._proj = val; + } + + get projectionType () { + return this._proj; + } + + set viewport (v) { + this._viewport = v; + } + + get viewport () { + return this._viewport; + } + + set fov (fov) { + this._fov = fov; + } + + get fov () { + return this._fov; + } + + set nearClip (nearClip) { + this._nearClip = nearClip; + } + + get nearClip () { + return this._nearClip; + } + + set farClip (farClip) { + this._farClip = farClip; + } + + get farClip () { + return this._farClip; + } + + set clearColor (val) { + this._clearColor = val; + } + + get clearColor () { + return this._clearColor; + } + + set clearDepth (val) { + this._clearDepth = val; + } + + get clearDepth () { + return this._clearDepth; + } + + set clearStencil (val) { + this._clearStencil = val; + } + + get clearStencil () { + return this._clearStencil; + } + + set clearFlag (val) { + this._clearFlag = val; + } + + get clearFlag () { + return this._clearFlag; + } + + get scene () { + return this._scene; + } + + get name () { + return this._name; + } + + get width () { return this._width; } - public get height (): number { + get height () { return this._height; } - public get aspect (): number { + get aspect () { return this._aspect; } - public get matView (): mat4 { + get matView () { return this._matView; } - public get matProj (): mat4 { + get matProj () { return this._matProj; } - public get matViewProj (): mat4 { + get matViewProj () { return this._matViewProj; } - public get matViewProjInv (): mat4 { + get matViewProjInv () { return this._matViewProjInv; } - public get frustum (): frustum { + get frustum () { return this._frustum; } - public rotate (rot: quat, ns?: NodeSpace) { - const space = (ns !== undefined ? ns : NodeSpace.LOCAL); - if (space === NodeSpace.LOCAL || space === NodeSpace.PARENT) { - this._node.getRotation(q_a); - this._node.setRotation(quat.multiply(q_a, q_a, rot)); - } else if (space === NodeSpace.WORLD) { - this._node.getWorldRotation(q_a); - this._node.setWorldRotation(quat.multiply(q_a, rot, q_a)); - } - } - - public rotateFromAxisAngle (axis: vec3, rad: number, ns?: NodeSpace) { - quat.fromAxisAngle(this._rotation, axis, rad); - const space = (ns !== undefined ? ns : NodeSpace.LOCAL); - if (space === NodeSpace.LOCAL || space === NodeSpace.PARENT) { - this._node.getRotation(q_a); - this._node.setRotation(quat.multiply(q_a, q_a, this._rotation)); - } else if (space === NodeSpace.WORLD) { - this._node.getWorldRotation(q_a); - this._node.setWorldRotation(quat.multiply(q_a, this._rotation, q_a)); - } - } - - public setRotation (rot: quat, ns?: NodeSpace) { - const space = (ns !== undefined ? ns : NodeSpace.LOCAL); - if (space === NodeSpace.LOCAL || space === NodeSpace.PARENT) { - this._node.setRotation(rot); - } else if (space === NodeSpace.WORLD) { - this._node.setWorldRotation(rot); - } - } - - public pitch (rad: number, ns?: NodeSpace) { - const space = (ns !== undefined ? ns : NodeSpace.LOCAL); - if (space === NodeSpace.LOCAL || space === NodeSpace.PARENT) { - this._node.getRotation(this._rotation); - quat.toAxisX(this._direction, this._rotation); - this.rotateFromAxisAngle(this._direction, rad, ns); - } else if (space === NodeSpace.WORLD) { - this._node.getWorldRotation(this._rotation); - quat.toAxisX(this._direction, this._rotation); - this.rotateFromAxisAngle(this._direction, rad, ns); - } - } - - public yaw (rad: number, ns?: NodeSpace) { - const space = (ns !== undefined ? ns : NodeSpace.LOCAL); - if (space === NodeSpace.LOCAL || space === NodeSpace.PARENT) { - this._node.getRotation(this._rotation); - quat.toAxisY(this._direction, this._rotation); - this.rotateFromAxisAngle(this._direction, rad, ns); - } else if (space === NodeSpace.WORLD) { - this._node.getWorldRotation(this._rotation); - quat.toAxisY(this._direction, this._rotation); - this.rotateFromAxisAngle(this._direction, rad, ns); - } - } - - public roll (rad: number, ns?: NodeSpace) { - const space = (ns !== undefined ? ns : NodeSpace.LOCAL); - if (space === NodeSpace.LOCAL || space === NodeSpace.PARENT) { - this._node.getRotation(this._rotation); - quat.toAxisZ(this._direction, this._rotation); - this.rotateFromAxisAngle(this._direction, rad, ns); - } else if (space === NodeSpace.WORLD) { - this._node.getWorldRotation(this._rotation); - quat.toAxisZ(this._direction, this._rotation); - this.rotateFromAxisAngle(this._direction, rad, ns); - } - } - - public set target (target: vec3) { - this._node.lookAt(target); - } - - 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 */ diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index bb13775a62..bc62661518 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -1,5 +1,5 @@ import { Root, _createSceneFun } from '../../core/root'; -import Node from '../../scene-graph/node'; +import { Node } from '../../scene-graph/node'; import { Camera } from './camera'; import Light from './light'; import Model from './model'; diff --git a/cocos/renderer/scene/scene.js b/cocos/renderer/scene/scene.js index 8d68d51083..39899fbb0c 100644 --- a/cocos/renderer/scene/scene.js +++ b/cocos/renderer/scene/scene.js @@ -2,7 +2,7 @@ import { FixedArray, RecyclePool } from '../../3d/memop'; import { ray, triangle, intersect } from '../../3d/geom-utils'; -import Layers from '../../scene-graph/layers'; +import { Layers } from '../../scene-graph/layers'; import { vec3, mat4 } from '../../core/vmath'; import gfx from '../../renderer/gfx'; diff --git a/cocos/scene-graph/base-node.d.ts b/cocos/scene-graph/base-node.d.ts new file mode 100644 index 0000000000..1d52aba5c9 --- /dev/null +++ b/cocos/scene-graph/base-node.d.ts @@ -0,0 +1,77 @@ +/**************************************************************************** + Copyright (c) 2013-2016 Chukong Technologies Inc. + Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +import { CCObject } from '../core/data/object'; +import { Component } from '../components/component'; + +export class BaseNode extends CCObject { + protected static _setScene(node: BaseNode): void; + + protected _parent: this | null; + protected _children: this[]; + protected _active: boolean; + protected _level: number; + protected _components: Component[]; + protected _persistNode: boolean; + + protected uuid: string; + protected children: this[]; + protected childrenCount: number; + protected active: boolean; + protected parent: this; + + public getParent(): this; + public setParent(value: this): void; + public getChildByUuid(uuid: string): this; + public getChildByName(name: string): this; + public getChildByPath(path: string): this; + public addChild(child: this): void; + public insertChild(child: this, siblingIndex: number): void; + public getSiblingIndex(): number; + public setSiblingIndex(index: number): void; + public walk(prefunc: (target: this) => any, postfunc: (target: this) => any): void; + public removeFromParent(cleanup: boolean): void; + public removeChild(child: this, cleanup: boolean): void; + public removeAllChildren(cleanup: boolean): void; + public isChildOf(parent: this): boolean; + + public getComponent(typeOrClassName: object | string): Component | null; + public getComponents(typeOrClassName: object | string): Component[]; + public getComponentInChildren(typeOrClassName: object | string): Component | null; + public getComponentsInChildren(typeOrClassName: object | string): Component[]; + public addComponent(typeOrClassName: object | string): Component | null; + public removeComponent(typeOrClassName: object | string): void; + public destroyAllChildren(): void; + public cleanup(): void; + public onRestore(): void; + + protected _onSetParent(oldParent: this): void; + protected _onPostActivated(): void; + protected _onBatchRestored(): void; + protected _onBatchCreated(): void; + protected _onHierarchyChanged(oldParent: this): void; + protected _onHierarchyChangedBase(oldParent: this): void; + protected _instantiate(cloned: this): this; +} diff --git a/cocos/scene-graph/base-node.js b/cocos/scene-graph/base-node.js index 457c9d7871..81343aa01a 100644 --- a/cocos/scene-graph/base-node.js +++ b/cocos/scene-graph/base-node.js @@ -132,7 +132,7 @@ function findChildComponents(children, constructor, components) { * @private */ @ccclass("cc._BaseNode") -export default class BaseNode extends CCObject { +export class BaseNode extends CCObject { @property _parent = null; diff --git a/cocos/scene-graph/index.js b/cocos/scene-graph/index.js index d3cab311db..94e05131b6 100644 --- a/cocos/scene-graph/index.js +++ b/cocos/scene-graph/index.js @@ -24,10 +24,10 @@ THE SOFTWARE. ****************************************************************************/ -export { default as BaseNode } from './base-node'; -export { default as Node } from './node'; +export { BaseNode } from './base-node'; +export { Node } from './node'; export { default as PrivateNode } from './private-node'; export { default as NodeActivator } from './node-activator'; export { default as Scene } from './scene'; export { default as find } from './find'; -export { default as Layers } from './layers'; +export { Layers } from './layers'; diff --git a/cocos/scene-graph/layers.js b/cocos/scene-graph/layers.ts similarity index 50% rename from cocos/scene-graph/layers.js rename to cocos/scene-graph/layers.ts index cbf668b1b3..47ac5ad384 100644 --- a/cocos/scene-graph/layers.js +++ b/cocos/scene-graph/layers.ts @@ -2,15 +2,27 @@ /** * Entity layer system */ -class Layers { +export class Layers { + + // built-in layers, reserved up to (1 << 7) + public static Default = (1 << 0); + public static IgnoreRaycast = (1 << 1); + public static Gizmos = (1 << 2); + public static Editor = (1 << 3); + // masks + public static All = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor]); + public static RaycastMask = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor, Layers.IgnoreRaycast]); + /** * Add a new layer * @param {string} name name of the new layer * @return {number} new layer's index */ - static addLayer(name) { - if (Layers._nextAvailable > 31) - return new Error('maximum layers reached.'); + public static addLayer (name: string): number | undefined { + if (Layers._nextAvailable > 31) { + console.warn('maximum layers reached.'); + return; + } Layers[name] = (1 << Layers._nextAvailable++); return Layers[name]; } @@ -20,10 +32,11 @@ class Layers { * @param {number[]} includes layers accepted by the mask * @return {number} the specified layer mask */ - static makeInclusiveMask(includes) { + public static makeInclusiveMask (includes: number[]): number { let mask = 0; - for (let i = 0; i < includes.length; i++) - mask |= includes[i]; + for (const inc of includes) { + mask |= inc; + } return mask; } @@ -32,7 +45,7 @@ class Layers { * @param {number[]} excludes layers rejected by the mask * @return {number} the specified layer mask */ - static makeExclusiveMask(excludes) { + public static makeExclusiveMask (excludes: number[]): number { return ~Layers.makeInclusiveMask(excludes); } @@ -42,22 +55,11 @@ class Layers { * @param {number} mask the testing layer mask * @return {boolean} true if accepted */ - static check(layer, mask) { - return (layer & mask) == layer; + public static check (layer: number, mask: number): boolean { + return (layer & mask) === layer; } + + private static _nextAvailable = 8; } -Layers._nextAvailable = 8; - -// built-in layers, reserved up to (1 << 7) -Layers.Default = (1 << 0); -Layers.IgnoreRaycast = (1 << 1); -Layers.Gizmos = (1 << 2); -Layers.Editor = (1 << 3); - -// masks -Layers.All = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor]); -Layers.RaycastMask = Layers.makeExclusiveMask([Layers.Gizmos, Layers.Editor, Layers.IgnoreRaycast]); - -export default Layers; cc.Layers = Layers; diff --git a/cocos/scene-graph/node-2d.js b/cocos/scene-graph/node-2d.js index b26011c840..d378db9d0f 100644 --- a/cocos/scene-graph/node-2d.js +++ b/cocos/scene-graph/node-2d.js @@ -32,7 +32,7 @@ import eventManager from '../core/platform/event-manager/CCEventManager'; import macro from '../core/platform/CCMacro'; import * as js from '../core/utils/js'; import EventTarget from '../core/event/event-target'; -import BaseNode from './base-node'; +import { BaseNode } from './base-node'; import { ccclass, property } from '../core/data/class-decorator'; const Flags = cc.Object.Flags; @@ -644,7 +644,7 @@ export default class Node extends BaseNode { localPosition.x = value; this.setLocalDirty(LocalDirtyFlag.POSITION); - + // fast check event if (this._eventMask & POSITION_ON) { // send event @@ -702,7 +702,7 @@ export default class Node extends BaseNode { } } } - + @property get z() { return this._position.z; @@ -1126,7 +1126,7 @@ export default class Node extends BaseNode { constructor (name) { super(name); this._reorderChildDirty = false; - + // cache component this._widget = null; // fast render component access @@ -1337,7 +1337,7 @@ export default class Node extends BaseNode { if (!this._activeInHierarchy) { // deactivate ActionManager and EventManager by default - + // ActionManager may not be inited in the editor worker. let manager = cc.director.getActionManager(); manager && manager.pauseTarget(this); @@ -1628,12 +1628,12 @@ export default class Node extends BaseNode { var listeners = useCapture ? this._capturingListeners : this._bubblingListeners; if (listeners) { listeners.remove(type, callback, target); - + if (target && target.__eventTargets) { js.array.fastRemove(target.__eventTargets, this); } } - + } } @@ -1713,7 +1713,7 @@ export default class Node extends BaseNode { * @param {*} [arg4] - Fourth argument in callback * @param {*} [arg5] - Fifth argument in callback * @example - * + * * eventTarget.emit('fire', event); * eventTarget.emit('fire', message, emitter); */ @@ -1774,7 +1774,7 @@ export default class Node extends BaseNode { h = this._contentSize.height, cameraPt = _vec2a, testPt = _vec2b; - + let camera = cc.Camera.findCamera(this); if (camera) { camera.getCameraToWorldPoint(point, cameraPt); @@ -1834,7 +1834,7 @@ export default class Node extends BaseNode { parent = parent.parent; } } - + /** * Get all the targets listening to the supplied type of event in the target's bubbling phase. * The bubbling phase comprises any SUBSEQUENT nodes encountered on the return trip to the root of the tree. @@ -1911,7 +1911,7 @@ export default class Node extends BaseNode { if (!CC_EDITOR || isFinite(y)) { locPosition.y = y; } - else { + else { return cc.error(ERR_INVALID_NUMBER, 'y of new position'); } this.setLocalDirty(LocalDirtyFlag.POSITION); @@ -2136,7 +2136,7 @@ export default class Node extends BaseNode { return out; } - + /* * Calculate and return world position. * This is not a public API yet, its usage could be updated @@ -2256,7 +2256,7 @@ export default class Node extends BaseNode { math.vec3.sub(_vec3_temp, _vec3_temp, pos); // NOTE: we use -z for view-dir math.vec3.normalize(_vec3_temp, _vec3_temp); math.quat.fromViewUp(_quat_temp, _vec3_temp, up); - + this.setWorldRot(_quat_temp); } @@ -2321,7 +2321,7 @@ export default class Node extends BaseNode { // position t.m12 = this._position.x; t.m13 = this._position.y; - + this._localMatDirty = 0; // Register dirty status of world matrix so that it can be recalculated this._worldMatDirty = true; @@ -2332,7 +2332,7 @@ export default class Node extends BaseNode { if (this._localMatDirty) { this._updateLocalMatrix(); } - + // Assume parent world matrix is correct let parent = this._parent; if (parent) { @@ -2403,7 +2403,7 @@ export default class Node extends BaseNode { this._updateLocalMatrix(); return math.mat4.copy(out, this._matrix); } - + /** * !#en * Get the world transform matrix (4x4) @@ -2517,7 +2517,7 @@ export default class Node extends BaseNode { out = AffineTrans.identity(); } this._updateLocalMatrix(); - + var contentSize = this._contentSize; _vec3_temp.x = -this._anchorPoint.x * contentSize.width; _vec3_temp.y = -this._anchorPoint.y * contentSize.height; @@ -2568,7 +2568,7 @@ export default class Node extends BaseNode { out = AffineTrans.identity(); } this._updateWorldMatrix(); - + var contentSize = this._contentSize; _vec3_temp.x = -this._anchorPoint.x * contentSize.width; _vec3_temp.y = -this._anchorPoint.y * contentSize.height; @@ -2673,7 +2673,7 @@ export default class Node extends BaseNode { convertTouchToNodeSpaceAR (touch) { return this.convertToNodeSpaceAR(touch.getLocation()); } - + /** * !#en * Returns a "local" axis aligned bounding box of the node.
@@ -2689,9 +2689,9 @@ export default class Node extends BaseNode { let width = this._contentSize.width; let height = this._contentSize.height; let rect = cc.rect( - -this._anchorPoint.x * width, - -this._anchorPoint.y * height, - width, + -this._anchorPoint.x * width, + -this._anchorPoint.y * height, + width, height); return rect.transformMat4(rect, this._matrix); } @@ -2723,9 +2723,9 @@ export default class Node extends BaseNode { let width = this._contentSize.width; let height = this._contentSize.height; let rect = cc.rect( - -this._anchorPoint.x * width, - -this._anchorPoint.y * height, - width, + -this._anchorPoint.x * width, + -this._anchorPoint.y * height, + width, height); var pMat = math.mat4.mul(this._worldMatrix, parentMat, this._matrix); @@ -2894,9 +2894,9 @@ if (CC_EDITOR) { // 3D Node Property /** - * !en + * !en * Switch 2D/3D node. The 2D nodes will run faster. - * !zh + * !zh * 切换 2D/3D 节点,2D 节点会有更高的运行效率 * @property {Boolean} is3DNode * @default false diff --git a/cocos/scene-graph/node.js b/cocos/scene-graph/node.js deleted file mode 100644 index 72461062ad..0000000000 --- a/cocos/scene-graph/node.js +++ /dev/null @@ -1,502 +0,0 @@ -import { vec3, mat4, quat } from '../core/vmath'; -import { Vec3 } from '../core/value-types'; -import BaseNode from './base-node'; -import Layers from './layers'; -import { EventTarget } from "../core/event"; -import { ccclass, property, mixins } from '../core/data/class-decorator'; - -let v3_a = cc.v3(); -let q_a = cc.quat(); -let q_b = cc.quat(); -let array_a = new Array(10); - -let EventType = cc.Enum({ - TRANSFORM_CHANGED: 'transform-changed', - POSITION_PART: 1, - ROTATION_PART: 2, - SCALE_PART: 4 -}); - -@ccclass('cc.Node') -@mixins(EventTarget) -class Node extends BaseNode { - // local transform - @property - _lpos = cc.v3(); - @property - _lrot = cc.quat(); - @property - _lscale = cc.v3(1, 1, 1); - @property - _layer = Layers.Default; // the layer this node belongs to - - @property - _euler = cc.v3(); // local rotation in euler angles, maintained here so that rotation angles could be greater than 360 degree. - - @property({ - type: Vec3 - }) - set eulerAngles(val) { - this.setRotationFromEuler(val.x, val.y, val.z); - } - get eulerAngles() { - if (this._eulerDirty) { - quat.toEuler(this._euler, this._lrot); - this._eulerDirty = false; - } - return this._euler; - } - - set layer(l) { - this._layer = l; - } - - get layer() { - return this._layer; - } - - // world transform - _pos = cc.v3(); - _rot = cc.quat(); - _scale = cc.v3(1, 1, 1); - _mat = cc.mat4(); - - _dirty = false; // does the world transform need to update? - _hasChanged = false; // has the transform changed in this frame? - - _matDirty = false; - _eulerDirty = false; - - // is node but not scene - static isNode(obj) { - return obj instanceof Node && (obj.constructor === Node || !(obj instanceof cc.Scene)); - } - - static EventType = EventType; - - constructor(name) { - super(name); - EventTarget.call(this); - } - - - // =============================== - // hierarchy - // =============================== - - /** - * invalidate all children after relevant events - */ - onRestore() { - super.onRestore(); - this.invalidateChildren(); - } - - _onSetParent(oldParent) { - super._onSetParent(oldParent); - this.invalidateChildren(); - } - - _onPostActivated() { - this.invalidateChildren(); - } - - // =============================== - // transform helper - // =============================== - - /** - * Set rotation by lookAt target point - * @param {vec3} pos target position - * @param {vec3} up the up vector, default to (0,1,0) - */ - lookAt(pos, up) { - this.getWorldPosition(v3_a); - vec3.sub(v3_a, v3_a, pos); // NOTE: we use -z for view-dir - vec3.normalize(v3_a, v3_a); - quat.fromViewUp(q_a, v3_a, up); - - this.setWorldRotation(q_a); - } - - /** - * Reset the `hasChanged` flag recursively - */ - resetHasChanged() { - this._hasChanged = false; - let len = this._children.length; - for (let i = 0; i < len; ++i) { - this._children[i].resetHasChanged(); - } - } - - /** - * invalidate the world transform information - * for this node and all its children recursively - */ - invalidateChildren() { - if (this._dirty && this._hasChanged) return; - this._dirty = this._hasChanged = true; - 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; - let cur = this, child, i = 0; - while (cur._dirty) { - // top level node - array_a[i++] = cur; - cur = cur._parent; - if (!cur || !cur._parent) { - cur = null; - break; - } - } - while (i) { - child = array_a[--i]; - if (cur) { - vec3.mul(child._pos, child._lpos, cur._scale); - vec3.transformQuat(child._pos, child._pos, cur._rot); - vec3.add(child._pos, child._pos, cur._pos); - quat.mul(child._rot, cur._rot, child._lrot); - vec3.mul(child._scale, cur._scale, child._lscale); - } - child._matDirty = true; // further deferred eval - child._dirty = false; - cur = child; - } - } - - updateWorldTransformFull() { - this.updateWorldTransform(); - if (!this._matDirty) return; - mat4.fromRTS(this._mat, this._rot, this._pos, this._scale); - this._matDirty = false; - } - - // =============================== - // transform - // =============================== - - /** - * set local position - * @param {vec3|number} val the new local position, or the x component of it - * @param {number} [y] the y component of the new local position - * @param {number} [z] the z component of the new local position - */ - setPosition(val, y, z) { - if (arguments.length === 1) { - vec3.copy(this._lpos, val); - } 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); - } - - /** - * get local position - * @param {vec3} [out] the receiving vector - * @return {vec3} the resulting vector - */ - getPosition(out) { - if (out) { - return vec3.set(out, this._lpos.x, this._lpos.y, this._lpos.z); - } else { - return vec3.copy(cc.v3(), this._lpos); - } - } - - /** - * set local rotation - * @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 - */ - setRotation(val, y, z, w) { - if (arguments.length === 1) { - quat.copy(this._lrot, val); - } else if (arguments.length === 4) { - quat.set(this._lrot, val, y, z, w); - } - quat.copy(this._rot, this._lrot); - this._eulerDirty = true; - - this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); - } - - /** - * set local rotation from euler angles - * @param {number} x - Angle to rotate around X axis in degrees. - * @param {number} y - Angle to rotate around Y axis in degrees. - * @param {number} z - Angle to rotate around Z axis in degrees. - */ - setRotationFromEuler(x, y, z) { - 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); - } - - /** - * get local rotation - * @param {quat} [out] the receiving quaternion - * @return {quat} the resulting quaternion - */ - getRotation(out) { - if (out) { - return quat.set(out, this._lrot.x, this._lrot.y, this._lrot.z, this._lrot.w); - } else { - return quat.copy(cc.quat(), this._lrot); - } - } - - /** - * set local scale - * @param {vec3|number} val the new local scale, or the x component of it - * @param {number} [y] the y component of the new local scale - * @param {number} [z] the z component of the new local scale - */ - setScale(val, y, z) { - if (arguments.length === 1) { - vec3.copy(this._lscale, val); - } 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); - } - - /** - * get local scale - * @param {vec3} [out] the receiving vector - * @return {vec3} the resulting vector - */ - getScale(out) { - if (out) { - return vec3.set(out, this._lscale.x, this._lscale.y, this._lscale.z); - } else { - return vec3.copy(cc.v3(), this._lscale); - } - } - - /** - * set world position - * @param {vec3|number} val the new world position, or the x component of it - * @param {number} [y] the y component of the new world position - * @param {number} [z] the z component of the new world position - */ - setWorldPosition(val, y, z) { - if (arguments.length === 1) { - vec3.copy(this._pos, val); - } else if (arguments.length === 3) { - vec3.set(this._pos, val, y, z); - } - let parent = this._parent, local = this._lpos; - if (parent) { - parent.updateWorldTransform(); - vec3.sub(local, this._pos, parent._pos); - vec3.transformQuat(local, local, quat.conjugate(q_a, parent._rot)); - vec3.div(local, local, parent._scale); - } else { - vec3.copy(this._lpos, this._pos); - } - - this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART); - } - - /** - * get world position - * @param {vec3} [out] the receiving vector - * @return {vec3} the resulting vector - */ - getWorldPosition(out) { - this.updateWorldTransform(); - if (out) { - return vec3.copy(out, this._pos); - } else { - return vec3.copy(cc.v3(), this._pos); - } - } - - /** - * set world rotation - * @param {quat|number} val the new world rotation, or the x component of it - * @param {number} [y] the y component of the new world rotation - * @param {number} [z] the z component of the new world rotation - * @param {number} [w] the w component of the new world rotation - */ - setWorldRotation(val, y, z, w) { - if (arguments.length === 1) { - quat.copy(this._rot, val); - } else if (arguments.length === 4) { - quat.set(this._rot, val, y, z, w); - } - if (this._parent) { - this._parent.getWorldRotation(q_a); - quat.mul(this._lrot, quat.conjugate(q_a, q_a), this._rot); - } else { - quat.copy(this._lrot, this._rot); - } - this._eulerDirty = true; - - this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); - } - - /** - * set world rotation from euler angles - * @param {number} x - Angle to rotate around X axis in degrees. - * @param {number} y - Angle to rotate around Y axis in degrees. - * @param {number} z - Angle to rotate around Z axis in degrees. - */ - setWorldRotationFromEuler(x, y, z) { - vec3.set(this._euler, x, y, z); - this._eulerDirty = false; - quat.fromEuler(this._rot, x, y, z); - if (this._parent) { - this._parent.getWorldRotation(q_a); - quat.mul(this._lrot, this._rot, quat.conjugate(q_a, q_a)); - } else { - quat.copy(this._lrot, this._rot); - } - - this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); - } - - /** - * get world rotation - * @param {quat} [out] the receiving quaternion - * @return {quat} the resulting quaternion - */ - getWorldRotation(out) { - this.updateWorldTransform(); - if (out) { - return quat.copy(out, this._rot); - } else { - return quat.copy(cc.quat(), this._rot); - } - } - - /** - * set world scale - * @param {vec3|number} val the new world scale, or the x component of it - * @param {number} [y] the y component of the new world scale - * @param {number} [z] the z component of the new world scale - */ - setWorldScale(val, y, z) { - if (arguments.length === 1) { - vec3.copy(this._scale, val); - } else if (arguments.length === 3) { - vec3.set(this._scale, val, y, z); - } - if (this._parent) { - this._parent.getWorldScale(v3_a); - vec3.div(this._lscale, this._scale, v3_a); - } else { - vec3.copy(this._lscale, this._scale); - } - - this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART); - } - - /** - * get world scale - * @param {vec3} [out] the receiving vector - * @return {vec3} the resulting vector - */ - getWorldScale(out) { - this.updateWorldTransform(); - if (out) { - return vec3.copy(out, this._scale); - } else { - return vec3.copy(cc.v3(), this._scale); - } - } - - /** - * get the matrix that transforms a point from local space into world space - * @param {mat4} [out] the receiving matrix - * @return {mat4} the resulting matrix - */ - getWorldMatrix(out) { - this.updateWorldTransformFull(); - if (out) { - return mat4.copy(out, this._mat); - } else { - return mat4.copy(cc.mat4(), this._mat); - } - } - - /** - * get world transform matrix (with only rotation and scale) - * @param {mat4} [out] the receiving matrix - * @return {mat4} the resulting matrix - */ - getWorldRS(out) { - this.updateWorldTransformFull(); - if (out) { - mat4.copy(out, this._mat); - } else { - out = mat4.copy(cc.mat4(), this._mat); - } - out.m12 = 0; out.m13 = 0; out.m14 = 0; - return out; - } - - /** - * get world transform matrix (with only rotation and translation) - * @param {mat4} [out] the receiving matrix - * @return {mat4} the resulting matrix - */ - getWorldRT(out) { - this.updateWorldTransform(); - if (!out) { - out = cc.mat4(); - } - return mat4.fromRT(out, this._rot, this._pos); - } -} - -if (CC_EDITOR) { - let repeat = (t, l) => t - Math.floor(t / l) * l; - let desc = { - set(val) { - this.setRotationFromEuler(val.x, val.y, val.z); - }, - get() { - if (this._eulerDirty) { - let eu = this._euler; - quat.toEuler(v3_a, this._lrot); - eu.x = repeat(v3_a.x - eu.x + 180, 360) + eu.x - 180; - eu.y = repeat(v3_a.y - eu.y + 180, 360) + eu.y - 180; - eu.z = repeat(v3_a.z - eu.z + 180, 360) + eu.z - 180; - this._eulerDirty = false; - } - return this._euler; - } - }; - Object.defineProperty(Node.prototype, 'eulerAngles', desc); -} - -cc.Node = Node; -export default Node; diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts new file mode 100644 index 0000000000..2915cc3ed2 --- /dev/null +++ b/cocos/scene-graph/node.ts @@ -0,0 +1,591 @@ +import { ccclass, mixins, property } from '../core/data/class-decorator'; +import { EventTarget } from '../core/event'; +import { Enum, Mat4, Quat, Vec3 } from '../core/value-types'; +import { mat4, quat, vec3 } from '../core/vmath'; +import { BaseNode } from './base-node'; +import { Layers } from './layers'; + +const v3_a = new Vec3(); +const q_a = new Quat(); +const array_a = new Array(10); + +const EventType = Enum({ + TRANSFORM_CHANGED: 'transform-changed', + POSITION_PART: 1, + ROTATION_PART: 2, + SCALE_PART: 4, +}); + +enum NodeSpace { + LOCAL, + WORLD, +} + +@ccclass('cc.Node') +@mixins(EventTarget) +class Node extends BaseNode { + public static EventType = EventType; + public static NodeSpace = NodeSpace; + + // is node but not scene + public static isNode (obj: object) { + return obj instanceof Node && (obj.constructor === Node || !(obj instanceof cc.Scene)); + } + // local transform + @property + public _lpos = new Vec3(); + @property + public _lrot = new Quat(); + @property + public _lscale = new Vec3(1, 1, 1); + @property + public _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. + + // world transform + public _pos = new Vec3(); + public _rot = new Quat(); + public _scale = new Vec3(1, 1, 1); + public _mat = new Mat4(); + + public _dirty = false; // does the world transform need to update? + public _hasChanged = false; // has the transform changed in this frame? + + public _matDirty = false; + public _eulerDirty = false; + + @property({ + type: Vec3, + }) + set eulerAngles (val) { + this.setRotationFromEuler(val.x, val.y, val.z); + } + get eulerAngles () { + if (this._eulerDirty) { + quat.toEuler(this._euler, this._lrot); + this._eulerDirty = false; + } + return this._euler; + } + + set layer (l) { + this._layer = l; + } + + get layer () { + return this._layer; + } + + constructor (name: string) { + super(name); + EventTarget.call(this); + } + + // =============================== + // hierarchy + // =============================== + + /** + * invalidate all children after relevant events + */ + public onRestore () { + super.onRestore(); + this.invalidateChildren(); + } + + public _onSetParent (oldParent: this) { + super._onSetParent(oldParent); + this.invalidateChildren(); + } + + public _onPostActivated () { + this.invalidateChildren(); + } + + // =============================== + // transform helper, convenient but not the most efficient + // =============================== + + /** + * Rotate the node + * @param rot - rotation to apply + * @param ns - the operating space + */ + public rotate (rot: Quat, ns?: NodeSpace) { + const space = ns || NodeSpace.LOCAL; + if (space === NodeSpace.LOCAL) { + this.getRotation(q_a); + this.setRotation(quat.multiply(q_a, q_a, rot)); + } else if (space === NodeSpace.WORLD) { + this.getWorldRotation(q_a); + this.setWorldRotation(quat.multiply(q_a, rot, q_a)); + } + } + + /** + * rotate the node around X axis + * @param rad - rotating angle + * @param ns - the operating space + */ + public pitch (rad: number, ns?: NodeSpace) { + const space = ns || NodeSpace.LOCAL; + if (space === NodeSpace.LOCAL) { + this.getWorldRotation(q_a); + quat.toAxisX(v3_a, q_a); + quat.fromAxisAngle(q_a, v3_a, rad); + this.rotate(q_a, ns); + } else if (space === NodeSpace.WORLD) { + quat.fromAxisAngle(q_a, vec3.UNIT_X, rad); + this.rotate(q_a, ns); + } + } + + /** + * rotate the node around Y axis + * @param rad - rotating angle + * @param ns - the operating space + */ + public yaw (rad: number, ns?: NodeSpace) { + const space = ns || NodeSpace.LOCAL; + if (space === NodeSpace.LOCAL) { + this.getWorldRotation(q_a); + quat.toAxisY(v3_a, q_a); + quat.fromAxisAngle(q_a, v3_a, rad); + this.rotate(q_a, ns); + } else if (space === NodeSpace.WORLD) { + quat.fromAxisAngle(q_a, vec3.UNIT_Y, rad); + this.rotate(q_a, ns); + } + } + + /** + * rotate the node around Z axis + * @param rad - rotating angle + * @param ns - the operating space + */ + public roll (rad: number, ns?: NodeSpace) { + const space = (ns !== undefined ? ns : NodeSpace.LOCAL); + if (space === NodeSpace.LOCAL) { + this.getWorldRotation(q_a); + quat.toAxisZ(v3_a, q_a); + quat.fromAxisAngle(q_a, v3_a, rad); + this.rotate(q_a, ns); + } else if (space === NodeSpace.WORLD) { + quat.fromAxisAngle(q_a, vec3.UNIT_Z, rad); + this.rotate(q_a, ns); + } + } + + public get direction (): Vec3 { + this.getRotation(q_a); + return vec3.transformQuat(new Vec3(), vec3.UNIT_Z, q_a); + } + + public set direction (dir: Vec3) { + quat.rotationTo(q_a, vec3.UNIT_Z, dir); + this.setRotation(q_a); + } + + /** + * Set rotation by lookAt target point + * @param pos - target position + * @param up - the up vector, default to (0,1,0) + */ + public lookAt (pos: Vec3, up: Vec3) { + this.getWorldPosition(v3_a); + vec3.sub(v3_a, v3_a, pos); // NOTE: we use -z for view-dir + vec3.normalize(v3_a, v3_a); + quat.fromViewUp(q_a, v3_a, up); + + this.setWorldRotation(q_a); + } + + // =============================== + // transform maintainer + // =============================== + + /** + * Reset the `hasChanged` flag recursively + */ + public resetHasChanged () { + this._hasChanged = false; + const len = this._children.length; + for (let i = 0; i < len; ++i) { + this._children[i].resetHasChanged(); + } + } + + /** + * invalidate the world transform information + * for this node and all its children recursively + */ + public invalidateChildren () { + if (this._dirty && this._hasChanged) { return; } + this._dirty = this._hasChanged = true; + 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. + */ + public updateWorldTransform () { + if (!this._dirty) { return; } + let cur: this | null = this; + let i = 0; + while (cur._dirty) { + // top level node + array_a[i++] = cur; + cur = cur._parent; + if (!cur || !cur._parent) { + cur = null; + break; + } + } + let child: this; + while (i) { + child = array_a[--i]; + if (cur) { + vec3.mul(child._pos, child._lpos, cur._scale); + vec3.transformQuat(child._pos, child._pos, cur._rot); + vec3.add(child._pos, child._pos, cur._pos); + quat.mul(child._rot, cur._rot, child._lrot); + vec3.mul(child._scale, cur._scale, child._lscale); + } + child._matDirty = true; // further deferred eval + child._dirty = false; + cur = child; + } + } + + public updateWorldTransformFull () { + this.updateWorldTransform(); + if (!this._matDirty) { return; } + mat4.fromRTS(this._mat, this._rot, this._pos, this._scale); + this._matDirty = false; + } + + // =============================== + // transform + // =============================== + + /** + * set local position + * @param val - the new local position, or the x component of it + * @param y - the y component of the new local position + * @param z - the z component of the new local position + */ + public setPosition (val: Vec3 | number, y?: number, z?: number) { + if (y === undefined || z === undefined) { + vec3.copy(this._lpos, val); + } 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); + } + + /** + * get local position + * @param out the receiving vector + * @returns the resulting vector + */ + public getPosition (out: Vec3): Vec3 { + if (out) { + return vec3.set(out, this._lpos.x, this._lpos.y, this._lpos.z); + } else { + return vec3.copy(new Vec3(), this._lpos); + } + } + + /** + * set local rotation + * @param val - the new local rotation, or the x component of it + * @param y - the y component of the new local rotation + * @param z - the z component of the new local rotation + * @param w - the w component of the new local rotation + */ + public setRotation (val: Quat | number, y?: number, z?: number, w?: number) { + if (y === undefined || z === undefined || w === undefined) { + quat.copy(this._lrot, val); + } else if (arguments.length === 4) { + quat.set(this._lrot, val, y, z, w); + } + quat.copy(this._rot, this._lrot); + this._eulerDirty = true; + + this.invalidateChildren(); + this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); + } + + /** + * set local rotation from euler angles + * @param x - Angle to rotate around X axis in degrees. + * @param y - Angle to rotate around Y axis in degrees. + * @param z - Angle to rotate around Z axis in degrees. + */ + public setRotationFromEuler (x: number, y: number, z: number) { + 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); + } + + /** + * get local rotation + * @param out - the receiving quaternion + * @returns the resulting quaternion + */ + public getRotation (out: Quat): Quat { + if (out) { + return quat.set(out, this._lrot.x, this._lrot.y, this._lrot.z, this._lrot.w); + } else { + return quat.copy(new Quat(), this._lrot); + } + } + + /** + * set local scale + * @param val - the new local scale, or the x component of it + * @param y - the y component of the new local scale + * @param z - the z component of the new local scale + */ + public setScale (val: Vec3 | number, y?: number, z?: number) { + if (y === undefined || z === undefined) { + vec3.copy(this._lscale, val); + } 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); + } + + /** + * get local scale + * @param out - the receiving vector + * @returns the resulting vector + */ + public getScale (out: Vec3): Vec3 { + if (out) { + return vec3.set(out, this._lscale.x, this._lscale.y, this._lscale.z); + } else { + return vec3.copy(new Vec3(), this._lscale); + } + } + + /** + * set world position + * @param val - the new world position, or the x component of it + * @param y - the y component of the new world position + * @param z - the z component of the new world position + */ + public setWorldPosition (val: Vec3 | number, y?: number, z?: number) { + if (y === undefined || z === undefined) { + vec3.copy(this._pos, val); + } else if (arguments.length === 3) { + vec3.set(this._pos, val, y, z); + } + const parent = this._parent; + const local = this._lpos; + if (parent) { + parent.updateWorldTransform(); + vec3.sub(local, this._pos, parent._pos); + vec3.transformQuat(local, local, quat.conjugate(q_a, parent._rot)); + vec3.div(local, local, parent._scale); + } else { + vec3.copy(this._lpos, this._pos); + } + + this.invalidateChildren(); + this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART); + } + + /** + * get world position + * @param out - the receiving vector + * @returns the resulting vector + */ + public getWorldPosition (out: Vec3): Vec3 { + this.updateWorldTransform(); + if (out) { + return vec3.copy(out, this._pos); + } else { + return vec3.copy(new Vec3(), this._pos); + } + } + + /** + * set world rotation + * @param val - the new world rotation, or the x component of it + * @param y - the y component of the new world rotation + * @param z - the z component of the new world rotation + * @param w - the w component of the new world rotation + */ + public setWorldRotation (val: Quat | number, y?: number, z?: number, w?: number) { + if (y === undefined || z === undefined || w === undefined) { + quat.copy(this._rot, val); + } else if (arguments.length === 4) { + quat.set(this._rot, val, y, z, w); + } + if (this._parent) { + this._parent.getWorldRotation(q_a); + quat.mul(this._lrot, quat.conjugate(q_a, q_a), this._rot); + } else { + quat.copy(this._lrot, this._rot); + } + this._eulerDirty = true; + + this.invalidateChildren(); + this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); + } + + /** + * set world rotation from euler angles + * @param x - Angle to rotate around X axis in degrees. + * @param y - Angle to rotate around Y axis in degrees. + * @param z - Angle to rotate around Z axis in degrees. + */ + public setWorldRotationFromEuler (x: number, y: number, z: number) { + vec3.set(this._euler, x, y, z); + this._eulerDirty = false; + quat.fromEuler(this._rot, x, y, z); + if (this._parent) { + this._parent.getWorldRotation(q_a); + quat.mul(this._lrot, this._rot, quat.conjugate(q_a, q_a)); + } else { + quat.copy(this._lrot, this._rot); + } + + this.invalidateChildren(); + this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); + } + + /** + * get world rotation + * @param out - the receiving quaternion + * @returns the resulting quaternion + */ + public getWorldRotation (out: Quat): Quat { + this.updateWorldTransform(); + if (out) { + return quat.copy(out, this._rot); + } else { + return quat.copy(new Quat(), this._rot); + } + } + + /** + * set world scale + * @param val - the new world scale, or the x component of it + * @param y - the y component of the new world scale + * @param z - the z component of the new world scale + */ + public setWorldScale (val: Vec3 | number, y?: number, z?: number) { + if (y === undefined || z === undefined) { + vec3.copy(this._scale, val); + } else if (arguments.length === 3) { + vec3.set(this._scale, val, y, z); + } + if (this._parent) { + this._parent.getWorldScale(v3_a); + vec3.div(this._lscale, this._scale, v3_a); + } else { + vec3.copy(this._lscale, this._scale); + } + + this.invalidateChildren(); + this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART); + } + + /** + * get world scale + * @param out - the receiving vector + * @returns the resulting vector + */ + public getWorldScale (out: Vec3): Vec3 { + this.updateWorldTransform(); + if (out) { + return vec3.copy(out, this._scale); + } else { + return vec3.copy(new Vec3(), this._scale); + } + } + + /** + * get the matrix that transforms a point from local space into world space + * @param out - the receiving matrix + * @returns the - resulting matrix + */ + public getWorldMatrix (out: Mat4): Mat4 { + this.updateWorldTransformFull(); + if (out) { + return mat4.copy(out, this._mat); + } else { + return mat4.copy(new Mat4(), this._mat); + } + } + + /** + * get world transform matrix (with only rotation and scale) + * @param out - the receiving matrix + * @returns the - resulting matrix + */ + public getWorldRS (out: Mat4): Mat4 { + this.updateWorldTransformFull(); + if (out) { + mat4.copy(out, this._mat); + } else { + out = mat4.copy(new Mat4(), this._mat); + } + out.m12 = 0; out.m13 = 0; out.m14 = 0; + return out; + } + + /** + * get world transform matrix (with only rotation and translation) + * @param out - the receiving matrix + * @returns the - resulting matrix + */ + public getWorldRT (out: Mat4): Mat4 { + this.updateWorldTransform(); + if (!out) { + out = new Mat4(); + } + return mat4.fromRT(out, this._rot, this._pos); + } +} + +if (CC_EDITOR) { + const repeat = (t: number, l: number) => t - Math.floor(t / l) * l; + Object.defineProperty(Node.prototype, 'eulerAngles', { + set (val: Vec3) { + this.setRotationFromEuler(val.x, val.y, val.z); + }, + get () { + if (this._eulerDirty) { + const eu = this._euler; + quat.toEuler(v3_a, this._lrot); + eu.x = repeat(v3_a.x - eu.x + 180, 360) + eu.x - 180; + eu.y = repeat(v3_a.y - eu.y + 180, 360) + eu.y - 180; + eu.z = repeat(v3_a.z - eu.z + 180, 360) + eu.z - 180; + this._eulerDirty = false; + } + return this._euler; + }, + }); +} + +cc.Node = Node; +export { Node }; diff --git a/cocos/scene-graph/private-node.js b/cocos/scene-graph/private-node.js index 50c22cb9bf..f17569b64e 100644 --- a/cocos/scene-graph/private-node.js +++ b/cocos/scene-graph/private-node.js @@ -25,7 +25,7 @@ 'use strict'; -import Node from './node'; +import { Node } from './node'; import { ccclass, property } from '../core/data/class-decorator'; const LocalDirtyFlag = Node._LocalDirtyFlag; @@ -164,4 +164,4 @@ export default class PrivateNode extends Node { cc.js.getset(PrivateNode.prototype, "parent", PrivateNode.prototype.getParent, PrivateNode.prototype.setParent); cc.js.getset(PrivateNode.prototype, "position", PrivateNode.prototype.getPosition, PrivateNode.prototype.setPosition); -cc.PrivateNode = PrivateNode; \ No newline at end of file +cc.PrivateNode = PrivateNode; diff --git a/cocos/scene-graph/scene.js b/cocos/scene-graph/scene.js index 2e508061b5..53bc05b996 100644 --- a/cocos/scene-graph/scene.js +++ b/cocos/scene-graph/scene.js @@ -23,10 +23,9 @@ THE SOFTWARE. ****************************************************************************/ -import Node from './node'; +import { Node } from './node'; import { ccclass, property } from '../core/data/class-decorator'; -import BaseNode from './base-node'; -import { RenderScene } from '../renderer/scene/render-scene'; +import { BaseNode } from './base-node'; /** * !#en