Forked from ayamflow/gist:96a1f554c3f88eef2f9d0024fc42940f
Created
May 15, 2024 12:37
-
-
Save saywassup/2682cfeb9bc63ea80be5ba501b731005 to your computer and use it in GitHub Desktop.
Threejs Fit plane to screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var cameraZ = camera.position.z; | |
| var planeZ = 5; | |
| var distance = cameraZ - planeZ; | |
| var aspect = viewWidth / viewHeight; | |
| var vFov = camera.fov * Math.PI / 180; | |
| var planeHeightAtDistance = 2 * Math.tan(vFov / 2) * distance; | |
| var planeWidthAtDistance = planeHeightAtDistance * aspect; | |
| // or | |
| let dist = camera.position.z - mesh.position.z; | |
| let height = ... // desired height to fit | |
| camera.fov = 2 * Math.atan(height / (2 * dist)) * (180 / Math.PI); | |
| camera.updateProjectionMatrix(); | |
| // Basically solving an AAS triangle https://www.mathsisfun.com/algebra/trig-solving-aas-triangles.html | |
| https://i.stack.imgur.com/PgSn3.jpg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment