From b42de567af331c4fbc7bb7e0f7f301e802d4de60 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Mon, 25 May 2020 18:05:52 +0800 Subject: [PATCH] fix skinning model pose calc when missing joint nodes (#6721) * fix skinning model pose calc when missing joint nodes * fix camera.worldToScreen when world pos is behind near plane * fix batched skinning model material instantiation --- .../batched-skinning-model-component.ts | 4 +-- cocos/core/math/vec3.ts | 10 +++---- .../models/skeletal-animation-utils.ts | 29 +++++++------------ cocos/core/renderer/scene/camera.ts | 1 + 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/cocos/core/3d/framework/batched-skinning-model-component.ts b/cocos/core/3d/framework/batched-skinning-model-component.ts index a3d2b0b48b..a942c1e239 100644 --- a/cocos/core/3d/framework/batched-skinning-model-component.ts +++ b/cocos/core/3d/framework/batched-skinning-model-component.ts @@ -205,7 +205,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { public _onMaterialModified (idx: number, material: Material | null) { this.cookMaterials(); - super._onMaterialModified(idx, this.getMaterial(idx)); + super._onMaterialModified(idx, this.getMaterialInstance(idx)); } public cook () { @@ -218,7 +218,7 @@ export class BatchedSkinningModelComponent extends SkinningModelComponent { if (!this._batchMaterial) { this._batchMaterial = this.getMaterial(0); } - const mat = this.getMaterial(0); + const mat = this.getMaterialInstance(0); if (!mat || !this._batchMaterial || !this._batchMaterial.effectAsset) { console.warn('incomplete batch material!'); return; } diff --git a/cocos/core/math/vec3.ts b/cocos/core/math/vec3.ts index b8fce62da7..7f64d5845d 100644 --- a/cocos/core/math/vec3.ts +++ b/cocos/core/math/vec3.ts @@ -357,7 +357,7 @@ export class Vec3 extends ValueType { const y = a.y; const z = a.z; let rhw = m.m03 * x + m.m07 * y + m.m11 * z + m.m15; - rhw = rhw ? 1 / rhw : 1; + rhw = rhw ? Math.abs(1 / rhw) : 1; out.x = (m.m00 * x + m.m04 * y + m.m08 * z + m.m12) * rhw; out.y = (m.m01 * x + m.m05 * y + m.m09 * z + m.m13) * rhw; out.z = (m.m02 * x + m.m06 * y + m.m10 * z + m.m14) * rhw; @@ -372,7 +372,7 @@ export class Vec3 extends ValueType { const y = a.y; const z = a.z; let rhw = m.m03 * x + m.m07 * y + m.m11 * z; - rhw = rhw ? 1 / rhw : 1; + rhw = rhw ? Math.abs(1 / rhw) : 1; out.x = (m.m00 * x + m.m04 * y + m.m08 * z) * rhw; out.y = (m.m01 * x + m.m05 * y + m.m09 * z) * rhw; out.z = (m.m02 * x + m.m06 * y + m.m10 * z) * rhw; @@ -399,9 +399,9 @@ export class Vec3 extends ValueType { const x = v.x; const y = v.y; const z = v.z; - out.x = m.m00 * x + m.m01 * y + m.m02 * z + m.m03; - out.y = m.m04 * x + m.m05 * y + m.m06 * z + m.m07; - out.x = m.m08 * x + m.m09 * y + m.m10 * z + m.m11; + out.x = m.m00 * x + m.m04 * y + m.m08 * z + m.m12; + out.y = m.m01 * x + m.m05 * y + m.m09 * z + m.m13; + out.x = m.m02 * x + m.m06 * y + m.m10 * z + m.m14; return out; } diff --git a/cocos/core/renderer/models/skeletal-animation-utils.ts b/cocos/core/renderer/models/skeletal-animation-utils.ts index e5a7f2f5da..63e4ab19b4 100644 --- a/cocos/core/renderer/models/skeletal-animation-utils.ts +++ b/cocos/core/renderer/models/skeletal-animation-utils.ts @@ -56,7 +56,7 @@ export function selectJointsMediumFormat (device: GFXDevice): GFXFormat { } // negative zeros cannot be decoded correctly at GLSL 100 minimum highp float precision, 1/1024 -// and it has a significant effect on the final transformation +// and it has a significant impact on the final transformation function makeStable (n: number) { return n ? n : 0; } // Linear Blending Skinning @@ -224,20 +224,16 @@ export class JointTexturePool { for (let i = 0; i < joints.length; i++) { const node = skinningRoot.getChildByPath(joints[i]); const bound = boneSpaceBounds[i]; - if (!node) { // don't skip null `bound` here, or it becomes mesh-specific - if (buildTexture) { uploadJointData(textureBuffer, 12 * i, Mat4.IDENTITY, i === 0); } - continue; - } - getWorldTransformUntilRoot(node, skinningRoot, m4_1); + const mat = node ? getWorldTransformUntilRoot(node, skinningRoot, m4_1) : Mat4.IDENTITY; if (bound) { - aabb.transform(ab_1, bound, m4_1); + aabb.transform(ab_1, bound, mat); ab_1.getBoundary(v3_3, v3_4); Vec3.min(v3_min, v3_min, v3_3); Vec3.max(v3_max, v3_max, v3_4); } if (buildTexture) { - Mat4.multiply(m4_1, m4_1, bindposes[i]); - uploadJointData(textureBuffer, 12 * i, m4_1, i === 0); + if (mat !== Mat4.IDENTITY) { Mat4.multiply(mat, mat, bindposes[i]); } + uploadJointData(textureBuffer, 12 * i, mat, i === 0); } } const bounds = [new aabb()]; texture.bounds.set(mesh.hash, bounds); @@ -285,24 +281,21 @@ export class JointTexturePool { const bound = bounds[frame]; for (let i = 0; i < totalJoints; i++, offset += 12) { const nodeData = clipData.data[joints[i]]; - if (!nodeData) { // don't skip null `boneSpaceBounds` here, or it becomes mesh-specific + let mat = Mat4.IDENTITY; + if (nodeData) { mat = nodeData.worldMatrix.values[frame] as Mat4; } + else { const node = skinningRoot.getChildByPath(joints[i]); // fallback to default pose - const mat = node ? getWorldTransformUntilRoot(node, skinningRoot, m4_1) : Mat4.IDENTITY; - if (buildTexture) { uploadJointData(textureBuffer, offset, mat, i === 0); } - continue; + mat = node ? getWorldTransformUntilRoot(node, skinningRoot, m4_1) : Mat4.IDENTITY; } - const matrix = nodeData.worldMatrix.values as Mat4[]; - const m = matrix[frame]; const boneSpaceBound = boneSpaceBounds[i]; if (boneSpaceBound) { - aabb.transform(ab_1, boneSpaceBound, m); + aabb.transform(ab_1, boneSpaceBound, mat); ab_1.getBoundary(v3_3, v3_4); Vec3.min(bound.center, bound.center, v3_3); Vec3.max(bound.halfExtents, bound.halfExtents, v3_4); } if (buildTexture) { - const bindpose = bindposes[i]; - Mat4.multiply(m4_1, m, bindpose); + if (mat !== Mat4.IDENTITY) { Mat4.multiply(m4_1, mat, bindposes[i]); } uploadJointData(textureBuffer, offset, m4_1, i === 0); } } diff --git a/cocos/core/renderer/scene/camera.ts b/cocos/core/renderer/scene/camera.ts index 5b97287179..3fbda67814 100644 --- a/cocos/core/renderer/scene/camera.ts +++ b/cocos/core/renderer/scene/camera.ts @@ -597,6 +597,7 @@ export class Camera { const ch = this.viewport.height * this._height; Vec3.transformMat4(out, worldPos, this.matViewProj); + out.x = cx + (out.x + 1) * 0.5 * cw; out.y = cy + (out.y + 1) * 0.5 * ch; out.z = out.z * 0.5 + 0.5;