animation data pipeline refactor: phase 1 (#4413)

* model space joint update

* bindpose in TRS
This commit is contained in:
Yun Hsiao Wu
2019-05-14 11:19:04 +08:00
committed by Weiliang Wang
parent cd06be751a
commit 566cbd51cc
6 changed files with 179 additions and 151 deletions
+3 -2
View File
@@ -26,16 +26,17 @@
import { Asset } from '../../../assets/asset';
import { ccclass, property } from '../../../core/data/class-decorator';
import { EventTargetFactory } from '../../../core/event/event-target-factory';
import { Enum } from '../../../core/value-types';
import { AudioPlayer, PlayingState } from './player';
import { AudioPlayerDOM } from './player-dom';
import { AudioPlayerWeb } from './player-web';
export const AudioType = {
export const AudioType = Enum({
UNKNOWN_AUDIO: -1,
WEB_AUDIO: 0,
DOM_AUDIO: 1,
WX_GAME_AUDIO: 2,
};
});
@ccclass('cc.AudioClip')
export class AudioClip extends EventTargetFactory(Asset) {
+5 -5
View File
@@ -26,7 +26,7 @@
import { _decorator } from '../../core/data';
const { ccclass, property } = _decorator;
import { Asset } from '../../assets/asset';
import { Mat4 } from '../../core/value-types';
import { Node } from '../../scene-graph/node';
/**
* CLASS Skeleton
@@ -48,21 +48,21 @@ export default class Skeleton extends Asset {
/**
* The inverse bind matrices of joints.
*/
@property([Mat4])
private _inverseBindMatrices: Mat4[] = [];
@property([Node])
private _bindposes: Node[] = [];
/**
* Gets the bind pose matrices of joints.
*/
get bindposes () {
return this._inverseBindMatrices;
return this._bindposes;
}
/**
* Sets the bind pose matrices of joints.
*/
set bindposes (value) {
this._inverseBindMatrices = value;
this._bindposes = value;
}
/**
+109 -100
View File
@@ -24,10 +24,10 @@
****************************************************************************/
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator';
import { Mat4 } from '../../core/value-types';
import { mat4 } from '../../core/vmath';
import { Mat4, Quat, Vec3 } from '../../core/value-types';
import { mat4, quat, vec3 } from '../../core/vmath';
import { GFXDevice } from '../../gfx/device';
import { __DEFER_BINDPOSE_COMPUTATION__, JointStorageKind, selectStorageKind, SkinningModel } from '../../renderer/models/skinning-model';
import { JointStorageKind, selectStorageKind, SkinningModel } from '../../renderer/models/skinning-model';
import { Node } from '../../scene-graph/node';
import { Mesh } from '../assets';
import { Material } from '../assets/material';
@@ -36,10 +36,45 @@ import { aabb } from '../geom-utils';
import { calculateBoneSpaceBounds } from '../misc/utils';
import { ModelComponent } from './model-component';
const _m4_tmp = new Mat4();
const _m4_tmp2 = new Mat4();
const v3_1 = new Vec3();
const qt_1 = new Quat();
const v3_2 = new Vec3();
const m4_1 = new Mat4();
const ab_1 = new aabb();
type SkinningTarget = Map<string, Node>;
class Joint {
public node: Node;
public position: Vec3 = new Vec3();
public rotation: Quat = new Quat();
public scale: Vec3 = new Vec3(1, 1, 1);
protected _lastUpdate = -1;
constructor (node: Node) { this.node = node; }
public update (parent: Joint) {
const totalFrames = cc.director.totalFrames;
if (this._lastUpdate >= totalFrames) { return; }
this._lastUpdate = totalFrames;
if (parent) {
vec3.mul(this.position, this.node.localPosition, parent.scale);
vec3.transformQuat(this.position, this.position, parent.rotation);
vec3.add(this.position, this.position, parent.position);
quat.mul(this.rotation, parent.rotation, this.node.localRotation);
vec3.mul(this.scale, parent.scale, this.node.localScale);
} else {
vec3.copy(this.position, this.node.localPosition);
quat.copy(this.rotation, this.node.localRotation);
vec3.copy(this.scale, this.node.localScale);
}
}
}
class JointManager {
public static get (node: Node) {
let joint = JointManager._joints.get(node);
if (!joint) { joint = new Joint(node); JointManager._joints.set(node, joint); }
return joint;
}
protected static _joints: Map<Node, Joint> = new Map();
}
/**
* @en The Skinning Model Component
@@ -80,14 +115,15 @@ export class SkinningModelComponent extends ModelComponent {
this._skinningRoot = value;
this._resetSkinningTarget();
}
@property(Skeleton)
private _skeleton: Skeleton | null = null;
@property(Node)
private _skinningRoot: Node | null = null;
private _skinningTarget: SkinningTarget | null = null;
private _joints: Joint[] = [];
private _jointParentIndices: number[] = [];
private _boneSpaceBounds: null | Array<aabb | null> = null;
constructor () {
@@ -102,119 +138,99 @@ export class SkinningModelComponent extends ModelComponent {
}
});
this._resetSkinningTarget();
if (this.mesh && this._skeleton) {
if (CC_EDITOR && this.mesh && this._skeleton) {
this._boneSpaceBounds = boneSpaceBoundsManager.use(this.mesh, this._skeleton);
}
}
public update (dt) {
public update () {
this._tryUpdateMatrices();
}
public onDestroy () {
}
public calculateSkinnedBounds (out?: aabb): boolean {
if (!this._skeleton ||
!this._boneSpaceBounds ||
!this._skinningTarget) {
return false;
}
out = out || new aabb();
let outInitialized = false;
const tmpAABB = new aabb();
for (let iJoint = 0; iJoint < this._skeleton.joints.length; ++iJoint) {
const bounds = this._boneSpaceBounds[iJoint];
if (!bounds) {
continue;
}
const joint = this._skeleton.joints[iJoint];
const targetNode = this._skinningTarget.get(joint);
if (!targetNode) {
continue;
}
const jointWorldTransform = _m4_tmp;
const transformedBoneBounds = tmpAABB;
targetNode.getWorldMatrix(jointWorldTransform);
aabb.transform(transformedBoneBounds, bounds, jointWorldTransform);
if (outInitialized) {
aabb.merge(out, out, transformedBoneBounds);
} else {
outInitialized = true;
aabb.copy(out, transformedBoneBounds);
}
}
return outInitialized;
}
public _tryUpdateMatrices () {
if (!this._skeleton || !this._skinningTarget || !this._model) {
if (!this._skeleton || !this._model) {
return;
}
const skinningModel = this._model as SkinningModel;
const skeleton = this._skeleton;
const skinningTarget = this._skinningTarget;
const len = this._joints.length;
const isTextureStorage = skinningModel.isTextureStorage();
for (let iJoint = 0; iJoint < this._skeleton.joints.length; ++iJoint) {
const targetNode = skinningTarget.get(this._skeleton.joints[iJoint]);
if (!targetNode) {
continue;
}
if (isTextureStorage && __DEFER_BINDPOSE_COMPUTATION__) {
skinningModel.updateJointMatrix(iJoint, targetNode.worldMatrix);
} else {
const bindpose = skeleton.bindposes[iJoint];
const jointMatrix = _m4_tmp;
mat4.multiply(jointMatrix, targetNode.worldMatrix, bindpose);
skinningModel.updateJointMatrix(iJoint, jointMatrix);
}
for (let i = 0; i < len; ++i) {
const cur = this._joints[i];
cur.update(this._joints[this._jointParentIndices[i]]);
const bindpose = skeleton.bindposes[i];
vec3.mul(v3_1, bindpose.localPosition, cur.scale);
vec3.transformQuat(v3_1, v3_1, cur.rotation);
vec3.add(v3_1, v3_1, cur.position);
quat.mul(qt_1, cur.rotation, bindpose.localRotation);
vec3.mul(v3_2, cur.scale, bindpose.localScale);
skinningModel.updateJointData(i, v3_1, qt_1, v3_2);
}
skinningModel.commitJointMatrices();
}
public calculateSkinnedBounds (out?: aabb): boolean {
if (!this._skeleton || !this._boneSpaceBounds) {
return false;
}
out = out || new aabb();
let outInitialized = false;
const len = this._joints.length;
for (let i = 0; i < len; ++i) {
const bounds = this._boneSpaceBounds[i];
const targetNode = this._joints[i].node;
if (!bounds || !targetNode) { continue; }
targetNode.getWorldMatrix(m4_1);
aabb.transform(ab_1, bounds, m4_1);
if (outInitialized) {
aabb.merge(out, out, ab_1);
} else {
outInitialized = true;
aabb.copy(out, ab_1);
}
}
return outInitialized;
}
public _updateModelParams () {
// Should bind skeleton before super create pso
this._bindSkeleton();
super._updateModelParams();
}
protected _onMeshChanged (old: Mesh | null) {
super._onMeshChanged(old);
if (this._skeleton) {
if (old) {
boneSpaceBoundsManager.unuse(old, this._skeleton);
}
if (this.mesh) {
this._boneSpaceBounds = boneSpaceBoundsManager.use(this.mesh, this._skeleton);
}
}
}
protected _onSkeletonChanged (old: Skeleton | null) {
if (this.mesh) {
if (old) {
boneSpaceBoundsManager.unuse(this.mesh, old);
}
if (this._skeleton) {
this._boneSpaceBounds = boneSpaceBoundsManager.use(this.mesh, this._skeleton);
}
}
protected _createModel () {
if (!this.node.scene) { return; }
const scene = this._getRenderScene();
this._model = scene.createModel(this._getModelConstructor(), this.node.parent!);
}
protected _getModelConstructor () {
return SkinningModel;
}
protected _onMeshChanged (old: Mesh | null) {
super._onMeshChanged(old);
if (!CC_EDITOR || !this._skeleton) { return; }
if (old) { boneSpaceBoundsManager.unuse(old, this._skeleton); }
if (this.mesh) { this._boneSpaceBounds = boneSpaceBoundsManager.use(this.mesh, this._skeleton); }
}
protected _onSkeletonChanged (old: Skeleton | null) {
if (!CC_EDITOR || !this.mesh) { return; }
if (old) { boneSpaceBoundsManager.unuse(this.mesh, old); }
if (this._skeleton) { this._boneSpaceBounds = boneSpaceBoundsManager.use(this.mesh, this._skeleton); }
}
protected _onMaterialModified (index: number, material: Material) {
const device = _getGlobalDevice()!;
const device: GFXDevice = cc.director.root && cc.director.root.device;
const kind = selectStorageKind(device);
const mat = this.getMaterial(index, CC_EDITOR);
if (mat) {
mat.recompileShaders({
CC_BINDPOSE_COMPUTATION_DEFERED: __DEFER_BINDPOSE_COMPUTATION__,
CC_USE_SKINNING: true,
CC_USE_JOINTS_TEXTURE: kind === JointStorageKind.textureRGBA32F || kind === JointStorageKind.textureRGBA8,
CC_JOINTS_TEXTURE_RGBA8: kind === JointStorageKind.textureRGBA8,
@@ -234,29 +250,22 @@ export class SkinningModelComponent extends ModelComponent {
return;
}
// Collect target to skin.
this._skinningTarget = new Map();
this._joints.length = 0;
const rootNode = this._skinningRoot;
this._skeleton.joints.forEach((joint) => {
const targetNode = rootNode.getChildByPath(joint);
this._skeleton.joints.forEach((path) => {
const targetNode = rootNode.getChildByPath(path);
if (!targetNode) {
console.warn(`Skinning target ${joint} not found in scene graph.`);
console.warn(`Skinning target ${path} not found in scene graph.`);
return;
}
this._skinningTarget!.set(joint, targetNode);
// guaranteed to exist except the root
const parentIdx = this._joints.findIndex((j) => j.node === targetNode.parent);
this._joints.push(JointManager.get(targetNode));
this._jointParentIndices.push(parentIdx);
});
}
}
function _getGlobalDevice (): GFXDevice | null {
// @ts-ignore
if (cc.director && cc.director.root) {
return cc.director.root.device;
} else {
return null;
}
}
interface ICachedBoneSpaceBounds {
bounds: Array<aabb | null>;
referenceCount: number;
+48 -17
View File
@@ -1,8 +1,14 @@
import mat3 from './mat3';
import quat from './quat';
import { EPSILON } from './utils';
import vec3 from './vec3';
// tslint:disable: one-variable-per-declaration
// tslint:disable: max-line-length
const v3_1 = new vec3();
const m3_1 = new mat3();
/**
* Mathematical 4x4 matrix.
*
@@ -1021,6 +1027,31 @@ class mat4 {
return out;
}
/**
* Decompose an affine matrix to a quaternion rotation, a translation offset and a scale vector.
* Assumes the transformation is combined in the order of Scale -> Rotate -> Translate.
* @param m - Matrix to decompose.
* @param q - Resulting rotation quaternion.
* @param v - Resulting translation offset.
* @param s - Resulting scale vector.
*/
public static toRTS (m: mat4, q: quat, v: vec3, s: vec3) {
s.x = vec3.mag(vec3.set(v3_1, m.m00, m.m01, m.m02));
m3_1.m00 = m.m00 / s.x;
m3_1.m01 = m.m01 / s.x;
m3_1.m02 = m.m02 / s.x;
s.y = vec3.mag(vec3.set(v3_1, m.m04, m.m05, m.m06));
m3_1.m03 = m.m04 / s.y;
m3_1.m04 = m.m05 / s.y;
m3_1.m05 = m.m06 / s.y;
s.z = vec3.mag(vec3.set(v3_1, m.m08, m.m09, m.m10));
m3_1.m06 = m.m08 / s.z;
m3_1.m07 = m.m09 / s.z;
m3_1.m08 = m.m10 / s.z;
quat.normalize(q, quat.fromMat3(q, m3_1));
vec3.set(v, m.m12, m.m13, m.m14);
}
/**
* Creates a matrix from a quaternion rotation, translation offset and scale vector.
* This is equivalent to (but much faster than):
@@ -1647,7 +1678,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, epsilon = EPSILON) {
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,
@@ -1659,22 +1690,22 @@ class mat4 {
b12 = b.m12, b13 = b.m13, b14 = b.m14, b15 = b.m15;
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)) &&
Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) &&
Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) &&
Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) &&
Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) &&
Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) &&
Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)) &&
Math.abs(a9 - b9) <= EPSILON * Math.max(1.0, Math.abs(a9), Math.abs(b9)) &&
Math.abs(a10 - b10) <= EPSILON * Math.max(1.0, Math.abs(a10), Math.abs(b10)) &&
Math.abs(a11 - b11) <= EPSILON * Math.max(1.0, Math.abs(a11), Math.abs(b11)) &&
Math.abs(a12 - b12) <= EPSILON * Math.max(1.0, Math.abs(a12), Math.abs(b12)) &&
Math.abs(a13 - b13) <= EPSILON * Math.max(1.0, Math.abs(a13), Math.abs(b13)) &&
Math.abs(a14 - b14) <= EPSILON * Math.max(1.0, Math.abs(a14), Math.abs(b14)) &&
Math.abs(a15 - b15) <= EPSILON * Math.max(1.0, Math.abs(a15), Math.abs(b15))
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)) &&
Math.abs(a2 - b2) <= epsilon * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
Math.abs(a3 - b3) <= epsilon * Math.max(1.0, Math.abs(a3), Math.abs(b3)) &&
Math.abs(a4 - b4) <= epsilon * Math.max(1.0, Math.abs(a4), Math.abs(b4)) &&
Math.abs(a5 - b5) <= epsilon * Math.max(1.0, Math.abs(a5), Math.abs(b5)) &&
Math.abs(a6 - b6) <= epsilon * Math.max(1.0, Math.abs(a6), Math.abs(b6)) &&
Math.abs(a7 - b7) <= epsilon * Math.max(1.0, Math.abs(a7), Math.abs(b7)) &&
Math.abs(a8 - b8) <= epsilon * Math.max(1.0, Math.abs(a8), Math.abs(b8)) &&
Math.abs(a9 - b9) <= epsilon * Math.max(1.0, Math.abs(a9), Math.abs(b9)) &&
Math.abs(a10 - b10) <= epsilon * Math.max(1.0, Math.abs(a10), Math.abs(b10)) &&
Math.abs(a11 - b11) <= epsilon * Math.max(1.0, Math.abs(a11), Math.abs(b11)) &&
Math.abs(a12 - b12) <= epsilon * Math.max(1.0, Math.abs(a12), Math.abs(b12)) &&
Math.abs(a13 - b13) <= epsilon * Math.max(1.0, Math.abs(a13), Math.abs(b13)) &&
Math.abs(a14 - b14) <= epsilon * Math.max(1.0, Math.abs(a14), Math.abs(b14)) &&
Math.abs(a15 - b15) <= epsilon * Math.max(1.0, Math.abs(a15), Math.abs(b15))
);
}
+2 -2
View File
@@ -382,7 +382,7 @@ export default class vec3 {
* @param a - Vector to invert.
* @return out.
*/
public static inverse<Out extends vec3> (out: Out, a: vec3) {
public static invert<Out extends vec3> (out: Out, a: vec3) {
out.x = 1.0 / a.x;
out.y = 1.0 / a.y;
out.z = 1.0 / a.z;
@@ -396,7 +396,7 @@ export default class vec3 {
* @param a - Vector to invert.
* @return out.
*/
public static inverseSafe<Out extends vec3> (out: Out, a: vec3) {
public static invertSafe<Out extends vec3> (out: Out, a: vec3) {
const { x, y, z } = a;
if (Math.abs(x) < EPSILON) {
+12 -25
View File
@@ -3,10 +3,11 @@
import Skeleton from '../../3d/assets/skeleton';
import { Filter, PixelFormat, WrapMode } from '../../assets/asset-enum';
import { Texture2D } from '../../assets/texture-2d';
import { mat4, vec3, vec4 } from '../../core/vmath';
import { Mat4 } from '../../core';
import { mat4, vec2, vec3, quat } from '../../core/vmath';
import { GFXBuffer } from '../../gfx/buffer';
import { GFXBufferUsageBit, GFXMemoryUsageBit } from '../../gfx/define';
import { GFXAPI, GFXDevice, GFXFeature } from '../../gfx/device';
import { GFXDevice, GFXFeature } from '../../gfx/device';
import { JointUniformCapacity, UBOSkinning, UBOSkinningTextureCase, UNIFORM_JOINTS_TEXTURE } from '../../pipeline/define';
import { Node } from '../../scene-graph/node';
import { Pass } from '../core/pass';
@@ -18,7 +19,7 @@ const textureSizeBuffer = new Float32Array(4);
const skinningVectors = JointUniformCapacity * 3; // both Mat3x4 and DQ
const vertexVectorLeeway = 30; // the minimum number of free vectors guaranteed in vertex shader when using uniform joint storage
export const __DEFER_BINDPOSE_COMPUTATION__ = true;
const m4_1 = new Mat4();
export enum JointStorageKind {
textureRGBA8,
@@ -65,16 +66,7 @@ export class SkinningModel extends Model {
if (storageKind === JointStorageKind.textureRGBA32F ||
storageKind === JointStorageKind.textureRGBA8) {
const mat4TextureKind = storageKind === JointStorageKind.textureRGBA32F ? Mat4TextureKind.rgba32f : Mat4TextureKind.rgba8888;
const mat4Texture = new MatrixTexture(
__DEFER_BINDPOSE_COMPUTATION__ ? skeleton.joints.length * 2 : skeleton.joints.length,
mat4TextureKind,
this._device);
if (__DEFER_BINDPOSE_COMPUTATION__) {
for (let iJoint = 0; iJoint < skeleton.bindposes.length; ++iJoint) {
mat4Texture.set(iJoint * 2 + 0, skeleton.bindposes[iJoint]);
}
}
const mat4Texture = new MatrixTexture(skeleton.joints.length, mat4TextureKind, this._device);
this._binded = {
skinningUBOBinding: UBOSkinningTextureCase.BLOCK.binding,
@@ -95,9 +87,7 @@ export class SkinningModel extends Model {
textureSizeBuffer, UBOSkinningTextureCase.JOINTS_TEXTURE_SIZE_OFFSET, textureSizeBuffer.byteLength);
} else {
if (skeleton.joints.length > JointUniformCapacity) {
console.error(
`Skeleton ${skeleton.name} has ${skeleton.joints.length} joints ` +
`which exceeds Cocos's max allowed joint count(${JointUniformCapacity}).`);
console.error(`Joints count exceeds the default limits: ${skeleton.joints.length}/${JointUniformCapacity} in ${skeleton.name}`);
}
this._binded = {
@@ -116,19 +106,16 @@ export class SkinningModel extends Model {
}
}
public updateJointMatrix (iMatrix: number, matrix: mat4) {
public updateJointData (iMatrix: number, pos: vec3, rot: quat, scale: vec3) {
if (!this._binded) {
return;
}
const { jointStorage, skinningUBO } = this._binded;
const { jointStorage } = this._binded;
mat4.fromRTS(m4_1, rot, pos, scale);
if (isTextureStorage(jointStorage)) {
if (__DEFER_BINDPOSE_COMPUTATION__) {
jointStorage.texture.set(iMatrix * 2 + 1, matrix);
} else {
jointStorage.texture.set(iMatrix, matrix);
}
jointStorage.texture.set(iMatrix, m4_1);
} else {
setMat4InUniform(jointStorage.nativeData, iMatrix, matrix);
setMat4InUniform(jointStorage.nativeData, iMatrix, m4_1);
}
}
@@ -286,7 +273,7 @@ class MatrixTexture {
}
public set (iMatrix: number, matrix: mat4) {
// use identical memory layout for all texture formats
// just use identical memory layout for all texture formats
mat4.array(this._nativeData, matrix, iMatrix * 4 * 4);
}