Skip to content

Commit

Permalink
Compute frustum splits for orthographic uniformly
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Aug 15, 2018
1 parent 4633ea6 commit ae0a4c1
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,16 @@ define([
*/
this.nearToFarDistance2D = 1.75e6;

/**
* Determines the uniform depth size in meters of each frustum of the multifrustum in orthographic. If a primitive or model
* shows z-fighting, decreasing this will eliminate the artifact, but decrease performance. On the other hand, increasing
* this will increase performance but may cause z-fighting among primitives.
*
* @type {Number}
* @default 1.75e6
*/
this.nearToFarDistanceOrthographic = 1.75e6;

/**
* This property is for debugging only; it is not for production use.
* <p>
Expand Down Expand Up @@ -808,7 +818,7 @@ define([
var farToNearRatio = this._logDepthBuffer ? this.logarithmicDepthFarToNearRatio : this.farToNearRatio;

var numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio));
updateFrustums(near, far, farToNearRatio, numFrustums, this._logDepthBuffer, this._frustumCommandsList, false, undefined);
updateFrustums(near, far, farToNearRatio, numFrustums, this._logDepthBuffer, this._frustumCommandsList, false, undefined, false, undefined);

// give frameState, camera, and screen space camera controller initial state before rendering
updateFrameState(this, 0.0, JulianDate.now());
Expand Down Expand Up @@ -1634,21 +1644,24 @@ define([
clearPasses(frameState.passes);
}

function updateFrustums(near, far, farToNearRatio, numFrustums, logDepth, frustumCommandsList, is2D, nearToFarDistance2D) {
function updateFrustums(near, far, farToNearRatio, numFrustums, logDepth, frustumCommandsList, is2D, nearToFarDistance2D, isOrthographic, nearToFarDistanceOrthographic) {
frustumCommandsList.length = numFrustums;
for (var m = 0; m < numFrustums; ++m) {
var curNear;
var curFar;

if (!is2D) {
if (is2D) {
curNear = Math.min(far - nearToFarDistance2D, near + m * nearToFarDistance2D);
curFar = Math.min(far, curNear + nearToFarDistance2D);
} else if (isOrthographic) {
curNear = near + nearToFarDistanceOrthographic * m;
curFar = Math.min(far, curNear + nearToFarDistanceOrthographic);
} else {
curNear = Math.max(near, Math.pow(farToNearRatio, m) * near);
curFar = farToNearRatio * curNear;
if (!logDepth) {
curFar = Math.min(far, curFar);
}
} else {
curNear = Math.min(far - nearToFarDistance2D, near + m * nearToFarDistance2D);
curFar = Math.min(far, curNear + nearToFarDistance2D);
}

var frustumCommands = frustumCommandsList[m];
Expand Down Expand Up @@ -1838,26 +1851,30 @@ define([
// Exploit temporal coherence. If the frustums haven't changed much, use the frustums computed
// last frame, else compute the new frustums and sort them by frustum again.
var is2D = scene.mode === SceneMode.SCENE2D;
var isOrthographic = (scene.camera.frustum instanceof OrthographicFrustum || scene.camera.frustum instanceof OrthographicOffCenterFrustum);
var logDepth = frameState.useLogDepth;
var farToNearRatio = logDepth ? scene.logarithmicDepthFarToNearRatio : scene.farToNearRatio;
var numFrustums;

if (!is2D) {
// The multifrustum for 3D/CV is non-uniformly distributed.
numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio));
} else {
if (is2D) {
// The multifrustum for 2D is uniformly distributed. To avoid z-fighting in 2D,
// the camera is moved to just before the frustum and the frustum depth is scaled
// to be in [1.0, nearToFarDistance2D].
far = Math.min(far, camera.position.z + scene.nearToFarDistance2D);
near = Math.min(near, far);
numFrustums = Math.ceil(Math.max(1.0, far - near) / scene.nearToFarDistance2D);
} else if (isOrthographic) {
// The multifrustum for orthographic is uniformly distributed.
numFrustums = Math.ceil(Math.max(1.0, far - near) / scene.nearToFarDistanceOrthographic);
} else {
// The multifrustum for 3D/CV is non-uniformly distributed.
numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio));
}

if (scene._updateFrustums || (near !== Number.MAX_VALUE && (numFrustums !== numberOfFrustums || (frustumCommandsList.length !== 0 &&
(near < frustumCommandsList[0].near || (far > frustumCommandsList[numberOfFrustums - 1].far && (logDepth || !CesiumMath.equalsEpsilon(far, frustumCommandsList[numberOfFrustums - 1].far, CesiumMath.EPSILON8)))))))) {
scene._updateFrustums = false;
updateFrustums(near, far, farToNearRatio, numFrustums, logDepth, frustumCommandsList, is2D, scene.nearToFarDistance2D);
updateFrustums(near, far, farToNearRatio, numFrustums, logDepth, frustumCommandsList, is2D, scene.nearToFarDistance2D, isOrthographic, scene.nearToFarDistanceOrthographic);
createPotentiallyVisibleSet(scene);
}

Expand Down

0 comments on commit ae0a4c1

Please sign in to comment.