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

SpotLightHelper: Fix offset when adjusting the scene / parent position #27487

Merged
merged 8 commits into from
Feb 12, 2024

Conversation

gkjohnson
Copy link
Collaborator

@gkjohnson gkjohnson commented Jan 3, 2024

Fixed #27437

Description

Previously SpotLightHelper was just copying the target light's matrixWorld into its local matrix field resulting in offsets when the parent was not at 0, 0, 0. Now the target light transform is correctly transformed into the local frame that the helper exists in and it's no longer required to add the helpers to the root of the scene.

This is for another PR but I'm wondering if "update" can just be removed and effectively called in "updateMatrixWorld"?

@gkjohnson gkjohnson added this to the r161 milestone Jan 3, 2024
Copy link

github-actions bot commented Jan 3, 2024

📦 Bundle size

Full ESM build, minified and gzipped.

Filesize dev Filesize PR Diff
676.2 kB (167.9 kB) 676.4 kB (168 kB) +193 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Filesize dev Filesize PR Diff
456.8 kB (110.7 kB) 456.8 kB (110.7 kB) +0 B

@Mugen87
Copy link
Collaborator

Mugen87 commented Jan 5, 2024

I understand this works but I'm a bit unhappy about the resulting complexity in updateMatrixWorld(). If the helper would have to be a child of the light, I assume things could be much easier.

@gkjohnson
Copy link
Collaborator Author

gkjohnson commented Jan 5, 2024

If the helper would have to be a child of the light, I assume things could be much easier.

The requirement to add helpers to the root scene has confused me countless times in the past. Asking users to add a helper to a specific parent so that it works betrays all expectation around the transform hierarchy works. This would be a significantly worse user experience for the helpers.

I understand this works but I'm a bit unhappy about the resulting complexity in updateMatrixWorld().

I don't agree that it's so complicated. These are matrix transformations that are used throughout the project. But either way this is a transformation that should be used across all helpers - BoxHelper, CameraHelper, DirectionalLightHelper, etc should all implement this the same way. If this is added then we should add a super class that shares an updateMatrixWorld implementation among all child helper classes.

Can you elaborate on what you feel is complex?

@Mugen87
Copy link
Collaborator

Mugen87 commented Jan 8, 2024

Can you elaborate on what you feel is complex?

The need to overwrite updateMatrixWorld() to begin with. But also the code itself is not something I would label as trivial.

My point is I favor an assumption like "spot light helper must be child of the light" if that makes the overall implementation easier.

@gkjohnson
Copy link
Collaborator Author

The need to overwrite updateMatrixWorld() to begin with.

There are quite a few other classes that also overwrite updateMatrixWorld including other Helpers. I would argue it's more user friendly to do this with the Helper-classes, anyway, since it means "update" doesn't need to be called. But either way the code here can be moved to the "update" function if this is an issue.

But also the code itself is not something I would label as trivial.

The transformation happening is no different than the one required for transforming an object into the camera frame. It's fundamental to rendering graphics.

@Mugen87
Copy link
Collaborator

Mugen87 commented Jan 14, 2024

I'm okay with merging this new version. As long as we keep the current helper policy we should make sure to fix #27437 and that's what the PR does.

@gkjohnson
Copy link
Collaborator Author

As long as we keep the current helper policy

What's the expectation for helpers? Some of them update themselves in updateMatrixWorld (Box3Helper, SkeletonHelper, PlaneHelper) while others require manually calling an update function (CameraHelper, DirectionalLightHelper, and the rest). Should their APIs be normalized?

@Mugen87
Copy link
Collaborator

Mugen87 commented Jan 14, 2024

If we want to refactor the world update matrix logic at some point, it would be good if updateMatrixWorld() and updateMatrix() are not overwritten in subclasses of Object3D.


}

this.matrix.decompose( this.position, this.quaternion, this.scale );
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are you doing this -- instead of retaining this.matrixAutoUpdate = false ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I suppose I could do that. It would probably be best to se matrixWorldAutoUpdate to false but tbh the matrix / matrixWorld update behavior is fairly confusing to me, now. For example the Object3D.lookAt function will call updateWorldMatrix which will automatically update the objects matrixWorld even if matrixWorldAutoUpdate is false.

It's not clear to me when they do and don't apply so I fallback to keeping all the values in sync which I think is more clear, anyway.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd avoid unnecessarily calling matrix.decompose() for this helper. JMO, of course.

if ( this.parent ) {

this.matrix
.copy( this.parent.matrixWorld )
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this matrix necessarily current?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I suppose that's true - I can call this.parent.updateMatrixWorld but it has the same caveats as mentioned in #27487 (comment) in that it will undermine the users parent.matrixWorldAutoUpdate setting if it's set to false.

@mrdoob mrdoob modified the milestones: r161, r162 Jan 31, 2024
@Mugen87 Mugen87 merged commit 1d2ab56 into mrdoob:dev Feb 12, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modifying the position of the scene can cause misalignment of the SpotLight and SpotLightHelper positions
4 participants