mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
unified default joints texture (#4922)
This commit is contained in:
committed by
Weiliang Wang
parent
c17f28201d
commit
56192c6267
+69
-73
@@ -58,6 +58,49 @@ export enum JointsMediumType {
|
||||
RGBA32F,
|
||||
}
|
||||
|
||||
const dq_0 = new Quat();
|
||||
const dq_1 = new Quat();
|
||||
const m4_1 = new Mat4();
|
||||
|
||||
// Linear Blending Skinning
|
||||
function uploadJointDataLBS (out: Float32Array, base: number, pos: Vec3, rot: Quat, scale: Vec3, firstBone: boolean) {
|
||||
mat4.fromRTS(m4_1, rot, pos, scale);
|
||||
out[base + 0] = m4_1.m00;
|
||||
out[base + 1] = m4_1.m01;
|
||||
out[base + 2] = m4_1.m02;
|
||||
out[base + 3] = m4_1.m12;
|
||||
out[base + 4] = m4_1.m04;
|
||||
out[base + 5] = m4_1.m05;
|
||||
out[base + 6] = m4_1.m06;
|
||||
out[base + 7] = m4_1.m13;
|
||||
out[base + 8] = m4_1.m08;
|
||||
out[base + 9] = m4_1.m09;
|
||||
out[base + 10] = m4_1.m10;
|
||||
out[base + 11] = m4_1.m14;
|
||||
}
|
||||
|
||||
// Dual Quaternion Skinning
|
||||
function uploadJointDataDQS (out: Float32Array, base: number, pos: Vec3, rot: Quat, scale: Vec3, firstBone: boolean) {
|
||||
// sign consistency
|
||||
if (firstBone) { quat.copy(dq_0, rot); }
|
||||
else if (quat.dot(dq_0, rot) < 0) { quat.scale(rot, rot, -1); }
|
||||
// conversion
|
||||
quat.set(dq_1, pos.x, pos.y, pos.z, 0);
|
||||
quat.scale(dq_1, quat.multiply(dq_1, dq_1, rot), 0.5);
|
||||
// upload
|
||||
out[base + 0] = rot.x;
|
||||
out[base + 1] = rot.y;
|
||||
out[base + 2] = rot.z;
|
||||
out[base + 3] = rot.w;
|
||||
out[base + 4] = dq_1.x;
|
||||
out[base + 5] = dq_1.y;
|
||||
out[base + 6] = dq_1.z;
|
||||
out[base + 7] = dq_1.w;
|
||||
out[base + 8] = scale.x;
|
||||
out[base + 9] = scale.y;
|
||||
out[base + 10] = scale.z;
|
||||
}
|
||||
|
||||
const _jointsFormat = {
|
||||
[JointsMediumType.RGBA8]: PixelFormat.RGBA8888,
|
||||
[JointsMediumType.RGBA32F]: PixelFormat.RGBA32F,
|
||||
@@ -67,19 +110,37 @@ const v3_1 = new Vec3();
|
||||
const qt_1 = new Quat();
|
||||
const v3_2 = new Vec3();
|
||||
|
||||
const dq_0 = new Quat();
|
||||
const dq_1 = new Quat();
|
||||
const m4_1 = new Mat4();
|
||||
// change here and cc-skinning.inc to use other skinning algorithms
|
||||
const uploadJointData = uploadJointDataDQS;
|
||||
let defaultJointsTexture: Texture2D | null = null;
|
||||
|
||||
/**
|
||||
* 骨骼资源。
|
||||
* 骨骼资源记录了每个关节(相对于`SkinningModelComponent.SkinningRoot`)的路径以及它的绑定姿势矩阵。
|
||||
* 骨骼资源记录了每个关节(相对于`SkinningModelComponent.skinningRoot`)的路径以及它的绑定姿势矩阵。
|
||||
*/
|
||||
@ccclass('cc.Skeleton')
|
||||
export class Skeleton extends Asset {
|
||||
|
||||
// change here and cc-skinning.inc to use other skinning algorithms
|
||||
public uploadJointData = this.uploadJointDataDQS;
|
||||
/**
|
||||
* 获取默认骨骼贴图
|
||||
*/
|
||||
public static getDefaultJointsTexture (device: GFXDevice) {
|
||||
if (defaultJointsTexture) { return defaultJointsTexture; }
|
||||
defaultJointsTexture = new Texture2D();
|
||||
defaultJointsTexture.setFilters(Filter.NEAREST, Filter.NEAREST);
|
||||
defaultJointsTexture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE);
|
||||
const type = selectJointsMediumType(device);
|
||||
const format = _jointsFormat[type];
|
||||
const width = Math.ceil(12 * 4 / GFXFormatInfos[format].size);
|
||||
defaultJointsTexture.reset({ width, height: 1, format });
|
||||
vec3.set(v3_1, 0, 0, 0);
|
||||
quat.set(qt_1, 0, 0, 0, 1);
|
||||
vec3.set(v3_2, 1, 1, 1);
|
||||
const textureBuffer = new Float32Array(width * 4);
|
||||
uploadJointData(textureBuffer, 0, v3_1, qt_1, v3_2, true);
|
||||
defaultJointsTexture.uploadData(textureBuffer.buffer);
|
||||
return defaultJointsTexture;
|
||||
}
|
||||
|
||||
@property([CCString])
|
||||
private _joints: string[] = [];
|
||||
@@ -136,36 +197,10 @@ export class Skeleton extends Asset {
|
||||
return super.destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认绑定姿势的骨骼贴图
|
||||
*/
|
||||
public getBindposeTexture (device: GFXDevice) {
|
||||
let texture = this._jointsTextures.get(null);
|
||||
if (texture) { return texture; }
|
||||
texture = new Texture2D();
|
||||
texture.setFilters(Filter.NEAREST, Filter.NEAREST);
|
||||
texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE);
|
||||
const type = selectJointsMediumType(device);
|
||||
const format = _jointsFormat[type];
|
||||
const width = Math.ceil(12 * 4 / GFXFormatInfos[format].size);
|
||||
const height = this.joints.length;
|
||||
texture.reset({ width, height, format });
|
||||
vec3.set(v3_1, 0, 0, 0);
|
||||
quat.set(qt_1, 0, 0, 0, 1);
|
||||
vec3.set(v3_2, 1, 1, 1);
|
||||
const textureBuffer = new Float32Array(width * height * 4);
|
||||
for (let i = 0; i < this.joints.length; i++) {
|
||||
this.uploadJointData(textureBuffer, 12 * i, v3_1, qt_1, v3_2, i === 0);
|
||||
}
|
||||
texture.uploadData(textureBuffer.buffer);
|
||||
this._jointsTextures.set(null, texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定动画片段的骨骼贴图
|
||||
*/
|
||||
public getJointsTexture (device: GFXDevice, clip: SkeletalAnimationClip) {
|
||||
public getJointsTextureWithClip (device: GFXDevice, clip: SkeletalAnimationClip) {
|
||||
let texture = this._jointsTextures.get(clip);
|
||||
if (texture) { return texture; }
|
||||
texture = new Texture2D();
|
||||
@@ -195,52 +230,13 @@ export class Skeleton extends Asset {
|
||||
vec3.add(v3_1, v3_1, T);
|
||||
quat.multiply(qt_1, R, bindpose.rotation);
|
||||
vec3.multiply(v3_2, S, bindpose.scale);
|
||||
this.uploadJointData(textureBuffer, 12 * (frames * i + frame), v3_1, qt_1, v3_2, i === 0);
|
||||
uploadJointData(textureBuffer, 12 * (frames * i + frame), v3_1, qt_1, v3_2, i === 0);
|
||||
}
|
||||
}
|
||||
texture.uploadData(textureBuffer.buffer);
|
||||
this._jointsTextures.set(clip, texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
// Linear Blending Skinning
|
||||
protected uploadJointDataLBS (out: Float32Array, base: number, pos: Vec3, rot: Quat, scale: Vec3, firstBone: boolean) {
|
||||
mat4.fromRTS(m4_1, rot, pos, scale);
|
||||
out[base + 0] = m4_1.m00;
|
||||
out[base + 1] = m4_1.m01;
|
||||
out[base + 2] = m4_1.m02;
|
||||
out[base + 3] = m4_1.m12;
|
||||
out[base + 4] = m4_1.m04;
|
||||
out[base + 5] = m4_1.m05;
|
||||
out[base + 6] = m4_1.m06;
|
||||
out[base + 7] = m4_1.m13;
|
||||
out[base + 8] = m4_1.m08;
|
||||
out[base + 9] = m4_1.m09;
|
||||
out[base + 10] = m4_1.m10;
|
||||
out[base + 11] = m4_1.m14;
|
||||
}
|
||||
|
||||
// Dual Quaternion Skinning
|
||||
protected uploadJointDataDQS (out: Float32Array, base: number, pos: Vec3, rot: Quat, scale: Vec3, firstBone: boolean) {
|
||||
// sign consistency
|
||||
if (firstBone) { quat.copy(dq_0, rot); }
|
||||
else if (quat.dot(dq_0, rot) < 0) { quat.scale(rot, rot, -1); }
|
||||
// conversion
|
||||
quat.set(dq_1, pos.x, pos.y, pos.z, 0);
|
||||
quat.scale(dq_1, quat.multiply(dq_1, dq_1, rot), 0.5);
|
||||
// upload
|
||||
out[base + 0] = rot.x;
|
||||
out[base + 1] = rot.y;
|
||||
out[base + 2] = rot.z;
|
||||
out[base + 3] = rot.w;
|
||||
out[base + 4] = dq_1.x;
|
||||
out[base + 5] = dq_1.y;
|
||||
out[base + 6] = dq_1.z;
|
||||
out[base + 7] = dq_1.w;
|
||||
out[base + 8] = scale.x;
|
||||
out[base + 9] = scale.y;
|
||||
out[base + 10] = scale.z;
|
||||
}
|
||||
}
|
||||
|
||||
cc.Skeleton = Skeleton;
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
* @category model
|
||||
*/
|
||||
|
||||
import { AnimationComponent } from '../../animation/animation-component';
|
||||
import { Filter, PixelFormat } from '../../assets/asset-enum';
|
||||
import { Texture2D } from '../../assets/texture-2d';
|
||||
import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../core/data/class-decorator';
|
||||
@@ -40,9 +41,8 @@ import { Node } from '../../scene-graph/node';
|
||||
import { Material } from '../assets/material';
|
||||
import { IMeshStruct, Mesh } from '../assets/mesh';
|
||||
import { Skeleton } from '../assets/skeleton';
|
||||
import { LCA, mapBuffer } from '../misc/utils';
|
||||
import { mapBuffer } from '../misc/utils';
|
||||
import { SkinningModelComponent } from './skinning-model-component';
|
||||
import { AnimationComponent } from '../../components';
|
||||
|
||||
const repeat = (n: number) => n - Math.floor(n);
|
||||
const batch_id: IGFXAttribute = { name: GFXAttributeName.ATTR_BATCH_ID, format: GFXFormat.R32F, isNormalized: false };
|
||||
@@ -140,15 +140,6 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent {
|
||||
super.skeleton = val;
|
||||
}
|
||||
|
||||
@property({ override: true, visible: false })
|
||||
get skinningRoot () {
|
||||
return this._skinningRoot;
|
||||
}
|
||||
set skinningRoot (val) {
|
||||
// @ts-ignore
|
||||
super.skinningRoot = val;
|
||||
}
|
||||
|
||||
public onLoad () {
|
||||
super.onLoad();
|
||||
this._batchMaterial = this.getSharedMaterial(0);
|
||||
@@ -207,11 +198,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent {
|
||||
}
|
||||
|
||||
public cookSkeletons () {
|
||||
// find the lowest AnimationComponent node as the new skinning root
|
||||
let root: Node | null = this.node;
|
||||
while (root && !root.getComponent(AnimationComponent)) { root = root.parent; }
|
||||
if (!root) { console.warn('skinning model component must be put under animation component'); return; }
|
||||
this._skinningRoot = root;
|
||||
if (!this._skinningRoot) { console.warn('no skinning root specified!'); return; }
|
||||
// merge joints accordingly
|
||||
const skeleton = new Skeleton();
|
||||
const bindposes: Mat4[] = [];
|
||||
|
||||
@@ -256,7 +256,7 @@ export class AnimationState extends Playable {
|
||||
for (const propertyCurve of propertyCurves) {
|
||||
const targetNode = root.getChildByPath(propertyCurve.path);
|
||||
if (!targetNode) {
|
||||
console.warn(`Target animation node referenced by path ${propertyCurve.path} is not found(from ${root.name}).`);
|
||||
// console.warn(`Target animation node referenced by path ${propertyCurve.path} is not found(from ${root.name}).`);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -114,22 +114,19 @@ export class SkeletalAnimationClip extends AnimationClip {
|
||||
}
|
||||
// convert to SkinningModelComponent.fid animation
|
||||
const values = [...Array(Math.ceil(this.sample * this._duration))].map((_, i) => i);
|
||||
root.getComponentsInChildren(RenderableComponent).forEach((comp) => {
|
||||
const isSkinningModel = comp instanceof SkinningModelComponent;
|
||||
if (isSkinningModel) {
|
||||
for (const comp of root.getComponentsInChildren(RenderableComponent)) {
|
||||
if (comp instanceof SkinningModelComponent && comp.skinningRoot === root) {
|
||||
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 } } };
|
||||
}
|
||||
if (!CC_EDITOR) { // rig non-skinning renderables
|
||||
if (isSkinningModel && (comp as SkinningModelComponent).skinningRoot === root) { return; }
|
||||
} else if (!CC_EDITOR) { // rig non-skinning renderables
|
||||
const path = getPathFromRoot(comp.node.parent!, root);
|
||||
const data = this.convertedData[path];
|
||||
if (!data || !data.props) { return; }
|
||||
if (!this.curveDatas[path]) { this.curveDatas[path] = {}; }
|
||||
this.curveDatas[path].props = this._convertToRiggingData(data.props, comp.node.parent!, root);
|
||||
}
|
||||
});
|
||||
}
|
||||
this._keys = [values.map((_, i) => i / this.sample)];
|
||||
this._converted = true;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
*/
|
||||
|
||||
import { Skeleton } from '../../3d/assets/skeleton';
|
||||
import { builtinResMgr } from '../../3d/builtin/init';
|
||||
import { SkeletalAnimationClip } from '../../animation';
|
||||
import { Texture2D } from '../../assets/texture-2d';
|
||||
import { GFXBuffer } from '../../gfx/buffer';
|
||||
@@ -67,7 +66,7 @@ export class SkinningModel extends Model {
|
||||
stride: UBOSkinningTexture.SIZE,
|
||||
});
|
||||
const nativeData = new Float32Array(4);
|
||||
const texture = builtinResMgr.get<Texture2D>('black-texture');
|
||||
const texture = Skeleton.getDefaultJointsTexture(this._device);
|
||||
this._jointsMedium = { buffer, nativeData, texture };
|
||||
}
|
||||
|
||||
@@ -83,12 +82,13 @@ export class SkinningModel extends Model {
|
||||
this._skeleton = skeleton;
|
||||
if (!skeleton || !skinningRoot) { return; }
|
||||
this._transform = skinningRoot;
|
||||
this._applyJointTexture(skeleton.getBindposeTexture(this._device));
|
||||
this._jointsMedium.texture = Skeleton.getDefaultJointsTexture(this._device);
|
||||
this._applyJointTexture(this._jointsMedium.texture);
|
||||
}
|
||||
|
||||
public uploadAnimationClip (clip: SkeletalAnimationClip) {
|
||||
if (!this._skeleton) { return; }
|
||||
this._applyJointTexture(this._skeleton.getJointsTexture(this._device, clip));
|
||||
this._applyJointTexture(this._skeleton.getJointsTextureWithClip(this._device, clip));
|
||||
if (this._jointsMedium) { this.uploadedClip = clip; }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user