-
Notifications
You must be signed in to change notification settings - Fork 90
Add camera matrix helpers #8755
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ import { isArray } from '@src/lib/utils' | |
|
|
||
| import * as TWEEN from '@tweenjs/tween.js' | ||
| import Hammer from 'hammerjs' | ||
| import type { Camera } from 'three' | ||
| import type { Camera, CoordinateSystem } from 'three' | ||
| import { | ||
| Euler, | ||
| MathUtils, | ||
|
|
@@ -15,6 +15,8 @@ import { | |
| Spherical, | ||
| Vector2, | ||
| Vector3, | ||
| WebGLCoordinateSystem, | ||
| WebGPUCoordinateSystem, | ||
| } from 'three' | ||
|
|
||
| import type { CameraProjectionType } from '@rust/kcl-lib/bindings/CameraProjectionType' | ||
|
|
@@ -1852,3 +1854,134 @@ export async function letEngineAnimateAndSyncCamAfter( | |
| }, | ||
| }) | ||
| } | ||
|
|
||
| function createOrthographicProjectionMatrix( | ||
| left: number, | ||
| right: number, | ||
| bottom: number, | ||
| top: number, | ||
| near: number, | ||
| far: number, | ||
| coordSystem: CoordinateSystem | ||
| ): Matrix4 { | ||
| const invW = 1.0 / (right - left) | ||
| const invH = 1.0 / (top - bottom) | ||
| const invD = 1.0 / (near - far) | ||
|
|
||
| switch (coordSystem) { | ||
| case WebGLCoordinateSystem: | ||
| // NOTE: This assumes a right-handed y-up view space and that [-near, -far] in view space maps | ||
| // to [-1, 1] in NDC space | ||
| return new Matrix4( | ||
| 2.0 * invW, 0.0, 0.0, -(right + left) * invW, | ||
| 0.0, 2.0 * invH, 0.0, -(top + bottom) * invH, | ||
| 0.0, 0.0, 2.0 * invD, (far + near) * invD, | ||
| 0.0, 0.0, 0.0, 1.0 | ||
| ) | ||
| case WebGPUCoordinateSystem: | ||
| // NOTE: This assumes a right-handed y-up view space and that [-near, -far] in view space maps | ||
| // to [0, 1] in NDC space | ||
| return new Matrix4( | ||
| 2.0 * invW, 0.0, 0.0, -(right + left) * invW, | ||
| 0.0, 2.0 * invH, 0.0, -(top + bottom) * invH, | ||
| 0.0, 0.0, invD, near * invD, | ||
| 0.0, 0.0, 0.0, 1.0 | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| function createPerspectiveProjectionMatrix( | ||
| fovY: number, | ||
| aspect: number, | ||
| near: number, | ||
| far: number, | ||
| coordSystem: CoordinateSystem | ||
| ): Matrix4 { | ||
| const y = Math.tan(0.5 * degToRad(fovY)) | ||
| const x = y * aspect | ||
|
|
||
| switch (coordSystem) { | ||
| case WebGLCoordinateSystem: | ||
| // NOTE: This assumes a right-handed y-up view space and that [-near, -far] in view space maps | ||
| // to [-1, 1] in NDC space | ||
| return new Matrix4( | ||
| 1.0 / x, 0.0, 0.0, 0.0, | ||
| 0.0, 1.0 / y, 0.0, 0.0, | ||
| 0.0, 0.0, -(far + near) / (far - near), -2.0 * far * near / (far - near), | ||
| 0.0, 0.0, -1.0, 0.0 | ||
| ) | ||
| case WebGPUCoordinateSystem: | ||
| // NOTE: This assumes a right-handed y-up view space and that [-near, -far] in view space maps | ||
| // to [0, 1] in NDC space | ||
| return new Matrix4( | ||
| 1.0 / x, 0.0, 0.0, 0.0, | ||
| 0.0, 1.0 / y, 0.0, 0.0, | ||
| 0.0, 0.0, far / (near - far), -far * near / (far - near), | ||
| 0.0, 0.0, -1.0, 0.0 | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| function toVector3(obj: { x: number; y: number; z: number }) { | ||
| return new Vector3(obj.x, obj.y, obj.z) | ||
| } | ||
|
|
||
| function toQuaternion(obj: { w: number; x: number; y: number; z: number }) { | ||
| return new Quaternion(obj.x, obj.y, obj.z, obj.w) | ||
| } | ||
|
|
||
| /** | ||
| * Creates a view matrix from the given CameraViewState in a way that is consistent with engine | ||
| */ | ||
| export function createViewMatrix(cam: CameraViewState): Matrix4 { | ||
| const invRot = new Matrix4() | ||
| invRot.makeRotationFromQuaternion( | ||
| toQuaternion(cam.pivot_rotation).conjugate() | ||
| ) | ||
|
|
||
| const invTrans = new Matrix4() | ||
| invTrans.makeTranslation(toVector3(cam.pivot_position).negate()) | ||
|
|
||
| const result = new Matrix4() | ||
| result.multiplyMatrices(invRot, invTrans) | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| /** | ||
| * Creates a projection matrix from the given CameraViewState in a way that is consistent with engine | ||
| */ | ||
| export function createProjectionMatrix( | ||
| camera: CameraViewState, | ||
| aspectRatio: number, | ||
| nearClip: number, | ||
| farClip: number, | ||
| coordSystem: CoordinateSystem = WebGLCoordinateSystem | ||
| ): Matrix4 { | ||
| if (camera.is_ortho) { | ||
| // NOTE: The height of the ortho frustum is derived from FOV and eye distance to ensure | ||
| // consistent scale when switching between projection modes | ||
| const height = Math.tan(0.5 * degToRad(camera.fov_y)) * camera.eye_offset | ||
| const width = height * aspectRatio | ||
|
|
||
| // NOTE: A mirrored far plane is used as the near plane in ortho mode to avoid clipping scene | ||
| // geometry when zooming in | ||
| return createOrthographicProjectionMatrix( | ||
| -width, | ||
| width, | ||
| -height, | ||
| height, | ||
| -farClip, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's a sneaky trick to allow "infinite" zoom in ortho mode. |
||
| farClip, | ||
| coordSystem | ||
| ) | ||
| } else { | ||
| return createPerspectiveProjectionMatrix( | ||
| camera.fov_y, | ||
| aspectRatio, | ||
| nearClip, | ||
| farClip, | ||
| coordSystem | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will we be able get the coordSystem from the engine? Or for now we can just assume WebGL
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should be determined by the active renderer on the FE (assumed WebGL).