-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
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
Sprite: take camera FOV into account #26535
Sprite: take camera FOV into account #26535
Conversation
Make `Sprite`s which use a `SpriteMaterial` with `sizeAttenuation` set to `false` also consider the FOV of a Perspective Camera. This ensures that `Sprite`s maintain a constant size in screen-space regardless of the changes in the FOV , not only the distance from the camera. The existing issue was that `Sprite`s would still change in size when the FOV of the camera was modified, even if `sizeAttenuation` was set to `false`. This is particularly useful when animating the FOV of the camera if one expects the Sprites with `sizeAttenuation` set to `false` to maintain their original size. To do this, modify the `sprite.glsl.js` shader to also take into account the camera FOV. Specifically, divide the `scale` factor by the tangent of half of the FOV. This value is located at the [1][1] position in the perspective projection matrix in OpenGL, so we use it directly. See https://stackoverflow.com/a/57822509 for details. Also add an example that shows non-size attenuated `Sprite`s specifically, with sliders that change the distance of the camera and the FOV of the camera.
📦 Bundle sizeFull ESM build, minified and gzipped.
🌳 Bundle size after tree-shakingMinimal build including a renderer, camera, empty scene, and dependencies.
|
Make the `Sprite` object also take the FOV of the camera into account in its raycast method.
I've realized that this change would cause a lot of developers issues if it was implemented like this: everyone relying on Basically it would be a breaking change in the sense that any developer that upgrades to a new version of Three which includes this change would suddenly have their sprites be way smaller than before the upgrade, without a clear reason why. Existing apps would suddenly look different. So I'll make this be dependent on another parameter on |
How about a one-liner in your app, instead, applied whenever For example, spriteA.scale.setScalar( 0.2 * camera.fov / 60 ); |
That's the first thing I went for, but then I realized that:
So in general the reason to do this is that we can achieve the same as the one liner above but do it without using any extra CPU cycles nor having to remember to do it for each sprite. |
@WestLangley what do you think of the above? Could we maybe try and work on this so we can merge it? Happy to discuss the changes if needed! |
Actually, I am not the one who makes that decision. :-) @Mugen87 will make a decision based on what he believes is best for the project. |
When you have a large number of sprites (let's say > 100), plane.quaternion.copy( camera.quaternion ); Using this in combination with The workaround @WestLangley has shared seems like a good solution. Considering the changes to the core (a new material property and the shader/renderer modifications), I'm afraid the PR seems not justified at the moment. |
Makes sense, thank you both for reviewing. |
Make
Sprite
s which use aSpriteMaterial
also consider the FOV of a Perspective Camera.This ensures that
Sprite
s maintain a constant size in screen-space regardless of the changes in the FOV , not only the distance from the camera.The existing issue was that
Sprite
s would still change in size when the FOV of the camera was modified, even ifsizeAttenuation
was set tofalse
.To do this, modify the
sprite.glsl.js
shader to also take into account the camera FOV.Specifically, multiply the
scale
factor by the tangent of half of the FOV. This value's reciprocal (i.e. 1 / the value) is located at the [1][1] position in the perspective projection matrix in OpenGL, so we use it directly.See https://stackoverflow.com/a/57822509 for details.
Finally, add an example that shows non-size attenuated
Sprite
s specifically, with sliders that change the distance of the camera and the FOV of the camera.Contribution also by @vincent-lecrubier-skydio.