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
Prev Previous commit
Next Next commit
Edit docs related to depth buffers
  • Loading branch information
Jeshurun Hembd committed Sep 10, 2024
commit 2d6ea50717b5266f6ee0a2dc0bc6c5fb06dc6d7e
6 changes: 2 additions & 4 deletions packages/engine/Source/Scene/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -2521,10 +2521,8 @@ function rectangleCameraPosition3D(camera, rectangle, result, updateCamera) {
const ellipsoid = camera._projection.ellipsoid;
const cameraRF = updateCamera ? camera : defaultRF;

const north = rectangle.north;
const south = rectangle.south;
let east = rectangle.east;
const west = rectangle.west;
const { north, south, west } = rectangle;
let { east } = rectangle;

// If we go across the International Date Line
if (west > east) {
Expand Down
3 changes: 3 additions & 0 deletions packages/engine/Source/Scene/GlobeDepth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import StencilFunction from "./StencilFunction.js";
import StencilOperation from "./StencilOperation.js";

/**
* @alias GlobeDepth
* @constructor
*
* @private
*/
function GlobeDepth() {
Expand Down
13 changes: 13 additions & 0 deletions packages/engine/Source/Scene/PickDepth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import FramebufferManager from "../Renderer/FramebufferManager.js";
import RenderState from "../Renderer/RenderState.js";

/**
* @alias PickDepth
* @constructor
*
* @private
*/
function PickDepth() {
Expand Down Expand Up @@ -72,6 +75,16 @@ const packedDepthScale = new Cartesian4(
1.0 / 16581375.0
);

/**
* Read the depth from the framebuffer at the given coordinate.
*
* @param {Context} context
* @param {number} x The x-coordinate at which to read the depth.
* @param {number} y The y-coordinate at which to read the depth.
* @returns {number} The depth read from the framebuffer.
*
* @private
*/
PickDepth.prototype.getDepth = function (context, x, y) {
// If this function is called before the framebuffer is created, the depth is undefined.
if (!defined(this.framebuffer)) {
Expand Down
7 changes: 4 additions & 3 deletions packages/engine/Source/Scene/Picking.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ Picking.prototype.pickPositionWorldCoordinates = function (
frustum = camera.frustum.clone(scratchOrthographicOffCenterFrustum);
}

const frustumCommandsList = defaultView.frustumCommandsList;
const { frustumCommandsList } = defaultView;
const numFrustums = frustumCommandsList.length;
for (let i = 0; i < numFrustums; ++i) {
const pickDepth = this.getPickDepth(scene, i);
Expand Down Expand Up @@ -855,15 +855,16 @@ function getRayIntersection(
const object = view.pickFramebuffer.end(scratchRectangle);

if (scene.context.depthTexture) {
const numFrustums = view.frustumCommandsList.length;
const { frustumCommandsList } = view;
const numFrustums = frustumCommandsList.length;
for (let i = 0; i < numFrustums; ++i) {
const pickDepth = picking.getPickDepth(scene, i);
const depth = pickDepth.getDepth(context, 0, 0);
if (!defined(depth)) {
continue;
}
if (depth > 0.0 && depth < 1.0) {
const renderedFrustum = view.frustumCommandsList[i];
const renderedFrustum = frustumCommandsList[i];
const near =
renderedFrustum.near *
(i !== 0 ? scene.opaqueFrustumNearOffset : 1.0);
Expand Down
38 changes: 18 additions & 20 deletions packages/engine/Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -2484,8 +2484,6 @@ function executeCommands(scene, passState) {
// Draw terrain classification
if (!environmentState.renderTranslucentDepthForPick) {
uniformState.updatePass(Pass.TERRAIN_CLASSIFICATION);
const commands = frustumCommands.commands[Pass.TERRAIN_CLASSIFICATION];
length = frustumCommands.indices[Pass.TERRAIN_CLASSIFICATION];

if (globeTranslucent) {
globeTranslucencyState.executeGlobeClassificationCommands(
Expand All @@ -2496,6 +2494,8 @@ function executeCommands(scene, passState) {
passState
);
} else {
const commands = frustumCommands.commands[Pass.TERRAIN_CLASSIFICATION];
length = frustumCommands.indices[Pass.TERRAIN_CLASSIFICATION];
for (let j = 0; j < length; ++j) {
executeCommand(commands[j], scene, context, passState);
}
Expand Down Expand Up @@ -3169,10 +3169,8 @@ function executeCommandsInViewport(
passState,
backgroundColor
) {
const environmentState = scene._environmentState;
const view = scene._view;
const renderTranslucentDepthForPick =
environmentState.renderTranslucentDepthForPick;
const { renderTranslucentDepthForPick } = scene._environmentState;

if (!firstViewport) {
scene.frameState.commandList.length = 0;
Expand Down Expand Up @@ -3355,21 +3353,21 @@ function updateDebugFrustumPlanes(scene) {

function updateShadowMaps(scene) {
const frameState = scene._frameState;
const shadowMaps = frameState.shadowMaps;
const { passes, shadowState, shadowMaps } = frameState;
const length = shadowMaps.length;

const shadowsEnabled =
length > 0 &&
!frameState.passes.pick &&
!frameState.passes.pickVoxel &&
!passes.pick &&
!passes.pickVoxel &&
scene.mode === SceneMode.SCENE3D;
if (shadowsEnabled !== frameState.shadowState.shadowsEnabled) {
if (shadowsEnabled !== shadowState.shadowsEnabled) {
// Update derived commands when shadowsEnabled changes
++frameState.shadowState.lastDirtyTime;
frameState.shadowState.shadowsEnabled = shadowsEnabled;
++shadowState.lastDirtyTime;
shadowState.shadowsEnabled = shadowsEnabled;
}

frameState.shadowState.lightShadowsEnabled = false;
shadowState.lightShadowsEnabled = false;

if (!shadowsEnabled) {
return;
Expand All @@ -3378,28 +3376,28 @@ function updateShadowMaps(scene) {
// Check if the shadow maps are different than the shadow maps last frame.
// If so, the derived commands need to be updated.
for (let j = 0; j < length; ++j) {
if (shadowMaps[j] !== frameState.shadowState.shadowMaps[j]) {
++frameState.shadowState.lastDirtyTime;
if (shadowMaps[j] !== shadowState.shadowMaps[j]) {
++shadowState.lastDirtyTime;
break;
}
}

frameState.shadowState.shadowMaps.length = 0;
frameState.shadowState.lightShadowMaps.length = 0;
shadowState.shadowMaps.length = 0;
shadowState.lightShadowMaps.length = 0;

for (let i = 0; i < length; ++i) {
const shadowMap = shadowMaps[i];
shadowMap.update(frameState);

frameState.shadowState.shadowMaps.push(shadowMap);
shadowState.shadowMaps.push(shadowMap);

if (shadowMap.fromLightSource) {
frameState.shadowState.lightShadowMaps.push(shadowMap);
frameState.shadowState.lightShadowsEnabled = true;
shadowState.lightShadowMaps.push(shadowMap);
shadowState.lightShadowsEnabled = true;
}

if (shadowMap.dirty) {
++frameState.shadowState.lastDirtyTime;
++shadowState.lastDirtyTime;
shadowMap.dirty = false;
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/engine/Source/Scene/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ function CommandExtent() {
}

/**
* @alias View
* @constructor
*
* @param {Scene} scene
* @param {Camera} camera
* @param {BoundingRectangle} viewport
*
* @private
*/
function View(scene, camera, viewport) {
Expand Down Expand Up @@ -62,6 +69,9 @@ function View(scene, camera, viewport) {
this.translucentTileClassification = new TranslucentTileClassification(
context
);
/**
* @type {PickDepth[]}
*/
this.pickDepths = [];
this.frustumCommandsList = [];
this.debugFrustumStatistics = undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Packs a depth value into a vec3 that can be represented by unsigned bytes.
* Packs a depth value into a vec4 that can be represented by unsigned bytes.
*
* @name czm_packDepth
* @glslFunction
*
* @param {float} depth The floating-point depth.
* @returns {vec3} The packed depth.
* @returns {vec4} The packed depth.
*/
vec4 czm_packDepth(float depth)
{
Expand Down