diff --git a/cocos/core/3d/framework/model-component.ts b/cocos/core/3d/framework/model-component.ts index 31e61f8416..3068e36992 100644 --- a/cocos/core/3d/framework/model-component.ts +++ b/cocos/core/3d/framework/model-component.ts @@ -118,7 +118,7 @@ export class ModelComponent extends RenderableComponent { @property set enableDynamicBatching (enable: boolean) { this._enableDynamicBatching = enable; - if (this._mesh && !this._mesh.hasFlatBuffers) { + if (this._mesh) { if (enable) { this._mesh.createFlatBuffers(); } else { diff --git a/cocos/core/animation/skeletal-animation-clip.ts b/cocos/core/animation/skeletal-animation-clip.ts index f318791a41..a09272ae02 100644 --- a/cocos/core/animation/skeletal-animation-clip.ts +++ b/cocos/core/animation/skeletal-animation-clip.ts @@ -27,6 +27,7 @@ * @category animation */ +import { SkinningModelComponent } from '../3d/framework/skinning-model-component'; import { ccclass } from '../data/class-decorator'; import { Mat4 } from '../math'; import { INode } from '../utils/interfaces'; @@ -44,6 +45,7 @@ type ConvertedData = Record { + if (!props._worldMatrix) { + const { position, rotation, scale } = props; + // fixed step pre-sample + this._convertToUniformSample(position); + this._convertToUniformSample(rotation); + this._convertToUniformSample(scale); + // transform to world space + this._convertToWorldSpace(path, props); + } + return props._worldMatrix; + }, + }); + } const values: number[] = new Array(Math.ceil(this.sample * this._duration / this.speed) + 1); for (let i = 0; i < values.length; i++) { values[i] = i; } - // sort the keys to make sure parent bone always comes first - for (const path of Object.keys(this.convertedData).sort()) { - const nodeData = this.convertedData[path]; - if (!nodeData.props) { continue; } - const { position, rotation, scale } = nodeData.props; - // fixed step pre-sample - this._convertToUniformSample(position, values); - this._convertToUniformSample(rotation, values); - this._convertToUniformSample(scale, values); - // transform to world space - this._convertToWorldSpace(path, nodeData.props); - } // create frameID animation const curves: ITargetCurveData[] = [{ modifiers: [ @@ -97,23 +107,45 @@ export class SkeletalAnimationClip extends AnimationClip { data: { keys: 0, values, interpolate: false }, }]; this.curves = curves; + this.originalKeys = this._keys; this._keys = [values.map((_, i) => i * this.speed / this.sample)]; this._duration = this._keys[0][values.length - 1]; this._converted = true; + if (!root || !root.scene) { return; } + // try eval for the existing skinning models + const comps = root.getComponentsInChildren(SkinningModelComponent); + for (let i = 0; i < comps.length; ++i) { + const comp = comps[i] as SkinningModelComponent; + if (comp.skinningRoot === root && comp.skeleton) { + for (const path of comp.skeleton.joints) { + const data = convertedData[path]; + // tslint:disable-next-line: no-unused-expression + data && data.props && data.props.worldMatrix; + } + } + } } - private _convertToUniformSample (curve: IPropertyCurveData, counts: number[]) { - const keys = this._keys[curve.keys]; curve.keys = 0; - curve.values = counts.map((_, i) => { - if (!keys || keys.length === 1) { return curve.values[0].clone(); } // never forget to clone - let time = i * this.speed / this.sample; - let idx = keys.findIndex((k) => k > time); - if (idx < 0) { idx = keys.length - 1; time = keys[idx]; } - else if (idx === 0) { idx = 1; } - const from = curve.values[idx - 1].clone(); - from.lerp(curve.values[idx], (time - keys[idx - 1]) / (keys[idx] - keys[idx - 1])); - return from; - }); + private _convertToUniformSample (curve: IPropertyCurveData) { + const keys = this.originalKeys[curve.keys]; curve.keys = 0; + const len = this._keys[0].length; + const values: any[] = []; + if (!keys || keys.length === 1) { + for (let i = 0; i < len; i++) { + values[i] = curve.values[0].clone(); // never forget to clone + } + } else { + for (let i = 0, idx = 0; i < len; i++) { + let time = i * this.speed / this.sample; + while (keys[idx] <= time) { idx++; } + if (idx > keys.length - 1) { idx = keys.length - 1; time = keys[idx]; } + else if (idx === 0) { idx = 1; } + const from = curve.values[idx - 1].clone(); + from.lerp(curve.values[idx], (time - keys[idx - 1]) / (keys[idx] - keys[idx - 1])); + values[i] = from; + } + } + curve.values = values; } private _convertToWorldSpace (path: string, props: IObjectCurveData) { @@ -127,7 +159,6 @@ export class SkeletalAnimationClip extends AnimationClip { const name = path.substring(0, idx); const data = this.convertedData[name]; if (!data || !data.props) { console.warn('no data for parent bone?'); return; } - // parent is already in world space pMatrix = data.props.worldMatrix.values; } // all props should have the same length now @@ -140,7 +171,7 @@ export class SkeletalAnimationClip extends AnimationClip { if (pMatrix) { Mat4.multiply(m, pMatrix[i], m); } } Object.keys(props).forEach((k) => delete props[k]); - props.worldMatrix = { keys: 0, interpolate: false, values: matrix }; + props._worldMatrix = { keys: 0, interpolate: false, values: matrix }; } } diff --git a/cocos/core/math/color.ts b/cocos/core/math/color.ts index 1e188b5053..f35c96b513 100644 --- a/cocos/core/math/color.ts +++ b/cocos/core/math/color.ts @@ -179,7 +179,7 @@ export class Color extends ValueType { * @zh 颜色转数组 * @param ofs 数组起始偏移量 */ - public static toArray (out: IWritableArrayLike, a: Out, ofs = 0) { + public static toArray > (out: Out, a: IColorLike, ofs = 0) { const scale = (a instanceof Color || a.a > 1) ? 1 / 255 : 1; out[ofs + 0] = a.r * scale; out[ofs + 1] = a.g * scale; diff --git a/cocos/core/math/mat3.ts b/cocos/core/math/mat3.ts index 08aac35022..dc4a7513e6 100644 --- a/cocos/core/math/mat3.ts +++ b/cocos/core/math/mat3.ts @@ -481,7 +481,7 @@ export class Mat3 extends ValueType { * @zh 矩阵转数组 * @param ofs 数组内的起始偏移量 */ - public static toArray (out: IWritableArrayLike, m: Out, ofs = 0) { + public static toArray > (out: Out, m: IMat3Like, ofs = 0) { out[ofs + 0] = m.m00; out[ofs + 1] = m.m01; out[ofs + 2] = m.m02; diff --git a/cocos/core/math/mat4.ts b/cocos/core/math/mat4.ts index 2038ec7643..3b9dc05f82 100644 --- a/cocos/core/math/mat4.ts +++ b/cocos/core/math/mat4.ts @@ -1164,7 +1164,7 @@ export class Mat4 extends ValueType { * @zh 矩阵转数组 * @param ofs 数组内的起始偏移量 */ - public static toArray (out: IWritableArrayLike, m: Out, ofs = 0) { + public static toArray > (out: Out, m: IMat4Like, ofs = 0) { out[ofs + 0] = m.m00; out[ofs + 1] = m.m01; out[ofs + 2] = m.m02; diff --git a/cocos/core/math/quat.ts b/cocos/core/math/quat.ts index 9fbb97b43a..e75ceb7d25 100644 --- a/cocos/core/math/quat.ts +++ b/cocos/core/math/quat.ts @@ -577,7 +577,7 @@ export class Quat extends ValueType { * @zh 四元数转数组 * @param ofs 数组内的起始偏移量 */ - public static toArray (out: IWritableArrayLike, q: Out, ofs = 0) { + public static toArray > (out: Out, q: IQuatLike, ofs = 0) { out[ofs + 0] = q.x; out[ofs + 1] = q.y; out[ofs + 2] = q.z; diff --git a/cocos/core/math/vec2.ts b/cocos/core/math/vec2.ts index 458d21b6c1..7cc8b75e2b 100644 --- a/cocos/core/math/vec2.ts +++ b/cocos/core/math/vec2.ts @@ -337,7 +337,7 @@ export class Vec2 extends ValueType { * @zh 向量转数组 * @param ofs 数组起始偏移量 */ - public static toArray (out: IWritableArrayLike, v: Out, ofs = 0) { + public static toArray > (out: Out, v: IVec2Like, ofs = 0) { out[ofs + 0] = v.x; out[ofs + 1] = v.y; return out; diff --git a/cocos/core/math/vec3.ts b/cocos/core/math/vec3.ts index cfbf0bf741..6b1219cf3f 100644 --- a/cocos/core/math/vec3.ts +++ b/cocos/core/math/vec3.ts @@ -529,7 +529,7 @@ export class Vec3 extends ValueType { * @zh 向量转数组 * @param ofs 数组起始偏移量 */ - public static toArray (out: IWritableArrayLike, v: Out, ofs = 0) { + public static toArray > (out: Out, v: IVec3Like, ofs = 0) { out[ofs + 0] = v.x; out[ofs + 1] = v.y; out[ofs + 2] = v.z; diff --git a/cocos/core/math/vec4.ts b/cocos/core/math/vec4.ts index d575cea326..18e7e788f3 100644 --- a/cocos/core/math/vec4.ts +++ b/cocos/core/math/vec4.ts @@ -415,7 +415,7 @@ export class Vec4 extends ValueType { * @zh 向量转数组 * @param ofs 数组起始偏移量 */ - public static toArray (out: IWritableArrayLike, v: Out, ofs = 0) { + public static toArray > (out: Out, v: IVec4Like, ofs = 0) { out[ofs + 0] = v.x; out[ofs + 1] = v.y; out[ofs + 2] = v.z; diff --git a/cocos/core/renderer/core/pass.ts b/cocos/core/renderer/core/pass.ts index 0c3f4f39dd..f4722b2a74 100644 --- a/cocos/core/renderer/core/pass.ts +++ b/cocos/core/renderer/core/pass.ts @@ -43,12 +43,12 @@ import { GFXSampler } from '../../gfx/sampler'; import { GFXShader } from '../../gfx/shader'; import { GFXTextureView } from '../../gfx/texture-view'; import { Mat3, Mat4, Vec2, Vec3, Vec4 } from '../../math'; +import { BatchedBuffer } from '../../pipeline/batched-buffer'; import { isBuiltinBinding, RenderPassStage, RenderPriority } from '../../pipeline/define'; import { getPhaseID } from '../../pipeline/pass-phase'; import { Root } from '../../root'; import { programLib } from './program-lib'; import { samplerLib } from './sampler-lib'; -import { BatchedBuffer } from '../../pipeline/batched-buffer'; export interface IDefineMap { [name: string]: number | boolean | string; } export interface IPassInfoFull extends IPassInfo { diff --git a/cocos/core/renderer/index.ts b/cocos/core/renderer/index.ts index 67400ff5f4..8898abbc9d 100644 --- a/cocos/core/renderer/index.ts +++ b/cocos/core/renderer/index.ts @@ -6,7 +6,7 @@ export { addStage }; export * from './core/texture-buffer-pool'; export { RenderQueue, PassStage } from './core/constants'; -export { Pass, IDefineMap } from './core/pass'; +export { Pass } from './core/pass'; export { programLib } from './core/program-lib'; import { samplerLib } from './core/sampler-lib'; cc.samplerLib = samplerLib;