Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SkinnedRenderer.boundingBox updates are not timely #2267

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions packages/core/src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export class Renderer extends Component implements IComponentCustomClone {
protected _dirtyUpdateFlag: number = 0;
@ignoreClone
protected _rendererLayer: Vector4 = new Vector4();
@ignoreClone
protected _transform: Transform;

@deepClone
private _shaderData: ShaderData = new ShaderData(ShaderDataGroup.Renderer);
Expand Down Expand Up @@ -170,7 +172,7 @@ export class Renderer extends Component implements IComponentCustomClone {
this._addResourceReferCount(this.shaderData, 1);

this._onTransformChanged = this._onTransformChanged.bind(this);
this._registerEntityTransformListener();
this._attachTransform(entity.transform);

shaderData.enableMacro(Renderer._receiveShadowMacro);
shaderData.setVector4(Renderer._rendererLayerProperty, this._rendererLayer);
Expand Down Expand Up @@ -364,7 +366,7 @@ export class Renderer extends Component implements IComponentCustomClone {
protected override _onDestroy(): void {
super._onDestroy();

this._unRegisterEntityTransformListener();
this._attachTransform(null);
this._addResourceReferCount(this.shaderData, -1);

const materials = this._materials;
Expand All @@ -390,7 +392,7 @@ export class Renderer extends Component implements IComponentCustomClone {
* @internal
*/
_updateTransformShaderData(context: RenderContext, onlyMVP: boolean, batched: boolean): void {
const worldMatrix = this.entity.transform.worldMatrix;
const worldMatrix = this._transform.worldMatrix;
if (onlyMVP) {
this._updateProjectionRelatedShaderData(context, worldMatrix, batched);
} else {
Expand Down Expand Up @@ -440,7 +442,7 @@ export class Renderer extends Component implements IComponentCustomClone {
Matrix.invert(worldMatrix, normalMatrix);
normalMatrix.transpose();

shaderData.setMatrix(Renderer._localMatrixProperty, this.entity.transform.localMatrix);
shaderData.setMatrix(Renderer._localMatrixProperty, this._transform.localMatrix);
shaderData.setMatrix(Renderer._worldMatrixProperty, worldMatrix);
shaderData.setMatrix(Renderer._mvMatrixProperty, mvMatrix);
shaderData.setMatrix(Renderer._mvInvMatrixProperty, mvInvMatrix);
Expand All @@ -463,15 +465,10 @@ export class Renderer extends Component implements IComponentCustomClone {
/**
* @internal
*/
protected _registerEntityTransformListener(): void {
this.entity.transform._updateFlagManager.addListener(this._onTransformChanged);
}

/**
* @internal
*/
protected _unRegisterEntityTransformListener(): void {
this.entity.transform._updateFlagManager.removeListener(this._onTransformChanged);
protected _attachTransform(transform: Transform): void {
this._transform?._updateFlagManager.removeListener(this._onTransformChanged);
transform?._updateFlagManager.addListener(this._onTransformChanged);
this._transform = transform;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/mesh/SkinnedMeshRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class SkinnedMeshRenderer extends MeshRenderer {
* @internal
*/
override _updateTransformShaderData(context: RenderContext, onlyMVP: boolean, batched: boolean): void {
const worldMatrix = (this.skin?.rootBone ?? this.entity).transform.worldMatrix;
const worldMatrix = this._transform.worldMatrix;
if (onlyMVP) {
this._updateProjectionRelatedShaderData(context, worldMatrix, batched);
} else {
Expand Down Expand Up @@ -219,7 +219,7 @@ export class SkinnedMeshRenderer extends MeshRenderer {
protected override _updateBounds(worldBounds: BoundingBox): void {
const rootBone = this.skin?.rootBone;
if (rootBone) {
BoundingBox.transform(this._localBounds, rootBone.transform.worldMatrix, worldBounds);
BoundingBox.transform(this._localBounds, this._transform.worldMatrix, worldBounds);
} else {
super._updateBounds(worldBounds);
}
Expand Down Expand Up @@ -265,6 +265,7 @@ export class SkinnedMeshRenderer extends MeshRenderer {
}
break;
case SkinUpdateFlag.RootBoneChanged:
this._attachTransform((<Entity>value).transform);
this._dirtyUpdateFlag |= RendererUpdateFlags.WorldVolume;
break;
}
Expand Down
10 changes: 9 additions & 1 deletion tests/src/core/SkinnedMeshRenderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ describe("SkinnedMeshRenderer", async () => {
new Matrix(1, 0, 0, -1, 0, 0, 0, -1, 0, 0, -2, -1).invert()
];

const position = new Vector3();
const modelMesh = new ModelMesh(engine);
const entity = rootEntity.createChild("SkinEntity");
entity.transform.position = position;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the position of entity after creating rootBone.

Setting the position of entity after creating rootBone ensures that all transformations are applied correctly.

- entity.transform.position = position;

Committable suggestion was skipped due to low confidence.

const rootBone = entity.createChild("RootBone");
rootBone.createChild("Joint0");
rootBone.createChild("Joint1");
Expand All @@ -84,6 +86,8 @@ describe("SkinnedMeshRenderer", async () => {

skinnedMR.skin = skin;
engine.update();
expect(skinnedMR.bounds.min).to.deep.eq(position);
expect(skinnedMR.bounds.max).to.deep.eq(position);

// Test that the skin is set correctly.
expect(skinnedMR.skin).to.be.equal(skin);
Expand All @@ -94,10 +98,14 @@ describe("SkinnedMeshRenderer", async () => {

// Test that the rootBone is set correctly.
expect(skinnedMR.rootBone).to.be.equal(rootBone0);

position.set(1, 0, 0);
rootBone0.transform.position = position;
expect(skinnedMR.bounds.min).to.deep.eq(position);
expect(skinnedMR.bounds.max).to.deep.eq(position);
});

it("clone", () => {
// @ts-ignore
const modelMesh = new ModelMesh(engine);

const positions = [new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0)];
Expand Down
Loading