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

Improve documentation and readability in Scene and depth-related code #12188

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions packages/engine/Source/Core/Matrix4.js
Original file line number Diff line number Diff line change
Expand Up @@ -937,8 +937,8 @@ Matrix4.computeOrthographicOffCenter = function (
*
* @param {number} left The number of meters to the left of the camera that will be in view.
* @param {number} right The number of meters to the right of the camera that will be in view.
* @param {number} bottom The number of meters below of the camera that will be in view.
* @param {number} top The number of meters above of the camera that will be in view.
* @param {number} bottom The number of meters below the camera that will be in view.
* @param {number} top The number of meters above the camera that will be in view.
* @param {number} near The distance to the near plane in meters.
* @param {number} far The distance to the far plane in meters.
* @param {Matrix4} result The object in which the result will be stored.
Expand Down
94 changes: 50 additions & 44 deletions packages/engine/Source/Core/PerspectiveFrustum.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,65 +167,71 @@ function update(frustum) {
}
//>>includeEnd('debug');

const f = frustum._offCenterFrustum;

if (
const changed =
frustum.fov !== frustum._fov ||
frustum.aspectRatio !== frustum._aspectRatio ||
frustum.near !== frustum._near ||
frustum.far !== frustum._far ||
frustum.xOffset !== frustum._xOffset ||
frustum.yOffset !== frustum._yOffset
) {
//>>includeStart('debug', pragmas.debug);
if (frustum.fov < 0 || frustum.fov >= Math.PI) {
throw new DeveloperError("fov must be in the range [0, PI).");
}

if (frustum.aspectRatio < 0) {
throw new DeveloperError("aspectRatio must be positive.");
}

if (frustum.near < 0 || frustum.near > frustum.far) {
throw new DeveloperError(
"near must be greater than zero and less than far."
);
}
//>>includeEnd('debug');

frustum._aspectRatio = frustum.aspectRatio;
frustum._fov = frustum.fov;
frustum._fovy =
frustum.aspectRatio <= 1
? frustum.fov
: Math.atan(Math.tan(frustum.fov * 0.5) / frustum.aspectRatio) * 2.0;
frustum._near = frustum.near;
frustum._far = frustum.far;
frustum._sseDenominator = 2.0 * Math.tan(0.5 * frustum._fovy);
frustum._xOffset = frustum.xOffset;
frustum._yOffset = frustum.yOffset;

f.top = frustum.near * Math.tan(0.5 * frustum._fovy);
f.bottom = -f.top;
f.right = frustum.aspectRatio * f.top;
f.left = -f.right;
f.near = frustum.near;
f.far = frustum.far;

f.right += frustum.xOffset;
f.left += frustum.xOffset;
f.top += frustum.yOffset;
f.bottom += frustum.yOffset;
frustum.yOffset !== frustum._yOffset;

if (!changed) {
return;
}

//>>includeStart('debug', pragmas.debug);
if (frustum.fov < 0 || frustum.fov >= Math.PI) {
throw new DeveloperError("fov must be in the range [0, PI).");
Copy link
Contributor

Choose a reason for hiding this comment

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

No changes strictly needed for the sake of this PR, but keep in mind that Check has some functions to help with number ranges like this.

}

if (frustum.aspectRatio < 0) {
throw new DeveloperError("aspectRatio must be positive.");
}

if (frustum.near < 0 || frustum.near > frustum.far) {
throw new DeveloperError(
"near must be greater than zero and less than far."
);
}
//>>includeEnd('debug');

frustum._aspectRatio = frustum.aspectRatio;
frustum._fov = frustum.fov;
frustum._fovy =
frustum.aspectRatio <= 1
? frustum.fov
: Math.atan(Math.tan(frustum.fov * 0.5) / frustum.aspectRatio) * 2.0;
frustum._near = frustum.near;
frustum._far = frustum.far;
frustum._sseDenominator = 2.0 * Math.tan(0.5 * frustum._fovy);
frustum._xOffset = frustum.xOffset;
frustum._yOffset = frustum.yOffset;

const f = frustum._offCenterFrustum;

f.top = frustum.near * Math.tan(0.5 * frustum._fovy);
f.bottom = -f.top;
f.right = frustum.aspectRatio * f.top;
f.left = -f.right;
f.near = frustum.near;
f.far = frustum.far;

f.right += frustum.xOffset;
f.left += frustum.xOffset;
f.top += frustum.yOffset;
f.bottom += frustum.yOffset;
}

Object.defineProperties(PerspectiveFrustum.prototype, {
/**
* Gets the perspective projection matrix computed from the view frustum.
* If necessary, the projection matrix will be recomputed.
*
* @memberof PerspectiveFrustum.prototype
* @type {Matrix4}
* @readonly
*
* @see PerspectiveOffCenterFrustum#projectionMatrix.
* @see PerspectiveFrustum#infiniteProjectionMatrix
*/
projectionMatrix: {
Expand Down
87 changes: 43 additions & 44 deletions packages/engine/Source/Core/PerspectiveOffCenterFrustum.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,58 +108,57 @@ function update(frustum) {
}
//>>includeEnd('debug');

const t = frustum.top;
const b = frustum.bottom;
const r = frustum.right;
const l = frustum.left;
const n = frustum.near;
const f = frustum.far;
const { top, bottom, right, left, near, far } = frustum;

const changed =
top !== frustum._top ||
bottom !== frustum._bottom ||
left !== frustum._left ||
right !== frustum._right ||
near !== frustum._near ||
far !== frustum._far;
if (!changed) {
return;
}

if (
t !== frustum._top ||
b !== frustum._bottom ||
l !== frustum._left ||
r !== frustum._right ||
n !== frustum._near ||
f !== frustum._far
) {
//>>includeStart('debug', pragmas.debug);
if (frustum.near <= 0 || frustum.near > frustum.far) {
throw new DeveloperError(
"near must be greater than zero and less than far."
);
}
//>>includeEnd('debug');

frustum._left = l;
frustum._right = r;
frustum._top = t;
frustum._bottom = b;
frustum._near = n;
frustum._far = f;
frustum._perspectiveMatrix = Matrix4.computePerspectiveOffCenter(
l,
r,
b,
t,
n,
f,
frustum._perspectiveMatrix
);
frustum._infinitePerspective = Matrix4.computeInfinitePerspectiveOffCenter(
l,
r,
b,
t,
n,
frustum._infinitePerspective
//>>includeStart('debug', pragmas.debug);
if (frustum.near <= 0 || frustum.near > frustum.far) {
throw new DeveloperError(
"near must be greater than zero and less than far."
);
}
//>>includeEnd('debug');

frustum._left = left;
frustum._right = right;
frustum._top = top;
frustum._bottom = bottom;
frustum._near = near;
frustum._far = far;
frustum._perspectiveMatrix = Matrix4.computePerspectiveOffCenter(
left,
right,
bottom,
top,
near,
far,
frustum._perspectiveMatrix
);
frustum._infinitePerspective = Matrix4.computeInfinitePerspectiveOffCenter(
left,
right,
bottom,
top,
near,
frustum._infinitePerspective
);
}

Object.defineProperties(PerspectiveOffCenterFrustum.prototype, {
/**
* Gets the perspective projection matrix computed from the view frustum.
* The projection matrix will be recomputed if any frustum parameters have changed.
*
* @memberof PerspectiveOffCenterFrustum.prototype
* @type {Matrix4}
* @readonly
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/Source/Renderer/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ Context.prototype.draw = function (
//>>includeEnd('debug');

passState = defaultValue(passState, this._defaultPassState);
// The command's framebuffer takes presidence over the pass' framebuffer, e.g., for off-screen rendering.
// The command's framebuffer takes precedence over the pass' framebuffer, e.g., for off-screen rendering.
const framebuffer = defaultValue(
drawCommand._framebuffer,
passState.framebuffer
Expand Down
3 changes: 3 additions & 0 deletions packages/engine/Source/Renderer/DrawCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const Flags = {
/**
* Represents a command to the renderer for drawing.
*
* @alias DrawCommand
* @constructor
*
* @private
*/
function DrawCommand(options) {
Expand Down
Loading