diff --git a/cocos/3d/assets/effect-asset.ts b/cocos/3d/assets/effect-asset.ts index ea8d6e292e..bb98d62d1f 100644 --- a/cocos/3d/assets/effect-asset.ts +++ b/cocos/3d/assets/effect-asset.ts @@ -1,6 +1,6 @@ import { Asset } from '../../assets/asset'; import { ccclass, property } from '../../core/data/class-decorator'; -import { GFXPrimitiveMode } from '../../gfx/define'; +import { GFXDynamicState, GFXPrimitiveMode } from '../../gfx/define'; import { GFXBlendState, GFXDepthStencilState, GFXRasterizerState } from '../../gfx/pipeline-state'; import { RenderPassStage } from '../../pipeline/define'; @@ -17,6 +17,7 @@ export interface IPassInfo { rasterizerState?: GFXRasterizerState; depthStencilState?: GFXDepthStencilState; blendState?: GFXBlendState; + dynamics?: GFXDynamicState[]; properties?: Record; } export interface ITechniqueInfo { diff --git a/cocos/3d/assets/material.ts b/cocos/3d/assets/material.ts index b015831c8f..676ea135d6 100644 --- a/cocos/3d/assets/material.ts +++ b/cocos/3d/assets/material.ts @@ -31,6 +31,12 @@ import { Pass, PassOverrides } from '../../renderer/core/pass'; import { RenderableComponent } from '../framework/renderable-component'; import { EffectAsset } from './effect-asset'; +export interface IMaterialInfo { + technique?: number; + defines?: Record; + effectName?: string; +} + @ccclass('cc.Material') export class Material extends Asset { public static getInstantiatedMaterial (mat: Material, rndCom: any) { @@ -58,6 +64,15 @@ export class Material extends Asset { protected _owner: RenderableComponent | null = null; protected _hash = 0; + constructor (info?: IMaterialInfo) { + super(); + if (info) { + if (info.technique) { this._techIdx = info.technique; } + if (info.defines) { this._defines = info.defines; } + if (info.effectName) { this.effectName = info.effectName; } + } + } + @property set effectAsset (val: EffectAsset | null) { if (!val || this.effectName !== val.name) { this.update(val, false); } @@ -105,7 +120,6 @@ export class Material extends Asset { * if you need to do per-frame property update. */ public setProperty (name: string, val: any, passIdx?: number) { - if (!val) { console.warn(`illegal property value: ${val}.`); return; } let success = false; if (passIdx === undefined) { // set property for all applicable passes const passes = this._passes; @@ -143,10 +157,16 @@ export class Material extends Asset { this.update(); } - public overridePipelineStates (overrides: PassOverrides, passIdx: number = 0) { + public overridePipelineStates (overrides: PassOverrides, passIdx?: number) { if (!this._passes || !this._effectAsset) { return; } const passInfos = Effect.getPassInfos(this._effectAsset, this._techIdx); - this._passes[passIdx].overridePipelineStates(passInfos[passIdx], overrides); + if (passIdx === undefined) { + for (let i = 0; i < this._passes.length; i++) { + this._passes[i].overridePipelineStates(passInfos[i], overrides); + } + } else { + this._passes[passIdx].overridePipelineStates(passInfos[passIdx], overrides); + } this.onPassChange(); } diff --git a/cocos/3d/geom-utils/aabb.js b/cocos/3d/geom-utils/aabb.ts similarity index 64% rename from cocos/3d/geom-utils/aabb.js rename to cocos/3d/geom-utils/aabb.ts index 523d831776..d702c33402 100644 --- a/cocos/3d/geom-utils/aabb.js +++ b/cocos/3d/geom-utils/aabb.ts @@ -1,12 +1,12 @@ +import { mat3, mat4, vec3 } from '../../core/vmath'; import enums from './enums'; -import { vec3, mat3 } from '../../core/vmath'; -let _v3_tmp = vec3.create(); -let _v3_tmp2 = vec3.create(); -let _m3_tmp = mat3.create(); +const _v3_tmp = vec3.create(); +const _v3_tmp2 = vec3.create(); +const _m3_tmp = mat3.create(); // https://zeuxcg.org/2010/10/17/aabb-from-obb-with-component-wise-abs/ -let transform_extent_m4 = function(out, extent, m4) { +const transform_extent_m4 = (out: vec3, extent: vec3, m4: mat4) => { _m3_tmp.m00 = Math.abs(m4.m00); _m3_tmp.m01 = Math.abs(m4.m01); _m3_tmp.m02 = Math.abs(m4.m02); _m3_tmp.m03 = Math.abs(m4.m04); _m3_tmp.m04 = Math.abs(m4.m05); _m3_tmp.m05 = Math.abs(m4.m06); _m3_tmp.m06 = Math.abs(m4.m08); _m3_tmp.m07 = Math.abs(m4.m09); _m3_tmp.m08 = Math.abs(m4.m10); @@ -14,34 +14,29 @@ let transform_extent_m4 = function(out, extent, m4) { }; export default class aabb { - constructor(px = 0, py = 0, pz = 0, w = 1, h = 1, l = 1) { - this._type = enums.SHAPE_AABB; - this.center = vec3.create(px, py, pz); - this.halfExtents = vec3.create(w, h, l); - } /** * create a new aabb * - * @param {number} px X coordinates for aabb's original point - * @param {number} py Y coordinates for aabb's original point - * @param {number} pz Z coordinates for aabb's original point - * @param {number} w the half of aabb width - * @param {number} h the half of aabb height - * @param {number} l the half of aabb length - * @return {aabb} + * @param px - X coordinates for aabb's original point + * @param py - Y coordinates for aabb's original point + * @param pz - Z coordinates for aabb's original point + * @param w - the half of aabb width + * @param h - the half of aabb height + * @param l - the half of aabb length + * @returns the resulting aabb */ - static create(px, py, pz, w, h, l) { + public static create (px?: number, py?: number, pz?: number, w?: number, h?: number, l?: number) { return new aabb(px, py, pz, w, h, l); } /** * clone a new aabb * - * @param {aabb} a the source aabb - * @return {aabb} + * @param a - the source aabb + * @returns the cloned aabb */ - static clone(a) { + public static clone (a: aabb) { return new aabb(a.center.x, a.center.y, a.center.z, a.halfExtents.x, a.halfExtents.y, a.halfExtents.z); } @@ -53,7 +48,7 @@ export default class aabb { * @param {aabb} a the source aabb * @return {aabb} */ - static copy(out, a) { + public static copy (out, a) { vec3.copy(out.center, a.center); vec3.copy(out.halfExtents, a.halfExtents); @@ -63,17 +58,17 @@ export default class aabb { /** * create a new aabb from two corner points * - * @param {aabb} out the receiving aabb - * @param {vec3} minPos lower corner position of the aabb - * @param {vec3} maxPos upper corner position of the aabb - * @return {aabb} + * @param out - the receiving aabb + * @param minPos - lower corner position of the aabb + * @param maxPos - upper corner position of the aabb + * @returns the resulting aabb */ - static fromPoints(out, minPos, maxPos) { + public static fromPoints (out: aabb, minPos: vec3, maxPos: vec3) { vec3.scale(out.center, vec3.add(_v3_tmp, minPos, maxPos), 0.5); vec3.scale(out.halfExtents, vec3.sub(_v3_tmp2, maxPos, minPos), 0.5); return out; } - + /** * Set the components of a aabb to the given values * @@ -87,18 +82,27 @@ export default class aabb { * @return {aabb} out * @function */ - static set(out, px, py, pz, w, h, l) { + public static set (out, px, py, pz, w, h, l) { vec3.set(out.center, px, py, pz); vec3.set(out.halfExtents, w, h, l); return out; } + public center: vec3; + public halfExtents: vec3; + protected _type: number = enums.SHAPE_AABB; + + constructor (px = 0, py = 0, pz = 0, w = 1, h = 1, l = 1) { + this.center = vec3.create(px, py, pz); + this.halfExtents = vec3.create(w, h, l); + } + /** * Get the bounding points of this shape * @param {vec3} minPos * @param {vec3} maxPos */ - getBoundary(minPos, maxPos) { + public getBoundary (minPos, maxPos) { vec3.sub(minPos, this.center, this.halfExtents); vec3.add(maxPos, this.center, this.halfExtents); } @@ -111,9 +115,13 @@ export default class aabb { * @param {vec3} scale the scale part of the transform * @param {aabb} [out] the target shape */ - transform(m, pos, rot, scale, out) { - if (!out) out = this; + public transform (m, pos, rot, scale, out) { + if (!out) { out = this; } vec3.transformMat4(out.center, this.center, m); transform_extent_m4(out.halfExtents, this.halfExtents, m); } + + get type (){ + return this._type; + } } diff --git a/cocos/3d/geom-utils/index.js b/cocos/3d/geom-utils/index.ts similarity index 100% rename from cocos/3d/geom-utils/index.js rename to cocos/3d/geom-utils/index.ts diff --git a/cocos/core/vmath/mat3.ts b/cocos/core/vmath/mat3.ts index e263029312..20d0eb2c4f 100644 --- a/cocos/core/vmath/mat3.ts +++ b/cocos/core/vmath/mat3.ts @@ -1,5 +1,6 @@ import { EPSILON } from './utils'; import vec3 from './vec3'; +// tslint:disable: one-variable-per-declaration /** * Mathematical 3x3 matrix. @@ -15,7 +16,8 @@ import vec3 from './vec3'; * 0, 0, 1, 0, * x, y, z, 0] * - * The same matrix in the [OpenGL documentation](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml) + * The same matrix in the + * [OpenGL documentation](https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml) * is written as: * * 1 0 0 x @@ -43,7 +45,7 @@ class mat3 { * @param m08 - Value assigned to element at column 2 row 2. * @return The newly created matrix. */ - public static create(m00 = 1, m01 = 0, m02 = 0, m03 = 0, m04 = 1, m05 = 0, m06 = 0, m07 = 0, m08 = 1) { + public static create (m00 = 1, m01 = 0, m02 = 0, m03 = 0, m04 = 1, m05 = 0, m06 = 0, m07 = 0, m08 = 1) { return new mat3(m00, m01, m02, m03, m04, m05, m06, m07, m08); } @@ -53,7 +55,7 @@ class mat3 { * @param a - Matrix to clone. * @return The newly created matrix. */ - public static clone(a) { + public static clone (a) { return new mat3( a.m00, a.m01, a.m02, a.m03, a.m04, a.m05, @@ -68,7 +70,7 @@ class mat3 { * @param a - The specified matrix. * @return out. */ - public static copy(out, a) { + public static copy (out, a) { out.m00 = a.m00; out.m01 = a.m01; out.m02 = a.m02; @@ -96,7 +98,7 @@ class mat3 { * @param m22 - Value assigned to element at column 2 row 2. * @return out. */ - public static set(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) { + public static set (out, m00, m01, m02, m10, m11, m12, m20, m21, m22) { out.m00 = m00; out.m01 = m01; out.m02 = m02; @@ -114,7 +116,7 @@ class mat3 { * * @return out. */ - public static identity(out) { + public static identity (out) { out.m00 = 1; out.m01 = 0; out.m02 = 0; @@ -134,7 +136,7 @@ class mat3 { * @param a - Matrix to transpose. * @return out. */ - public static transpose(out, a) { + public static transpose (out, a) { // If we are transposing ourselves we can skip a few steps but have to cache some values if (out === a) { const a01 = a.m01, a02 = a.m02, a12 = a.m05; @@ -166,7 +168,7 @@ class mat3 { * @param a - Matrix to invert. * @return out. */ - public static invert(out, a) { + public static invert (out, a) { const a00 = a.m00, a01 = a.m01, a02 = a.m02, a10 = a.m03, a11 = a.m04, a12 = a.m05, a20 = a.m06, a21 = a.m07, a22 = a.m08; @@ -202,7 +204,7 @@ class mat3 { * @param a - Matrix to calculate. * @return out. */ - public static adjoint(out, a) { + public static adjoint (out, a) { const a00 = a.m00, a01 = a.m01, a02 = a.m02, a10 = a.m03, a11 = a.m04, a12 = a.m05, a20 = a.m06, a21 = a.m07, a22 = a.m08; @@ -225,7 +227,7 @@ class mat3 { * @param a - Matrix to calculate. * @return Determinant of a. */ - public static determinant(a) { + public static determinant (a) { const a00 = a.m00, a01 = a.m01, a02 = a.m02, a10 = a.m03, a11 = a.m04, a12 = a.m05, a20 = a.m06, a21 = a.m07, a22 = a.m08; @@ -241,7 +243,7 @@ class mat3 { * @param b - The second operand. * @return out. */ - public static multiply(out, a, b) { + public static multiply (out, a, b) { const a00 = a.m00, a01 = a.m01, a02 = a.m02, a10 = a.m03, a11 = a.m04, a12 = a.m05, a20 = a.m06, a21 = a.m07, a22 = a.m08; @@ -267,7 +269,7 @@ class mat3 { /** * Alias of {@link mat3.multiply}. */ - public static mul(out, a, b) { + public static mul (out, a, b) { return mat3.multiply(out, a, b); } @@ -279,7 +281,7 @@ class mat3 { * @param v - The translation offset. * @return out. */ - public static translate(out, a, v) { + public static translate (out, a, v) { const a00 = a.m00, a01 = a.m01, a02 = a.m02, a10 = a.m03, a11 = a.m04, a12 = a.m05, a20 = a.m06, a21 = a.m07, a22 = a.m08; @@ -307,7 +309,7 @@ class mat3 { * @param rad - The rotation angle. * @return out */ - public static rotate(out, a, rad) { + public static rotate (out, a, rad) { const a00 = a.m00, a01 = a.m01, a02 = a.m02, a10 = a.m03, a11 = a.m04, a12 = a.m05, a20 = a.m06, a21 = a.m07, a22 = a.m08; @@ -336,8 +338,8 @@ class mat3 { * @param a - Matrix to multiply. * @param v - The scale vector. * @return out - **/ - public static scale(out, a, v) { + */ + public static scale (out, a, v) { const x = v.x, y = v.y; out.m00 = x * a.m00; @@ -361,7 +363,7 @@ class mat3 { * @param a - The 4x4 matrix. * @return out. */ - public static fromMat4(out, a) { + public static fromMat4 (out, a) { out.m00 = a.m00; out.m01 = a.m01; out.m02 = a.m02; @@ -385,7 +387,7 @@ class mat3 { * @param v - The translation offset. * @return out. */ - public static fromTranslation(out, v) { + public static fromTranslation (out, v) { out.m00 = 1; out.m01 = 0; out.m02 = 0; @@ -409,7 +411,7 @@ class mat3 { * @param rad - The rotation angle. * @return out. */ - public static fromRotation(out, rad) { + public static fromRotation (out, rad) { const s = Math.sin(rad), c = Math.cos(rad); out.m00 = c; @@ -437,7 +439,7 @@ class mat3 { * @param v - Scale vector. * @return out. */ - public static fromScaling(out, v) { + public static fromScaling (out, v) { out.m00 = v.x; out.m01 = 0; out.m02 = 0; @@ -458,8 +460,8 @@ class mat3 { * @param out - Matrix to store result. * @param a - The 2x3 matrix. * @return out. - **/ - public static fromMat2d(out, a) { + */ + public static fromMat2d (out, a) { out.m00 = a.m00; out.m01 = a.m01; out.m02 = 0; @@ -482,7 +484,7 @@ class mat3 { * * @return out. */ - public static fromQuat(out, q) { + public static fromQuat (out, q) { const x = q.x, y = q.y, z = q.z, w = q.w; const x2 = x + x; const y2 = y + y; @@ -522,13 +524,13 @@ class mat3 { * * @return out */ - public static fromViewUp(out, view, up) { - const _fromViewUpIIFE = (function() { + public static fromViewUp (_out, _view, _up) { + const _fromViewUpIIFE = (() => { const default_up = vec3.create(0, 1, 0); const x = vec3.create(0, 0, 0); const y = vec3.create(0, 0, 0); - return function(out, view, up) { + return (out, view, up) => { if (vec3.sqrMag(view) < EPSILON * EPSILON) { mat3.identity(out); return out; @@ -553,7 +555,7 @@ class mat3 { return out; }; })(); - return _fromViewUpIIFE(out, view, up); + return _fromViewUpIIFE(_out, _view, _up); } /** @@ -564,7 +566,7 @@ class mat3 { * * @return out. */ - public static normalFromMat4(out, a) { + public static normalFromMat4 (out, a) { const a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, @@ -612,7 +614,7 @@ class mat3 { * @param a - The matrix. * @return String representation of this matrix. */ - public static str(a) { + public static str (a) { return `mat3(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03}, ${a.m04}, ${a.m05}, ${a.m06}, ${a.m07}, ${a.m08})`; } @@ -623,7 +625,7 @@ class mat3 { * @param m - The matrix. * @return out. */ - public static array(out, m) { + public static array (out, m) { out[0] = m.m00; out[1] = m.m01; out[2] = m.m02; @@ -643,8 +645,10 @@ class mat3 { * @param a - Matrix to calculate Frobenius norm of. * @return - The frobenius norm. */ - public static frob(a) { - return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2) + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + Math.pow(a.m06, 2) + Math.pow(a.m07, 2) + Math.pow(a.m08, 2))); + public static frob (a) { + return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + + Math.pow(a.m03, 2) + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + Math.pow(a.m06, 2) + + Math.pow(a.m07, 2) + Math.pow(a.m08, 2))); } /** @@ -655,7 +659,7 @@ class mat3 { * @param b - The second operand. * @return out. */ - public static add(out, a, b) { + public static add (out, a, b) { out.m00 = a.m00 + b.m00; out.m01 = a.m01 + b.m01; out.m02 = a.m02 + b.m02; @@ -676,7 +680,7 @@ class mat3 { * @param b - The second operand. * @return out. */ - public static subtract(out, a, b) { + public static subtract (out, a, b) { out.m00 = a.m00 - b.m00; out.m01 = a.m01 - b.m01; out.m02 = a.m02 - b.m02; @@ -692,7 +696,7 @@ class mat3 { /** * Alias of {@link mat3.subtract}. */ - public static sub(out, a, b) { + public static sub (out, a, b) { return mat3.subtract(out, a, b); } @@ -704,7 +708,7 @@ class mat3 { * @param b - The scale number. * @return out. */ - public static multiplyScalar(out, a, b) { + public static multiplyScalar (out, a, b) { out.m00 = a.m00 * b; out.m01 = a.m01 * b; out.m02 = a.m02 * b; @@ -726,7 +730,7 @@ class mat3 { * @param scale - The scale number. * @return out. */ - public static multiplyScalarAndAdd(out, a, b, scale) { + public static multiplyScalarAndAdd (out, a, b, scale) { out.m00 = a.m00 + (b.m00 * scale); out.m01 = a.m01 + (b.m01 * scale); out.m02 = a.m02 + (b.m02 * scale); @@ -746,7 +750,7 @@ class mat3 { * @param b - The second matrix. * @return True if the matrices are equal, false otherwise. */ - public static exactEquals(a, b) { + public static exactEquals (a, b) { return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && a.m03 === b.m03 && a.m04 === b.m04 && a.m05 === b.m05 && a.m06 === b.m06 && a.m07 === b.m07 && a.m08 === b.m08; @@ -759,9 +763,11 @@ class mat3 { * @param b - The second matrix. * @return True if the matrices are equal, false otherwise. */ - public static equals(a, b) { + public static equals (a, b) { + // tslint:disable: max-line-length const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05, a6 = a.m06, a7 = a.m07, a8 = a.m08; const b0 = b.m00, b1 = b.m01, b2 = b.m02, b3 = b.m03, b4 = b.m04, b5 = b.m05, b6 = b.m06, b7 = b.m07, b8 = b.m08; + // tslint:enable: max-line-length return ( Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && @@ -774,6 +780,17 @@ class mat3 { Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)) ); } + + public m00: number; + public m01: number; + public m02: number; + public m03: number; + public m04: number; + public m05: number; + public m06: number; + public m07: number; + public m08: number; + /** * Creates a matrix, with elements specified separately. * @@ -787,7 +804,7 @@ class mat3 { * @param m07 - Value assigned to element at column 2 row 1. * @param m08 - Value assigned to element at column 2 row 2. */ - constructor( + constructor ( m00 = 1, m01 = 0, m02 = 0, m03 = 0, m04 = 1, m05 = 0, m06 = 0, m07 = 0, m08 = 1, @@ -795,55 +812,55 @@ class mat3 { /** * The element at column 0 row 0. * @type {number} - * */ + */ this.m00 = m00; /** * The element at column 0 row 1. * @type {number} - * */ + */ this.m01 = m01; /** * The element at column 0 row 2. * @type {number} - * */ + */ this.m02 = m02; /** * The element at column 1 row 0. * @type {number} - * */ + */ this.m03 = m03; /** * The element at column 1 row 1. * @type {number} - * */ + */ this.m04 = m04; /** * The element at column 1 row 2. * @type {number} - * */ + */ this.m05 = m05; /** * The element at column 2 row 0. * @type {number} - * */ + */ this.m06 = m06; /** * The element at column 2 row 1. * @type {number} - * */ + */ this.m07 = m07; /** * The element at column 2 row 2. * @type {number} - * */ + */ this.m08 = m08; } } diff --git a/cocos/core/vmath/mat4.ts b/cocos/core/vmath/mat4.ts index a2a99a9539..c200b7aa9e 100644 --- a/cocos/core/vmath/mat4.ts +++ b/cocos/core/vmath/mat4.ts @@ -1,5 +1,7 @@ import { EPSILON } from './utils'; +// tslint:disable: one-variable-per-declaration + /** * Mathematical 4x4 matrix. * @@ -27,24 +29,6 @@ import { EPSILON } from './utils'; * apparent lack of consistency between the memory layout and the documentation. */ class mat4 { - - public m00: number = 1; - public m01: number = 0; - public m02: number = 0; - public m03: number = 0; - public m04: number = 0; - public m05: number = 1; - public m06: number = 0; - public m07: number = 0; - public m08: number = 0; - public m09: number = 0; - public m10: number = 1; - public m11: number = 0; - public m12: number = 0; - public m13: number = 0; - public m14: number = 0; - public m15: number = 1; - /** * Creates a matrix, with elements specified separately. * @@ -66,7 +50,7 @@ class mat4 { * @param m15 - Value assigned to element at column 3 row 3. * @return The newly created matrix. */ - public static create( + public static create ( m00 = 1, m01 = 0, m02 = 0, m03 = 0, m04 = 0, m05 = 1, m06 = 0, m07 = 0, m08 = 0, m09 = 0, m10 = 1, m11 = 0, @@ -85,7 +69,7 @@ class mat4 { * @param a - Matrix to clone. * @return The newly created matrix. */ - public static clone(a) { + public static clone (a) { return new mat4( a.m00, a.m01, a.m02, a.m03, a.m04, a.m05, a.m06, a.m07, @@ -101,7 +85,7 @@ class mat4 { * @param a - The specified matrix. * @return out. */ - public static copy(out, a) { + public static copy (out, a) { out.m00 = a.m00; out.m01 = a.m01; out.m02 = a.m02; @@ -143,7 +127,7 @@ class mat4 { * @param m33 - Value assigned to element at column 3 row 3. * @return out. */ - public static set(out, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { + public static set (out, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { out.m00 = m00; out.m01 = m01; out.m02 = m02; @@ -169,7 +153,7 @@ class mat4 { * @param out - Matrix to modified. * @return out. */ - public static identity(out) { + public static identity (out) { out.m00 = 1; out.m01 = 0; out.m02 = 0; @@ -196,7 +180,7 @@ class mat4 { * @param a - Matrix to transpose. * @return out. */ - public static transpose(out, a) { + public static transpose (out, a) { // If we are transposing ourselves we can skip a few steps but have to cache some values if (out === a) { const a01 = a.m01, a02 = a.m02, a03 = a.m03, @@ -244,7 +228,7 @@ class mat4 { * @param a - Matrix to invert. * @return out. */ - public static invert(out, a) { + public static invert (out, a) { const a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, @@ -298,7 +282,7 @@ class mat4 { * @param a - Matrix to calculate. * @return out. */ - public static adjoint(out, a) { + public static adjoint (out, a) { const a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, @@ -329,7 +313,7 @@ class mat4 { * @param a - Matrix to calculate. * @return Determinant of a. */ - public static determinant(a) { + public static determinant (a) { const a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, @@ -360,7 +344,7 @@ class mat4 { * @param b - The second operand. * @return out. */ - public static multiply(out, a, b) { + public static multiply (out, a, b) { const a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, @@ -396,7 +380,7 @@ class mat4 { /** * Alias of {@link mat4.multiply}. */ - public static mul(out, a, b) { + public static mul (out, a, b) { return mat4.multiply(out, a, b); } @@ -408,9 +392,9 @@ class mat4 { * @param v - The translation offset. * @return out. */ - public static translate(out, a, v) { - let x = v.x, y = v.y, z = v.z, - a00, a01, a02, a03, + public static translate (out, a, v) { + const x = v.x, y = v.y, z = v.z; + let a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23; @@ -444,8 +428,8 @@ class mat4 { * @param a - Matrix to multiply. * @param v - The scale vector. * @return out - **/ - public static scale(out, a, v) { + */ + public static scale (out, a, v) { const x = v.x, y = v.y, z = v.z; out.m00 = a.m00 * x; @@ -476,7 +460,7 @@ class mat4 { * @param axis - The rotation axis. * @return out. */ - public static rotate(out, a, rad, axis) { + public static rotate (out, a, rad, axis) { let x = axis.x, y = axis.y, z = axis.z; let s, c, t, a00, a01, a02, a03, @@ -543,7 +527,7 @@ class mat4 { * @param rad - The rotation angle. * @return out. */ - public static rotateX(out, a, rad) { + public static rotateX (out, a, rad) { const s = Math.sin(rad), c = Math.cos(rad), a10 = a.m04, @@ -587,7 +571,7 @@ class mat4 { * @param rad - The rotation angle. * @return out. */ - public static rotateY(out, a, rad) { + public static rotateY (out, a, rad) { const s = Math.sin(rad), c = Math.cos(rad), a00 = a.m00, @@ -631,7 +615,7 @@ class mat4 { * @param rad - The rotation angle. * @return out. */ - public static rotateZ(out, a, rad) { + public static rotateZ (out, a, rad) { const s = Math.sin(rad), c = Math.cos(rad), a00 = a.m00, @@ -679,7 +663,7 @@ class mat4 { * @param v - The translation offset. * @return out. */ - public static fromTranslation(out, v) { + public static fromTranslation (out, v) { out.m00 = 1; out.m01 = 0; out.m02 = 0; @@ -710,7 +694,7 @@ class mat4 { * @param v - The scale vector. * @return out. */ - public static fromScaling(out, v) { + public static fromScaling (out, v) { out.m00 = v.x; out.m01 = 0; out.m02 = 0; @@ -742,7 +726,7 @@ class mat4 { * @param axis - The rotation axis. * @return out. */ - public static fromRotation(out, rad, axis) { + public static fromRotation (out, rad, axis) { let x = axis.x, y = axis.y, z = axis.z; let len = Math.sqrt(x * x + y * y + z * z); let s, c, t; @@ -791,7 +775,7 @@ class mat4 { * @param rad - The rotation angle. * @return out. */ - public static fromXRotation(out, rad) { + public static fromXRotation (out, rad) { const s = Math.sin(rad), c = Math.cos(rad); @@ -826,7 +810,7 @@ class mat4 { * @param rad - The rotation angle. * @return out. */ - public static fromYRotation(out, rad) { + public static fromYRotation (out, rad) { const s = Math.sin(rad), c = Math.cos(rad); @@ -861,7 +845,7 @@ class mat4 { * @param rad - The rotation angle. * @return out. */ - public static fromZRotation(out, rad) { + public static fromZRotation (out, rad) { const s = Math.sin(rad), c = Math.cos(rad); @@ -900,7 +884,7 @@ class mat4 { * @param v - Translation vector. * @return out. */ - public static fromRT(out, q, v) { + public static fromRT (out, q, v) { // Quaternion math const x = q.x, y = q.y, z = q.z, w = q.w; const x2 = x + x; @@ -946,7 +930,7 @@ class mat4 { * @param {mat4} mat - Matrix to be decomposed. * @return out. */ - public static getTranslation(out, mat) { + public static getTranslation (out, mat) { out.x = mat.m12; out.y = mat.m13; out.z = mat.m14; @@ -964,7 +948,7 @@ class mat4 { * @param {mat4} mat - Matrix to be decomposed. * @return out. */ - public static getScaling(out, mat) { + public static getScaling (out, mat) { const m11 = mat.m00, m12 = mat.m01, m13 = mat.m02, @@ -991,8 +975,8 @@ class mat4 { * @param mat - Matrix to be decomposed. * @return out. */ - public static getRotation(out, mat) { - // Algorithm taken from http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm + public static getRotation (out, mat) { + // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm const trace = mat.m00 + mat.m05 + mat.m10; let S = 0; @@ -1042,7 +1026,7 @@ class mat4 { * @param s - Scale vector. * @return out. */ - public static fromRTS(out, q, v, s) { + public static fromRTS (out, q, v, s) { // Quaternion math const x = q.x, y = q.y, z = q.z, w = q.w; const x2 = x + x; @@ -1083,7 +1067,8 @@ class mat4 { } /** - * Creates a matrix from a quaternion rotation, translation offset and scale vector, rotating and scaling around the given origin. + * Creates a matrix from a quaternion rotation, translation offset and scale vector, + * rotating and scaling around the given origin. * This is equivalent to (but much faster than): * * mat4.identity(dest); @@ -1102,7 +1087,7 @@ class mat4 { * @param o The origin vector around which to scale and rotate. * @return out. */ - public static fromRTSOrigin(out, q, v, s, o) { + public static fromRTSOrigin (out, q, v, s, o) { // Quaternion math const x = q.x, y = q.y, z = q.z, w = q.w; const x2 = x + x; @@ -1155,7 +1140,7 @@ class mat4 { * * @return out. */ - public static fromQuat(out, q) { + public static fromQuat (out, q) { const x = q.x, y = q.y, z = q.z, w = q.w; const x2 = x + x; const y2 = y + y; @@ -1206,7 +1191,7 @@ class mat4 { * @param far - Far bound of the frustum. * @return out. */ - public static frustum(out, left, right, bottom, top, near, far) { + public static frustum (out, left, right, bottom, top, near, far) { const rl = 1 / (right - left); const tb = 1 / (top - bottom); const nf = 1 / (near - far); @@ -1240,7 +1225,7 @@ class mat4 { * @param far - Far bound of the frustum. * @return out. */ - public static perspective(out, fovy, aspect, near, far) { + public static perspective (out, fovy, aspect, near, far) { const f = 1.0 / Math.tan(fovy / 2); const nf = 1 / (near - far); @@ -1274,7 +1259,7 @@ class mat4 { * @param far - Far bound of the frustum. * @return out. */ - public static perspectiveFromFieldOfView(out, fov, near, far) { + public static perspectiveFromFieldOfView (out, fov, near, far) { const upTan = Math.tan(fov.upDegrees * Math.PI / 180.0); const downTan = Math.tan(fov.downDegrees * Math.PI / 180.0); const leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0); @@ -1313,7 +1298,7 @@ class mat4 { * @param far - Far bound of the frustum. * @return out. */ - public static ortho(out, left, right, bottom, top, near, far) { + public static ortho (out, left, right, bottom, top, near, far) { const lr = 1 / (left - right); const bt = 1 / (bottom - top); const nf = 1 / (near - far); @@ -1346,7 +1331,7 @@ class mat4 { * @param up - Vector pointing up. * @return out */ - public static lookAt(out, eye, center, up) { + public static lookAt (out, eye, center, up) { let x0, x1, x2, y0, y1, y2, z0, z1, z2, len; const eyex = eye.x; const eyey = eye.y; @@ -1405,8 +1390,67 @@ class mat4 { * @param a - The matrix. * @return String representation of this matrix. */ - public static str(a) { - return `mat4(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03}, ${a.m04}, ${a.m05}, ${a.m06}, ${a.m07}, ${a.m08}, ${a.m09}, ${a.m10}, ${a.m11}, ${a.m12}, ${a.m13}, ${a.m14}, ${a.m15})`; + public static str (a) { + return `mat4(${a.m00}, ${a.m01}, ${a.m02}, ${a.m03}, ${a.m04}, ${a.m05}, ${a.m06}, ${a.m07}, ` + + `${a.m08}, ${a.m09}, ${a.m10}, ${a.m11}, ${a.m12}, ${a.m13}, ${a.m14}, ${a.m15})`; + } + + /** + * Calculates normal matrix (transpose inverse). + * + * @param out - Matrix to store result. + * @param a - A 4x4 matrix to derive the normal matrix from. + * + * @return out. + */ + public static normalMatrix (out, a) { + const a00 = a.m00, a01 = a.m01, a02 = a.m02, a03 = a.m03, + a10 = a.m04, a11 = a.m05, a12 = a.m06, a13 = a.m07, + a20 = a.m08, a21 = a.m09, a22 = a.m10, a23 = a.m11, + a30 = a.m12, a31 = a.m13, a32 = a.m14, a33 = a.m15; + + const b00 = a00 * a11 - a01 * a10; + const b01 = a00 * a12 - a02 * a10; + const b02 = a00 * a13 - a03 * a10; + const b03 = a01 * a12 - a02 * a11; + const b04 = a01 * a13 - a03 * a11; + const b05 = a02 * a13 - a03 * a12; + const b06 = a20 * a31 - a21 * a30; + const b07 = a20 * a32 - a22 * a30; + const b08 = a20 * a33 - a23 * a30; + const b09 = a21 * a32 - a22 * a31; + const b10 = a21 * a33 - a23 * a31; + const b11 = a22 * a33 - a23 * a32; + + // Calculate the determinant + let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; + + if (!det) { + return null; + } + det = 1.0 / det; + + out.m00 = (a11 * b11 - a12 * b10 + a13 * b09) * det; + out.m01 = (a12 * b08 - a10 * b11 - a13 * b07) * det; + out.m02 = (a10 * b10 - a11 * b08 + a13 * b06) * det; + out.m03 = 0; + + out.m04 = (a02 * b10 - a01 * b11 - a03 * b09) * det; + out.m05 = (a00 * b11 - a02 * b08 + a03 * b07) * det; + out.m06 = (a01 * b08 - a00 * b10 - a03 * b06) * det; + out.m07 = 0; + + out.m08 = (a31 * b05 - a32 * b04 + a33 * b03) * det; + out.m09 = (a32 * b02 - a30 * b05 - a33 * b01) * det; + out.m10 = (a30 * b04 - a31 * b02 + a33 * b00) * det; + out.m11 = 0; + + out.m12 = 0; + out.m13 = 0; + out.m14 = 0; + out.m15 = 1; + + return out; } /** @@ -1416,7 +1460,7 @@ class mat4 { * @param m - The matrix. * @return out. */ - public static array(out, m) { + public static array (out, m) { out[0] = m.m00; out[1] = m.m01; out[2] = m.m02; @@ -1443,8 +1487,11 @@ class mat4 { * @param a - Matrix to calculate Frobenius norm of. * @return - The frobenius norm. */ - public static frob(a) { - return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2) + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + Math.pow(a.m06, 2) + Math.pow(a.m07, 2) + Math.pow(a.m08, 2) + Math.pow(a.m09, 2) + Math.pow(a.m10, 2) + Math.pow(a.m11, 2) + Math.pow(a.m12, 2) + Math.pow(a.m13, 2) + Math.pow(a.m14, 2) + Math.pow(a.m15, 2))); + public static frob (a) { + return (Math.sqrt(Math.pow(a.m00, 2) + Math.pow(a.m01, 2) + Math.pow(a.m02, 2) + Math.pow(a.m03, 2) + + Math.pow(a.m04, 2) + Math.pow(a.m05, 2) + Math.pow(a.m06, 2) + Math.pow(a.m07, 2) + Math.pow(a.m08, 2) + + Math.pow(a.m09, 2) + Math.pow(a.m10, 2) + Math.pow(a.m11, 2) + Math.pow(a.m12, 2) + Math.pow(a.m13, 2) + + Math.pow(a.m14, 2) + Math.pow(a.m15, 2))); } /** @@ -1455,7 +1502,7 @@ class mat4 { * @param b - The second operand. * @return out. */ - public static add(out, a, b) { + public static add (out, a, b) { out.m00 = a.m00 + b.m00; out.m01 = a.m01 + b.m01; out.m02 = a.m02 + b.m02; @@ -1483,7 +1530,7 @@ class mat4 { * @param b - The second operand. * @return out. */ - public static subtract(out, a, b) { + public static subtract (out, a, b) { out.m00 = a.m00 - b.m00; out.m01 = a.m01 - b.m01; out.m02 = a.m02 - b.m02; @@ -1506,7 +1553,7 @@ class mat4 { /** * Alias of {@link mat4.subtract}. */ - public static sub(out, a, b) { + public static sub (out, a, b) { return mat4.subtract(out, a, b); } @@ -1518,7 +1565,7 @@ class mat4 { * @param b - The scale number. * @return out. */ - public static multiplyScalar(out, a, b) { + public static multiplyScalar (out, a, b) { out.m00 = a.m00 * b; out.m01 = a.m01 * b; out.m02 = a.m02 * b; @@ -1547,7 +1594,7 @@ class mat4 { * @param scale - The scale number. * @return out. */ - public static multiplyScalarAndAdd(out, a, b, scale) { + public static multiplyScalarAndAdd (out, a, b, scale) { out.m00 = a.m00 + (b.m00 * scale); out.m01 = a.m01 + (b.m01 * scale); out.m02 = a.m02 + (b.m02 * scale); @@ -1574,7 +1621,7 @@ class mat4 { * @param b - The second matrix. * @return True if the matrices are equal, false otherwise. */ - public static exactEquals(a, b) { + public static exactEquals (a, b) { return a.m00 === b.m00 && a.m01 === b.m01 && a.m02 === b.m02 && a.m03 === b.m03 && a.m04 === b.m04 && a.m05 === b.m05 && a.m06 === b.m06 && a.m07 === b.m07 && a.m08 === b.m08 && a.m09 === b.m09 && a.m10 === b.m10 && a.m11 === b.m11 && @@ -1588,7 +1635,7 @@ class mat4 { * @param b - The second matrix. * @return True if the matrices are equal, false otherwise. */ - public static equals(a, b) { + public static equals (a, b) { const a0 = a.m00, a1 = a.m01, a2 = a.m02, a3 = a.m03, a4 = a.m04, a5 = a.m05, a6 = a.m06, a7 = a.m07, a8 = a.m08, a9 = a.m09, a10 = a.m10, a11 = a.m11, @@ -1618,6 +1665,24 @@ class mat4 { Math.abs(a15 - b15) <= EPSILON * Math.max(1.0, Math.abs(a15), Math.abs(b15)) ); } + + public m00: number = 1; + public m01: number = 0; + public m02: number = 0; + public m03: number = 0; + public m04: number = 0; + public m05: number = 1; + public m06: number = 0; + public m07: number = 0; + public m08: number = 0; + public m09: number = 0; + public m10: number = 1; + public m11: number = 0; + public m12: number = 0; + public m13: number = 0; + public m14: number = 0; + public m15: number = 1; + /** * Creates a matrix, with elements specified separately. * @@ -1638,7 +1703,7 @@ class mat4 { * @param m14 - Value assigned to element at column 3 row 2. * @param m15 - Value assigned to element at column 3 row 3. */ - constructor( + constructor ( m00 = 1, m01 = 0, m02 = 0, m03 = 0, m04 = 0, m05 = 1, m06 = 0, m07 = 0, m08 = 0, m09 = 0, m10 = 1, m11 = 0, @@ -1647,97 +1712,97 @@ class mat4 { /** * The element at column 0 row 0. * @type {number} - * */ + */ this.m00 = m00; /** * The element at column 0 row 1. * @type {number} - * */ + */ this.m01 = m01; /** * The element at column 0 row 2. * @type {number} - * */ + */ this.m02 = m02; /** * The element at column 0 row 3. * @type {number} - * */ + */ this.m03 = m03; /** * The element at column 1 row 0. * @type {number} - * */ + */ this.m04 = m04; /** * The element at column 1 row 1. * @type {number} - * */ + */ this.m05 = m05; /** * The element at column 1 row 2. * @type {number} - * */ + */ this.m06 = m06; /** * The element at column 1 row 3. * @type {number} - * */ + */ this.m07 = m07; /** * The element at column 2 row 0. * @type {number} - * */ + */ this.m08 = m08; /** * The element at column 2 row 1. * @type {number} - * */ + */ this.m09 = m09; /** * The element at column 2 row 2. * @type {number} - * */ + */ this.m10 = m10; /** * The element at column 2 row 3. * @type {number} - * */ + */ this.m11 = m11; /** * The element at column 3 row 0. * @type {number} - * */ + */ this.m12 = m12; /** * The element at column 3 row 1. * @type {number} - * */ + */ this.m13 = m13; /** * The element at column 3 row 2. * @type {number} - * */ + */ this.m14 = m14; /** * The element at column 3 row 3. * @type {number} - * */ + */ this.m15 = m15; } } diff --git a/cocos/renderer/core/effect.ts b/cocos/renderer/core/effect.ts index 635142d11c..1300a0ba6d 100644 --- a/cocos/renderer/core/effect.ts +++ b/cocos/renderer/core/effect.ts @@ -1,68 +1,15 @@ // Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. -import { EffectAsset, IShaderInfo } from '../../3d/assets/effect-asset'; -import { CCObject } from '../../core/data'; -import { Color, Mat4, Vec2, Vec3, Vec4 } from '../../core/value-types'; -import { GFXType } from '../../gfx/define'; +import { EffectAsset } from '../../3d/assets/effect-asset'; import { RenderPassStage } from '../../pipeline/define'; -import Texture2D from '../gfx/texture-2d'; -import TextureCube from '../gfx/texture-cube'; import { IPassInfoFull, Pass } from './pass'; -const _typeMap = { - [GFXType.INT]: 'Number', - [GFXType.INT2]: Vec2, - [GFXType.INT3]: Vec3, - [GFXType.INT4]: Vec4, - [GFXType.FLOAT]: 'Number', - [GFXType.FLOAT2]: Vec2, - [GFXType.FLOAT3]: Vec3, - [GFXType.FLOAT4]: Vec4, - [GFXType.COLOR4]: Color, - [GFXType.MAT4]: Mat4, - [GFXType.SAMPLER2D]: Texture2D, - [GFXType.SAMPLER_CUBE]: TextureCube, - boolean: 'Boolean', - default: CCObject, - number: 'Number', -}; -const getInstanceType = (t: number | string) => _typeMap[t] || _typeMap.default; - -export interface IPropertyMap { [name: string]: { type: number, value: any }; } export interface IDefineMap { [name: string]: number | boolean; } export interface IEffectInfo { techIdx?: number; defines?: IDefineMap; } -type DefaultValue = number | boolean | number[] | string; -const _ctorMap = { - Boolean: (v?: DefaultValue) => v || false, - Number: (v?: DefaultValue) => v || 0, - [Vec2]: (v?: DefaultValue) => Array.isArray(v) ? new Vec2(v[0], v[1]) : new Vec2(), - [Vec3]: (v?: DefaultValue) => Array.isArray(v) ? new Vec3(v[0], v[1], v[2]) : new Vec3(), - [Vec4]: (v?: DefaultValue) => Array.isArray(v) ? new Vec4(v[0], v[1], v[2], v[3]) : new Vec4(), - [Color]: (v?: DefaultValue) => Array.isArray(v) ? new Color(v[0] * 255, v[1] * 255, v[2] * 255, - (v[3] || 1) * 255) : new Color(), - [Mat4]: (v?: DefaultValue) => Array.isArray(v) ? new Mat4( - v[0], v[1], v[2], v[3], - v[4], v[5], v[6], v[7], - v[8], v[9], v[10], v[11], - v[12], v[13], v[14], v[15], - ) : new Mat4(), - [Texture2D]: () => null, - [TextureCube]: () => null, - [CCObject]: () => null, -}; -const getInstanceCtor = (t: number | string) => _ctorMap[getInstanceType(t)]; -const getProgram = (name: string): IShaderInfo => cc.game._programLib.getTemplate(name); -const getDefines = (name: string, prog: IShaderInfo) => { - const block = prog.blocks.find((b) => b.members.find((u) => u.name === name) !== undefined); - if (block) { return block.defines; } - const s = prog.samplers.find((u) => u.name === name); - if (s) { return s.defines; } -}; - export class Effect { public static getPassInfos (effect: EffectAsset, techIdx: number) { return effect.techniques[techIdx].passes; @@ -76,51 +23,18 @@ export class Effect { const passes: Pass[] = new Array(passNum); for (let k = 0; k < passNum; ++k) { const passInfo = tech.passes[k] as IPassInfoFull; - const prog = getProgram(passInfo.program); + const prog = cc.game._programLib.getTemplate(passInfo.program); passInfo.shader = cc.game._programLib.getGFXShader(passInfo.program, defines || {}); passInfo.renderPass = cc.director.root.pipeline.getRenderPass(passInfo.stage || RenderPassStage.DEFAULT); passInfo.globals = cc.director.root.pipeline.globalUBO; - passInfo.blocks = prog.blocks; - passInfo.samplers = prog.samplers; + passInfo.blocks = prog!.blocks; + passInfo.samplers = prog!.samplers; const pass = new Pass(cc.game._gfxDevice); pass.initialize(passInfo); passes[k] = pass; } return passes; } - - public static parseForInspector (effect: EffectAsset) { - const res: object[] = []; - for (const tech of effect.techniques) { - const props: Array> = []; - const defines: Record = {}; - for (const pass of tech.passes) { - const prog = getProgram(pass.program); - prog.defines.forEach((define) => { - defines[define.name] = { - defines: define.defines, - instanceType: getInstanceType(define.type), - value: getInstanceCtor(define.type)(), - }; - }); - const list: Record = {}; - props.push(list); - if (!pass.properties) { continue; } - for (const p of Object.keys(pass.properties)) { - const prop = pass.properties[p]; - const defs = getDefines(p, prog); - if (!defs) { continue; } - list[p] = { - defines: defs, - instanceType: getInstanceType(prop.type), - value: getInstanceCtor(prop.type)(), - }; - } - } - res.push({ props, defines }); - } - return res; - } } cc.Effect = Effect; diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index d488f981bf..07933d0658 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -4,7 +4,8 @@ import { IBlockInfo, IPassInfo, ISamplerInfo } from '../../3d/assets/effect-asse import { color4, mat2, mat3, mat4, vec2, vec3, vec4 } from '../../core/vmath'; import { GFXBindingLayout, IGFXBinding } from '../../gfx/binding-layout'; import { GFXBuffer } from '../../gfx/buffer'; -import { GFXBindingType, GFXBufferUsageBit, GFXMemoryUsageBit, GFXPrimitiveMode, GFXType } from '../../gfx/define'; +import { GFXBindingType, GFXBufferUsageBit, GFXDynamicState, + GFXMemoryUsageBit, GFXPrimitiveMode, GFXType } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; import { GFXPipelineLayout } from '../../gfx/pipeline-layout'; import { GFXBlendState, GFXBlendTarget, GFXDepthStencilState, @@ -105,6 +106,7 @@ export class Pass { protected _bs: GFXBlendState = new GFXBlendState(); protected _dss: GFXDepthStencilState = new GFXDepthStencilState(); protected _rs: GFXRasterizerState = new GFXRasterizerState(); + protected _dynamics: GFXDynamicState[] = []; protected _handleMap: Record = {}; protected _blocks: IBlock[] = []; // external references @@ -294,9 +296,10 @@ export class Pass { return res; } - protected _fillinPipelineInfo (info: Partial) { + protected _fillinPipelineInfo (info: PassOverrides) { if (info.primitive !== undefined) { this._primitive = info.primitive; } if (info.stage !== undefined) { this._stage = info.stage; } + if (info.dynamics !== undefined) { this._dynamics = info.dynamics; } const bs = this._bs; if (info.blendState) { @@ -333,7 +336,7 @@ function serializeBlendState (bs: GFXBlendState) { } function serializeRasterizerState (rs: GFXRasterizerState) { - const res = `,rs,${rs.cullMode},${rs.depthBias},${rs.depthBiasFactor},${rs.isFrontFaceCCW}`; + const res = `,rs,${rs.cullMode},${rs.depthBias},${rs.isFrontFaceCCW}`; return res; } diff --git a/cocos/renderer/models/particle-batch-model.ts b/cocos/renderer/models/particle-batch-model.ts index b6f18006cb..f9a0b6aedd 100644 --- a/cocos/renderer/models/particle-batch-model.ts +++ b/cocos/renderer/models/particle-batch-model.ts @@ -2,10 +2,13 @@ import gfx from '../gfx'; -import { Model } from '../scene/model'; -import { GFXBufferUsageBit, GFXMemoryUsageBit, GFXFormat, GFXAttributeName, GFXPrimitiveMode, GFXFormatInfos } from '../../gfx/define'; +import { GFX_DRAW_INFO_SIZE, GFXBuffer, IGFXIndirectBuffer } from '../../gfx/buffer'; +import { GFXAttributeName, GFXBufferUsageBit, GFXFormat, GFXFormatInfos, + GFXMemoryUsageBit, GFXPrimitiveMode } from '../../gfx/define'; import { IGFXInputAttribute } from '../../gfx/input-assembler'; -import { GFXBuffer, GFX_DRAW_INFO_SIZE, IGFXIndirectBuffer } from '../../gfx/buffer'; +import { Node } from '../../scene-graph/node'; +import { Model } from '../scene/model'; +import { RenderScene } from '../scene/render-scene'; export default class ParticleBatchModel extends Model { @@ -19,8 +22,8 @@ export default class ParticleBatchModel extends Model { private _iaInfo: IGFXIndirectBuffer; private _iaInfoBuffer: GFXBuffer; - constructor () { - super(); + constructor (scene: RenderScene, node: Node) { + super(scene, node); this._type = 'particle-batch'; this._capacity = 0; @@ -38,8 +41,8 @@ export default class ParticleBatchModel extends Model { firstIndex: 0, vertexOffset: 0, instanceCount: 0, - firstInstance: 0 - }] + firstInstance: 0, + }], }; this._iaInfoBuffer = this._device.createBuffer({ usage: GFXBufferUsageBit.INDIRECT, diff --git a/cocos/renderer/scene/model.ts b/cocos/renderer/scene/model.ts index d272d143d5..8ff0eadc50 100644 --- a/cocos/renderer/scene/model.ts +++ b/cocos/renderer/scene/model.ts @@ -6,7 +6,6 @@ import { RecyclePool } from '../../3d/memop'; import { Vec3 } from '../../core/value-types'; import { mat4 } from '../../core/vmath'; import { GFXBuffer } from '../../gfx/buffer'; -import { GFXCommandBuffer } from '../../gfx/command-buffer'; import { GFXBufferUsageBit, GFXMemoryUsageBit } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; import { GFXPipelineState } from '../../gfx/pipeline-state'; @@ -27,55 +26,42 @@ const _subMeshPool = new RecyclePool(() => { */ export class Model { + protected _type: string = 'default'; protected _device: GFXDevice; - protected _type: string; - private _scene: RenderScene | null; + private _scene: RenderScene; + private _node: Node; private _id: number; - private _isEnable: boolean; - private _node: Node | null; - private _viewID: number; - private _cameraID: number; - private _userKey: number; - private _worldBounds: aabb; - private _modelBounds: aabb; - private _cmdBuffers: GFXCommandBuffer[]; - private _uboLocal: UBOLocal; - private _localUBO: GFXBuffer | null; - private _subModels: SubModel[]; + private _isEnable: boolean = true; + private _viewID: number = -1; + private _cameraID: number = -1; + private _userKey: number = -1; + private _worldBounds: aabb | null = null; + private _modelBounds: aabb | null = null; + private _subModels: SubModel[] = []; private _matPSORecord: Map; private _matRefCount: Map; + private _uboLocal: UBOLocal; + private _localUBO: GFXBuffer; + /** * Setup a default empty model */ - constructor () { - this._scene = null; - this._id = 0; - - this._type = 'default'; - this._isEnable = true; - this._node = null; - this._viewID = -1; - this._cameraID = -1; - this._userKey = -1; - this._cmdBuffers = new Array(); - this._uboLocal = new UBOLocal(); - this._localUBO = null; + constructor (scene: RenderScene, node: Node) { this._device = cc.director.root.device; - this._subModels = new Array(); + this._scene = scene; + this._node = node; + this._id = this._scene.generateModelId(); + this._matPSORecord = new Map(); this._matRefCount = new Map(); - } - - public initialize () { + this._uboLocal = new UBOLocal(); this._localUBO = this._device.createBuffer({ usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, memUsage: GFXMemoryUsageBit.HOST, size: UBOLocal.SIZE, stride: UBOLocal.SIZE, }); - if (this._localUBO) { - this._localUBO.update(this._uboLocal.view); - } + this._localUBO.update(this._uboLocal.view); } public destroy () { @@ -85,53 +71,49 @@ export class Model { } } - set scene (scene: RenderScene | null) { + set scene (scene: RenderScene) { this._scene = scene; - - if (this._scene) { - this._id = this._scene.generateModelId(); - } + this._id = this._scene.generateModelId(); } - get scene (): RenderScene | null { + get scene () { return this._scene; } - get id (): number { + get id () { return this._id; } - get subModelNum (): number { + get subModelNum () { return this._subModels.length; } - public getSubModel (idx: number): SubModel { + public getSubModel (idx: number) { return this._subModels[idx]; } public _updateTransform () { - if (this._node == null) { - return; - } - if (!this._node._hasChanged || !this._worldBounds) { + if (!this._node.hasChanged || !this._modelBounds) { return; } this._node.updateWorldTransformFull(); - this._modelBounds.transform(this._node._mat, this._node._pos, - this._node._rot, this._node._scale, this._worldBounds); + + this._modelBounds.transform( + // @ts-ignore + this._node._mat, this._node._pos, this._node._rot, this._node._scale, + this._worldBounds); } public updateUBOs () { - if (this._node == null) { - return; - } + // @ts-ignore mat4.array(_temp_floatx16, this._node._mat); - this._node._mat.invert(_temp_mat4); this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_OFFSET); + // @ts-ignore + mat4.normalMatrix(_temp_mat4, this._node._mat); mat4.array(_temp_floatx16, _temp_mat4); this._uboLocal.view.set(_temp_floatx16, UBOLocal.MAT_WORLD_IT_OFFSET); - this._localUBO!.update(this._uboLocal.view); + this._localUBO.update(this._uboLocal.view); for (const mat of this._matPSORecord.keys()) { for (const pass of mat.passes) { @@ -158,15 +140,15 @@ export class Model { this._isEnable = isEnable; } - public isEnable (): boolean { + public isEnable () { return this._isEnable; } /** * Get the hosting node of this camera - * @returns {Node} the hosting node + * @returns the hosting node */ - get node (): Node | null { + get node () { return this._node; } @@ -174,18 +156,18 @@ export class Model { * Set the hosting node of this model * @param {Node} node the hosting node */ - set node (node: Node | null) { + set node (node: Node) { this._node = node; } - get worldBounds (): aabb { + get worldBounds () { return this._worldBounds; } - get modelBounds (): aabb { + get modelBounds () { return this._modelBounds; } - get viewID (): number { + get viewID () { return this._viewID; } @@ -257,11 +239,11 @@ export class Model { this._userKey = key; } - get uboLocal (): UBOLocal { + get uboLocal () { return this._uboLocal; } - get localUBO (): GFXBuffer { - return this._localUBO!; + get localUBO () { + return this._localUBO; } } diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index 33d2265180..5da41535f7 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -2,6 +2,7 @@ import { IBArray } from '../../3d/assets/mesh'; import { intersect, ray, triangle } from '../../3d/geom-utils'; import { RecyclePool } from '../../3d/memop'; import { Root } from '../../core/root'; +import { Mat4, Vec3 } from '../../core/value-types'; import { mat4, vec3 } from '../../core/vmath'; import { GFXPrimitiveMode } from '../../gfx/define'; import { Layers } from '../../scene-graph/layers'; @@ -121,10 +122,8 @@ export class RenderScene { this._pointLights = []; } - public createModel (clazz: new () => T): T { - const model = new clazz(); - model.initialize(); - model.scene = this; + public createModel (clazz: new (s: RenderScene, n: Node) => T, node: Node): T { + const model = new clazz(this, node); this._models.push(model); return model; } @@ -153,7 +152,7 @@ export class RenderScene { * Cast a ray into the scene, record all the intersected models in the result array * @param worldRay - the testing ray * @param mask - the layer mask to filter the models - * @return the results array + * @returns the results array */ public raycast (worldRay: ray, mask: number = Layers.RaycastMask): IRaycastResult[] { pool.reset(); @@ -167,15 +166,17 @@ export class RenderScene { // broadphase distance = intersect.ray_aabb(modelRay, m.modelBounds); if (distance <= 0) { continue; } - const subModel = m.subMeshData; - if (!subModel || !subModel.geometricInfo) { continue; } - const { positions: vb, indices: ib, doubleSided: sides } = subModel.geometricInfo; - narrowphase(vb, ib, subModel.primitiveMode, sides); - if (distance < Infinity) { - const r = pool.add(); - r.node = node; - r.distance = distance * vec3.magnitude(vec3.mul(v3, modelRay.d, node._scale)); - results[pool.length - 1] = r; + for (let i = 0; i < m.subModelNum; ++i) { + const subModel = m.getSubModel(i).subMeshData; + if (!subModel || !subModel.geometricInfo) { continue; } + const { positions: vb, indices: ib, doubleSided: sides } = subModel.geometricInfo; + narrowphase(vb, ib, subModel.primitiveMode, sides); + if (distance < Infinity) { + const r = pool.add(); + r.node = node; + r.distance = distance * vec3.magnitude(vec3.mul(v3, modelRay.d, node._scale)); + results[pool.length - 1] = r; + } } } results.length = pool.length; @@ -184,8 +185,8 @@ export class RenderScene { } const modelRay = ray.create(); -const v3 = vec3.create(); -const m4 = mat4.create(); +const v3 = new Vec3(); +const m4 = new Mat4(); let distance = Infinity; const tri = triangle.create(); const pool = new RecyclePool(() => { diff --git a/cocos/renderer/scene/submodel.ts b/cocos/renderer/scene/submodel.ts index 580ac011b2..00962ea902 100644 --- a/cocos/renderer/scene/submodel.ts +++ b/cocos/renderer/scene/submodel.ts @@ -67,7 +67,7 @@ export class SubModel { } for (let i = 0; i < this._material!.passes.length; i++) { if (this._material!.passes[i].primitive !== this._subMeshObject!.primitiveMode) { - cc.error('the model(%d)\'s primitive type doesn\'t match its pass\'s'); + cc.error('the model(%d)\'s primitive type doesn\'t match its pass\'s', i); } this.recordCommandBuffer(i); } diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts index 876d38f52c..304a99aa71 100644 --- a/cocos/scene-graph/node.ts +++ b/cocos/scene-graph/node.ts @@ -271,6 +271,10 @@ class Node extends EventTarget(BaseNode) { return this._euler; } + get hasChanged () { + return this._hasChanged; + } + set layer (l) { this._layer = l; }