mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-05 11:02:40 +08:00
cache joints textures (#5028)
This commit is contained in:
committed by
Weiliang Wang
parent
a3992a3257
commit
43a27ee1e1
@@ -31,6 +31,7 @@ import { Asset } from '../../assets/asset';
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { CCString } from '../../core/data/utils/attribute';
|
||||
import { Mat4, Quat, Vec3 } from '../../core/math';
|
||||
import { murmurhash2_32_gc } from '../../core/utils/murmurhash2_gc';
|
||||
|
||||
export interface IBindTRS {
|
||||
position: Vec3;
|
||||
@@ -53,6 +54,8 @@ export class Skeleton extends Asset {
|
||||
|
||||
private _bindTRS: IBindTRS[] = [];
|
||||
|
||||
private _hash = 0;
|
||||
|
||||
/**
|
||||
* 所有关节的绑定姿势矩阵。该数组的长度始终与 `this.joints` 的长度相同。
|
||||
*/
|
||||
@@ -69,6 +72,12 @@ export class Skeleton extends Asset {
|
||||
Mat4.toRTS(m, rotation, position, scale);
|
||||
return { position, rotation, scale };
|
||||
});
|
||||
this._hash = murmurhash2_32_gc(this.bindposes.reduce((acc, cur) => acc +
|
||||
cur.m00.toPrecision(2) + cur.m01.toPrecision(2) + cur.m02.toPrecision(2) + cur.m03.toPrecision(2) +
|
||||
cur.m04.toPrecision(2) + cur.m05.toPrecision(2) + cur.m06.toPrecision(2) + cur.m07.toPrecision(2) +
|
||||
cur.m08.toPrecision(2) + cur.m09.toPrecision(2) + cur.m10.toPrecision(2) + cur.m11.toPrecision(2) +
|
||||
cur.m12.toPrecision(2) + cur.m13.toPrecision(2) + cur.m14.toPrecision(2) + cur.m15.toPrecision(2)
|
||||
, ''), 666);
|
||||
}
|
||||
|
||||
get bindTRS () {
|
||||
@@ -86,6 +95,10 @@ export class Skeleton extends Asset {
|
||||
this._joints = value;
|
||||
}
|
||||
|
||||
get hash () {
|
||||
return this._hash;
|
||||
}
|
||||
|
||||
public onLoaded () {
|
||||
this.bindposes = this._bindposes;
|
||||
}
|
||||
|
||||
+12
-12
File diff suppressed because one or more lines are too long
@@ -10,6 +10,7 @@ import binarySearchEpsilon from '../core/utils/binary-search';
|
||||
import { AnimCurve, IPropertyCurveData, RatioSampler } from './animation-curve';
|
||||
import { WrapMode as AnimationWrapMode } from './types';
|
||||
import { INode } from '../core/utils/interfaces';
|
||||
import { murmurhash2_32_gc } from '../core/utils/murmurhash2_gc';
|
||||
|
||||
interface IAnimationEventData {
|
||||
frame: number;
|
||||
@@ -176,6 +177,8 @@ export class AnimationClip extends Asset {
|
||||
@property
|
||||
protected _stepness = 0;
|
||||
|
||||
protected _hash = 0;
|
||||
|
||||
/**
|
||||
* 动画的周期。
|
||||
*/
|
||||
@@ -223,11 +226,16 @@ export class AnimationClip extends Asset {
|
||||
this._applyStepness();
|
||||
}
|
||||
|
||||
get hash () {
|
||||
return this._hash;
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
this.duration = this._duration;
|
||||
this.speed = this.speed;
|
||||
this.wrapMode = this.wrapMode;
|
||||
this.frameRate = this.sample;
|
||||
this._hash = murmurhash2_32_gc(`${this._uuid} ${this._duration} ${this._keys[0] && this._keys[0].length}`, 666);
|
||||
}
|
||||
|
||||
public getPropertyCurves (root: INode): ReadonlyArray<IPropertyCurve> {
|
||||
|
||||
@@ -97,9 +97,6 @@ export class SkeletalAnimationClip extends AnimationClip {
|
||||
const path = getPathFromRoot(comp.node, root);
|
||||
if (!this.curveDatas[path]) { this.curveDatas[path] = {}; }
|
||||
this.curveDatas[path].comps = { [getClassName(comp)]: { frameID: { keys: 0, values, interpolate: false } } };
|
||||
} else { // rig non-skinning renderables
|
||||
// console.warn('[deprecated] attachments should be specified in SkeletalAnimationComponent');
|
||||
// if (!CC_EDITOR) { this._convertToRiggingData(comp.node.parent!, root); }
|
||||
}
|
||||
}
|
||||
this._keys = [values.map((_, i) => i / this.sample)];
|
||||
@@ -149,91 +146,6 @@ export class SkeletalAnimationClip extends AnimationClip {
|
||||
Vec3.multiply(S, parentS, S);
|
||||
}
|
||||
}
|
||||
|
||||
// this incorrectly assumes any rig node will never be the parent nodes of others
|
||||
// can only be fixed if we use a different rigging interface other than just attach child node to bones
|
||||
private _convertToRiggingData (targetNode: INode, root: INode) {
|
||||
const targetPath = getPathFromRoot(targetNode, root);
|
||||
// find lowest joint animation
|
||||
let animPath = targetPath;
|
||||
let source = this.convertedData[animPath];
|
||||
let animNode = targetNode;
|
||||
while (!source || !source.props) {
|
||||
const idx = animPath.lastIndexOf('/');
|
||||
animPath = animPath.substring(0, idx);
|
||||
source = this.convertedData[animPath];
|
||||
animNode = animNode.parent!;
|
||||
if (idx < 0) { return; }
|
||||
}
|
||||
// create initial animation data
|
||||
const data: IObjectCurveData = {
|
||||
position: { keys: 0, interpolate: false, values: source.props.position.values.map((v) => v.clone()) },
|
||||
rotation: { keys: 0, interpolate: false, values: source.props.rotation.values.map((v) => v.clone()) },
|
||||
scale: { keys: 0, interpolate: false, values: source.props.scale.values.map((v) => v.clone()) },
|
||||
};
|
||||
const position = data.position.values;
|
||||
const rotation = data.rotation.values;
|
||||
const scale = data.scale.values;
|
||||
// apply downstream bindpose
|
||||
let target = targetNode;
|
||||
Vec3.set(v3_1, 0, 0, 0);
|
||||
Quat.set(qt_1, 0, 0, 0, 1);
|
||||
Vec3.set(v3_2, 1, 1, 1);
|
||||
while (target !== animNode) {
|
||||
Vec3.multiply(v3_3, v3_1, target.scale);
|
||||
Vec3.transformQuat(v3_3, v3_3, target.rotation);
|
||||
Vec3.add(v3_1, v3_3, target.position);
|
||||
Quat.multiply(qt_1, target.rotation, qt_1);
|
||||
Vec3.multiply(v3_2, target.scale, v3_2);
|
||||
target = target.parent!;
|
||||
}
|
||||
for (let i = 0; i < position.length; i++) {
|
||||
const T = position[i];
|
||||
const R = rotation[i];
|
||||
const S = scale[i];
|
||||
Vec3.multiply(v3_3, v3_1, S);
|
||||
Vec3.transformQuat(v3_3, v3_3, R);
|
||||
Vec3.add(T, v3_3, T);
|
||||
Quat.multiply(R, R, qt_1);
|
||||
Vec3.multiply(S, S, v3_2);
|
||||
}
|
||||
// apply inverse upstream bindpose
|
||||
target = animNode.parent === root ? animNode : animNode.parent!;
|
||||
Vec3.set(v3_1, 0, 0, 0);
|
||||
Quat.set(qt_1, 0, 0, 0, 1);
|
||||
Vec3.set(v3_2, 1, 1, 1);
|
||||
while (target !== root) {
|
||||
Vec3.multiply(v3_3, v3_1, target.scale);
|
||||
Vec3.transformQuat(v3_3, v3_3, target.rotation);
|
||||
Vec3.add(v3_1, v3_3, target.position);
|
||||
Quat.multiply(qt_1, target.rotation, qt_1);
|
||||
Vec3.multiply(v3_2, target.scale, v3_2);
|
||||
target = target.parent!;
|
||||
}
|
||||
Quat.invert(qt_1, qt_1);
|
||||
Vec3.invert(v3_2, v3_2);
|
||||
Vec3.negate(v3_1, v3_1);
|
||||
Vec3.transformQuat(v3_1, v3_1, qt_1);
|
||||
Vec3.multiply(v3_1, v3_1, v3_2);
|
||||
for (let i = 0; i < position.length; i++) {
|
||||
const T = position[i];
|
||||
const R = rotation[i];
|
||||
const S = scale[i];
|
||||
Vec3.multiply(T, T, v3_2);
|
||||
Vec3.transformQuat(T, T, qt_1);
|
||||
Vec3.add(T, T, v3_1);
|
||||
Quat.multiply(R, qt_1, R);
|
||||
Vec3.multiply(S, v3_2, S);
|
||||
}
|
||||
// wrap up
|
||||
if (!this.curveDatas[targetPath]) { this.curveDatas[targetPath] = {}; }
|
||||
this.curveDatas[targetPath].props = data;
|
||||
}
|
||||
}
|
||||
|
||||
const v3_1 = new Vec3();
|
||||
const v3_2 = new Vec3();
|
||||
const qt_1 = new Quat();
|
||||
const v3_3 = new Vec3();
|
||||
|
||||
cc.SkeletalAnimationClip = SkeletalAnimationClip;
|
||||
|
||||
@@ -114,21 +114,21 @@ export class JointsTexturePool {
|
||||
|
||||
private _device: GFXDevice;
|
||||
private _pool: TextureBufferPool;
|
||||
private _defaultTextureBuffer: ITextureBufferHandle | null = null;
|
||||
private _defaultTextureBuffers: Map<number, ITextureBufferHandle> = new Map();
|
||||
private _textureBuffers: Map<string, ITextureBufferHandle> = new Map();
|
||||
|
||||
constructor (device: GFXDevice) {
|
||||
this._device = device;
|
||||
this._pool = new TextureBufferPool(device);
|
||||
}
|
||||
|
||||
public initialize (maxChunks: number = 16) {
|
||||
public initialize (maxChunks: number = 8) {
|
||||
const format = _jointsFormat[selectJointsMediumType(this._device)];
|
||||
const scale = 16 / GFXFormatInfos[format].size;
|
||||
this._pool.initialize({
|
||||
format,
|
||||
maxChunks: maxChunks * scale,
|
||||
inOrderFree: false,
|
||||
roundUpFn: roundUpTextureSize
|
||||
roundUpFn: roundUpTextureSize,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -140,14 +140,10 @@ export class JointsTexturePool {
|
||||
* 获取默认骨骼贴图
|
||||
*/
|
||||
public getDefaultJointsTexture (skeleton?: Skeleton) {
|
||||
let len: number = 1; let texture: ITextureBufferHandle | null = null;
|
||||
if (skeleton && skeleton.joints.length > 1) {
|
||||
len = skeleton.joints.length;
|
||||
texture = this._pool.alloc(len * 12 * Float32Array.BYTES_PER_ELEMENT);
|
||||
} else {
|
||||
if (this._defaultTextureBuffer) { return this._defaultTextureBuffer; }
|
||||
texture = this._defaultTextureBuffer = this._pool.alloc(len * 12 * Float32Array.BYTES_PER_ELEMENT);
|
||||
}
|
||||
let len: number = skeleton && skeleton.joints.length || 1;
|
||||
let texture: ITextureBufferHandle | null = this._defaultTextureBuffers.get(len) || null;
|
||||
if (texture) { return texture; }
|
||||
texture = this._pool.alloc(len * 12 * Float32Array.BYTES_PER_ELEMENT);
|
||||
if (!texture) { return null; }
|
||||
Vec3.set(v3_1, 0, 0, 0);
|
||||
Quat.set(qt_1, 0, 0, 0, 1);
|
||||
@@ -157,6 +153,7 @@ export class JointsTexturePool {
|
||||
uploadJointData(textureBuffer, 12 * i, v3_1, qt_1, v3_2, i === 0);
|
||||
}
|
||||
this._pool.update(texture, textureBuffer.buffer);
|
||||
this._defaultTextureBuffers.set(len, texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
@@ -164,9 +161,12 @@ export class JointsTexturePool {
|
||||
* 获取指定动画片段的骨骼贴图
|
||||
*/
|
||||
public getJointsTextureWithAnimation (skeleton: Skeleton, clip: SkeletalAnimationClip) {
|
||||
const hash = `${skeleton.hash}${clip.hash}`;
|
||||
let texture: ITextureBufferHandle | null = this._textureBuffers.get(hash) || null;
|
||||
if (texture) { return texture; }
|
||||
const frames = clip.keys[0].length;
|
||||
const bufSize = skeleton.joints.length * 12 * frames;
|
||||
const texture = this._pool.alloc(bufSize * Float32Array.BYTES_PER_ELEMENT);
|
||||
texture = this._pool.alloc(bufSize * Float32Array.BYTES_PER_ELEMENT);
|
||||
if (!texture) { return null; }
|
||||
const textureBuffer = new Float32Array(bufSize);
|
||||
const data = clip.convertedData;
|
||||
@@ -190,6 +190,7 @@ export class JointsTexturePool {
|
||||
}
|
||||
}
|
||||
this._pool.update(texture, textureBuffer.buffer);
|
||||
this._textureBuffers.set(hash, texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user