Origin/bugfix (#408)

* The final `set skeleton` call destroys and rebuilds the GPU bone resources that were bound in the previous two steps. However, the descriptor set is not rebound. As a result, the draw call for this subModel cannot access the bone matrices, so the entire character fails to render (this is not vertex deformation; the character does not appear at all). The fix should replace the reference in `descriptor.buffer` with the new buffer, mark it dirty, and lazily rebuild the descriptor before the next draw.

* opt

* fix: retain negative scale signs in Node.setWorldScale

* test: retain negative scale signs in Node.setWorldScale

* fix
This commit is contained in:
llsansun
2026-11-12 09:48:55 +08:00
committed by GitHub
parent 4a0dbc977a
commit 111fe1755d
5 changed files with 365 additions and 8 deletions
+12 -3
View File
@@ -2819,9 +2819,18 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable {
Mat3.multiplyMat4(m3_1, m3_1, m4_2);
const localScale = self._lscale;
localScale.x = Vec3.set(v3_a, m3_1.m00, m3_1.m01, m3_1.m02).length();
localScale.y = Vec3.set(v3_a, m3_1.m03, m3_1.m04, m3_1.m05).length();
localScale.z = Vec3.set(v3_a, m3_1.m06, m3_1.m07, m3_1.m08).length();
// Restore the requested signs. For negative X, the parent may already supply a reflection.
const parentScale = parent._scale;
const parentSign = (parentScale.x < 0 ? -1 : 1) * (parentScale.y < 0 ? -1 : 1) * (parentScale.z < 0 ? -1 : 1);
const localScaleX = Vec3.set(v3_a, m3_1.m00, m3_1.m01, m3_1.m02).length() * (worldScale.x < 0 ? -parentSign : 1);
const localScaleY = Vec3.set(v3_a, m3_1.m03, m3_1.m04, m3_1.m05).length() * (worldScale.y < 0 ? -1 : 1);
const localScaleZ = Vec3.set(v3_a, m3_1.m06, m3_1.m07, m3_1.m08).length() * (worldScale.z < 0 ? -1 : 1);
if ((localScale.x < 0) !== (localScaleX < 0)
|| (localScale.y < 0) !== (localScaleY < 0)
|| (localScale.z < 0) !== (localScaleZ < 0)) {
rotationFlag = TransformBit.ROTATION;
}
Vec3.set(localScale, localScaleX, localScaleY, localScaleZ);
if (localScale.x === 0 || localScale.y === 0 || localScale.z === 0) {
rotationFlag = TransformBit.ROTATION;
}
+22 -5
View File
@@ -684,7 +684,12 @@ const Quaternion &Node::getWorldRotation() const { // NOLINT(misc-no-recursion)
void Node::setWorldScale(float x, float y, float z) {
bool forceUpdate = _parent != nullptr && (_transformFlags & static_cast<uint32_t>(TransformBit::SCALE)) != static_cast<uint32_t>(TransformBit::NONE);
if (!forceUpdate && _worldScale.approxEquals({x, y, z})) {
// World-scale decomposition can hide local mirror signs. Preserve the same-value no-op for nonnegative targets.
const bool parentedNonnegativeTarget = _parent != nullptr && x >= 0.F && y >= 0.F && z >= 0.F;
if (!forceUpdate && (_parent == nullptr || (y >= 0.F && z >= 0.F)) &&
_worldScale.approxEquals({x, y, z}) &&
(_worldScale.x < 0.F) == (x < 0.F) &&
(parentedNonnegativeTarget || ((_localScale.y < 0.F) == (y < 0.F) && (_localScale.z < 0.F) == (z < 0.F)))) {
return;
}
@@ -738,10 +743,22 @@ void Node::setWorldScale(float x, float y, float z) {
// remove rotation part of the local matrix
Mat3::multiply(localRotInv, localRS, &localRS);
// extract scaling part from local matrix
_localScale.x = Vec3{localRS.m[0], localRS.m[1], localRS.m[2]}.length();
_localScale.y = Vec3{localRS.m[3], localRS.m[4], localRS.m[5]}.length();
_localScale.z = Vec3{localRS.m[6], localRS.m[7], localRS.m[8]}.length();
// Restore the requested signs after extracting the scale magnitudes.
// A negative X request accounts for parent reflection. A nonnegative X keeps
// the original positive local X behavior.
const Vec3 &parentScale = _parent->_worldScale;
const float parentSign = (parentScale.x < 0.F ? -1.F : 1.F) *
(parentScale.y < 0.F ? -1.F : 1.F) *
(parentScale.z < 0.F ? -1.F : 1.F);
const float localScaleX = Vec3{localRS.m[0], localRS.m[1], localRS.m[2]}.length() * (_worldScale.x < 0.F ? -parentSign : 1.F);
const float localScaleY = Vec3{localRS.m[3], localRS.m[4], localRS.m[5]}.length() * (_worldScale.y < 0.F ? -1.F : 1.F);
const float localScaleZ = Vec3{localRS.m[6], localRS.m[7], localRS.m[8]}.length() * (_worldScale.z < 0.F ? -1.F : 1.F);
if ((_localScale.x < 0.F) != (localScaleX < 0.F) ||
(_localScale.y < 0.F) != (localScaleY < 0.F) ||
(_localScale.z < 0.F) != (localScaleZ < 0.F)) {
rotationFlag = TransformBit::ROTATION;
}
_localScale.set(localScaleX, localScaleY, localScaleZ);
if (_localScale.x == 0 || _localScale.y == 0 || _localScale.z == 0) {
rotationFlag = TransformBit::ROTATION;
+147
View File
@@ -239,4 +239,151 @@ TEST(NodeTest, angleAfterQuaternionWriteback) {
EXPECT_NEAR(node->getAngle2D(), 120.F, 1e-4F);
}
TEST(NodeTest, setWorldScaleSignedTargetsAreIdempotent) {
for (int signs = 0; signs < 8; ++signs) {
cc::IntrusivePtr<cc::Node> parent(new Node());
cc::IntrusivePtr<cc::Node> child(new Node());
child->setParent(parent);
const Vec3 target((signs & 1) ? -2.F : 2.F,
(signs & 2) ? -3.F : 3.F,
(signs & 4) ? -4.F : 4.F);
child->setWorldScale(target);
Mat4 expected;
Mat4::fromRTS(Quaternion::identity(), Vec3::ZERO, target, &expected);
EXPECT_TRUE(child->getScale().approxEquals(target));
EXPECT_TRUE(child->getWorldMatrix().approxEquals(expected));
child->setWorldScale(target);
EXPECT_TRUE(child->getScale().approxEquals(target));
EXPECT_TRUE(child->getWorldMatrix().approxEquals(expected));
}
}
TEST(NodeTest, setWorldScaleWithSignedParent) {
for (int signs = 0; signs < 8; ++signs) {
cc::IntrusivePtr<cc::Node> parent(new Node());
const float signX = (signs & 1) ? -1.F : 1.F;
const float signY = (signs & 2) ? -1.F : 1.F;
const float signZ = (signs & 4) ? -1.F : 1.F;
parent->setScale(2.F * signX, 3.F * signY, 4.F * signZ);
cc::IntrusivePtr<cc::Node> child(new Node());
child->setParent(parent);
child->setWorldScale(-6.F, 9.F, 8.F);
const Vec3 expectedLocal(-3.F * signX * signY * signZ, 3.F, 2.F);
EXPECT_TRUE(child->getScale().approxEquals(expectedLocal));
EXPECT_TRUE(child->getWorldScale().approxEquals(Vec3(-6.F, 9.F, 8.F)));
Mat4 expected;
Mat4::fromRTS(Quaternion::identity(), Vec3::ZERO,
Vec3(-6.F * signY * signZ, 9.F * signY, 8.F * signZ), &expected);
EXPECT_TRUE(child->getWorldMatrix().approxEquals(expected));
child->setWorldScale(-6.F, 9.F, 8.F);
EXPECT_TRUE(child->getScale().approxEquals(expectedLocal));
EXPECT_TRUE(child->getWorldMatrix().approxEquals(expected));
child->setWorldScale(6.F, 9.F, 8.F);
EXPECT_TRUE(child->getScale().approxEquals(Vec3(3.F, 3.F, 2.F)));
EXPECT_TRUE(child->getWorldScale().approxEquals(Vec3(6.F * signX * signY * signZ, 9.F, 8.F)));
Mat4::fromRTS(Quaternion::identity(), Vec3::ZERO,
Vec3(6.F * signX, 9.F * signY, 8.F * signZ), &expected);
EXPECT_TRUE(child->getWorldMatrix().approxEquals(expected));
}
}
TEST(NodeTest, setWorldScaleSignsDoNotDependOnPreviousLocalScale) {
cc::IntrusivePtr<cc::Node> parent(new Node());
cc::IntrusivePtr<cc::Node> child(new Node());
child->setParent(parent);
child->setScale(-1.F, -1.F, -1.F);
child->getWorldMatrix();
child->setWorldScale(2.F, -3.F, 4.F);
EXPECT_TRUE(child->getScale().approxEquals(Vec3(2.F, -3.F, 4.F)));
const Mat4 firstMatrix = child->getWorldMatrix();
child->setWorldScale(2.F, -3.F, 4.F);
EXPECT_TRUE(child->getScale().approxEquals(Vec3(2.F, -3.F, 4.F)));
EXPECT_TRUE(child->getWorldMatrix().approxEquals(firstMatrix));
child->setWorldScale(2.F, 3.F, 4.F);
EXPECT_TRUE(child->getScale().approxEquals(Vec3(2.F, 3.F, 4.F)));
}
TEST(NodeTest, setWorldScaleChangesMirrorAxisWhenWorldScaleMatches) {
cc::IntrusivePtr<cc::Node> parent(new Node());
cc::IntrusivePtr<cc::Node> child(new Node());
child->setParent(parent);
child->setWorldScale(2.F, -3.F, 4.F);
EXPECT_TRUE(child->getWorldScale().approxEquals(Vec3(-2.F, 3.F, 4.F)));
Mat4 expected;
Mat4::fromRTS(Quaternion::identity(), Vec3::ZERO, Vec3(2.F, -3.F, 4.F), &expected);
EXPECT_TRUE(child->getWorldMatrix().approxEquals(expected));
child->setWorldScale(-2.F, 3.F, 4.F);
EXPECT_TRUE(child->getScale().approxEquals(Vec3(-2.F, 3.F, 4.F)));
Mat4::fromRTS(Quaternion::identity(), Vec3::ZERO, Vec3(-2.F, 3.F, 4.F), &expected);
EXPECT_TRUE(child->getWorldMatrix().approxEquals(expected));
child->setWorldScale(-2.F, 3.F, 4.F);
EXPECT_TRUE(child->getWorldMatrix().approxEquals(expected));
child->setScale(2.F, -3.F, -4.F);
EXPECT_TRUE(child->getWorldScale().approxEquals(Vec3(2.F, 3.F, 4.F)));
const Mat4 matrixBefore = child->getWorldMatrix();
child->setWorldScale(2.F, 3.F, 4.F);
EXPECT_TRUE(child->getScale().approxEquals(Vec3(2.F, -3.F, -4.F)));
EXPECT_TRUE(child->getWorldMatrix().approxEquals(matrixBefore));
}
TEST(NodeTest, setWorldScaleChangesXWithTinyNegativeY) {
cc::IntrusivePtr<cc::Node> parent(new Node());
cc::IntrusivePtr<cc::Node> child(new Node());
child->setParent(parent);
child->setScale(2.F, -1e-7F, 4.F);
EXPECT_FLOAT_EQ(child->getWorldMatrix().m[0], 2.F);
child->setWorldScale(-2.F, -1e-7F, 4.F);
EXPECT_FLOAT_EQ(child->getWorldMatrix().m[0], -2.F);
}
TEST(NodeTest, setWorldScalePreservesTinyXSignChange) {
cc::IntrusivePtr<cc::Node> parent(new Node());
cc::IntrusivePtr<cc::Node> child(new Node());
child->setParent(parent);
child->setScale(1e-7F, 1.F, 1.F);
child->getWorldScale();
child->setWorldScale(-1e-7F, 1.F, 1.F);
EXPECT_TRUE(child->getScale().x < 0.F);
EXPECT_TRUE(child->getWorldScale().x < 0.F);
EXPECT_TRUE(child->getWorldMatrix().m[0] < 0.F);
child->setWorldScale(1e-7F, 1.F, 1.F);
EXPECT_TRUE(child->getScale().x > 0.F);
EXPECT_TRUE(child->getWorldScale().x > 0.F);
EXPECT_TRUE(child->getWorldMatrix().m[0] > 0.F);
}
TEST(NodeTest, setWorldScalePreservesLocalRotationWithSignedTargets) {
cc::IntrusivePtr<cc::Node> parent(new Node());
parent->setScale(2.F, 2.F, 2.F);
cc::IntrusivePtr<cc::Node> child(new Node());
child->setParent(parent);
child->setRotationFromEuler(10.F, 30.F, 25.F);
const Quaternion localRotation = child->getRotation();
child->setScale(-1.F, -1.F, 1.F);
child->getWorldMatrix();
child->setWorldScale(-2.F, -3.F, 4.F);
EXPECT_TRUE(child->getRotation().approxEquals(localRotation));
Mat4 expected;
Mat4::fromRTS(localRotation, Vec3::ZERO, Vec3(-2.F, -3.F, 4.F), &expected);
EXPECT_TRUE(child->getWorldMatrix().approxEquals(expected));
child->setWorldScale(-2.F, -3.F, 4.F);
EXPECT_TRUE(child->getRotation().approxEquals(localRotation));
EXPECT_TRUE(child->getWorldMatrix().approxEquals(expected));
}
TEST(NodeTest, setWorldScaleSignChangeRefreshesWorldRotation) {
cc::IntrusivePtr<cc::Node> parent(new Node());
cc::IntrusivePtr<cc::Node> child(new Node());
child->setParent(parent);
child->setScale(1.F, -1.F, 1.F);
const Quaternion before = child->getWorldRotation();
EXPECT_FALSE(before.approxEquals(Quaternion::identity()));
child->setWorldScale(-2.F, 3.F, 4.F);
EXPECT_TRUE(child->getRotation().approxEquals(Quaternion::identity()));
EXPECT_TRUE(child->getWorldRotation().approxEquals(Quaternion::identity()));
}
} // namespace
+179
View File
@@ -0,0 +1,179 @@
import { Mat4, Quat, Vec3 } from '../../cocos/core/math';
import { Node, TransformBit } from '../../cocos/scene-graph';
import type { UISkew } from '../../cocos/2d/framework/ui-skew';
import { captureWarnIDs } from '../utils/log-capture';
function expectVec3 (actual: Readonly<Vec3>, expected: Readonly<Vec3>): void {
expect(actual.x).toBeCloseTo(expected.x, 8);
expect(actual.y).toBeCloseTo(expected.y, 8);
expect(actual.z).toBeCloseTo(expected.z, 8);
}
function signedScale (signs: number, x: number, y: number, z: number): Vec3 {
return new Vec3(signs & 1 ? -x : x, signs & 2 ? -y : y, signs & 4 ? -z : z);
}
describe('Node.setWorldScale signed scale', () => {
test('accepts negative input on every axis and preserves the requested mirror in the matrix', () => {
const parent = new Node();
const child = new Node();
child.parent = parent;
const warnings = captureWarnIDs();
for (let signs = 0; signs < 8; ++signs) {
const target = signedScale(signs, 2, 3, 4);
child.setWorldScale(target);
expectVec3(child.scale, target);
expectVec3(new Vec3(child.worldMatrix.m00, child.worldMatrix.m05, child.worldMatrix.m10), target);
const expectedWorld = new Vec3(target.x * target.y * target.z < 0 ? -2 : 2, 3, 4);
expectVec3(child.worldScale, expectedWorld);
const matrix = child.worldMatrix.clone();
child.setWorldScale(target.x, target.y, target.z);
expect(child.worldMatrix.equals(matrix)).toBe(true);
}
expect(warnings.captured).toEqual([]);
});
test('preserves a negative X sign even when its magnitude is below the native equality tolerance', () => {
const parent = new Node();
const child = new Node();
child.parent = parent;
child.setScale(1e-7, 1, 1);
child.getWorldScale();
child.setWorldScale(-1e-7, 1, 1);
expect(child.scale.x).toBeLessThan(0);
expect(child.worldMatrix.m00).toBeLessThan(0);
});
test('changes the requested mirror axis even when canonical world-scale readback is identical', () => {
const parent = new Node();
const child = new Node();
child.parent = parent;
child.setWorldScale(2, -3, 4);
expectVec3(child.worldScale, new Vec3(-2, 3, 4));
child.setWorldScale(-2, 3, 4);
expectVec3(child.worldScale, new Vec3(-2, 3, 4));
expectVec3(new Vec3(child.worldMatrix.m00, child.worldMatrix.m05, child.worldMatrix.m10), new Vec3(-2, 3, 4));
child.setScale(2, -3, -4);
child.getWorldScale();
child.setWorldScale(2, 3, 4);
expectVec3(child.scale, new Vec3(2, 3, 4));
});
test('restores requested signs independently of old local signs with orientation-preserving parents', () => {
for (const parentSigns of [0, 3, 5, 6]) {
for (let localSigns = 0; localSigns < 8; ++localSigns) {
for (let targetSigns = 0; targetSigns < 8; ++targetSigns) {
const parent = new Node();
parent.setScale(signedScale(parentSigns, 2, 3, 4));
parent.setRotationFromEuler(10, 25, 40);
const child = new Node();
child.parent = parent;
child.setRotationFromEuler(20, 35, 15);
child.setScale(signedScale(localSigns, 1, 2, 3));
const rotation = child.rotation.clone();
const target = signedScale(targetSigns, 2, 3, 4);
child.setWorldScale(target);
const expectedWorld = new Vec3(target.x * target.y * target.z < 0 ? -2 : 2, 3, 4);
expectVec3(child.worldScale, expectedWorld);
expect(child.rotation.equals(rotation)).toBe(true);
expect(child.scale.y < 0).toBe(target.y < 0);
expect(child.scale.z < 0).toBe(target.z < 0);
expect(child.scale.x < 0).toBe(target.x < 0);
const matrix = child.worldMatrix.clone();
child.setWorldScale(target);
expect(child.worldMatrix.equals(matrix)).toBe(true);
expectVec3(child.worldScale, expectedWorld);
}
}
}
});
test('preserves inherited reflections for positive scale and for an already negative world X', () => {
const cases = [
{ parentScale: new Vec3(-1, 1, 1), expected: new Vec3(-2, 3, 4) },
{ parentScale: new Vec3(1, -1, 1), expected: new Vec3(2, -3, 4) },
{ parentScale: new Vec3(1, 1, -1), expected: new Vec3(2, 3, -4) },
];
for (const { parentScale, expected } of cases) {
const parent = new Node();
parent.setScale(parentScale);
const child = new Node();
child.parent = parent;
child.setWorldScale(2, 3, 4);
expectVec3(new Vec3(child.worldMatrix.m00, child.worldMatrix.m05, child.worldMatrix.m10), expected);
expectVec3(child.worldScale, new Vec3(-2, 3, 4));
const matrix = child.worldMatrix.clone();
child.setWorldScale(-2, 3, 4);
expect(child.worldMatrix.equals(matrix)).toBe(true);
}
});
test('applies negative Y and Z independently while inheriting the parent X reflection', () => {
const parent = new Node();
parent.setScale(-1, 1, 1);
const child = new Node();
child.parent = parent;
child.setWorldScale(2, -3, -4);
expectVec3(child.scale, new Vec3(2, -3, -4));
expectVec3(new Vec3(child.worldMatrix.m00, child.worldMatrix.m05, child.worldMatrix.m10), new Vec3(-2, -3, -4));
const matrix = child.worldMatrix.clone();
child.setWorldScale(2, -3, -4);
expect(child.worldMatrix.equals(matrix)).toBe(true);
});
test('recomputes world rotation caches when the local mirror signs change', () => {
const parent = new Node();
const child = new Node();
child.parent = parent;
const grandchild = new Node();
grandchild.parent = child;
grandchild.setRotationFromEuler(20, 35, 15);
child.getWorldRotation();
grandchild.getWorldRotation();
child.setWorldScale(2, -3, 4);
const expected = new Quat();
Mat4.toSRT(child.worldMatrix, expected, null, null);
expect(child.worldRotation.equals(expected)).toBe(true);
Mat4.toSRT(grandchild.worldMatrix, expected, null, null);
expect(grandchild.worldRotation.equals(expected)).toBe(true);
});
test('keeps the existing zero-scale recovery path while restoring negative signs', () => {
const parent = new Node();
parent.setScale(2, 2, 2);
const child = new Node();
child.parent = parent;
child.setRotationFromEuler(10, 20, 30);
const rotation = child.rotation.clone();
child.setWorldScale(0, 0, 0);
child.getWorldMatrix();
child.setWorldScale(-2, -3, 4);
expectVec3(child.scale, new Vec3(-1, -1.5, 2));
expectVec3(child.worldScale, new Vec3(2, 3, 4));
expect(child.rotation.equals(rotation)).toBe(true);
const matrix = child.worldMatrix.clone();
child.setWorldScale(-2, -3, 4);
expect(child.worldMatrix.equals(matrix)).toBe(true);
});
test('uses parent reflection signs without counting the UISkew component reflection', () => {
const root = new Node();
const parent = new Node();
parent.parent = root;
parent.setScale(2, 3, 4);
const child = new Node();
child.parent = parent;
// The UISkew lifecycle is stubbed; matrix composition and decomposition run normally.
parent._uiProps._uiSkewComp = { x: 180, y: 0, rotational: true, isSkewEnabled: () => true } as UISkew;
Node._incSkewCompCount();
try {
parent.invalidateChildren(TransformBit.SKEW);
child.setWorldScale(-2, 3, 4);
expectVec3(child.scale, new Vec3(-1, 1, 1));
expectVec3(child.worldScale, new Vec3(-2, 3, 4));
} finally {
parent._uiProps._uiSkewComp = null;
Node._decSkewCompCount();
}
});
});
@@ -0,0 +1,5 @@
{
"constantOverrides": {
"USE_UI_SKEW": true
}
}