lazy eval memory heavy animation data conversions (#5382)

* fix interface

* lazy eval memory heavy animation data conversions
This commit is contained in:
Yun Hsiao Wu
2019-09-12 18:35:29 +08:00
committed by pandamicro
parent 9c4bbd3f86
commit 9db6e9b49a
11 changed files with 67 additions and 36 deletions
+1 -1
View File
@@ -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 {
+57 -26
View File
@@ -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<string, {
@ccclass('cc.SkeletalAnimationClip')
export class SkeletalAnimationClip extends AnimationClip {
public originalKeys: number[][] = [];
public convertedData: ConvertedData = {};
protected _converted = false;
@@ -73,20 +75,28 @@ export class SkeletalAnimationClip extends AnimationClip {
}
});
this.convertedData = convertedData; this.curves = [];
// lazy eval the conversion due to memory-heavy ops
// many animation paths may not be actually in-use
for (const path of Object.keys(convertedData)) {
const props = convertedData[path] && convertedData[path].props;
if (!props) { continue; }
Object.defineProperty(props, 'worldMatrix', {
get: () => {
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 };
}
}
+1 -1
View File
@@ -179,7 +179,7 @@ export class Color extends ValueType {
* @zh 颜色转数组
* @param ofs 数组起始偏移量
*/
public static toArray <Out extends IColorLike> (out: IWritableArrayLike<number>, a: Out, ofs = 0) {
public static toArray <Out extends IWritableArrayLike<number>> (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;
+1 -1
View File
@@ -481,7 +481,7 @@ export class Mat3 extends ValueType {
* @zh 矩阵转数组
* @param ofs 数组内的起始偏移量
*/
public static toArray <Out extends IMat3Like> (out: IWritableArrayLike<number>, m: Out, ofs = 0) {
public static toArray <Out extends IWritableArrayLike<number>> (out: Out, m: IMat3Like, ofs = 0) {
out[ofs + 0] = m.m00;
out[ofs + 1] = m.m01;
out[ofs + 2] = m.m02;
+1 -1
View File
@@ -1164,7 +1164,7 @@ export class Mat4 extends ValueType {
* @zh 矩阵转数组
* @param ofs 数组内的起始偏移量
*/
public static toArray <Out extends IMat4Like> (out: IWritableArrayLike<number>, m: Out, ofs = 0) {
public static toArray <Out extends IWritableArrayLike<number>> (out: Out, m: IMat4Like, ofs = 0) {
out[ofs + 0] = m.m00;
out[ofs + 1] = m.m01;
out[ofs + 2] = m.m02;
+1 -1
View File
@@ -577,7 +577,7 @@ export class Quat extends ValueType {
* @zh 四元数转数组
* @param ofs 数组内的起始偏移量
*/
public static toArray <Out extends IQuatLike> (out: IWritableArrayLike<number>, q: Out, ofs = 0) {
public static toArray <Out extends IWritableArrayLike<number>> (out: Out, q: IQuatLike, ofs = 0) {
out[ofs + 0] = q.x;
out[ofs + 1] = q.y;
out[ofs + 2] = q.z;
+1 -1
View File
@@ -337,7 +337,7 @@ export class Vec2 extends ValueType {
* @zh 向量转数组
* @param ofs 数组起始偏移量
*/
public static toArray <Out extends IVec2Like> (out: IWritableArrayLike<number>, v: Out, ofs = 0) {
public static toArray <Out extends IWritableArrayLike<number>> (out: Out, v: IVec2Like, ofs = 0) {
out[ofs + 0] = v.x;
out[ofs + 1] = v.y;
return out;
+1 -1
View File
@@ -529,7 +529,7 @@ export class Vec3 extends ValueType {
* @zh 向量转数组
* @param ofs 数组起始偏移量
*/
public static toArray <Out extends IVec3Like> (out: IWritableArrayLike<number>, v: Out, ofs = 0) {
public static toArray <Out extends IWritableArrayLike<number>> (out: Out, v: IVec3Like, ofs = 0) {
out[ofs + 0] = v.x;
out[ofs + 1] = v.y;
out[ofs + 2] = v.z;
+1 -1
View File
@@ -415,7 +415,7 @@ export class Vec4 extends ValueType {
* @zh 向量转数组
* @param ofs 数组起始偏移量
*/
public static toArray <Out extends IVec4Like> (out: IWritableArrayLike<number>, v: Out, ofs = 0) {
public static toArray <Out extends IWritableArrayLike<number>> (out: Out, v: IVec4Like, ofs = 0) {
out[ofs + 0] = v.x;
out[ofs + 1] = v.y;
out[ofs + 2] = v.z;
+1 -1
View File
@@ -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 {
+1 -1
View File
@@ -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;