gpu-curtains
Version:
gpu-curtains is a 3D WebGPU rendering engine. It can be used as a standalone 3D engine, but also includes extra classes focused on mapping 3d objects to DOM elements; It allows users to synchronize values such as position, sizing, or scale between them.
190 lines (189 loc) • 4.65 kB
JavaScript
import { Camera } from "./Camera.mjs";
//#region src/core/cameras/OrthographicCamera.ts
var OrthographicCamera = class extends Camera {
/** @ignore */
#left;
/** @ignore */
#right;
/** @ignore */
#top;
/** @ignore */
#bottom;
/**
* OrthographicCamera constructor
* @param parameters - {@link OrthographicCameraParams} used to create our {@link OrthographicCamera}.
*/
constructor({ label = "Orthographic camera", near = .1, far = 150, left = -1, right = 1, top = 1, bottom = -1, pixelRatio = 1, onMatricesChanged = () => {} } = {}) {
super({
label,
near,
far,
pixelRatio,
onMatricesChanged
});
this.position.set(0, 0, 10);
this.setOrthographic({
near,
far,
left,
right,
top,
bottom,
pixelRatio
});
}
/**
* Get the {@link OrthographicCamera.left | left} frustum plane value.
*/
get left() {
return this.#left;
}
/**
* Set the {@link OrthographicCamera.left | left} frustum plane value. Update the {@link projectionMatrix} only if the value actually changed.
* @param left - New left frustum plane value.
*/
set left(left) {
if (left !== this.left) {
this.#left = left;
this.shouldUpdateProjectionMatrices();
}
}
/**
* Get the {@link OrthographicCamera.right | right} frustum plane value.
*/
get right() {
return this.#right;
}
/**
* Set the {@link OrthographicCamera.right | right} frustum plane value. Update the {@link projectionMatrix} only if the value actually changed.
* @param right - New right frustum plane value.
*/
set right(right) {
if (right !== this.right) {
this.#right = right;
this.shouldUpdateProjectionMatrices();
}
}
/**
* Get the {@link OrthographicCamera.top | top} frustum plane value.
*/
get top() {
return this.#top;
}
/**
* Set the {@link OrthographicCamera.top | top} frustum plane value. Update the {@link projectionMatrix} only if the value actually changed.
* @param top - New top frustum plane value.
*/
set top(top) {
if (top !== this.top) {
this.#top = top;
this.shouldUpdateProjectionMatrices();
}
}
/**
* Get the {@link OrthographicCamera.bottom | bottom} frustum plane value.
*/
get bottom() {
return this.#bottom;
}
/**
* Set the {@link OrthographicCamera.bottom | bottom} frustum plane value. Update the {@link projectionMatrix} only if the value actually changed.
* @param bottom - New bottom frustum plane value.
*/
set bottom(bottom) {
if (bottom !== this.bottom) {
this.#bottom = bottom;
this.shouldUpdateProjectionMatrices();
}
}
/**
* Sets the {@link OrthographicCamera} orthographic projection settings. Update the {@link projectionMatrix} if needed.
* @param parameters - {@link OrthographicCameraOptions} to use for the orthographic projection.
*/
setOrthographic({ near = this.near, far = this.far, left = this.left, right = this.right, top = this.top, bottom = this.bottom, pixelRatio = this.pixelRatio }) {
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
this.pixelRatio = pixelRatio;
this.near = near;
this.far = far;
}
/**
* Get visible width / height at a given z-depth from our {@link OrthographicCamera} parameters.
* @param depth - Depth to use for calculations - unused since width and height does not change according to depth in orthographic projection.
* @returns - Visible width and height.
*/
getVisibleSizeAtDepth(depth = 0) {
return {
width: this.right - this.left,
height: this.top - this.bottom
};
}
/**
* Sets visible width / height at a depth of 0.
*/
setVisibleSize() {
this.visibleSize = this.getVisibleSizeAtDepth();
}
/**
* Updates the {@link OrthographicCamera} {@link projectionMatrix}.
*/
updateProjectionMatrix() {
this.projectionMatrix.makeOrthographic({
left: this.left,
right: this.right,
top: this.top,
bottom: this.bottom,
near: this.near,
far: this.far
});
}
/**
* Get the current {@link OrthographicCamera} frustum planes in the [left, right, top, bottom, near, far] order.
* @returns - Frustum planes as an array of 6 faces in the [left, right, top, bottom, near, far] order, made of {@link Float32Array} of length 4.
* @readonly
*/
get frustumPlanes() {
return [
new Float32Array([
1,
0,
0,
-this.right
]),
new Float32Array([
-1,
0,
0,
this.left
]),
new Float32Array([
0,
1,
0,
-this.top
]),
new Float32Array([
0,
-1,
0,
this.bottom
]),
new Float32Array([
0,
0,
1,
-this.near
]),
new Float32Array([
0,
0,
-1,
this.far
])
];
}
};
//#endregion
export { OrthographicCamera };