From 041ee30b2a6d371861b3ef94d063f4ef8cc44185 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Wed, 18 May 2016 15:29:06 -0400 Subject: [PATCH 1/7] Have fixed frustum depth in 2D. WIP. --- Apps/Sandcastle/gallery/3D Models.html | 7 ++-- Source/Scene/Scene.js | 50 ++++++++++++++++++++------ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/Apps/Sandcastle/gallery/3D Models.html b/Apps/Sandcastle/gallery/3D Models.html index ea87d6df57ac..e1dbae2c18fe 100644 --- a/Apps/Sandcastle/gallery/3D Models.html +++ b/Apps/Sandcastle/gallery/3D Models.html @@ -29,7 +29,8 @@ //Sandcastle_Begin var viewer = new Cesium.Viewer('cesiumContainer', { infoBox : false, - selectionIndicator : false + selectionIndicator : false, + sceneMode : Cesium.SceneMode.SCENE2D }); function createModel(url, height) { @@ -54,7 +55,7 @@ viewer.trackedEntity = entity; } -var options = [{ +var options = [/*{ text : 'Aircraft', onselect : function() { createModel('../../SampleData/models/CesiumAir/Cesium_Air.glb', 5000.0); @@ -69,7 +70,7 @@ onselect : function() { createModel('../../SampleData/models/CesiumMilkTruck/CesiumMilkTruck-kmc.glb', 0); } -}, { +},*/ { text : 'Skinned character', onselect : function() { createModel('../../SampleData/models/CesiumMan/Cesium_Man.glb', 0); diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 8796ce7de157..ae733835452c 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -577,11 +577,13 @@ define([ this._cameraVR = undefined; this._aspectRatioVR = undefined; + this.frustumSize2D = 1.0e6; + // initial guess at frustums. var near = camera.frustum.near; var far = camera.frustum.far; var numFrustums = Math.ceil(Math.log(far / near) / Math.log(this.farToNearRatio)); - updateFrustums(near, far, this.farToNearRatio, numFrustums, this._frustumCommandsList); + updateFrustums(near, far, this.farToNearRatio, numFrustums, this._frustumCommandsList, false, undefined); // give frameState, camera, and screen space camera controller initial state before rendering updateFrameState(this, 0.0, JulianDate.now()); @@ -1086,11 +1088,19 @@ define([ clearPasses(frameState.passes); } - function updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList) { + function updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, frustumSize2D) { frustumCommandsList.length = numFrustums; for (var m = 0; m < numFrustums; ++m) { - var curNear = Math.max(near, Math.pow(farToNearRatio, m) * near); - var curFar = Math.min(far, farToNearRatio * curNear); + var curNear; + var curFar; + + if (!is2D) { + curNear = Math.max(near, Math.pow(farToNearRatio, m) * near); + curFar = Math.min(far, farToNearRatio * curNear); + } else { + curNear = near + m * frustumSize2D; + curFar = curNear + frustumSize2D; + } var frustumCommands = frustumCommandsList[m]; if (!defined(frustumCommands)) { @@ -1257,11 +1267,19 @@ 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 farToNearRatio = scene.farToNearRatio; - var numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio)); + + var numFrustums; + if (!is2D) { + numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio)); + } else { + numFrustums = Math.ceil(Math.max(1.0, far - near) / scene.frustumSize2D); + } + if (near !== Number.MAX_VALUE && (numFrustums !== numberOfFrustums || (frustumCommandsList.length !== 0 && (near < frustumCommandsList[0].near || far > frustumCommandsList[numberOfFrustums - 1].far)))) { - updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList); + updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, scene.frustumSize2D); createPotentiallyVisibleSet(scene); } } @@ -1553,9 +1571,18 @@ define([ var index = numFrustums - i - 1; var frustumCommands = frustumCommandsList[index]; - // Avoid tearing artifacts between adjacent frustums in the opaque passes - frustum.near = index !== 0 ? frustumCommands.near * OPAQUE_FRUSTUM_NEAR_OFFSET : frustumCommands.near; - frustum.far = frustumCommands.far; + if (scene.mode !== SceneMode.SCENE2D) { + // Avoid tearing artifacts between adjacent frustums in the opaque passes + frustum.near = index !== 0 ? frustumCommands.near * OPAQUE_FRUSTUM_NEAR_OFFSET : frustumCommands.near; + frustum.far = frustumCommands.far; + us.updateFrustum(frustum); + } else { + camera.position.z = frustum.right - frustum.left;//frustumCommands.near + 1.0; + frustum.far = frustumCommands.far - frustumCommands.near; + frustum.near = 1.0; + us.update(scene.frameState); + us.updateFrustum(frustum); + } var globeDepth = scene.debugShowGlobeDepth ? getDebugGlobeDepth(scene, index) : scene._globeDepth; @@ -1565,7 +1592,6 @@ define([ passState.framebuffer = globeDepth.framebuffer; } - us.updateFrustum(frustum); clearDepth.execute(context, passState); us.updatePass(Pass.GLOBE); @@ -1611,7 +1637,7 @@ define([ } } - if (index !== 0) { + if (index !== 0 && scene.mode !== SceneMode.SCENE2D) { // Do not overlap frustums in the translucent pass to avoid blending artifacts frustum.near = frustumCommands.near; us.updateFrustum(frustum); @@ -1622,12 +1648,14 @@ define([ commands.length = frustumCommands.indices[Pass.TRANSLUCENT]; executeTranslucentCommands(scene, executeCommand, passState, commands); + /* if (defined(globeDepth) && environmentState.useGlobeDepthFramebuffer && scene.useDepthPicking) { // PERFORMANCE_IDEA: Use MRT to avoid the extra copy. var pickDepth = getPickDepth(scene, index); pickDepth.update(context, globeDepth.framebuffer.depthStencilTexture); pickDepth.executeCopyDepth(context, passState); } + */ } } From f924c16d220eb4ea519c55b1d8e2aa35a5e4ae4a Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Wed, 18 May 2016 16:18:25 -0400 Subject: [PATCH 2/7] Update maximum frustum size in 2D and fix terrain rendering. --- Apps/Sandcastle/gallery/3D Models.html | 4 ++-- Source/Scene/Scene.js | 32 +++++++++++++------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Apps/Sandcastle/gallery/3D Models.html b/Apps/Sandcastle/gallery/3D Models.html index e1dbae2c18fe..03199964b94b 100644 --- a/Apps/Sandcastle/gallery/3D Models.html +++ b/Apps/Sandcastle/gallery/3D Models.html @@ -55,7 +55,7 @@ viewer.trackedEntity = entity; } -var options = [/*{ +var options = [{ text : 'Aircraft', onselect : function() { createModel('../../SampleData/models/CesiumAir/Cesium_Air.glb', 5000.0); @@ -70,7 +70,7 @@ onselect : function() { createModel('../../SampleData/models/CesiumMilkTruck/CesiumMilkTruck-kmc.glb', 0); } -},*/ { +}, { text : 'Skinned character', onselect : function() { createModel('../../SampleData/models/CesiumMan/Cesium_Man.glb', 0); diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index ae733835452c..1d21177f2979 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -577,7 +577,7 @@ define([ this._cameraVR = undefined; this._aspectRatioVR = undefined; - this.frustumSize2D = 1.0e6; + this.frustumSize2D = 1.75e6; // initial guess at frustums. var near = camera.frustum.near; @@ -1099,7 +1099,7 @@ define([ curFar = Math.min(far, farToNearRatio * curNear); } else { curNear = near + m * frustumSize2D; - curFar = curNear + frustumSize2D; + curFar = Math.min(far, curNear + frustumSize2D); } var frustumCommands = frustumCommandsList[m]; @@ -1562,6 +1562,8 @@ define([ var clearDepth = scene._depthClearCommand; var depthPlane = scene._depthPlane; + var height2D = camera.position.z; + // Execute commands in each frustum in back to front order var j; var frustumCommandsList = scene._frustumCommandsList; @@ -1571,18 +1573,10 @@ define([ var index = numFrustums - i - 1; var frustumCommands = frustumCommandsList[index]; - if (scene.mode !== SceneMode.SCENE2D) { - // Avoid tearing artifacts between adjacent frustums in the opaque passes - frustum.near = index !== 0 ? frustumCommands.near * OPAQUE_FRUSTUM_NEAR_OFFSET : frustumCommands.near; - frustum.far = frustumCommands.far; - us.updateFrustum(frustum); - } else { - camera.position.z = frustum.right - frustum.left;//frustumCommands.near + 1.0; - frustum.far = frustumCommands.far - frustumCommands.near; - frustum.near = 1.0; - us.update(scene.frameState); - us.updateFrustum(frustum); - } + // Avoid tearing artifacts between adjacent frustums in the opaque passes + frustum.near = index !== 0 ? frustumCommands.near * OPAQUE_FRUSTUM_NEAR_OFFSET : frustumCommands.near; + frustum.far = frustumCommands.far; + us.updateFrustum(frustum); var globeDepth = scene.debugShowGlobeDepth ? getDebugGlobeDepth(scene, index) : scene._globeDepth; @@ -1624,6 +1618,14 @@ define([ } } + if (scene.mode === SceneMode.SCENE2D) { + camera.position.z = height2D - frustumCommands.near + 1.0; + frustum.far = Math.max(1.0, frustumCommands.far - frustumCommands.near); + frustum.near = 1.0; + us.update(scene.frameState); + us.updateFrustum(frustum); + } + // Execute commands in order by pass up to the translucent pass. // Translucent geometry needs special handling (sorting/OIT). var startPass = Pass.GROUND + 1; @@ -1648,14 +1650,12 @@ define([ commands.length = frustumCommands.indices[Pass.TRANSLUCENT]; executeTranslucentCommands(scene, executeCommand, passState, commands); - /* if (defined(globeDepth) && environmentState.useGlobeDepthFramebuffer && scene.useDepthPicking) { // PERFORMANCE_IDEA: Use MRT to avoid the extra copy. var pickDepth = getPickDepth(scene, index); pickDepth.update(context, globeDepth.framebuffer.depthStencilTexture); pickDepth.executeCopyDepth(context, passState); } - */ } } From 8de8d23f78cb7aac2b660881503c7a27f59bc6eb Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Wed, 18 May 2016 17:52:46 -0400 Subject: [PATCH 3/7] Clamp number of frustums so that the last one extends just beyond the terrain. --- Source/Scene/Scene.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 1d21177f2979..d7b6dd59f632 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -1274,6 +1274,8 @@ define([ if (!is2D) { numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio)); } else { + far = Math.min(far, camera.position.z + scene.frustumSize2D); + near = Math.min(near, far); numFrustums = Math.ceil(Math.max(1.0, far - near) / scene.frustumSize2D); } From afa6b4d42ebb1193a335dcb67986a9f96f4cea71 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Wed, 18 May 2016 19:33:18 -0400 Subject: [PATCH 4/7] Add comments, update doc, revert Sandcastle example. --- Apps/Sandcastle/gallery/3D Models.html | 3 +-- Source/Scene/Scene.js | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Apps/Sandcastle/gallery/3D Models.html b/Apps/Sandcastle/gallery/3D Models.html index 03199964b94b..ea87d6df57ac 100644 --- a/Apps/Sandcastle/gallery/3D Models.html +++ b/Apps/Sandcastle/gallery/3D Models.html @@ -29,8 +29,7 @@ //Sandcastle_Begin var viewer = new Cesium.Viewer('cesiumContainer', { infoBox : false, - selectionIndicator : false, - sceneMode : Cesium.SceneMode.SCENE2D + selectionIndicator : false }); function createModel(url, height) { diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index d7b6dd59f632..682530c4f9f5 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -539,6 +539,15 @@ define([ */ this.fog = new Fog(); + /** + * Determines the uniform depth size in meters of each frustum of the multifrustum in 2D. If a primitive or model close + * to the surface 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 close to thesurface. + * @type {Number} + * @default 1.75e6 + */ + this.frustumSize2D = 1.75e6; + this._terrainExaggeration = defaultValue(options.terrainExaggeration, 1.0); this._performanceDisplay = undefined; @@ -577,8 +586,6 @@ define([ this._cameraVR = undefined; this._aspectRatioVR = undefined; - this.frustumSize2D = 1.75e6; - // initial guess at frustums. var near = camera.frustum.near; var far = camera.frustum.far; @@ -1272,8 +1279,10 @@ define([ var numFrustums; if (!is2D) { + // The multifrustum for 3D/CV is non-uniformly distributed. numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio)); } else { + // The multifrustum for 2D is uniformly distributed. far = Math.min(far, camera.position.z + scene.frustumSize2D); near = Math.min(near, far); numFrustums = Math.ceil(Math.max(1.0, far - near) / scene.frustumSize2D); @@ -1621,6 +1630,8 @@ define([ } if (scene.mode === SceneMode.SCENE2D) { + // To avoid z-fighting in 2D, move the camera to just before the frustum + // and scale the frustum depth to be in [1.0, frustumSize2D]. camera.position.z = height2D - frustumCommands.near + 1.0; frustum.far = Math.max(1.0, frustumCommands.far - frustumCommands.near); frustum.near = 1.0; From d75117d514ed931ade2520075e6a248e4fc5408d Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 19 May 2016 14:16:08 -0400 Subject: [PATCH 5/7] Rename frustumSize2D -> nearToFarDistance2D and update comment. --- Source/Scene/Scene.js | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 682530c4f9f5..8860ddf491fb 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -400,6 +400,14 @@ define([ * @default 1000.0 */ this.farToNearRatio = 1000.0; + /** + * Determines the uniform depth size in meters of each frustum of the multifrustum in 2D. If a primitive or model close + * to the surface 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 close to thesurface. + * @type {Number} + * @default 1.75e6 + */ + this.nearToFarDistance2D = 1.75e6; /** * This property is for debugging only; it is not for production use. @@ -539,15 +547,6 @@ define([ */ this.fog = new Fog(); - /** - * Determines the uniform depth size in meters of each frustum of the multifrustum in 2D. If a primitive or model close - * to the surface 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 close to thesurface. - * @type {Number} - * @default 1.75e6 - */ - this.frustumSize2D = 1.75e6; - this._terrainExaggeration = defaultValue(options.terrainExaggeration, 1.0); this._performanceDisplay = undefined; @@ -1095,7 +1094,7 @@ define([ clearPasses(frameState.passes); } - function updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, frustumSize2D) { + function updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, nearToFarDistance2D) { frustumCommandsList.length = numFrustums; for (var m = 0; m < numFrustums; ++m) { var curNear; @@ -1105,8 +1104,8 @@ define([ curNear = Math.max(near, Math.pow(farToNearRatio, m) * near); curFar = Math.min(far, farToNearRatio * curNear); } else { - curNear = near + m * frustumSize2D; - curFar = Math.min(far, curNear + frustumSize2D); + curNear = near + m * nearToFarDistance2D; + curFar = Math.min(far, curNear + nearToFarDistance2D); } var frustumCommands = frustumCommandsList[m]; @@ -1282,15 +1281,17 @@ define([ // The multifrustum for 3D/CV is non-uniformly distributed. numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio)); } else { - // The multifrustum for 2D is uniformly distributed. - far = Math.min(far, camera.position.z + scene.frustumSize2D); + // The multifrustum for 2D is uniformly distributed. To avoid z-fighting in 2D, + // the camera i smoved 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.frustumSize2D); + numFrustums = Math.ceil(Math.max(1.0, far - near) / scene.nearToFarDistance2D); } if (near !== Number.MAX_VALUE && (numFrustums !== numberOfFrustums || (frustumCommandsList.length !== 0 && (near < frustumCommandsList[0].near || far > frustumCommandsList[numberOfFrustums - 1].far)))) { - updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, scene.frustumSize2D); + updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, scene.nearToFarDistance2D); createPotentiallyVisibleSet(scene); } } @@ -1631,7 +1632,7 @@ define([ if (scene.mode === SceneMode.SCENE2D) { // To avoid z-fighting in 2D, move the camera to just before the frustum - // and scale the frustum depth to be in [1.0, frustumSize2D]. + // and scale the frustum depth to be in [1.0, nearToFarDistance2D]. camera.position.z = height2D - frustumCommands.near + 1.0; frustum.far = Math.max(1.0, frustumCommands.far - frustumCommands.near); frustum.near = 1.0; From 1f746fc54074e54982833216c482f6726bec383b Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 19 May 2016 14:18:37 -0400 Subject: [PATCH 6/7] Update CHANGES.md. --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 788373833209..2f55c744e8b4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ Change Log * Fixed a bug that was causing errors to be thrown when picking and terrain was enabled. [#3779](https://github.com/AnalyticalGraphicsInc/cesium/issues/3779) * Fixed issue where labels were disappearing. [3730](https://github.com/AnalyticalGraphicsInc/cesium/issues/3730) * Added `CullingVolume.fromBoundingSphere`. +* Added `Scene.nearToFarDistance2D` that determines the size of each frustum of the multifrustum in 2D. ### 1.21 - 2016-05-02 From 8f1248ba610819058a42f49733852027fcbadaa5 Mon Sep 17 00:00:00 2001 From: Patrick Cozzi Date: Thu, 19 May 2016 14:36:09 -0400 Subject: [PATCH 7/7] Whitespace --- Source/Scene/Scene.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 8860ddf491fb..80dc73bb43ac 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -400,6 +400,7 @@ define([ * @default 1000.0 */ this.farToNearRatio = 1000.0; + /** * Determines the uniform depth size in meters of each frustum of the multifrustum in 2D. If a primitive or model close * to the surface shows z-fighting, decreasing this will eliminate the artifact, but decrease performance. On the @@ -2564,4 +2565,4 @@ define([ }; return Scene; -}); \ No newline at end of file +});