From 80f5976420f9a61f67f6ffa25c82d4ffb95c7575 Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Tue, 26 Sep 2017 16:49:23 +0300 Subject: [PATCH 01/68] added optional logarithmic depth buffer --- Source/Scene/GlobeSurfaceShaderSet.js | 3 ++ Source/Scene/GlobeSurfaceTileProvider.js | 3 ++ Source/Scene/Scene.js | 31 +++++++++++++++++---- Source/Shaders/GlobeFS.glsl | 6 ++++ Source/Shaders/GlobeVS.glsl | 2 ++ Source/Widgets/CesiumWidget/CesiumWidget.js | 3 +- Source/Widgets/Viewer/Viewer.js | 3 +- 7 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Source/Scene/GlobeSurfaceShaderSet.js b/Source/Scene/GlobeSurfaceShaderSet.js index 2442df1a5efb..9f69571ae84c 100644 --- a/Source/Scene/GlobeSurfaceShaderSet.js +++ b/Source/Scene/GlobeSurfaceShaderSet.js @@ -112,6 +112,9 @@ define([ vs.defines.push(quantizationDefine); fs.defines.push('TEXTURE_UNITS ' + numberOfDayTextures); + if (frameState.logDepthBuffer) { + fs.defines.push('LOG_DEPTH_BUFFER'); + } if (applyBrightness) { fs.defines.push('APPLY_BRIGHTNESS'); } diff --git a/Source/Scene/GlobeSurfaceTileProvider.js b/Source/Scene/GlobeSurfaceTileProvider.js index 00c047e2b83d..123570ea88cc 100644 --- a/Source/Scene/GlobeSurfaceTileProvider.js +++ b/Source/Scene/GlobeSurfaceTileProvider.js @@ -763,6 +763,9 @@ define([ function createTileUniformMap(frameState) { var uniformMap = { + u_far : function() { + return frameState.camera._scene._frustumCommandsList[0].far; + }, u_initialColor : function() { return this.properties.initialColor; }, diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 2e649fa00944..3ed5464c693b 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -230,6 +230,8 @@ define([ */ function Scene(options) { options = defaultValue(options, defaultValue.EMPTY_OBJECT); + this.logDepthBuffer = defaultValue(options.logDepthBuffer, false); + var canvas = options.canvas; var contextOptions = options.contextOptions; var creditContainer = options.creditContainer; @@ -256,6 +258,7 @@ define([ this._jobScheduler = new JobScheduler(); this._frameState = new FrameState(context, new CreditDisplay(creditContainer), this._jobScheduler); this._frameState.scene3DOnly = defaultValue(options.scene3DOnly, false); + this._frameState.logDepthBuffer = this.logDepthBuffer; var ps = new PassState(context); ps.viewport = new BoundingRectangle(); @@ -676,8 +679,16 @@ define([ // 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, false, undefined); + var numFrustums; + var farToNearRatio; + if (this.logDepthBuffer) { + numFrustums = 1; + farToNearRatio = far / near; + } else { + numFrustums = Math.ceil(Math.log(far / near) / Math.log(this.farToNearRatio)); + farToNearRatio = this.farToNearRatio; + } + updateFrustums(near, far, farToNearRatio, numFrustums, this._frustumCommandsList, false, undefined); // give frameState, camera, and screen space camera controller initial state before rendering updateFrameState(this, 0.0, JulianDate.now()); @@ -1527,12 +1538,18 @@ 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 farToNearRatio = 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)); + if (scene.logDepthBuffer) { + numFrustums = 1; + farToNearRatio = far / near; + } else { + numFrustums = Math.ceil(Math.log(far / near) / Math.log(scene.farToNearRatio)); + } } else { // 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 @@ -1542,8 +1559,10 @@ define([ 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 && !CesiumMath.equalsEpsilon(far, frustumCommandsList[numberOfFrustums - 1].far, CesiumMath.EPSILON8)))))) { + if ((scene.logDepthBuffer && + !CesiumMath.equalsEpsilon(far, frustumCommandsList[numberOfFrustums - 1].far, CesiumMath.EPSILON8)) || + (near !== Number.MAX_VALUE && (numFrustums !== numberOfFrustums || (frustumCommandsList.length !== 0 && + (near < frustumCommandsList[0].near || (far > frustumCommandsList[numberOfFrustums - 1].far && !CesiumMath.equalsEpsilon(far, frustumCommandsList[numberOfFrustums - 1].far, CesiumMath.EPSILON8))))))) { updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, scene.nearToFarDistance2D); createPotentiallyVisibleSet(scene); } diff --git a/Source/Shaders/GlobeFS.glsl b/Source/Shaders/GlobeFS.glsl index 34f8bc02c4ca..e727d3d7a74e 100644 --- a/Source/Shaders/GlobeFS.glsl +++ b/Source/Shaders/GlobeFS.glsl @@ -1,6 +1,8 @@ //#define SHOW_TILE_BOUNDARIES +#extension GL_EXT_frag_depth : enable uniform vec4 u_initialColor; +uniform float u_far; #if TEXTURE_UNITS > 0 uniform sampler2D u_dayTextures[TEXTURE_UNITS]; @@ -54,6 +56,7 @@ uniform vec2 u_lightingFadeDistance; varying vec3 v_positionMC; varying vec3 v_positionEC; +varying vec4 v_position; varying vec3 v_textureCoordinates; varying vec3 v_normalMC; varying vec3 v_normalEC; @@ -205,6 +208,9 @@ void main() #else gl_FragColor = finalColor; #endif +#ifdef LOG_DEPTH_BUFFER + gl_FragDepthEXT = log(v_position.w + 1.) / log(u_far + 1.); +#endif } #ifdef SHOW_REFLECTIVE_OCEAN diff --git a/Source/Shaders/GlobeVS.glsl b/Source/Shaders/GlobeVS.glsl index a9cbc9ed2382..4c5ed05ad33e 100644 --- a/Source/Shaders/GlobeVS.glsl +++ b/Source/Shaders/GlobeVS.glsl @@ -17,6 +17,7 @@ uniform vec2 u_southMercatorYAndOneOverHeight; varying vec3 v_positionMC; varying vec3 v_positionEC; +varying vec4 v_position; varying vec3 v_textureCoordinates; varying vec3 v_normalMC; @@ -151,6 +152,7 @@ void main() v_textureCoordinates = vec3(textureCoordinates, webMercatorT); + v_position = gl_Position; #if defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL) v_positionEC = (u_modifiedModelView * vec4(position, 1.0)).xyz; v_positionMC = position3DWC; // position in model coordinates diff --git a/Source/Widgets/CesiumWidget/CesiumWidget.js b/Source/Widgets/CesiumWidget/CesiumWidget.js index da432e22142a..1f7a1eed698a 100644 --- a/Source/Widgets/CesiumWidget/CesiumWidget.js +++ b/Source/Widgets/CesiumWidget/CesiumWidget.js @@ -261,7 +261,8 @@ define([ scene3DOnly : defaultValue(options.scene3DOnly, false), terrainExaggeration : options.terrainExaggeration, shadows : options.shadows, - mapMode2D : options.mapMode2D + mapMode2D : options.mapMode2D, + logDepthBuffer : options.logDepthBuffer }); this._scene = scene; diff --git a/Source/Widgets/Viewer/Viewer.js b/Source/Widgets/Viewer/Viewer.js index 2d0e990e488b..7300a1fcb3cc 100644 --- a/Source/Widgets/Viewer/Viewer.js +++ b/Source/Widgets/Viewer/Viewer.js @@ -434,7 +434,8 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to terrainExaggeration : options.terrainExaggeration, shadows : options.shadows, terrainShadows : options.terrainShadows, - mapMode2D : options.mapMode2D + mapMode2D : options.mapMode2D, + logDepthBuffer : options.logDepthBuffer }); var dataSourceCollection = options.dataSources; From 36264f77e04cde992e51dcc787155e8776237041 Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Wed, 27 Sep 2017 15:30:31 +0300 Subject: [PATCH 02/68] fixed pickinig issue with log depth buffer --- Source/Scene/Scene.js | 12 ++++++++---- Source/Scene/SceneTransforms.js | 6 ++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 3ed5464c693b..be0b2e83fbc7 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -1559,10 +1559,14 @@ define([ numFrustums = Math.ceil(Math.max(1.0, far - near) / scene.nearToFarDistance2D); } - if ((scene.logDepthBuffer && - !CesiumMath.equalsEpsilon(far, frustumCommandsList[numberOfFrustums - 1].far, CesiumMath.EPSILON8)) || - (near !== Number.MAX_VALUE && (numFrustums !== numberOfFrustums || (frustumCommandsList.length !== 0 && - (near < frustumCommandsList[0].near || (far > frustumCommandsList[numberOfFrustums - 1].far && !CesiumMath.equalsEpsilon(far, frustumCommandsList[numberOfFrustums - 1].far, CesiumMath.EPSILON8))))))) { + var farChange; + if (frustumCommandsList.length !== 0){ + farChange = far / frustumCommandsList[numberOfFrustums - 1].far; + } + if ((near !== Number.MAX_VALUE && (numFrustums !== numberOfFrustums || (frustumCommandsList.length !== 0 && + ((scene.logDepthBuffer && + (isFinite(farChange) && !CesiumMath.equalsEpsilon(1, farChange, CesiumMath.EPSILON8))) || + (near < frustumCommandsList[0].near || (far > frustumCommandsList[numberOfFrustums - 1].far && !CesiumMath.equalsEpsilon(far, frustumCommandsList[numberOfFrustums - 1].far, CesiumMath.EPSILON8)))))))) { updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, scene.nearToFarDistance2D); createPotentiallyVisibleSet(scene); } diff --git a/Source/Scene/SceneTransforms.js b/Source/Scene/SceneTransforms.js index 8dbb0a652fb0..1c3dc135a8c9 100644 --- a/Source/Scene/SceneTransforms.js +++ b/Source/Scene/SceneTransforms.js @@ -306,6 +306,12 @@ define([ var ndc = Cartesian4.clone(Cartesian4.UNIT_W, scratchNDC); ndc.x = (drawingBufferPosition.x - viewport.x) / viewport.width * 2.0 - 1.0; ndc.y = (drawingBufferPosition.y - viewport.y) / viewport.height * 2.0 - 1.0; + if (scene.logDepthBuffer) { + var frustumCommand = scene._frustumCommandsList[0]; + var far = frustumCommand.far; + var near = frustumCommand.near; + depth = far * (1 - near/(Math.exp(depth * Math.log(far + 1.)) - 1)) / (far - near); + } ndc.z = (depth * 2.0) - 1.0; ndc.w = 1.0; From e15adbf74933213598ca5e505c80f9dac6a71a65 Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Wed, 27 Sep 2017 15:56:28 +0300 Subject: [PATCH 03/68] removed trailing decimal point to avoid eslint warn --- Source/Scene/SceneTransforms.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Scene/SceneTransforms.js b/Source/Scene/SceneTransforms.js index 1c3dc135a8c9..33c303a5e672 100644 --- a/Source/Scene/SceneTransforms.js +++ b/Source/Scene/SceneTransforms.js @@ -310,7 +310,7 @@ define([ var frustumCommand = scene._frustumCommandsList[0]; var far = frustumCommand.far; var near = frustumCommand.near; - depth = far * (1 - near/(Math.exp(depth * Math.log(far + 1.)) - 1)) / (far - near); + depth = far * (1 - near/(Math.exp(depth * Math.log(far + 1)) - 1)) / (far - near); } ndc.z = (depth * 2.0) - 1.0; ndc.w = 1.0; From bd5428ff9479518719bb51b1fc78beb4aef84516 Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Wed, 27 Sep 2017 16:18:19 +0300 Subject: [PATCH 04/68] added to CONTRIBUTORS --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 119d7c8ecb62..0c4d20107eaf 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -72,6 +72,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu * [Dave Whipps](https://github.com/dwhipps) * [Geoscan](https://www.geoscan.aero) * [Andrey Orlov](https://github.com/AndreyOrlov) + * [George Vinokhodov](https://github.com/Vineg) * [The Imagineers](https://www.theimagineers.com/) * [Heerco Grond](https://github.com/HeercoGrond) * [Camptocamp SA](https://www.camptocamp.com/) From ceedc326d648f0f997dc61b36209a945305ba1e2 Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Wed, 27 Sep 2017 17:45:10 +0300 Subject: [PATCH 05/68] fallback to multifrustum if no fragDepth extension --- Source/Scene/Scene.js | 12 +++++++++++- Source/Shaders/GlobeFS.glsl | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index be0b2e83fbc7..44c6f649bf7a 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -230,7 +230,6 @@ define([ */ function Scene(options) { options = defaultValue(options, defaultValue.EMPTY_OBJECT); - this.logDepthBuffer = defaultValue(options.logDepthBuffer, false); var canvas = options.canvas; var contextOptions = options.contextOptions; @@ -253,6 +252,17 @@ define([ creditContainer.style['padding-right'] = '5px'; canvas.parentNode.appendChild(creditContainer); } + if (options.logDepthBuffer) { + if (context.fragmentDepth) { + this.logDepthBuffer = true; + } else { + console.log('Can\'t use logarithmic depth buffer, since fragDepth extension not supported.\ +Fall back to multifrustum view'); + this.logDepthBuffer = false; + } + } else { + this.logDepthBuffer = false; + } this._id = createGuid(); this._jobScheduler = new JobScheduler(); diff --git a/Source/Shaders/GlobeFS.glsl b/Source/Shaders/GlobeFS.glsl index e727d3d7a74e..56ee64969775 100644 --- a/Source/Shaders/GlobeFS.glsl +++ b/Source/Shaders/GlobeFS.glsl @@ -1,5 +1,7 @@ //#define SHOW_TILE_BOUNDARIES +#ifdef GL_EXT_frag_depth #extension GL_EXT_frag_depth : enable +#endif uniform vec4 u_initialColor; uniform float u_far; From d70db84948273e2a9aebcc2b42afcfdfcafe6a3d Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Thu, 28 Sep 2017 14:50:56 +0300 Subject: [PATCH 06/68] Update CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index aa13b9f20e12..8793e10c9b77 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -24,6 +24,7 @@ Change Log * Fixed loading of binary glTFs containing CRN or KTX textures. [#5753](https://github.com/AnalyticalGraphicsInc/cesium/pull/5753) * Fixed specular computation for certain models using the `KHR_materials_common` extension. [#5773](https://github.com/AnalyticalGraphicsInc/cesium/pull/5773) * Fixed a picking bug in the `3D Tiles Interactivity` Sandcastle demo. [#5703](https://github.com/AnalyticalGraphicsInc/cesium/issues/5703) +* Added option "logDepthBuffer" to Cesium.Viewer. With this option globe is rendered in 1 frustum with logarithmic depth. It helps to avoid artifacts on the connection of two frustums. ### 1.36 - 2017-08-01 From a79138da454e30cde55dfa6d4a46960f771dfb6e Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Fri, 13 Oct 2017 17:45:57 +0300 Subject: [PATCH 07/68] added log depth to objects, removed option to turn it off --- Source/Scene/BillboardCollection.js | 2 ++ Source/Scene/ClassificationPrimitive.js | 14 +++++----- Source/Scene/EllipsoidPrimitive.js | 1 + Source/Scene/GlobeSurfaceTileProvider.js | 5 ++-- Source/Scene/Model.js | 26 ++++++++++++++++--- Source/Scene/PointCloud3DTileContent.js | 11 ++++++-- Source/Scene/Scene.js | 13 ++++------ Source/Scene/SceneTransforms.js | 2 +- .../PerInstanceColorAppearanceFS.glsl | 6 +++++ .../PerInstanceColorAppearanceVS.glsl | 2 ++ .../PerInstanceFlatColorAppearanceFS.glsl | 6 +++++ .../PerInstanceFlatColorAppearanceVS.glsl | 2 ++ .../PolylineMaterialAppearanceVS.glsl | 4 ++- .../TexturedMaterialAppearanceFS.glsl | 8 +++++- Source/Shaders/BillboardCollectionFS.glsl | 6 +++++ Source/Shaders/BillboardCollectionVS.glsl | 2 ++ .../Shaders/Builtin/Functions/logDepth.glsl | 6 +++++ Source/Shaders/DepthPlaneFS.glsl | 8 +++++- Source/Shaders/EllipsoidFS.glsl | 14 ++-------- Source/Shaders/GlobeFS.glsl | 5 +--- Source/Shaders/PolylineFS.glsl | 6 +++++ Source/Shaders/PolylineVS.glsl | 2 ++ Source/Shaders/ShadowVolumeFS.glsl | 3 ++- Source/Shaders/ShadowVolumeVS.glsl | 2 ++ Source/Widgets/CesiumWidget/CesiumWidget.js | 3 +-- Source/Widgets/Viewer/Viewer.js | 3 +-- 26 files changed, 115 insertions(+), 47 deletions(-) create mode 100644 Source/Shaders/Builtin/Functions/logDepth.glsl diff --git a/Source/Scene/BillboardCollection.js b/Source/Scene/BillboardCollection.js index ec9c06b36195..017c69b4e38f 100644 --- a/Source/Scene/BillboardCollection.js +++ b/Source/Scene/BillboardCollection.js @@ -4,6 +4,7 @@ define([ '../Core/Cartesian2', '../Core/Cartesian3', '../Core/Color', + '../Core/combine', '../Core/ComponentDatatype', '../Core/defaultValue', '../Core/defined', @@ -39,6 +40,7 @@ define([ Cartesian2, Cartesian3, Color, + combine, ComponentDatatype, defaultValue, defined, diff --git a/Source/Scene/ClassificationPrimitive.js b/Source/Scene/ClassificationPrimitive.js index 49da56ec4403..7002deebe39d 100644 --- a/Source/Scene/ClassificationPrimitive.js +++ b/Source/Scene/ClassificationPrimitive.js @@ -1,5 +1,6 @@ define([ '../Core/ColorGeometryInstanceAttribute', + '../Core/combine', '../Core/defaultValue', '../Core/defined', '../Core/defineProperties', @@ -25,6 +26,7 @@ define([ './StencilOperation' ], function( ColorGeometryInstanceAttribute, + combine, defaultValue, defined, defineProperties, @@ -569,7 +571,7 @@ define([ } } - function createColorCommands(classificationPrimitive, colorCommands) { + function createColorCommands(classificationPrimitive, colorCommands, frameState) { var primitive = classificationPrimitive._primitive; var length = primitive._va.length * 3; colorCommands.length = length * 2; @@ -634,7 +636,7 @@ define([ } } - function createPickCommands(classificationPrimitive, pickCommands) { + function createPickCommands(classificationPrimitive, pickCommands, frameState) { var primitive = classificationPrimitive._primitive; var pickOffsets = primitive._pickOffsets; var length = pickOffsets.length * 3; @@ -710,9 +712,9 @@ define([ } } - function createCommands(classificationPrimitive, appearance, material, translucent, twoPasses, colorCommands, pickCommands) { - createColorCommands(classificationPrimitive, colorCommands); - createPickCommands(classificationPrimitive, pickCommands); + function createCommands(classificationPrimitive, appearance, material, translucent, twoPasses, colorCommands, pickCommands, frameState) { + createColorCommands(classificationPrimitive, colorCommands, frameState); + createPickCommands(classificationPrimitive, pickCommands, frameState); } function boundingVolumeIndex(commandIndex, length) { @@ -870,7 +872,7 @@ define([ createShaderProgram(that, frameState); }; primitiveOptions._createCommandsFunction = function(primitive, appearance, material, translucent, twoPasses, colorCommands, pickCommands) { - createCommands(that, undefined, undefined, true, false, colorCommands, pickCommands); + createCommands(that, undefined, undefined, true, false, colorCommands, pickCommands, frameState); }; if (defined(this._updateAndQueueCommandsFunction)) { diff --git a/Source/Scene/EllipsoidPrimitive.js b/Source/Scene/EllipsoidPrimitive.js index cd4ceff8f342..ae94ae608c2e 100644 --- a/Source/Scene/EllipsoidPrimitive.js +++ b/Source/Scene/EllipsoidPrimitive.js @@ -262,6 +262,7 @@ define([ * @exception {DeveloperError} this.material must be defined. */ EllipsoidPrimitive.prototype.update = function(frameState) { + if (!this.show || (frameState.mode !== SceneMode.SCENE3D) || (!defined(this.center)) || diff --git a/Source/Scene/GlobeSurfaceTileProvider.js b/Source/Scene/GlobeSurfaceTileProvider.js index 123570ea88cc..1fbe406dcc11 100644 --- a/Source/Scene/GlobeSurfaceTileProvider.js +++ b/Source/Scene/GlobeSurfaceTileProvider.js @@ -6,6 +6,7 @@ define([ '../Core/Cartesian4', '../Core/Color', '../Core/ColorGeometryInstanceAttribute', + '../Core/Combine', '../Core/defaultValue', '../Core/defined', '../Core/defineProperties', @@ -50,6 +51,7 @@ define([ Cartesian4, Color, ColorGeometryInstanceAttribute, + combine, defaultValue, defined, defineProperties, @@ -763,9 +765,6 @@ define([ function createTileUniformMap(frameState) { var uniformMap = { - u_far : function() { - return frameState.camera._scene._frustumCommandsList[0].far; - }, u_initialColor : function() { return this.properties.initialColor; }, diff --git a/Source/Scene/Model.js b/Source/Scene/Model.js index 5fb81de4deea..5b36f3943ccf 100644 --- a/Source/Scene/Model.js +++ b/Source/Scene/Model.js @@ -1,4 +1,4 @@ -define([ + define([ '../Core/BoundingSphere', '../Core/Cartesian2', '../Core/Cartesian3', @@ -2027,11 +2027,16 @@ define([ return defined(gltf.asset) ? defaultValue(gltf.asset.premultipliedAlpha, false) : false; } - function modifyShaderForColor(shader, premultipliedAlpha) { + function modifyShaderForColorAndDepth(shader, premultipliedAlpha) { shader = ShaderSource.replaceMain(shader, 'gltf_blend_main'); + shader = '#ifdef GL_EXT_frag_depth \n' + + '#extension GL_EXT_frag_depth : enable \n' + + '#endif \n' + + shader; shader += 'uniform vec4 gltf_color; \n' + 'uniform float gltf_colorBlend; \n' + + 'varying vec4 v_position; \n' + 'void main() \n' + '{ \n' + ' gltf_blend_main(); \n'; @@ -2055,6 +2060,20 @@ define([ ' float highlight = ceil(gltf_colorBlend); \n' + ' gl_FragColor.rgb *= mix(gltf_color.rgb, vec3(1.0), highlight); \n' + ' gl_FragColor.a *= gltf_color.a; \n' + + ' czm_logDepth(v_position.w); \n' + + '} \n'; + + return shader; + } + + function modifyVertexShaderForDepth(shader) { + shader = ShaderSource.replaceMain(shader, 'gltf_morph_main'); + shader += + 'varying vec4 v_position; \n' + + 'void main() \n' + + '{ \n' + + ' gltf_morph_main(); \n' + + ' v_position = u_projectionMatrix * (u_modelViewMatrix * vec4(a_position,1.0)); \n' + '} \n'; return shader; @@ -2110,7 +2129,8 @@ define([ } var premultipliedAlpha = hasPremultipliedAlpha(model); - var blendFS = modifyShaderForColor(fs, premultipliedAlpha); + var blendFS = modifyShaderForColorAndDepth(fs, premultipliedAlpha); + vs = modifyVertexShaderForDepth(vs); var drawVS = modifyShader(vs, id, model._vertexShaderLoaded); var drawFS = modifyShader(blendFS, id, model._fragmentShaderLoaded); diff --git a/Source/Scene/PointCloud3DTileContent.js b/Source/Scene/PointCloud3DTileContent.js index 7e447c28412f..0fb361e9bb55 100644 --- a/Source/Scene/PointCloud3DTileContent.js +++ b/Source/Scene/PointCloud3DTileContent.js @@ -898,6 +898,7 @@ define([ var vs = 'attribute vec3 a_position; \n' + 'varying vec4 v_color; \n' + + 'varying vec4 v_position; \n' + 'uniform vec2 u_pointSizeAndTilesetTime; \n' + 'uniform vec4 u_constantColor; \n' + 'uniform vec4 u_highlightColor; \n' + @@ -1014,7 +1015,8 @@ define([ } vs += ' v_color = color; \n' + - ' gl_Position = czm_modelViewProjection * vec4(position, 1.0); \n'; + ' gl_Position = czm_modelViewProjection * vec4(position, 1.0); \n' + + ' v_position = gl_Position; \n'; if (hasNormals && backFaceCulling) { vs += ' float visible = step(-normal.z, 0.0); \n' + @@ -1027,10 +1029,15 @@ define([ vs += '} \n'; - var fs = 'varying vec4 v_color; \n' + + var fs = '#ifdef GL_EXT_frag_depth \n' + + ' #extension GL_EXT_frag_depth : enable \n' + + '#endif \n' + + 'varying vec4 v_color; \n' + + 'varying vec4 v_position; \n' + 'void main() \n' + '{ \n' + ' gl_FragColor = v_color; \n' + + ' czm_logDepth(v_position.w); \n' + '} \n'; var drawVS = vs; diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 44c6f649bf7a..54c146bf8b56 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -252,15 +252,11 @@ define([ creditContainer.style['padding-right'] = '5px'; canvas.parentNode.appendChild(creditContainer); } - if (options.logDepthBuffer) { - if (context.fragmentDepth) { - this.logDepthBuffer = true; - } else { - console.log('Can\'t use logarithmic depth buffer, since fragDepth extension not supported.\ -Fall back to multifrustum view'); - this.logDepthBuffer = false; - } + if (context.fragmentDepth) { + this.logDepthBuffer = true; } else { + console.log('Can\'t use logarithmic depth buffer, since fragDepth extension not supported.' + + 'Fall back to multifrustum view'); this.logDepthBuffer = false; } @@ -293,6 +289,7 @@ Fall back to multifrustum view'); this._computeCommandList = []; this._frustumCommandsList = []; + var that = this; this._overlayCommandList = []; this._pickFramebuffer = undefined; diff --git a/Source/Scene/SceneTransforms.js b/Source/Scene/SceneTransforms.js index 33c303a5e672..db3738feaba4 100644 --- a/Source/Scene/SceneTransforms.js +++ b/Source/Scene/SceneTransforms.js @@ -310,7 +310,7 @@ define([ var frustumCommand = scene._frustumCommandsList[0]; var far = frustumCommand.far; var near = frustumCommand.near; - depth = far * (1 - near/(Math.exp(depth * Math.log(far + 1)) - 1)) / (far - near); + depth = far * (1 - 1 /(Math.exp(depth * Math.log(far / near + 1)) - 1)) / (far - near); } ndc.z = (depth * 2.0) - 1.0; ndc.w = 1.0; diff --git a/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl b/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl index 6bef5b438655..d78b3daf6805 100644 --- a/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl +++ b/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl @@ -1,6 +1,11 @@ +#ifdef GL_EXT_frag_depth +#extension GL_EXT_frag_depth : enable +#endif + varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec4 v_color; +varying vec4 v_position; void main() { @@ -19,4 +24,5 @@ void main() material.alpha = v_color.a; gl_FragColor = czm_phong(normalize(positionToEyeEC), material); + czm_logDepth(v_position.w); } diff --git a/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl b/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl index b429e64c6896..82ed45a4edaa 100644 --- a/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl @@ -7,6 +7,7 @@ attribute float batchId; varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec4 v_color; +varying vec4 v_position; void main() { @@ -17,4 +18,5 @@ void main() v_color = color; gl_Position = czm_modelViewProjectionRelativeToEye * p; + v_position = gl_Position; } diff --git a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl index 3649a0883f23..93ae1c912931 100644 --- a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl +++ b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl @@ -1,6 +1,12 @@ +#ifdef GL_EXT_frag_depth +#extension GL_EXT_frag_depth : enable +#endif + varying vec4 v_color; +varying vec4 v_position; void main() { gl_FragColor = v_color; + czm_logDepth(v_position.w); } diff --git a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl index ce015c678423..570328ea1d9f 100644 --- a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl @@ -4,6 +4,7 @@ attribute vec4 color; attribute float batchId; varying vec4 v_color; +varying vec4 v_position; void main() { @@ -12,4 +13,5 @@ void main() v_color = color; gl_Position = czm_modelViewProjectionRelativeToEye * p; + v_position = gl_Position; } diff --git a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl index 6b61f09369c6..3a171d2fa2fa 100644 --- a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl @@ -9,8 +9,9 @@ attribute vec2 st; attribute float batchId; varying float v_width; -varying vec2 v_st; +varying vec2 v_st; varying float v_polylineAngle; +varying vec4 v_position; void main() { @@ -27,4 +28,5 @@ void main() vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, v_polylineAngle); gl_Position = czm_viewportOrthographic * positionWC; + v_position = (czm_modelViewRelativeToEye * p);\n\; } diff --git a/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl b/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl index 1a52fc8d7e84..56ef7f5b8d44 100644 --- a/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl +++ b/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl @@ -1,3 +1,7 @@ +#ifdef GL_EXT_frag_depth +#extension GL_EXT_frag_depth : enable +#endif + varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec2 v_st; @@ -6,7 +10,7 @@ void main() { vec3 positionToEyeEC = -v_positionEC; - vec3 normalEC = normalize(v_normalEC);; + vec3 normalEC = normalize(v_normalEC); #ifdef FACE_FORWARD normalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC); #endif @@ -22,4 +26,6 @@ void main() #else gl_FragColor = czm_phong(normalize(positionToEyeEC), material); #endif + + czm_logDepth(-v_positionEC.z); } diff --git a/Source/Shaders/BillboardCollectionFS.glsl b/Source/Shaders/BillboardCollectionFS.glsl index 64b133fdbf27..9ccd4c8ed2be 100644 --- a/Source/Shaders/BillboardCollectionFS.glsl +++ b/Source/Shaders/BillboardCollectionFS.glsl @@ -1,6 +1,11 @@ +#ifdef GL_EXT_frag_depth +#extension GL_EXT_frag_depth : enable +#endif + uniform sampler2D u_atlas; varying vec2 v_textureCoordinates; +varying vec4 v_position; #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; @@ -45,4 +50,5 @@ void main() #else gl_FragColor = color; #endif + czm_logDepth(-v_position.z); } diff --git a/Source/Shaders/BillboardCollectionVS.glsl b/Source/Shaders/BillboardCollectionVS.glsl index b7dede3a6d20..19a02873ecc4 100644 --- a/Source/Shaders/BillboardCollectionVS.glsl +++ b/Source/Shaders/BillboardCollectionVS.glsl @@ -12,6 +12,7 @@ attribute vec4 pixelOffsetScaleByDistance; // near, nearScale, f attribute vec3 distanceDisplayConditionAndDisableDepth; // near, far, disableDepthTestDistance varying vec2 v_textureCoordinates; +varying vec4 v_position; #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; @@ -254,6 +255,7 @@ void main() vec4 positionWC = computePositionWindowCoordinates(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters); gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); + v_position = positionEC; v_textureCoordinates = textureCoordinates; #ifdef DISABLE_DEPTH_DISTANCE diff --git a/Source/Shaders/Builtin/Functions/logDepth.glsl b/Source/Shaders/Builtin/Functions/logDepth.glsl new file mode 100644 index 000000000000..0f5f8c47ae86 --- /dev/null +++ b/Source/Shaders/Builtin/Functions/logDepth.glsl @@ -0,0 +1,6 @@ +void czm_logDepth(float z) +{ +#ifdef GL_EXT_frag_depth + gl_FragDepthEXT = log(z / czm_currentFrustum.x + 1.) / log( czm_currentFrustum.y / czm_currentFrustum.x + 1.); +#endif +} diff --git a/Source/Shaders/DepthPlaneFS.glsl b/Source/Shaders/DepthPlaneFS.glsl index 4d898e118716..4d4bf13fb038 100644 --- a/Source/Shaders/DepthPlaneFS.glsl +++ b/Source/Shaders/DepthPlaneFS.glsl @@ -1,3 +1,7 @@ +#ifdef GL_EXT_frag_depth +#extension GL_EXT_frag_depth : enable +#endif + varying vec4 positionEC; void main() @@ -7,7 +11,9 @@ void main() vec3 direction = normalize(positionEC.xyz); czm_ray ray = czm_ray(vec3(0.0), direction); - + + czm_logDepth(-positionEC.z); + czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid); if (!czm_isEmpty(intersection)) { diff --git a/Source/Shaders/EllipsoidFS.glsl b/Source/Shaders/EllipsoidFS.glsl index a982dbcddd1a..083d77edc516 100644 --- a/Source/Shaders/EllipsoidFS.glsl +++ b/Source/Shaders/EllipsoidFS.glsl @@ -90,18 +90,8 @@ void main() gl_FragColor = mix(insideFaceColor, outsideFaceColor, outsideFaceColor.a); gl_FragColor.a = 1.0 - (1.0 - insideFaceColor.a) * (1.0 - outsideFaceColor.a); - + #ifdef WRITE_DEPTH -#ifdef GL_EXT_frag_depth - t = (intersection.start != 0.0) ? intersection.start : intersection.stop; - vec3 positionEC = czm_pointAlongRay(ray, t); - vec4 positionCC = czm_projection * vec4(positionEC, 1.0); - float z = positionCC.z / positionCC.w; - - float n = czm_depthRange.near; - float f = czm_depthRange.far; - - gl_FragDepthEXT = (z * (f - n) + f + n) * 0.5; -#endif + czm_logDepth(v_positionEC.z); #endif } diff --git a/Source/Shaders/GlobeFS.glsl b/Source/Shaders/GlobeFS.glsl index 56ee64969775..75184e304eee 100644 --- a/Source/Shaders/GlobeFS.glsl +++ b/Source/Shaders/GlobeFS.glsl @@ -4,7 +4,6 @@ #endif uniform vec4 u_initialColor; -uniform float u_far; #if TEXTURE_UNITS > 0 uniform sampler2D u_dayTextures[TEXTURE_UNITS]; @@ -210,9 +209,7 @@ void main() #else gl_FragColor = finalColor; #endif -#ifdef LOG_DEPTH_BUFFER - gl_FragDepthEXT = log(v_position.w + 1.) / log(u_far + 1.); -#endif + czm_logDepth(v_position.w); } #ifdef SHOW_REFLECTIVE_OCEAN diff --git a/Source/Shaders/PolylineFS.glsl b/Source/Shaders/PolylineFS.glsl index e409035aed29..1f8f23648750 100644 --- a/Source/Shaders/PolylineFS.glsl +++ b/Source/Shaders/PolylineFS.glsl @@ -1,4 +1,9 @@ +#ifdef GL_EXT_frag_depth +#extension GL_EXT_frag_depth : enable +#endif + varying vec2 v_st; +varying vec4 v_position; void main() { @@ -10,4 +15,5 @@ void main() czm_material material = czm_getMaterial(materialInput); gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); + czm_logDepth(-v_position.z); } diff --git a/Source/Shaders/PolylineVS.glsl b/Source/Shaders/PolylineVS.glsl index ce95b001a470..8cf5da174f0c 100644 --- a/Source/Shaders/PolylineVS.glsl +++ b/Source/Shaders/PolylineVS.glsl @@ -16,6 +16,7 @@ varying vec2 v_st; varying float v_width; varying vec4 czm_pickColor; varying float v_polylineAngle; +varying vec4 v_position; void main() { @@ -92,6 +93,7 @@ void main() vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, v_polylineAngle); gl_Position = czm_viewportOrthographic * positionWC * show; + v_position = gl_Position; v_st = vec2(texCoord, clamp(expandDir, 0.0, 1.0)); v_width = width; diff --git a/Source/Shaders/ShadowVolumeFS.glsl b/Source/Shaders/ShadowVolumeFS.glsl index 4eac3f5ee824..56f18aa1d4b7 100644 --- a/Source/Shaders/ShadowVolumeFS.glsl +++ b/Source/Shaders/ShadowVolumeFS.glsl @@ -5,6 +5,7 @@ // emulated noperspective varying float v_WindowZ; varying vec4 v_color; +varying vec4 v_position; void writeDepthClampedToFarPlane() { @@ -16,5 +17,5 @@ void writeDepthClampedToFarPlane() void main(void) { gl_FragColor = v_color; - writeDepthClampedToFarPlane(); + czm_logDepth(v_position.w); } diff --git a/Source/Shaders/ShadowVolumeVS.glsl b/Source/Shaders/ShadowVolumeVS.glsl index a85fc04a548e..a3dec21f637a 100644 --- a/Source/Shaders/ShadowVolumeVS.glsl +++ b/Source/Shaders/ShadowVolumeVS.glsl @@ -12,6 +12,7 @@ uniform float u_globeMinimumAltitude; // emulated noperspective varying float v_WindowZ; varying vec4 v_color; +varying vec4 v_position; vec4 depthClampFarPlane(vec4 vertexInClipCoordinates) { @@ -35,4 +36,5 @@ void main() #endif gl_Position = depthClampFarPlane(czm_modelViewProjectionRelativeToEye * position); + v_position = gl_Position; } diff --git a/Source/Widgets/CesiumWidget/CesiumWidget.js b/Source/Widgets/CesiumWidget/CesiumWidget.js index 1f7a1eed698a..da432e22142a 100644 --- a/Source/Widgets/CesiumWidget/CesiumWidget.js +++ b/Source/Widgets/CesiumWidget/CesiumWidget.js @@ -261,8 +261,7 @@ define([ scene3DOnly : defaultValue(options.scene3DOnly, false), terrainExaggeration : options.terrainExaggeration, shadows : options.shadows, - mapMode2D : options.mapMode2D, - logDepthBuffer : options.logDepthBuffer + mapMode2D : options.mapMode2D }); this._scene = scene; diff --git a/Source/Widgets/Viewer/Viewer.js b/Source/Widgets/Viewer/Viewer.js index 7300a1fcb3cc..2d0e990e488b 100644 --- a/Source/Widgets/Viewer/Viewer.js +++ b/Source/Widgets/Viewer/Viewer.js @@ -434,8 +434,7 @@ Either specify options.terrainProvider instead or set options.baseLayerPicker to terrainExaggeration : options.terrainExaggeration, shadows : options.shadows, terrainShadows : options.terrainShadows, - mapMode2D : options.mapMode2D, - logDepthBuffer : options.logDepthBuffer + mapMode2D : options.mapMode2D }); var dataSourceCollection = options.dataSources; From 4a79331ae7d25f210e514358342bdae602f4b33c Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Fri, 13 Oct 2017 18:00:58 +0300 Subject: [PATCH 08/68] removed unused variable --- Source/Scene/Scene.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 54c146bf8b56..db7151d944da 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -289,7 +289,6 @@ define([ this._computeCommandList = []; this._frustumCommandsList = []; - var that = this; this._overlayCommandList = []; this._pickFramebuffer = undefined; From 50c019ccd7fa6355ca0c2ed72ac07b564aedd11c Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Fri, 13 Oct 2017 18:20:42 +0300 Subject: [PATCH 09/68] fix wrong require --- Source/Scene/GlobeSurfaceTileProvider.js | 2 +- Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl | 2 ++ Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/Scene/GlobeSurfaceTileProvider.js b/Source/Scene/GlobeSurfaceTileProvider.js index 1fbe406dcc11..26ec3d725821 100644 --- a/Source/Scene/GlobeSurfaceTileProvider.js +++ b/Source/Scene/GlobeSurfaceTileProvider.js @@ -6,7 +6,7 @@ define([ '../Core/Cartesian4', '../Core/Color', '../Core/ColorGeometryInstanceAttribute', - '../Core/Combine', + '../Core/combine', '../Core/defaultValue', '../Core/defined', '../Core/defineProperties', diff --git a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl index 5bb9d0188828..38c9542cde12 100644 --- a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl @@ -9,6 +9,7 @@ attribute vec4 color; attribute float batchId; varying vec4 v_color; +varying vec4 v_position; void main() { @@ -25,4 +26,5 @@ void main() float angle; vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, angle); gl_Position = czm_viewportOrthographic * positionWC; + v_position = (czm_modelViewRelativeToEye * p); } diff --git a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl index 3a171d2fa2fa..6bd317ec3906 100644 --- a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl @@ -28,5 +28,5 @@ void main() vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, v_polylineAngle); gl_Position = czm_viewportOrthographic * positionWC; - v_position = (czm_modelViewRelativeToEye * p);\n\; + v_position = (czm_modelViewRelativeToEye * p); } From 043ebcca1ddacf8dcc121d994a28761a31d5fd52 Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Fri, 13 Oct 2017 20:54:08 +0300 Subject: [PATCH 10/68] moved extension to logDepth function --- Source/Scene/Model.js | 4 ---- Source/Scene/PointCloud3DTileContent.js | 5 +---- .../Shaders/Appearances/PerInstanceColorAppearanceFS.glsl | 4 ---- .../Appearances/PerInstanceFlatColorAppearanceFS.glsl | 4 ---- .../Shaders/Appearances/TexturedMaterialAppearanceFS.glsl | 4 ---- Source/Shaders/BillboardCollectionFS.glsl | 4 ---- Source/Shaders/Builtin/Functions/logDepth.glsl | 3 +++ Source/Shaders/DepthPlaneFS.glsl | 4 ---- Source/Shaders/EllipsoidFS.glsl | 6 ------ Source/Shaders/GlobeFS.glsl | 4 ---- Source/Shaders/PolylineFS.glsl | 4 ---- Source/Shaders/ShadowVolumeFS.glsl | 4 ---- 12 files changed, 4 insertions(+), 46 deletions(-) diff --git a/Source/Scene/Model.js b/Source/Scene/Model.js index 4cbc9ca847ab..6ee0e51a2c48 100644 --- a/Source/Scene/Model.js +++ b/Source/Scene/Model.js @@ -2014,10 +2014,6 @@ function modifyShaderForColorAndDepth(shader, premultipliedAlpha) { shader = ShaderSource.replaceMain(shader, 'gltf_blend_main'); - shader = '#ifdef GL_EXT_frag_depth \n' + - '#extension GL_EXT_frag_depth : enable \n' + - '#endif \n' + - shader; shader += 'uniform vec4 gltf_color; \n' + 'uniform float gltf_colorBlend; \n' + diff --git a/Source/Scene/PointCloud3DTileContent.js b/Source/Scene/PointCloud3DTileContent.js index 1e9e44912a46..43f0789d3de5 100644 --- a/Source/Scene/PointCloud3DTileContent.js +++ b/Source/Scene/PointCloud3DTileContent.js @@ -1031,10 +1031,7 @@ define([ vs += '} \n'; - var fs = '#ifdef GL_EXT_frag_depth \n' + - ' #extension GL_EXT_frag_depth : enable \n' + - '#endif \n' + - 'varying vec4 v_color; \n' + + var fs = 'varying vec4 v_color; \n' + 'varying vec4 v_position; \n' + 'void main() \n' + '{ \n' + diff --git a/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl b/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl index d78b3daf6805..7ec3960f4535 100644 --- a/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl +++ b/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl @@ -1,7 +1,3 @@ -#ifdef GL_EXT_frag_depth -#extension GL_EXT_frag_depth : enable -#endif - varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec4 v_color; diff --git a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl index 93ae1c912931..80cb20472f35 100644 --- a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl +++ b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl @@ -1,7 +1,3 @@ -#ifdef GL_EXT_frag_depth -#extension GL_EXT_frag_depth : enable -#endif - varying vec4 v_color; varying vec4 v_position; diff --git a/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl b/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl index 56ef7f5b8d44..c1da24fa15cb 100644 --- a/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl +++ b/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl @@ -1,7 +1,3 @@ -#ifdef GL_EXT_frag_depth -#extension GL_EXT_frag_depth : enable -#endif - varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec2 v_st; diff --git a/Source/Shaders/BillboardCollectionFS.glsl b/Source/Shaders/BillboardCollectionFS.glsl index 9ccd4c8ed2be..c2edfc9366b8 100644 --- a/Source/Shaders/BillboardCollectionFS.glsl +++ b/Source/Shaders/BillboardCollectionFS.glsl @@ -1,7 +1,3 @@ -#ifdef GL_EXT_frag_depth -#extension GL_EXT_frag_depth : enable -#endif - uniform sampler2D u_atlas; varying vec2 v_textureCoordinates; diff --git a/Source/Shaders/Builtin/Functions/logDepth.glsl b/Source/Shaders/Builtin/Functions/logDepth.glsl index 0f5f8c47ae86..25c19cca1bcf 100644 --- a/Source/Shaders/Builtin/Functions/logDepth.glsl +++ b/Source/Shaders/Builtin/Functions/logDepth.glsl @@ -1,3 +1,6 @@ +#ifdef GL_EXT_frag_depth +#extension GL_EXT_frag_depth : enable +#endif void czm_logDepth(float z) { #ifdef GL_EXT_frag_depth diff --git a/Source/Shaders/DepthPlaneFS.glsl b/Source/Shaders/DepthPlaneFS.glsl index 4d4bf13fb038..5361950e5b66 100644 --- a/Source/Shaders/DepthPlaneFS.glsl +++ b/Source/Shaders/DepthPlaneFS.glsl @@ -1,7 +1,3 @@ -#ifdef GL_EXT_frag_depth -#extension GL_EXT_frag_depth : enable -#endif - varying vec4 positionEC; void main() diff --git a/Source/Shaders/EllipsoidFS.glsl b/Source/Shaders/EllipsoidFS.glsl index 083d77edc516..500f5ddd643d 100644 --- a/Source/Shaders/EllipsoidFS.glsl +++ b/Source/Shaders/EllipsoidFS.glsl @@ -1,9 +1,3 @@ -#ifdef WRITE_DEPTH -#ifdef GL_EXT_frag_depth -#extension GL_EXT_frag_depth : enable -#endif -#endif - uniform vec3 u_radii; uniform vec3 u_oneOverEllipsoidRadiiSquared; diff --git a/Source/Shaders/GlobeFS.glsl b/Source/Shaders/GlobeFS.glsl index 75184e304eee..18872c6f24ca 100644 --- a/Source/Shaders/GlobeFS.glsl +++ b/Source/Shaders/GlobeFS.glsl @@ -1,8 +1,4 @@ //#define SHOW_TILE_BOUNDARIES -#ifdef GL_EXT_frag_depth -#extension GL_EXT_frag_depth : enable -#endif - uniform vec4 u_initialColor; #if TEXTURE_UNITS > 0 diff --git a/Source/Shaders/PolylineFS.glsl b/Source/Shaders/PolylineFS.glsl index 1f8f23648750..a4f052f5baec 100644 --- a/Source/Shaders/PolylineFS.glsl +++ b/Source/Shaders/PolylineFS.glsl @@ -1,7 +1,3 @@ -#ifdef GL_EXT_frag_depth -#extension GL_EXT_frag_depth : enable -#endif - varying vec2 v_st; varying vec4 v_position; diff --git a/Source/Shaders/ShadowVolumeFS.glsl b/Source/Shaders/ShadowVolumeFS.glsl index 56f18aa1d4b7..3341933dd9f8 100644 --- a/Source/Shaders/ShadowVolumeFS.glsl +++ b/Source/Shaders/ShadowVolumeFS.glsl @@ -1,7 +1,3 @@ -#ifdef GL_EXT_frag_depth -#extension GL_EXT_frag_depth : enable -#endif - // emulated noperspective varying float v_WindowZ; varying vec4 v_color; From 3d5cd2b84893739caf178c41f8cd8f32dd3e9cf1 Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Fri, 10 Nov 2017 13:08:24 +0300 Subject: [PATCH 11/68] fixed polyline v_position --- Source/Shaders/PolylineVS.glsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Shaders/PolylineVS.glsl b/Source/Shaders/PolylineVS.glsl index 8cf5da174f0c..682708924b2c 100644 --- a/Source/Shaders/PolylineVS.glsl +++ b/Source/Shaders/PolylineVS.glsl @@ -93,7 +93,7 @@ void main() vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, v_polylineAngle); gl_Position = czm_viewportOrthographic * positionWC * show; - v_position = gl_Position; + v_position = czm_modelViewRelativeToEye * p; v_st = vec2(texCoord, clamp(expandDir, 0.0, 1.0)); v_width = width; From 451761c2b4719c326a783831cc030c47e2d199c1 Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Mon, 20 Nov 2017 15:40:23 +0300 Subject: [PATCH 12/68] Modified 3d tiles vertex shader. --- Source/Scene/Model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Scene/Model.js b/Source/Scene/Model.js index f3e0806363f1..0a661dd3e9fb 100644 --- a/Source/Scene/Model.js +++ b/Source/Scene/Model.js @@ -2054,7 +2054,7 @@ 'void main() \n' + '{ \n' + ' gltf_morph_main(); \n' + - ' v_position = u_projectionMatrix * (u_modelViewMatrix * vec4(a_position,1.0)); \n' + + ' v_position = gl_Position; \n' + '} \n'; return shader; From db0630f6ed413c3883de44d0fa8df07c4b8c5868 Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Tue, 5 Dec 2017 12:04:16 +0300 Subject: [PATCH 13/68] fixed 3d tiles picking --- Source/Scene/Model.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Source/Scene/Model.js b/Source/Scene/Model.js index 0a661dd3e9fb..3ca77e3fdf2e 100644 --- a/Source/Scene/Model.js +++ b/Source/Scene/Model.js @@ -2012,12 +2012,11 @@ return defined(gltf.asset) ? defaultValue(gltf.asset.premultipliedAlpha, false) : false; } - function modifyShaderForColorAndDepth(shader, premultipliedAlpha) { + function modifyShaderForColor(shader, premultipliedAlpha) { shader = ShaderSource.replaceMain(shader, 'gltf_blend_main'); shader += 'uniform vec4 gltf_color; \n' + 'uniform float gltf_colorBlend; \n' + - 'varying vec4 v_position; \n' + 'void main() \n' + '{ \n' + ' gltf_blend_main(); \n'; @@ -2041,6 +2040,18 @@ ' float highlight = ceil(gltf_colorBlend); \n' + ' gl_FragColor.rgb *= mix(gltf_color.rgb, vec3(1.0), highlight); \n' + ' gl_FragColor.a *= gltf_color.a; \n' + + '} \n'; + + return shader; + } + + function modifyFsShaderForDepth(shader) { + shader = ShaderSource.replaceMain(shader, 'czm_main'); + shader += + 'varying vec4 v_position; \n' + + 'void main() \n' + + '{ \n' + + ' czm_main(); \n' + ' czm_logDepth(v_position.w); \n' + '} \n'; @@ -2110,7 +2121,8 @@ } var premultipliedAlpha = hasPremultipliedAlpha(model); - var blendFS = modifyShaderForColorAndDepth(fs, premultipliedAlpha); + fs = modifyFsShaderForDepth(fs); + var blendFS = modifyShaderForColor(fs, premultipliedAlpha); vs = modifyVertexShaderForDepth(vs); var drawVS = modifyShader(vs, id, model._vertexShaderLoaded); From 8191c36512c79c67f8566f94622f4a1db490ece1 Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Fri, 29 Dec 2017 18:18:17 +0300 Subject: [PATCH 14/68] fixed polyline depth --- Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl index 38c9542cde12..ee902a01ddf5 100644 --- a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl @@ -27,4 +27,5 @@ void main() vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, angle); gl_Position = czm_viewportOrthographic * positionWC; v_position = (czm_modelViewRelativeToEye * p); + v_position.w = -v_position.z; } From 665909cbefa06f49368f70207f93aebfa089a238 Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Tue, 9 Jan 2018 19:03:24 +0300 Subject: [PATCH 15/68] fixed interpolation issue --- Source/Scene/Model.js | 8 +++---- Source/Scene/PointCloud3DTileContent.js | 8 +++---- Source/Scene/SceneTransforms.js | 24 ++++++++++++++++--- .../Appearances/AllMaterialAppearanceVS.glsl | 2 ++ .../PerInstanceColorAppearanceFS.glsl | 4 ++-- .../PerInstanceColorAppearanceVS.glsl | 4 ++-- .../PerInstanceFlatColorAppearanceFS.glsl | 4 ++-- .../PerInstanceFlatColorAppearanceVS.glsl | 4 ++-- .../PolylineColorAppearanceVS.glsl | 5 ++-- .../PolylineMaterialAppearanceVS.glsl | 4 ++-- .../TexturedMaterialAppearanceFS.glsl | 3 ++- .../TexturedMaterialAppearanceVS.glsl | 2 ++ Source/Shaders/BillboardCollectionFS.glsl | 4 ++-- Source/Shaders/BillboardCollectionVS.glsl | 4 ++-- .../Shaders/Builtin/Functions/logDepth.glsl | 4 ++-- Source/Shaders/DepthPlaneFS.glsl | 2 +- Source/Shaders/EllipsoidFS.glsl | 5 ++-- Source/Shaders/EllipsoidVS.glsl | 2 ++ Source/Shaders/GlobeFS.glsl | 4 ++-- Source/Shaders/GlobeVS.glsl | 4 ++-- Source/Shaders/PolylineFS.glsl | 4 ++-- Source/Shaders/PolylineVS.glsl | 4 ++-- Source/Shaders/ShadowVolumeFS.glsl | 4 ++-- Source/Shaders/ShadowVolumeVS.glsl | 4 ++-- 24 files changed, 70 insertions(+), 47 deletions(-) diff --git a/Source/Scene/Model.js b/Source/Scene/Model.js index ffdf00a40100..96d5a34832a0 100644 --- a/Source/Scene/Model.js +++ b/Source/Scene/Model.js @@ -2055,11 +2055,11 @@ function modifyFsShaderForDepth(shader) { shader = ShaderSource.replaceMain(shader, 'czm_main'); shader += - 'varying vec4 v_position; \n' + + 'varying float v_inverse_depth; \n' + 'void main() \n' + '{ \n' + ' czm_main(); \n' + - ' czm_logDepth(v_position.w); \n' + + ' czm_logDepth(v_inverse_depth); \n' + '} \n'; return shader; @@ -2068,11 +2068,11 @@ function modifyVertexShaderForDepth(shader) { shader = ShaderSource.replaceMain(shader, 'gltf_morph_main'); shader += - 'varying vec4 v_position; \n' + + 'varying float v_inverse_depth; \n' + 'void main() \n' + '{ \n' + ' gltf_morph_main(); \n' + - ' v_position = gl_Position; \n' + + ' v_inverse_depth = 1. / gl_Position.w; \n' + '} \n'; return shader; diff --git a/Source/Scene/PointCloud3DTileContent.js b/Source/Scene/PointCloud3DTileContent.js index 43f0789d3de5..e48ed93b902b 100644 --- a/Source/Scene/PointCloud3DTileContent.js +++ b/Source/Scene/PointCloud3DTileContent.js @@ -898,7 +898,7 @@ define([ var vs = 'attribute vec3 a_position; \n' + 'varying vec4 v_color; \n' + - 'varying vec4 v_position; \n' + + 'varying float v_inverse_depth; \n' + 'uniform vec2 u_pointSizeAndTilesetTime; \n' + 'uniform vec4 u_constantColor; \n' + 'uniform vec4 u_highlightColor; \n' + @@ -1016,7 +1016,7 @@ define([ vs += ' v_color = color; \n' + ' gl_Position = czm_modelViewProjection * vec4(position, 1.0); \n' + - ' v_position = gl_Position; \n'; + ' v_inverse_depth = 1. / gl_Position.w; \n'; if (hasNormals && backFaceCulling) { vs += ' float visible = step(-normal.z, 0.0); \n' + @@ -1032,11 +1032,11 @@ define([ vs += '} \n'; var fs = 'varying vec4 v_color; \n' + - 'varying vec4 v_position; \n' + + 'varying float v_inverse_depth; \n' + 'void main() \n' + '{ \n' + ' gl_FragColor = v_color; \n' + - ' czm_logDepth(v_position.w); \n' + + ' czm_logDepth(v_inverse_depth); \n' + '} \n'; var drawVS = vs; diff --git a/Source/Scene/SceneTransforms.js b/Source/Scene/SceneTransforms.js index db3738feaba4..517b5d5ff1a9 100644 --- a/Source/Scene/SceneTransforms.js +++ b/Source/Scene/SceneTransforms.js @@ -4,6 +4,7 @@ define([ '../Core/Cartesian3', '../Core/Cartesian4', '../Core/Cartographic', + '../Core/Color', '../Core/defined', '../Core/DeveloperError', '../Core/Math', @@ -18,6 +19,7 @@ define([ Cartesian3, Cartesian4, Cartographic, + Color, defined, DeveloperError, CesiumMath, @@ -310,7 +312,13 @@ define([ var frustumCommand = scene._frustumCommandsList[0]; var far = frustumCommand.far; var near = frustumCommand.near; - depth = far * (1 - 1 /(Math.exp(depth * Math.log(far / near + 1)) - 1)) / (far - near); + /* transforming logarithmic depth of form + * log(z / near) / log( far / near); + * to perspective form + * (far - far * near / z) / (far - near) + */ + console.log(Math.exp(depth * Math.log(far / near)) * near); + depth = far * (1 - 1 /(Math.exp(depth * Math.log(far / near)))) / (far - near); } ndc.z = (depth * 2.0) - 1.0; ndc.w = 1.0; @@ -336,8 +344,18 @@ define([ var w = 1.0 / worldCoords.w; Cartesian3.multiplyByScalar(worldCoords, w, worldCoords); } - - return Cartesian3.fromCartesian4(worldCoords, result); + result = Cartesian3.fromCartesian4(worldCoords, result); +// window.vw.entities.add({ +// name : 'Red sphere with black outline', +// position: result, +// ellipsoid : { +// radii : new Cartesian3(30.0, 30.0, 30.0), +// material : Color.RED.withAlpha(0.5), +// outline : true, +// outlineColor : Color.BLACK +// } +// }); + return result; }; return SceneTransforms; diff --git a/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl index 33d9d300a304..48b8db4416a2 100644 --- a/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl @@ -11,6 +11,7 @@ varying vec3 v_normalEC; varying vec3 v_tangentEC; varying vec3 v_bitangentEC; varying vec2 v_st; +v_inverse_depth = -1. / v_positionEC.z; void main() { @@ -21,6 +22,7 @@ void main() v_tangentEC = czm_normal * tangent; // tangent in eye coordinates v_bitangentEC = czm_normal * bitangent; // bitangent in eye coordinates v_st = st; + v_inverse_depth = -1. / v_positionEC.z; gl_Position = czm_modelViewProjectionRelativeToEye * p; } diff --git a/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl b/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl index 7ec3960f4535..abf0c39b733f 100644 --- a/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl +++ b/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl @@ -1,7 +1,7 @@ varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec4 v_color; -varying vec4 v_position; +varying float v_inverse_depth; void main() { @@ -20,5 +20,5 @@ void main() material.alpha = v_color.a; gl_FragColor = czm_phong(normalize(positionToEyeEC), material); - czm_logDepth(v_position.w); + czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl b/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl index 82ed45a4edaa..e54c2648f2dc 100644 --- a/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl @@ -7,7 +7,7 @@ attribute float batchId; varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec4 v_color; -varying vec4 v_position; +varying float v_inverse_depth; void main() { @@ -18,5 +18,5 @@ void main() v_color = color; gl_Position = czm_modelViewProjectionRelativeToEye * p; - v_position = gl_Position; + v_inverse_depth = 1. / gl_Position.w; } diff --git a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl index 80cb20472f35..08eebb3b5e98 100644 --- a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl +++ b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl @@ -1,8 +1,8 @@ varying vec4 v_color; -varying vec4 v_position; +varying float v_inverse_depth; void main() { gl_FragColor = v_color; - czm_logDepth(v_position.w); + czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl index 570328ea1d9f..32332459a445 100644 --- a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl @@ -4,7 +4,7 @@ attribute vec4 color; attribute float batchId; varying vec4 v_color; -varying vec4 v_position; +varying float v_inverse_depth; void main() { @@ -13,5 +13,5 @@ void main() v_color = color; gl_Position = czm_modelViewProjectionRelativeToEye * p; - v_position = gl_Position; + v_inverse_depth = 1. / gl_Position.w; } diff --git a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl index ee902a01ddf5..16a28d53975a 100644 --- a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl @@ -9,7 +9,7 @@ attribute vec4 color; attribute float batchId; varying vec4 v_color; -varying vec4 v_position; +varying float v_inverse_depth; void main() { @@ -26,6 +26,5 @@ void main() float angle; vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, angle); gl_Position = czm_viewportOrthographic * positionWC; - v_position = (czm_modelViewRelativeToEye * p); - v_position.w = -v_position.z; + v_inverse_depth = -1. / (czm_modelViewRelativeToEye * p).z; } diff --git a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl index 6bd317ec3906..358ebd7e69d9 100644 --- a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl @@ -11,7 +11,7 @@ attribute float batchId; varying float v_width; varying vec2 v_st; varying float v_polylineAngle; -varying vec4 v_position; +varying float v_inverse_depth; void main() { @@ -28,5 +28,5 @@ void main() vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, v_polylineAngle); gl_Position = czm_viewportOrthographic * positionWC; - v_position = (czm_modelViewRelativeToEye * p); + v_inverse_depth = -1. / (czm_modelViewRelativeToEye * p).z; } diff --git a/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl b/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl index c1da24fa15cb..9e2c4a91c157 100644 --- a/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl +++ b/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl @@ -1,6 +1,7 @@ varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec2 v_st; +varying float v_inverse_depth; void main() { @@ -23,5 +24,5 @@ void main() gl_FragColor = czm_phong(normalize(positionToEyeEC), material); #endif - czm_logDepth(-v_positionEC.z); + czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl index 19c102bba321..e9243376a0fa 100644 --- a/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl @@ -7,6 +7,7 @@ attribute float batchId; varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec2 v_st; +varying float v_inverse_depth; void main() { @@ -14,6 +15,7 @@ void main() v_positionEC = (czm_modelViewRelativeToEye * p).xyz; // position in eye coordinates v_normalEC = czm_normal * normal; // normal in eye coordinates + v_inverse_depth = -1. / v_positionEC.z; v_st = st; gl_Position = czm_modelViewProjectionRelativeToEye * p; diff --git a/Source/Shaders/BillboardCollectionFS.glsl b/Source/Shaders/BillboardCollectionFS.glsl index c2edfc9366b8..5285f3b3f8fb 100644 --- a/Source/Shaders/BillboardCollectionFS.glsl +++ b/Source/Shaders/BillboardCollectionFS.glsl @@ -1,7 +1,7 @@ uniform sampler2D u_atlas; varying vec2 v_textureCoordinates; -varying vec4 v_position; +varying float v_inverse_depth; #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; @@ -46,5 +46,5 @@ void main() #else gl_FragColor = color; #endif - czm_logDepth(-v_position.z); + czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/BillboardCollectionVS.glsl b/Source/Shaders/BillboardCollectionVS.glsl index 19a02873ecc4..1e58d25cc08f 100644 --- a/Source/Shaders/BillboardCollectionVS.glsl +++ b/Source/Shaders/BillboardCollectionVS.glsl @@ -12,7 +12,7 @@ attribute vec4 pixelOffsetScaleByDistance; // near, nearScale, f attribute vec3 distanceDisplayConditionAndDisableDepth; // near, far, disableDepthTestDistance varying vec2 v_textureCoordinates; -varying vec4 v_position; +varying float v_inverse_depth; #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; @@ -255,7 +255,7 @@ void main() vec4 positionWC = computePositionWindowCoordinates(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters); gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); - v_position = positionEC; + v_inverse_depth = -1. / positionEC.z; v_textureCoordinates = textureCoordinates; #ifdef DISABLE_DEPTH_DISTANCE diff --git a/Source/Shaders/Builtin/Functions/logDepth.glsl b/Source/Shaders/Builtin/Functions/logDepth.glsl index 25c19cca1bcf..0b3527458917 100644 --- a/Source/Shaders/Builtin/Functions/logDepth.glsl +++ b/Source/Shaders/Builtin/Functions/logDepth.glsl @@ -1,9 +1,9 @@ #ifdef GL_EXT_frag_depth #extension GL_EXT_frag_depth : enable #endif -void czm_logDepth(float z) +void czm_logDepth(float w) { #ifdef GL_EXT_frag_depth - gl_FragDepthEXT = log(z / czm_currentFrustum.x + 1.) / log( czm_currentFrustum.y / czm_currentFrustum.x + 1.); + gl_FragDepthEXT = -log(w * czm_currentFrustum.x) / log( czm_currentFrustum.y / czm_currentFrustum.x); #endif } diff --git a/Source/Shaders/DepthPlaneFS.glsl b/Source/Shaders/DepthPlaneFS.glsl index 5361950e5b66..64da577930e2 100644 --- a/Source/Shaders/DepthPlaneFS.glsl +++ b/Source/Shaders/DepthPlaneFS.glsl @@ -8,7 +8,7 @@ void main() vec3 direction = normalize(positionEC.xyz); czm_ray ray = czm_ray(vec3(0.0), direction); - czm_logDepth(-positionEC.z); + czm_logDepth(-1. / positionEC.z); czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid); if (!czm_isEmpty(intersection)) diff --git a/Source/Shaders/EllipsoidFS.glsl b/Source/Shaders/EllipsoidFS.glsl index 500f5ddd643d..acfddb472c02 100644 --- a/Source/Shaders/EllipsoidFS.glsl +++ b/Source/Shaders/EllipsoidFS.glsl @@ -2,6 +2,7 @@ uniform vec3 u_radii; uniform vec3 u_oneOverEllipsoidRadiiSquared; varying vec3 v_positionEC; +varying float v_inverse_depth; vec4 computeEllipsoidColor(czm_ray ray, float intersection, float side) { @@ -85,7 +86,5 @@ void main() gl_FragColor = mix(insideFaceColor, outsideFaceColor, outsideFaceColor.a); gl_FragColor.a = 1.0 - (1.0 - insideFaceColor.a) * (1.0 - outsideFaceColor.a); -#ifdef WRITE_DEPTH - czm_logDepth(v_positionEC.z); -#endif + czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/EllipsoidVS.glsl b/Source/Shaders/EllipsoidVS.glsl index af801899fd64..9c6252dd4221 100644 --- a/Source/Shaders/EllipsoidVS.glsl +++ b/Source/Shaders/EllipsoidVS.glsl @@ -3,6 +3,7 @@ attribute vec3 position; uniform vec3 u_radii; varying vec3 v_positionEC; +varying float v_inverse_depth; void main() { @@ -14,6 +15,7 @@ void main() v_positionEC = (czm_modelView * p).xyz; // position in eye coordinates gl_Position = czm_modelViewProjection * p; // position in clip coordinates + v_inverse_depth = 1. / v_positionEC.z; // With multi-frustum, when the ellipsoid primitive is positioned on the intersection of two frustums // and close to terrain, the terrain (writes depth) in the closest frustum can overwrite part of the diff --git a/Source/Shaders/GlobeFS.glsl b/Source/Shaders/GlobeFS.glsl index 8b9acefb87b7..52443f92b010 100644 --- a/Source/Shaders/GlobeFS.glsl +++ b/Source/Shaders/GlobeFS.glsl @@ -57,7 +57,7 @@ uniform float u_minimumBrightness; varying vec3 v_positionMC; varying vec3 v_positionEC; -varying vec4 v_position; +varying float v_inverse_depth; varying vec3 v_textureCoordinates; varying vec3 v_normalMC; varying vec3 v_normalEC; @@ -228,7 +228,7 @@ void main() #else gl_FragColor = finalColor; #endif - czm_logDepth(v_position.w); + czm_logDepth(v_inverse_depth); } #ifdef SHOW_REFLECTIVE_OCEAN diff --git a/Source/Shaders/GlobeVS.glsl b/Source/Shaders/GlobeVS.glsl index c4e555c79b3b..23ee727f924a 100644 --- a/Source/Shaders/GlobeVS.glsl +++ b/Source/Shaders/GlobeVS.glsl @@ -17,7 +17,7 @@ uniform vec2 u_southMercatorYAndOneOverHeight; varying vec3 v_positionMC; varying vec3 v_positionEC; -varying vec4 v_position; +varying float v_inverse_depth; varying vec3 v_textureCoordinates; varying vec3 v_normalMC; @@ -157,7 +157,7 @@ void main() v_textureCoordinates = vec3(textureCoordinates, webMercatorT); - v_position = gl_Position; + v_inverse_depth = 1. / gl_Position.w; #if defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL) v_positionEC = (u_modifiedModelView * vec4(position, 1.0)).xyz; v_positionMC = position3DWC; // position in model coordinates diff --git a/Source/Shaders/PolylineFS.glsl b/Source/Shaders/PolylineFS.glsl index a4f052f5baec..f162dcfa4b50 100644 --- a/Source/Shaders/PolylineFS.glsl +++ b/Source/Shaders/PolylineFS.glsl @@ -1,5 +1,5 @@ varying vec2 v_st; -varying vec4 v_position; +varying float v_inverse_depth; void main() { @@ -11,5 +11,5 @@ void main() czm_material material = czm_getMaterial(materialInput); gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); - czm_logDepth(-v_position.z); + czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/PolylineVS.glsl b/Source/Shaders/PolylineVS.glsl index 682708924b2c..4015531d8c92 100644 --- a/Source/Shaders/PolylineVS.glsl +++ b/Source/Shaders/PolylineVS.glsl @@ -16,7 +16,7 @@ varying vec2 v_st; varying float v_width; varying vec4 czm_pickColor; varying float v_polylineAngle; -varying vec4 v_position; +varying float v_inverse_depth; void main() { @@ -93,7 +93,7 @@ void main() vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, v_polylineAngle); gl_Position = czm_viewportOrthographic * positionWC * show; - v_position = czm_modelViewRelativeToEye * p; + v_inverse_depth = 1. / (czm_modelViewRelativeToEye * p).z; v_st = vec2(texCoord, clamp(expandDir, 0.0, 1.0)); v_width = width; diff --git a/Source/Shaders/ShadowVolumeFS.glsl b/Source/Shaders/ShadowVolumeFS.glsl index 3341933dd9f8..354fbc34a3a2 100644 --- a/Source/Shaders/ShadowVolumeFS.glsl +++ b/Source/Shaders/ShadowVolumeFS.glsl @@ -1,7 +1,7 @@ // emulated noperspective varying float v_WindowZ; varying vec4 v_color; -varying vec4 v_position; +varying float v_inverse_depth; void writeDepthClampedToFarPlane() { @@ -13,5 +13,5 @@ void writeDepthClampedToFarPlane() void main(void) { gl_FragColor = v_color; - czm_logDepth(v_position.w); + czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/ShadowVolumeVS.glsl b/Source/Shaders/ShadowVolumeVS.glsl index a3dec21f637a..c02cf7681094 100644 --- a/Source/Shaders/ShadowVolumeVS.glsl +++ b/Source/Shaders/ShadowVolumeVS.glsl @@ -12,7 +12,7 @@ uniform float u_globeMinimumAltitude; // emulated noperspective varying float v_WindowZ; varying vec4 v_color; -varying vec4 v_position; +varying float v_inverse_depth; vec4 depthClampFarPlane(vec4 vertexInClipCoordinates) { @@ -36,5 +36,5 @@ void main() #endif gl_Position = depthClampFarPlane(czm_modelViewProjectionRelativeToEye * position); - v_position = gl_Position; + v_inverse_depth = 1. / gl_Position.w; } From 22bfd3492ef90a55ea083884b4e3c727ce48328a Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Tue, 9 Jan 2018 19:46:24 +0300 Subject: [PATCH 16/68] removed unnecessary variable --- Source/Scene/Model.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Scene/Model.js b/Source/Scene/Model.js index 5eacd3e309fa..8c2bf2c014e8 100644 --- a/Source/Scene/Model.js +++ b/Source/Scene/Model.js @@ -1913,7 +1913,6 @@ var premultipliedAlpha = hasPremultipliedAlpha(model); fs = modifyFsShaderForDepth(fs); - var blendFS = modifyShaderForColor(fs, premultipliedAlpha); vs = modifyVertexShaderForDepth(vs); var finalFS = modifyShaderForColor(fs, premultipliedAlpha); if (ClippingPlaneCollection.isSupported()) { From 417c3f9b61ba71b45ac39afae3a3c62f42b5cc7e Mon Sep 17 00:00:00 2001 From: George Vinokhodov Date: Fri, 9 Feb 2018 15:51:22 +0300 Subject: [PATCH 17/68] fixed some artifacts --- Source/Renderer/ShaderSource.js | 3 +++ Source/Scene/Model.js | 6 +++--- Source/Shaders/Builtin/Functions/logDepth.glsl | 3 --- Source/Shaders/ShadowVolumeFS.glsl | 4 ++-- Source/Shaders/ShadowVolumeVS.glsl | 7 ++++--- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index b1f4684b93df..9fe1ff4cbd6c 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -228,6 +228,9 @@ define([ precision highp float;\n\ #else\n\ precision mediump float;\n\ +#endif\n\n\ +#ifdef GL_EXT_frag_depth\n\ +#extension GL_EXT_frag_depth : enable\n\ #endif\n\n'; } diff --git a/Source/Scene/Model.js b/Source/Scene/Model.js index b2186a8a71d8..4be3d050426c 100644 --- a/Source/Scene/Model.js +++ b/Source/Scene/Model.js @@ -1853,7 +1853,7 @@ function modifyFsShaderForDepth(shader) { shader = ShaderSource.replaceMain(shader, 'czm_main'); shader += - 'varying float v_inverse_depth; \n' + + 'varying float v_inverse_depth;\n' + 'void main() \n' + '{ \n' + ' czm_main(); \n' + @@ -1866,11 +1866,11 @@ function modifyVertexShaderForDepth(shader) { shader = ShaderSource.replaceMain(shader, 'gltf_morph_main'); shader += - 'varying float v_inverse_depth; \n' + + 'varying float v_inverse_depth;\n' + 'void main() \n' + '{ \n' + ' gltf_morph_main(); \n' + - ' v_inverse_depth = 1. / gl_Position.w; \n' + + ' v_inverse_depth = 1. / ((u_projectionMatrix * u_modelViewMatrix * vec4(a_position,1.0)).w); \n' + '} \n'; return shader; diff --git a/Source/Shaders/Builtin/Functions/logDepth.glsl b/Source/Shaders/Builtin/Functions/logDepth.glsl index 0b3527458917..9c3f6487f028 100644 --- a/Source/Shaders/Builtin/Functions/logDepth.glsl +++ b/Source/Shaders/Builtin/Functions/logDepth.glsl @@ -1,6 +1,3 @@ -#ifdef GL_EXT_frag_depth -#extension GL_EXT_frag_depth : enable -#endif void czm_logDepth(float w) { #ifdef GL_EXT_frag_depth diff --git a/Source/Shaders/ShadowVolumeFS.glsl b/Source/Shaders/ShadowVolumeFS.glsl index 71fcf81bebde..b6df103f05b0 100644 --- a/Source/Shaders/ShadowVolumeFS.glsl +++ b/Source/Shaders/ShadowVolumeFS.glsl @@ -8,7 +8,7 @@ uniform vec4 u_highlightColor; varying vec4 v_color; #endif -varying float v_inverse_depth; +varying float v_depth; void main(void) { @@ -18,5 +18,5 @@ void main(void) gl_FragColor = v_color; #endif //czm_writeDepthClampedToFarPlane(); - czm_logDepth(v_inverse_depth); + czm_logDepth(1. / v_depth); } diff --git a/Source/Shaders/ShadowVolumeVS.glsl b/Source/Shaders/ShadowVolumeVS.glsl index da6775f26474..a305d810b8c2 100644 --- a/Source/Shaders/ShadowVolumeVS.glsl +++ b/Source/Shaders/ShadowVolumeVS.glsl @@ -19,7 +19,7 @@ uniform float u_globeMinimumAltitude; #ifndef VECTOR_TILE varying vec4 v_color; #endif -varying float v_inverse_depth; +varying float v_depth; void main() { @@ -37,7 +37,8 @@ void main() //extrudeDirection is zero for the top layer position = position + vec4(extrudeDirection * delta, 0.0); #endif - gl_Position = czm_depthClampFarPlane(czm_modelViewProjectionRelativeToEye * position); + vec4 positionEC = czm_modelViewProjectionRelativeToEye * position; + gl_Position = czm_depthClampFarPlane(positionEC); #endif - v_inverse_depth = 1. / gl_Position.w; + v_depth = gl_Position.w; } From f5ad7c635bd1144d9ae284a4a09af31628563a1e Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 9 Feb 2018 15:48:56 -0500 Subject: [PATCH 18/68] Use uniform semantic for model-view-projection matrix. --- Source/Scene/BillboardCollection.js | 2 -- Source/Scene/ClassificationModel.js | 25 +++++++++++-------- Source/Scene/EllipsoidPrimitive.js | 1 - Source/Scene/Model.js | 32 +++--------------------- Source/Scene/ModelUtility.js | 38 +++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 42 deletions(-) diff --git a/Source/Scene/BillboardCollection.js b/Source/Scene/BillboardCollection.js index b19e251314a2..2f5737f4558b 100644 --- a/Source/Scene/BillboardCollection.js +++ b/Source/Scene/BillboardCollection.js @@ -4,7 +4,6 @@ define([ '../Core/Cartesian2', '../Core/Cartesian3', '../Core/Color', - '../Core/combine', '../Core/ComponentDatatype', '../Core/defaultValue', '../Core/defined', @@ -40,7 +39,6 @@ define([ Cartesian2, Cartesian3, Color, - combine, ComponentDatatype, defaultValue, defined, diff --git a/Source/Scene/ClassificationModel.js b/Source/Scene/ClassificationModel.js index f5955f6bb818..76dd0de637c0 100644 --- a/Source/Scene/ClassificationModel.js +++ b/Source/Scene/ClassificationModel.js @@ -663,23 +663,25 @@ define([ } function createProgram(model) { - var positionName = ModelUtility.getAttributeOrUniformBySemantic(model.gltf, 'POSITION'); - var batchIdName = ModelUtility.getAttributeOrUniformBySemantic(model.gltf, '_BATCHID'); + var gltf = model.gltf; + + var positionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'POSITION'); + var batchIdName = ModelUtility.getAttributeOrUniformBySemantic(gltf, '_BATCHID'); var attributeLocations = {}; attributeLocations[positionName] = 0; attributeLocations[batchIdName] = 1; - var modelViewProjectionName = ModelUtility.getAttributeOrUniformBySemantic(model.gltf, 'MODELVIEWPROJECTION'); + var modelViewProjectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'MODELVIEWPROJECTION'); var uniformDecl; var computePosition; if (!defined(modelViewProjectionName)) { - var projectionName = ModelUtility.getAttributeOrUniformBySemantic(model.gltf, 'PROJECTION'); - var modelViewName = ModelUtility.getAttributeOrUniformBySemantic(model.gltf, 'MODELVIEW'); + var projectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'PROJECTION'); + var modelViewName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'MODELVIEW'); if (!defined(modelViewName)) { - modelViewName = ModelUtility.getAttributeOrUniformBySemantic(model.gltf, 'CESIUM_RTC_MODELVIEW'); + modelViewName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'CESIUM_RTC_MODELVIEW'); } uniformDecl = @@ -700,19 +702,22 @@ define([ ' gl_Position = czm_depthClampFarPlane(positionInClipCoords);\n' + '}\n'; var fs = - '#ifdef GL_EXT_frag_depth\n' + - '#extension GL_EXT_frag_depth : enable\n' + - '#endif\n' + + //'#ifdef GL_EXT_frag_depth\n' + + //'#extension GL_EXT_frag_depth : enable\n' + + //'#endif\n' + 'void main() \n' + '{ \n' + ' gl_FragColor = vec4(1.0); \n' + - ' czm_writeDepthClampedToFarPlane();\n' + + //' czm_writeDepthClampedToFarPlane();\n' + '}\n'; if (model.extensionsUsed.WEB3D_quantized_attributes) { vs = modifyShaderForQuantizedAttributes(vs, model); } + vs = ModelUtility.modifyVertexShaderForLogDepth(gltf, vs); + fs = ModelUtility.modifyFragmentShaderForLogDepth(fs); + var drawVS = modifyShader(vs, model._vertexShaderLoaded); var drawFS = modifyShader(fs, model._classificationShaderLoaded); diff --git a/Source/Scene/EllipsoidPrimitive.js b/Source/Scene/EllipsoidPrimitive.js index ae94ae608c2e..cd4ceff8f342 100644 --- a/Source/Scene/EllipsoidPrimitive.js +++ b/Source/Scene/EllipsoidPrimitive.js @@ -262,7 +262,6 @@ define([ * @exception {DeveloperError} this.material must be defined. */ EllipsoidPrimitive.prototype.update = function(frameState) { - if (!this.show || (frameState.mode !== SceneMode.SCENE3D) || (!defined(this.center)) || diff --git a/Source/Scene/Model.js b/Source/Scene/Model.js index 4be3d050426c..8d33b3dc6eef 100644 --- a/Source/Scene/Model.js +++ b/Source/Scene/Model.js @@ -1,4 +1,4 @@ - define([ +define([ '../Core/BoundingSphere', '../Core/Cartesian2', '../Core/Cartesian3', @@ -1850,32 +1850,6 @@ return shader; } - function modifyFsShaderForDepth(shader) { - shader = ShaderSource.replaceMain(shader, 'czm_main'); - shader += - 'varying float v_inverse_depth;\n' + - 'void main() \n' + - '{ \n' + - ' czm_main(); \n' + - ' czm_logDepth(v_inverse_depth); \n' + - '} \n'; - - return shader; - } - - function modifyVertexShaderForDepth(shader) { - shader = ShaderSource.replaceMain(shader, 'gltf_morph_main'); - shader += - 'varying float v_inverse_depth;\n' + - 'void main() \n' + - '{ \n' + - ' gltf_morph_main(); \n' + - ' v_inverse_depth = 1. / ((u_projectionMatrix * u_modelViewMatrix * vec4(a_position,1.0)).w); \n' + - '} \n'; - - return shader; - } - function modifyShader(shader, programName, callback) { if (defined(callback)) { shader = callback(shader, programName); @@ -1926,8 +1900,8 @@ } var premultipliedAlpha = hasPremultipliedAlpha(model); - fs = modifyFsShaderForDepth(fs); - vs = modifyVertexShaderForDepth(vs); + fs = ModelUtility.modifyFragmentShaderForLogDepth(fs); + vs = ModelUtility.modifyVertexShaderForLogDepth(model.gltf, vs); var finalFS = modifyShaderForColor(fs, premultipliedAlpha); if (ClippingPlaneCollection.isSupported()) { finalFS = modifyShaderForClippingPlanes(finalFS); diff --git a/Source/Scene/ModelUtility.js b/Source/Scene/ModelUtility.js index 671ce0e0db14..2af8d0173f67 100644 --- a/Source/Scene/ModelUtility.js +++ b/Source/Scene/ModelUtility.js @@ -277,6 +277,44 @@ define([ }; }; + ModelUtility.modifyFragmentShaderForLogDepth = function(shader) { + shader = ShaderSource.replaceMain(shader, 'czm_depth_main'); + shader += + 'varying float v_inverse_depth;\n' + + 'void main() \n' + + '{ \n' + + ' czm_depth_main(); \n' + + ' czm_logDepth(v_inverse_depth); \n' + + '} \n'; + + return shader; + }; + + ModelUtility.modifyVertexShaderForLogDepth = function(gltf, shader) { + var positionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'POSITION'); + var modelViewProjectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'MODELVIEWPROJECTION'); + if (!defined(modelViewProjectionName)) { + var projectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'PROJECTION'); + var modelViewName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'MODELVIEW'); + if (!defined(modelViewName)) { + modelViewName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'CESIUM_RTC_MODELVIEW'); + } + modelViewProjectionName = projectionName + ' * ' + modelViewName; + } + + shader = ShaderSource.replaceMain(shader, 'czm_depth_main'); + shader += + 'varying float v_inverse_depth;\n' + + 'void main() \n' + + '{ \n' + + ' czm_depth_main(); \n' + + //' v_inverse_depth = 1.0 / ((u_projectionMatrix * u_modelViewMatrix * vec4(a_position,1.0)).w); \n' + + ' v_inverse_depth = 1.0 / (' + modelViewProjectionName + ' * vec4(' + positionName + '.xyz, 1.0)).w; \n' + + '} \n'; + + return shader; + }; + function getScalarUniformFunction(value) { var that = { value : value, From 23f99c29f6ca6c24787b28e845fbc8569aca3373 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 9 Feb 2018 18:01:26 -0500 Subject: [PATCH 19/68] Use decoded position attribute for log z. --- Source/Scene/ModelUtility.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Source/Scene/ModelUtility.js b/Source/Scene/ModelUtility.js index 2af8d0173f67..41a391e2ee6e 100644 --- a/Source/Scene/ModelUtility.js +++ b/Source/Scene/ModelUtility.js @@ -292,6 +292,11 @@ define([ ModelUtility.modifyVertexShaderForLogDepth = function(gltf, shader) { var positionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'POSITION'); + var decodedPositionName = positionName.replace('a_', 'gltf_a_dec_'); + if (shader.indexOf(decodedPositionName) !== -1) { + positionName = decodedPositionName; + } + var modelViewProjectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'MODELVIEWPROJECTION'); if (!defined(modelViewProjectionName)) { var projectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'PROJECTION'); From 33cfca71256a070292b3f01ca5d090fb222e1f47 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 12 Feb 2018 13:36:19 -0500 Subject: [PATCH 20/68] Fix classification model log depth. --- Source/Scene/ClassificationModel.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Source/Scene/ClassificationModel.js b/Source/Scene/ClassificationModel.js index 76dd0de637c0..484a19415ba5 100644 --- a/Source/Scene/ClassificationModel.js +++ b/Source/Scene/ClassificationModel.js @@ -715,12 +715,12 @@ define([ vs = modifyShaderForQuantizedAttributes(vs, model); } - vs = ModelUtility.modifyVertexShaderForLogDepth(gltf, vs); - fs = ModelUtility.modifyFragmentShaderForLogDepth(fs); - var drawVS = modifyShader(vs, model._vertexShaderLoaded); var drawFS = modifyShader(fs, model._classificationShaderLoaded); + drawVS = ModelUtility.modifyVertexShaderForLogDepth(gltf, drawVS); + drawFS = ModelUtility.modifyFragmentShaderForLogDepth(drawFS); + model._shaderProgram = { vertexShaderSource : drawVS, fragmentShaderSource : drawFS, @@ -731,6 +731,9 @@ define([ var pickVS = modifyShader(vs, model._pickVertexShaderLoaded); var pickFS = modifyShader(fs, model._pickFragmentShaderLoaded); + pickVS = ModelUtility.modifyVertexShaderForLogDepth(gltf, pickVS); + pickFS = ModelUtility.modifyFragmentShaderForLogDepth(pickFS); + model._pickShaderProgram = { vertexShaderSource : pickVS, fragmentShaderSource : pickFS, From caf032a59070a8abc499ad3d600dc6f6cccd55a6 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 12 Feb 2018 16:23:00 -0500 Subject: [PATCH 21/68] Write globe log depth on pick. --- Source/Scene/GlobeSurfaceShaderSet.js | 2 ++ Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl | 4 ++-- Source/Shaders/ShadowVolumeFS.glsl | 2 +- Source/Shaders/ShadowVolumeVS.glsl | 3 +-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Source/Scene/GlobeSurfaceShaderSet.js b/Source/Scene/GlobeSurfaceShaderSet.js index 149215ea2096..a6f5290a49d1 100644 --- a/Source/Scene/GlobeSurfaceShaderSet.js +++ b/Source/Scene/GlobeSurfaceShaderSet.js @@ -245,9 +245,11 @@ define([ // pass through fragment shader. only depth is rendered for the globe on a pick pass var fs = + 'varying float v_inverse_depth;\n' + 'void main()\n' + '{\n' + ' gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n' + + ' czm_logDepth(v_inverse_depth);\n' + '}\n'; pickShader = this._pickShaderPrograms[flags] = ShaderProgram.fromCache({ diff --git a/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl index 48b8db4416a2..6303472f2aaa 100644 --- a/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl @@ -11,7 +11,7 @@ varying vec3 v_normalEC; varying vec3 v_tangentEC; varying vec3 v_bitangentEC; varying vec2 v_st; -v_inverse_depth = -1. / v_positionEC.z; +varying float v_inverse_depth; void main() { @@ -22,7 +22,7 @@ void main() v_tangentEC = czm_normal * tangent; // tangent in eye coordinates v_bitangentEC = czm_normal * bitangent; // bitangent in eye coordinates v_st = st; - v_inverse_depth = -1. / v_positionEC.z; + v_inverse_depth = -1.0 / v_positionEC.z; gl_Position = czm_modelViewProjectionRelativeToEye * p; } diff --git a/Source/Shaders/ShadowVolumeFS.glsl b/Source/Shaders/ShadowVolumeFS.glsl index b6df103f05b0..49133e99af11 100644 --- a/Source/Shaders/ShadowVolumeFS.glsl +++ b/Source/Shaders/ShadowVolumeFS.glsl @@ -18,5 +18,5 @@ void main(void) gl_FragColor = v_color; #endif //czm_writeDepthClampedToFarPlane(); - czm_logDepth(1. / v_depth); + czm_logDepth(1.0 / v_depth); } diff --git a/Source/Shaders/ShadowVolumeVS.glsl b/Source/Shaders/ShadowVolumeVS.glsl index a305d810b8c2..bf795fdd1abf 100644 --- a/Source/Shaders/ShadowVolumeVS.glsl +++ b/Source/Shaders/ShadowVolumeVS.glsl @@ -37,8 +37,7 @@ void main() //extrudeDirection is zero for the top layer position = position + vec4(extrudeDirection * delta, 0.0); #endif - vec4 positionEC = czm_modelViewProjectionRelativeToEye * position; - gl_Position = czm_depthClampFarPlane(positionEC); + gl_Position = czm_depthClampFarPlane(czm_modelViewProjectionRelativeToEye * position); #endif v_depth = gl_Position.w; } From 8d4951956ab9d98d84506be9695027fc405f2b4d Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 13 Feb 2018 16:06:21 -0500 Subject: [PATCH 22/68] Fix issue with the BIM tileset. --- Source/Scene/Model.js | 8 ++++++-- Source/Scene/ModelUtility.js | 15 +++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Source/Scene/Model.js b/Source/Scene/Model.js index 8d33b3dc6eef..5a117e19c976 100644 --- a/Source/Scene/Model.js +++ b/Source/Scene/Model.js @@ -1900,8 +1900,6 @@ define([ } var premultipliedAlpha = hasPremultipliedAlpha(model); - fs = ModelUtility.modifyFragmentShaderForLogDepth(fs); - vs = ModelUtility.modifyVertexShaderForLogDepth(model.gltf, vs); var finalFS = modifyShaderForColor(fs, premultipliedAlpha); if (ClippingPlaneCollection.isSupported()) { finalFS = modifyShaderForClippingPlanes(finalFS); @@ -1910,6 +1908,9 @@ define([ var drawVS = modifyShader(vs, id, model._vertexShaderLoaded); var drawFS = modifyShader(finalFS, id, model._fragmentShaderLoaded); + drawVS = ModelUtility.modifyVertexShaderForLogDepth(model.gltf, drawVS); + drawFS = ModelUtility.modifyFragmentShaderForLogDepth(drawFS); + model._rendererResources.programs[id] = ShaderProgram.fromCache({ context : context, vertexShaderSource : drawVS, @@ -1926,6 +1927,9 @@ define([ pickFS = ShaderSource.createPickFragmentShaderSource(fs, 'uniform'); } + pickVS = ModelUtility.modifyVertexShaderForLogDepth(model.gltf, pickVS); + pickFS = ModelUtility.modifyFragmentShaderForLogDepth(pickFS); + model._rendererResources.pickPrograms[id] = ShaderProgram.fromCache({ context : context, vertexShaderSource : pickVS, diff --git a/Source/Scene/ModelUtility.js b/Source/Scene/ModelUtility.js index 41a391e2ee6e..3e5cf536e43c 100644 --- a/Source/Scene/ModelUtility.js +++ b/Source/Scene/ModelUtility.js @@ -280,11 +280,12 @@ define([ ModelUtility.modifyFragmentShaderForLogDepth = function(shader) { shader = ShaderSource.replaceMain(shader, 'czm_depth_main'); shader += - 'varying float v_inverse_depth;\n' + + '\n' + + 'varying float v_depth; \n' + 'void main() \n' + '{ \n' + ' czm_depth_main(); \n' + - ' czm_logDepth(v_inverse_depth); \n' + + ' czm_logDepth(1.0 / v_depth); \n' + '} \n'; return shader; @@ -301,7 +302,9 @@ define([ if (!defined(modelViewProjectionName)) { var projectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'PROJECTION'); var modelViewName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'MODELVIEW'); - if (!defined(modelViewName)) { + if (shader.indexOf('czm_instanced_modelView ') !== -1) { + modelViewName = 'czm_instanced_modelView'; + } else if (!defined(modelViewName)) { modelViewName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'CESIUM_RTC_MODELVIEW'); } modelViewProjectionName = projectionName + ' * ' + modelViewName; @@ -309,12 +312,12 @@ define([ shader = ShaderSource.replaceMain(shader, 'czm_depth_main'); shader += - 'varying float v_inverse_depth;\n' + + '\n' + + 'varying float v_depth;\n' + 'void main() \n' + '{ \n' + ' czm_depth_main(); \n' + - //' v_inverse_depth = 1.0 / ((u_projectionMatrix * u_modelViewMatrix * vec4(a_position,1.0)).w); \n' + - ' v_inverse_depth = 1.0 / (' + modelViewProjectionName + ' * vec4(' + positionName + '.xyz, 1.0)).w; \n' + + ' v_depth = (' + modelViewProjectionName + ' * vec4(' + positionName + '.xyz, 1.0)).w; \n' + '} \n'; return shader; From b6db3814442e2338b3daf55991584c9077790d51 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 13 Feb 2018 16:59:46 -0500 Subject: [PATCH 23/68] Fix depth for polylines in a polyline collection. --- Source/Scene/Scene.js | 3 +-- .../Shaders/Appearances/PerInstanceColorAppearanceVS.glsl | 6 +++--- .../Appearances/PerInstanceFlatColorAppearanceVS.glsl | 6 +++--- Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl | 2 +- .../Shaders/Appearances/PolylineMaterialAppearanceVS.glsl | 6 +++--- .../Shaders/Appearances/TexturedMaterialAppearanceVS.glsl | 6 +++--- Source/Shaders/BillboardCollectionVS.glsl | 2 +- Source/Shaders/DepthPlaneFS.glsl | 4 ++-- Source/Shaders/GlobeVS.glsl | 2 +- Source/Shaders/PolylineVS.glsl | 2 +- Source/Shaders/ShadowVolumeVS.glsl | 2 +- 11 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 3ea111d877cc..6e71a9516d04 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -1745,8 +1745,7 @@ define([ farChange = far / frustumCommandsList[numberOfFrustums - 1].far; } if ((near !== Number.MAX_VALUE && (numFrustums !== numberOfFrustums || (frustumCommandsList.length !== 0 && - ((scene.logDepthBuffer && - (isFinite(farChange) && !CesiumMath.equalsEpsilon(1, farChange, CesiumMath.EPSILON8))) || + ((scene.logDepthBuffer && isFinite(farChange) && !CesiumMath.equalsEpsilon(1, farChange, CesiumMath.EPSILON8)) || (near < frustumCommandsList[0].near || (far > frustumCommandsList[numberOfFrustums - 1].far && !CesiumMath.equalsEpsilon(far, frustumCommandsList[numberOfFrustums - 1].far, CesiumMath.EPSILON8)))))))) { updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, scene.nearToFarDistance2D); createPotentiallyVisibleSet(scene); diff --git a/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl b/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl index e54c2648f2dc..777a36080e6f 100644 --- a/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl @@ -9,14 +9,14 @@ varying vec3 v_normalEC; varying vec4 v_color; varying float v_inverse_depth; -void main() +void main() { vec4 p = czm_computePosition(); v_positionEC = (czm_modelViewRelativeToEye * p).xyz; // position in eye coordinates v_normalEC = czm_normal * normal; // normal in eye coordinates v_color = color; - + gl_Position = czm_modelViewProjectionRelativeToEye * p; - v_inverse_depth = 1. / gl_Position.w; + v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl index 32332459a445..f840c3a02cf7 100644 --- a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl @@ -6,12 +6,12 @@ attribute float batchId; varying vec4 v_color; varying float v_inverse_depth; -void main() +void main() { vec4 p = czm_computePosition(); v_color = color; - + gl_Position = czm_modelViewProjectionRelativeToEye * p; - v_inverse_depth = 1. / gl_Position.w; + v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl index 16a28d53975a..c85213647c18 100644 --- a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl @@ -26,5 +26,5 @@ void main() float angle; vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, angle); gl_Position = czm_viewportOrthographic * positionWC; - v_inverse_depth = -1. / (czm_modelViewRelativeToEye * p).z; + v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; } diff --git a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl index 358ebd7e69d9..448e484afe94 100644 --- a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl @@ -9,9 +9,9 @@ attribute vec2 st; attribute float batchId; varying float v_width; -varying vec2 v_st; +varying vec2 v_st; varying float v_polylineAngle; -varying float v_inverse_depth; +varying float v_inverse_depth; void main() { @@ -28,5 +28,5 @@ void main() vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, v_polylineAngle); gl_Position = czm_viewportOrthographic * positionWC; - v_inverse_depth = -1. / (czm_modelViewRelativeToEye * p).z; + v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; } diff --git a/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl index e9243376a0fa..3673351ba78a 100644 --- a/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl @@ -9,14 +9,14 @@ varying vec3 v_normalEC; varying vec2 v_st; varying float v_inverse_depth; -void main() +void main() { vec4 p = czm_computePosition(); v_positionEC = (czm_modelViewRelativeToEye * p).xyz; // position in eye coordinates v_normalEC = czm_normal * normal; // normal in eye coordinates - v_inverse_depth = -1. / v_positionEC.z; v_st = st; - + gl_Position = czm_modelViewProjectionRelativeToEye * p; + v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/BillboardCollectionVS.glsl b/Source/Shaders/BillboardCollectionVS.glsl index 4d0176f1b084..e6bce42c8828 100644 --- a/Source/Shaders/BillboardCollectionVS.glsl +++ b/Source/Shaders/BillboardCollectionVS.glsl @@ -258,7 +258,7 @@ void main() vec4 positionWC = computePositionWindowCoordinates(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters); gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); - v_inverse_depth = -1. / positionEC.z; + v_inverse_depth = -1.0 / positionEC.z; v_textureCoordinates = textureCoordinates; #ifdef DISABLE_DEPTH_DISTANCE diff --git a/Source/Shaders/DepthPlaneFS.glsl b/Source/Shaders/DepthPlaneFS.glsl index 64da577930e2..0ea5f88f9c1d 100644 --- a/Source/Shaders/DepthPlaneFS.glsl +++ b/Source/Shaders/DepthPlaneFS.glsl @@ -4,11 +4,11 @@ void main() { // TODO: make arbitrary ellipsoid czm_ellipsoid ellipsoid = czm_getWgs84EllipsoidEC(); - + vec3 direction = normalize(positionEC.xyz); czm_ray ray = czm_ray(vec3(0.0), direction); - czm_logDepth(-1. / positionEC.z); + czm_logDepth(-1.0 / positionEC.z); czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid); if (!czm_isEmpty(intersection)) diff --git a/Source/Shaders/GlobeVS.glsl b/Source/Shaders/GlobeVS.glsl index 8aec6ee6c283..d8fae1bfe816 100644 --- a/Source/Shaders/GlobeVS.glsl +++ b/Source/Shaders/GlobeVS.glsl @@ -157,7 +157,7 @@ void main() v_textureCoordinates = vec3(textureCoordinates, webMercatorT); - v_inverse_depth = 1. / gl_Position.w; + v_inverse_depth = 1.0 / gl_Position.w; #if defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL) || defined(APPLY_MATERIAL) v_positionEC = (u_modifiedModelView * vec4(position, 1.0)).xyz; v_positionMC = position3DWC; // position in model coordinates diff --git a/Source/Shaders/PolylineVS.glsl b/Source/Shaders/PolylineVS.glsl index 4015531d8c92..9ee70b977c3f 100644 --- a/Source/Shaders/PolylineVS.glsl +++ b/Source/Shaders/PolylineVS.glsl @@ -93,7 +93,7 @@ void main() vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, v_polylineAngle); gl_Position = czm_viewportOrthographic * positionWC * show; - v_inverse_depth = 1. / (czm_modelViewRelativeToEye * p).z; + v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; v_st = vec2(texCoord, clamp(expandDir, 0.0, 1.0)); v_width = width; diff --git a/Source/Shaders/ShadowVolumeVS.glsl b/Source/Shaders/ShadowVolumeVS.glsl index bf795fdd1abf..c731f502454e 100644 --- a/Source/Shaders/ShadowVolumeVS.glsl +++ b/Source/Shaders/ShadowVolumeVS.glsl @@ -39,5 +39,5 @@ void main() #endif gl_Position = czm_depthClampFarPlane(czm_modelViewProjectionRelativeToEye * position); #endif - v_depth = gl_Position.w; + v_depth = gl_Position.w; } From c94e5ad61b4b34a0c56621ae1f95447db11e58d2 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 13 Feb 2018 17:26:36 -0500 Subject: [PATCH 24/68] Update moon. --- Source/Shaders/EllipsoidFS.glsl | 24 +++++++++++++++--------- Source/Shaders/EllipsoidVS.glsl | 8 +++----- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Source/Shaders/EllipsoidFS.glsl b/Source/Shaders/EllipsoidFS.glsl index acfddb472c02..81e94ecae151 100644 --- a/Source/Shaders/EllipsoidFS.glsl +++ b/Source/Shaders/EllipsoidFS.glsl @@ -2,7 +2,6 @@ uniform vec3 u_radii; uniform vec3 u_oneOverEllipsoidRadiiSquared; varying vec3 v_positionEC; -varying float v_inverse_depth; vec4 computeEllipsoidColor(czm_ray ray, float intersection, float side) { @@ -37,17 +36,17 @@ void main() // PERFORMANCE_TODO: When dynamic branching is available, compute ratio of maximum and minimum radii // in the vertex shader. Only when it is larger than some constant, march along the ray. // Otherwise perform one intersection test which will be the common case. - + // Test if the ray intersects a sphere with the ellipsoid's maximum radius. // For very oblate ellipsoids, using the ellipsoid's radii for an intersection test // may cause false negatives. This will discard fragments before marching the ray forward. float maxRadius = max(u_radii.x, max(u_radii.y, u_radii.z)) * 1.5; vec3 direction = normalize(v_positionEC); vec3 ellipsoidCenter = czm_modelView[3].xyz; - + float t1 = -1.0; float t2 = -1.0; - + float b = -2.0 * dot(direction, ellipsoidCenter); float c = dot(ellipsoidCenter, ellipsoidCenter) - maxRadius * maxRadius; @@ -56,22 +55,22 @@ void main() t1 = (-b - sqrt(discriminant)) * 0.5; t2 = (-b + sqrt(discriminant)) * 0.5; } - + if (t1 < 0.0 && t2 < 0.0) { discard; } - + float t = min(t1, t2); if (t < 0.0) { t = 0.0; } - + // March ray forward to intersection with larger sphere and find // actual intersection point with ellipsoid. czm_ellipsoid ellipsoid = czm_ellipsoidNew(ellipsoidCenter, u_radii); czm_ray ray = czm_ray(t * direction, direction); czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid); - + if (czm_isEmpty(intersection)) { discard; @@ -86,5 +85,12 @@ void main() gl_FragColor = mix(insideFaceColor, outsideFaceColor, outsideFaceColor.a); gl_FragColor.a = 1.0 - (1.0 - insideFaceColor.a) * (1.0 - outsideFaceColor.a); - czm_logDepth(v_inverse_depth); +#ifdef WRITE_DEPTH +#ifdef GL_EXT_frag_depth + t = (intersection.start != 0.0) ? intersection.start : intersection.stop; + vec3 positionEC = czm_pointAlongRay(ray, t); + vec4 positionCC = czm_projection * vec4(positionEC, 1.0); + czm_logDepth(1.0 / positionCC.w); +#endif +#endif } diff --git a/Source/Shaders/EllipsoidVS.glsl b/Source/Shaders/EllipsoidVS.glsl index 9c6252dd4221..98c1269cd66a 100644 --- a/Source/Shaders/EllipsoidVS.glsl +++ b/Source/Shaders/EllipsoidVS.glsl @@ -3,9 +3,8 @@ attribute vec3 position; uniform vec3 u_radii; varying vec3 v_positionEC; -varying float v_inverse_depth; -void main() +void main() { // In the vertex data, the cube goes from (-1.0, -1.0, -1.0) to (1.0, 1.0, 1.0) in model coordinates. // Scale to consider the radii. We could also do this once on the CPU when using the BoxGeometry, @@ -15,10 +14,9 @@ void main() v_positionEC = (czm_modelView * p).xyz; // position in eye coordinates gl_Position = czm_modelViewProjection * p; // position in clip coordinates - v_inverse_depth = 1. / v_positionEC.z; - // With multi-frustum, when the ellipsoid primitive is positioned on the intersection of two frustums - // and close to terrain, the terrain (writes depth) in the closest frustum can overwrite part of the + // With multi-frustum, when the ellipsoid primitive is positioned on the intersection of two frustums + // and close to terrain, the terrain (writes depth) in the closest frustum can overwrite part of the // ellipsoid (does not write depth) that was rendered in the farther frustum. // // Here, we clamp the depth in the vertex shader to avoid being overwritten; however, this creates From 7d716e984fa7f7ebd2547593816344f063d31de4 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 15 Feb 2018 16:37:16 -0500 Subject: [PATCH 25/68] Add missing log depth to appearances, point primitives and vector tile polylines. --- .../Appearances/AllMaterialAppearanceFS.glsl | 3 +++ .../Appearances/AllMaterialAppearanceVS.glsl | 2 +- .../Appearances/BasicMaterialAppearanceFS.glsl | 9 ++++++--- .../Appearances/BasicMaterialAppearanceVS.glsl | 6 ++++-- .../EllipsoidSurfaceAppearanceFS.glsl | 17 ++++++++++------- .../EllipsoidSurfaceAppearanceVS.glsl | 6 ++++-- Source/Shaders/PointPrimitiveCollectionFS.glsl | 2 ++ Source/Shaders/PointPrimitiveCollectionVS.glsl | 3 +++ Source/Shaders/Vector3DTilePolylinesVS.glsl | 3 +++ 9 files changed, 36 insertions(+), 15 deletions(-) diff --git a/Source/Shaders/Appearances/AllMaterialAppearanceFS.glsl b/Source/Shaders/Appearances/AllMaterialAppearanceFS.glsl index 672750f1f023..745f0fd43853 100644 --- a/Source/Shaders/Appearances/AllMaterialAppearanceFS.glsl +++ b/Source/Shaders/Appearances/AllMaterialAppearanceFS.glsl @@ -3,6 +3,7 @@ varying vec3 v_normalEC; varying vec3 v_tangentEC; varying vec3 v_bitangentEC; varying vec2 v_st; +varying float v_inverse_depth; void main() { @@ -26,4 +27,6 @@ void main() #else gl_FragColor = czm_phong(normalize(positionToEyeEC), material); #endif + + czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl index 6303472f2aaa..6cd5de782fcf 100644 --- a/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl @@ -22,7 +22,7 @@ void main() v_tangentEC = czm_normal * tangent; // tangent in eye coordinates v_bitangentEC = czm_normal * bitangent; // bitangent in eye coordinates v_st = st; - v_inverse_depth = -1.0 / v_positionEC.z; gl_Position = czm_modelViewProjectionRelativeToEye * p; + v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/Appearances/BasicMaterialAppearanceFS.glsl b/Source/Shaders/Appearances/BasicMaterialAppearanceFS.glsl index 95be0556c016..b26987f45854 100644 --- a/Source/Shaders/Appearances/BasicMaterialAppearanceFS.glsl +++ b/Source/Shaders/Appearances/BasicMaterialAppearanceFS.glsl @@ -1,9 +1,10 @@ varying vec3 v_positionEC; varying vec3 v_normalEC; +varying float v_inverse_depth; void main() { - vec3 positionToEyeEC = -v_positionEC; + vec3 positionToEyeEC = -v_positionEC; vec3 normalEC = normalize(v_normalEC); #ifdef FACE_FORWARD @@ -14,10 +15,12 @@ void main() materialInput.normalEC = normalEC; materialInput.positionToEyeEC = positionToEyeEC; czm_material material = czm_getMaterial(materialInput); - -#ifdef FLAT + +#ifdef FLAT gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); #else gl_FragColor = czm_phong(normalize(positionToEyeEC), material); #endif + + czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/BasicMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/BasicMaterialAppearanceVS.glsl index 0d496a877e7c..ea184f96fab0 100644 --- a/Source/Shaders/Appearances/BasicMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/BasicMaterialAppearanceVS.glsl @@ -5,13 +5,15 @@ attribute float batchId; varying vec3 v_positionEC; varying vec3 v_normalEC; +varying float v_inverse_depth; -void main() +void main() { vec4 p = czm_computePosition(); v_positionEC = (czm_modelViewRelativeToEye * p).xyz; // position in eye coordinates v_normalEC = czm_normal * normal; // normal in eye coordinates - + gl_Position = czm_modelViewProjectionRelativeToEye * p; + v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceFS.glsl b/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceFS.glsl index 7b1054b4acc9..19db3fbf7a99 100644 --- a/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceFS.glsl +++ b/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceFS.glsl @@ -1,33 +1,36 @@ varying vec3 v_positionMC; varying vec3 v_positionEC; varying vec2 v_st; +varying float v_inverse_depth; void main() { czm_materialInput materialInput; - + vec3 normalEC = normalize(czm_normal3D * czm_geodeticSurfaceNormal(v_positionMC, vec3(0.0), vec3(1.0))); #ifdef FACE_FORWARD normalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC); #endif - + materialInput.s = v_st.s; materialInput.st = v_st; materialInput.str = vec3(v_st, 0.0); - + // Convert tangent space material normal to eye space materialInput.normalEC = normalEC; materialInput.tangentToEyeMatrix = czm_eastNorthUpToEyeCoordinates(v_positionMC, materialInput.normalEC); - + // Convert view vector to world space - vec3 positionToEyeEC = -v_positionEC; + vec3 positionToEyeEC = -v_positionEC; materialInput.positionToEyeEC = positionToEyeEC; czm_material material = czm_getMaterial(materialInput); - -#ifdef FLAT + +#ifdef FLAT gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); #else gl_FragColor = czm_phong(normalize(positionToEyeEC), material); #endif + + czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceVS.glsl b/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceVS.glsl index 1bd13c9a5382..98d73454ce9d 100644 --- a/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceVS.glsl +++ b/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceVS.glsl @@ -6,14 +6,16 @@ attribute float batchId; varying vec3 v_positionMC; varying vec3 v_positionEC; varying vec2 v_st; +varying float v_inverse_depth; -void main() +void main() { vec4 p = czm_computePosition(); v_positionMC = position3DHigh + position3DLow; // position in model coordinates v_positionEC = (czm_modelViewRelativeToEye * p).xyz; // position in eye coordinates v_st = st; - + gl_Position = czm_modelViewProjectionRelativeToEye * p; + v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/PointPrimitiveCollectionFS.glsl b/Source/Shaders/PointPrimitiveCollectionFS.glsl index 42b5703fe74d..7cb1e9c997fd 100644 --- a/Source/Shaders/PointPrimitiveCollectionFS.glsl +++ b/Source/Shaders/PointPrimitiveCollectionFS.glsl @@ -2,6 +2,7 @@ varying vec4 v_color; varying vec4 v_outlineColor; varying float v_innerPercent; varying float v_pixelDistance; +varying float v_inverse_depth; #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; @@ -46,4 +47,5 @@ void main() #else gl_FragColor = color; #endif + czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/PointPrimitiveCollectionVS.glsl b/Source/Shaders/PointPrimitiveCollectionVS.glsl index 05c576606b8d..abf8f86761d3 100644 --- a/Source/Shaders/PointPrimitiveCollectionVS.glsl +++ b/Source/Shaders/PointPrimitiveCollectionVS.glsl @@ -11,6 +11,7 @@ varying vec4 v_color; varying vec4 v_outlineColor; varying float v_innerPercent; varying float v_pixelDistance; +varying float v_inverse_depth; #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; @@ -180,6 +181,8 @@ void main() v_pixelDistance = 2.0 / totalSize; gl_PointSize = totalSize; + v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; + #ifdef RENDER_FOR_PICK v_pickColor = pickColor; #endif diff --git a/Source/Shaders/Vector3DTilePolylinesVS.glsl b/Source/Shaders/Vector3DTilePolylinesVS.glsl index e30c2559ee63..832ab7d80428 100644 --- a/Source/Shaders/Vector3DTilePolylinesVS.glsl +++ b/Source/Shaders/Vector3DTilePolylinesVS.glsl @@ -6,6 +6,8 @@ attribute float a_batchId; uniform mat4 u_modifiedModelView; +varying float v_inverse_depth; + void main() { float expandDir = expandAndWidth.x; @@ -19,4 +21,5 @@ void main() float angle; vec4 positionWC = getPolylineWindowCoordinatesEC(p, prev, next, expandDir, width, usePrev, angle); gl_Position = czm_viewportOrthographic * positionWC; + v_inverse_depth = 1.0 / (czm_projection * u_modifiedModelView * currentPosition).w; } From 567a110033a39ff8b6717a5e5982f03d27e57664 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 15 Feb 2018 17:01:20 -0500 Subject: [PATCH 26/68] Fix disable depth test distance. --- Source/Shaders/BillboardCollectionVS.glsl | 3 ++- Source/Shaders/PointPrimitiveCollectionVS.glsl | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/Shaders/BillboardCollectionVS.glsl b/Source/Shaders/BillboardCollectionVS.glsl index e6bce42c8828..80a2ca42f2f8 100644 --- a/Source/Shaders/BillboardCollectionVS.glsl +++ b/Source/Shaders/BillboardCollectionVS.glsl @@ -258,7 +258,7 @@ void main() vec4 positionWC = computePositionWindowCoordinates(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters); gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); - v_inverse_depth = -1.0 / positionEC.z; + v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; v_textureCoordinates = textureCoordinates; #ifdef DISABLE_DEPTH_DISTANCE @@ -277,6 +277,7 @@ void main() { // Position z on the near plane. gl_Position.z = -gl_Position.w; + v_inverse_depth = 1.0 / czm_currentFrustum.x; } } #endif diff --git a/Source/Shaders/PointPrimitiveCollectionVS.glsl b/Source/Shaders/PointPrimitiveCollectionVS.glsl index abf8f86761d3..a8f4de6f371d 100644 --- a/Source/Shaders/PointPrimitiveCollectionVS.glsl +++ b/Source/Shaders/PointPrimitiveCollectionVS.glsl @@ -151,6 +151,7 @@ void main() vec4 positionWC = czm_eyeToWindowCoordinates(positionEC); gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); + v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; #ifdef DISABLE_DEPTH_DISTANCE float disableDepthTestDistance = distanceDisplayConditionAndDisableDepth.z; @@ -168,6 +169,7 @@ void main() { // Position z on the near plane. gl_Position.z = -gl_Position.w; + v_inverse_depth = 1.0 / czm_currentFrustum.x; } } #endif @@ -181,8 +183,6 @@ void main() v_pixelDistance = 2.0 / totalSize; gl_PointSize = totalSize; - v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; - #ifdef RENDER_FOR_PICK v_pickColor = pickColor; #endif From 7c46e11acfcca8f52af1de2d55f63ce46cf615cd Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 16 Feb 2018 14:17:06 -0500 Subject: [PATCH 27/68] Minor clean up. --- Source/Scene/ClassificationPrimitive.js | 14 ++++++-------- Source/Scene/GlobeSurfaceShaderSet.js | 3 --- Source/Scene/PointCloud3DTileContent.js | 2 +- Source/Scene/Scene.js | 19 +++++++------------ Source/Scene/SceneTransforms.js | 5 ++--- 5 files changed, 16 insertions(+), 27 deletions(-) diff --git a/Source/Scene/ClassificationPrimitive.js b/Source/Scene/ClassificationPrimitive.js index 8ef08a09ffba..c4ec4affb04b 100644 --- a/Source/Scene/ClassificationPrimitive.js +++ b/Source/Scene/ClassificationPrimitive.js @@ -1,6 +1,5 @@ define([ '../Core/ColorGeometryInstanceAttribute', - '../Core/combine', '../Core/defaultValue', '../Core/defined', '../Core/defineProperties', @@ -26,7 +25,6 @@ define([ './StencilOperation' ], function( ColorGeometryInstanceAttribute, - combine, defaultValue, defined, defineProperties, @@ -594,7 +592,7 @@ define([ }); } - function createColorCommands(classificationPrimitive, colorCommands, frameState) { + function createColorCommands(classificationPrimitive, colorCommands) { var primitive = classificationPrimitive._primitive; var length = primitive._va.length * 3; colorCommands.length = length; @@ -669,7 +667,7 @@ define([ } } - function createPickCommands(classificationPrimitive, pickCommands, frameState) { + function createPickCommands(classificationPrimitive, pickCommands) { var primitive = classificationPrimitive._primitive; var pickOffsets = primitive._pickOffsets; var length = pickOffsets.length * 3; @@ -737,9 +735,9 @@ define([ } } - function createCommands(classificationPrimitive, appearance, material, translucent, twoPasses, colorCommands, pickCommands, frameState) { - createColorCommands(classificationPrimitive, colorCommands, frameState); - createPickCommands(classificationPrimitive, pickCommands, frameState); + function createCommands(classificationPrimitive, appearance, material, translucent, twoPasses, colorCommands, pickCommands) { + createColorCommands(classificationPrimitive, colorCommands); + createPickCommands(classificationPrimitive, pickCommands); } function boundingVolumeIndex(commandIndex, length) { @@ -892,7 +890,7 @@ define([ createShaderProgram(that, frameState); }; primitiveOptions._createCommandsFunction = function(primitive, appearance, material, translucent, twoPasses, colorCommands, pickCommands) { - createCommands(that, undefined, undefined, true, false, colorCommands, pickCommands, frameState); + createCommands(that, undefined, undefined, true, false, colorCommands, pickCommands); }; if (defined(this._updateAndQueueCommandsFunction)) { diff --git a/Source/Scene/GlobeSurfaceShaderSet.js b/Source/Scene/GlobeSurfaceShaderSet.js index a6f5290a49d1..41df7c8e554e 100644 --- a/Source/Scene/GlobeSurfaceShaderSet.js +++ b/Source/Scene/GlobeSurfaceShaderSet.js @@ -117,9 +117,6 @@ define([ vs.defines.push(quantizationDefine); fs.defines.push('TEXTURE_UNITS ' + numberOfDayTextures); - if (frameState.logDepthBuffer) { - fs.defines.push('LOG_DEPTH_BUFFER'); - } if (applyBrightness) { fs.defines.push('APPLY_BRIGHTNESS'); } diff --git a/Source/Scene/PointCloud3DTileContent.js b/Source/Scene/PointCloud3DTileContent.js index bd0b46144e59..f312745e6909 100644 --- a/Source/Scene/PointCloud3DTileContent.js +++ b/Source/Scene/PointCloud3DTileContent.js @@ -1101,7 +1101,7 @@ define([ vs += ' v_color = color; \n' + ' gl_Position = czm_modelViewProjection * vec4(position, 1.0); \n' + - ' v_inverse_depth = 1. / gl_Position.w; \n'; + ' v_inverse_depth = 1.0 / gl_Position.w; \n'; if (hasNormals && backFaceCulling) { vs += ' float visible = step(-normal.z, 0.0); \n' + diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 6e71a9516d04..8cef52d6c191 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -239,7 +239,6 @@ define([ */ function Scene(options) { options = defaultValue(options, defaultValue.EMPTY_OBJECT); - var canvas = options.canvas; var contextOptions = options.contextOptions; var creditContainer = options.creditContainer; @@ -262,20 +261,16 @@ define([ creditContainer.style['padding-right'] = '5px'; canvas.parentNode.appendChild(creditContainer); } - if (context.fragmentDepth) { - this.logDepthBuffer = true; - } else { - this.logDepthBuffer = false; - } if (!defined(creditViewport)) { creditViewport = canvas.parentNode; } + this._logDepthBuffer = context.fragmentDepth; + this._id = createGuid(); this._jobScheduler = new JobScheduler(); this._frameState = new FrameState(context, new CreditDisplay(creditContainer, ' • ', creditViewport), this._jobScheduler); this._frameState.scene3DOnly = defaultValue(options.scene3DOnly, false); - this._frameState.logDepthBuffer = this.logDepthBuffer; this._removeCreditContainer = !hasCreditContainer; this._creditContainer = creditContainer; @@ -773,7 +768,7 @@ define([ var far = camera.frustum.far; var numFrustums; var farToNearRatio; - if (this.logDepthBuffer) { + if (this._logDepthBuffer) { numFrustums = 1; farToNearRatio = far / near; } else { @@ -1719,13 +1714,13 @@ 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 logDepth = scene._logDepthBuffer && !(camera.frustum instanceof OrthographicFrustum || camera.frustum instanceof OrthographicOffCenterFrustum); var farToNearRatio = scene.farToNearRatio; var numFrustums; if (!is2D) { // The multifrustum for 3D/CV is non-uniformly distributed. - if (scene.logDepthBuffer) { + if (logDepth) { numFrustums = 1; farToNearRatio = far / near; } else { @@ -1733,7 +1728,7 @@ define([ } } else { // 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 + // 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); @@ -1745,7 +1740,7 @@ define([ farChange = far / frustumCommandsList[numberOfFrustums - 1].far; } if ((near !== Number.MAX_VALUE && (numFrustums !== numberOfFrustums || (frustumCommandsList.length !== 0 && - ((scene.logDepthBuffer && isFinite(farChange) && !CesiumMath.equalsEpsilon(1, farChange, CesiumMath.EPSILON8)) || + ((logDepth && isFinite(farChange) && !CesiumMath.equalsEpsilon(1, farChange, CesiumMath.EPSILON8)) || (near < frustumCommandsList[0].near || (far > frustumCommandsList[numberOfFrustums - 1].far && !CesiumMath.equalsEpsilon(far, frustumCommandsList[numberOfFrustums - 1].far, CesiumMath.EPSILON8)))))))) { updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, scene.nearToFarDistance2D); createPotentiallyVisibleSet(scene); diff --git a/Source/Scene/SceneTransforms.js b/Source/Scene/SceneTransforms.js index 9191f5a84b40..cd6331aa1034 100644 --- a/Source/Scene/SceneTransforms.js +++ b/Source/Scene/SceneTransforms.js @@ -308,7 +308,7 @@ define([ var ndc = Cartesian4.clone(Cartesian4.UNIT_W, scratchNDC); ndc.x = (drawingBufferPosition.x - viewport.x) / viewport.width * 2.0 - 1.0; ndc.y = (drawingBufferPosition.y - viewport.y) / viewport.height * 2.0 - 1.0; - if (scene.logDepthBuffer) { + if (scene._logDepthBuffer) { var frustumCommand = scene._frustumCommandsList[0]; var far = frustumCommand.far; var near = frustumCommand.near; @@ -343,8 +343,7 @@ define([ var w = 1.0 / worldCoords.w; Cartesian3.multiplyByScalar(worldCoords, w, worldCoords); } - result = Cartesian3.fromCartesian4(worldCoords, result); - return result; + return Cartesian3.fromCartesian4(worldCoords, result); }; return SceneTransforms; From e331d4ce4eb123f8600a8c274f97dd034e15fffe Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 16 Feb 2018 17:58:39 -0500 Subject: [PATCH 28/68] Use derived commands to enable log depth and disable when using an orthographic projection. --- Source/Renderer/ShaderSource.js | 3 - Source/Scene/Camera.js | 1 + Source/Scene/Scene.js | 69 +++++++++++++++++-- .../Shaders/Builtin/Functions/logDepth.glsl | 2 +- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index 9fe1ff4cbd6c..b1f4684b93df 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -228,9 +228,6 @@ define([ precision highp float;\n\ #else\n\ precision mediump float;\n\ -#endif\n\n\ -#ifdef GL_EXT_frag_depth\n\ -#extension GL_EXT_frag_depth : enable\n\ #endif\n\n'; } diff --git a/Source/Scene/Camera.js b/Source/Scene/Camera.js index e7e7719b898c..eca553c44f84 100644 --- a/Source/Scene/Camera.js +++ b/Source/Scene/Camera.js @@ -3229,6 +3229,7 @@ define([ Cartesian3.clone(camera.right, result.right); Matrix4.clone(camera._transform, result.transform); result._transformChanged = true; + result.frustum = camera.frustum; return result; }; diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 8cef52d6c191..eb3bf9e680a6 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -695,6 +695,7 @@ define([ this._cameraClone = Camera.clone(camera); this._screenSpaceCameraController = new ScreenSpaceCameraController(this); this._mapMode2D = defaultValue(options.mapMode2D, MapMode2D.INFINITE_SCROLL); + this._frustumChanged = true; // Keeps track of the state of a frame. FrameState is the state across // the primitives of the scene. This state is for internally keeping track @@ -1431,9 +1432,20 @@ define([ } var derivedCommands = command.derivedCommands; - if (command.dirty && defined(derivedCommands)) { + if ((scene._frustumChanged || command.dirty) && defined(derivedCommands)) { command.dirty = false; + var frustum = scene.camera.frustum; + var useLogDepth = scene._logDepthBuffer && !(frustum instanceof OrthographicFrustum || frustum instanceof OrthographicOffCenterFrustum); + if (useLogDepth) { + derivedCommands.logDepth = createLogDepthCommand(command, context, derivedCommands.logDepth); + command = derivedCommands.logDepth.logDepthCommand; + } + + if (scene.frameState.passes.pick) { + return; + } + if (shadowsEnabled && (command.receiveShadows || command.castShadows)) { derivedCommands.shadows = ShadowMap.createDerivedCommands(shadowMaps, lightShadowMaps, command, shadowsDirty, context, derivedCommands.shadows); } @@ -1540,9 +1552,7 @@ define([ command.debugOverlappingFrustums = 0; } - if (!scene.frameState.passes.pick) { - updateDerivedCommands(scene, command); - } + updateDerivedCommands(scene, command); var frustumCommandsList = scene._frustumCommandsList; var length = frustumCommandsList.length; @@ -1871,6 +1881,8 @@ define([ command.derivedCommands.shadows.receiveCommand.execute(context, passState); } else if (scene.frameState.passes.depth && defined(command.derivedCommands.depth)) { command.derivedCommands.depth.depthOnlyCommand.execute(context, passState); + } else if (scene._logDepthBuffer && defined(command.derivedCommands.logDepth)) { + command.derivedCommands.logDepth.logDepthCommand.execute(context, passState); } else { command.execute(context, passState); } @@ -3067,8 +3079,9 @@ define([ tryAndCatchError(this, time, update); this._postUpdate.raiseEvent(this, time); + this._frustumChanged = this._camera !== this._cameraClone.frustum; var cameraChanged = checkForCameraUpdates(this); - var shouldRender = !this.requestRenderMode || this._renderRequested || cameraChanged || (this.mode === SceneMode.MORPHING); + var shouldRender = !this.requestRenderMode || this._renderRequested || cameraChanged || this._frustumChanged || (this.mode === SceneMode.MORPHING); if (!shouldRender && defined(this.maximumRenderTimeChange) && defined(this._lastRenderTime)) { var difference = Math.abs(JulianDate.secondsDifference(this._lastRenderTime, time)); shouldRender = shouldRender || difference > this.maximumRenderTimeChange; @@ -3370,6 +3383,52 @@ define([ return result; } + function getLogDepthShaderProgram(context, shaderProgram) { + var shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, 'logDepth'); + if (!defined(shader)) { + var attributeLocations = shaderProgram._attributeLocations; + var fs = shaderProgram.fragmentShaderSource.clone(); + fs.defines = defined(fs.defines) ? fs.defines.slice(0) : []; + fs.defines.push('LOG_DEPTH'); + + var extension = + '#ifdef GL_EXT_frag_depth \n' + + '#extension GL_EXT_frag_depth : enable \n' + + '#endif \n'; + fs.sources.push(extension); + + shader = context.shaderCache.createDerivedShaderProgram(shaderProgram, 'logDepth', { + vertexShaderSource : shaderProgram.vertexShaderSource, + fragmentShaderSource : fs, + attributeLocations : attributeLocations + }); + } + + return shader; + } + + function createLogDepthCommand(command, context, result) { + if (!defined(result)) { + result = {}; + } + + var shader; + if (defined(result.logDepthCommand)) { + shader = result.logDepthCommand.shaderProgram; + } + + result.logDepthCommand = DrawCommand.shallowClone(command, result.logDepthCommand); + + if (!defined(shader) || result.shaderProgramId !== command.shaderProgram.id) { + result.logDepthCommand.shaderProgram = getLogDepthShaderProgram(context, command.shaderProgram); + result.shaderProgramId = command.shaderProgram.id; + } else { + result.logDepthCommand.shaderProgram = shader; + } + + return result; + } + function renderTranslucentDepthForPick(scene, drawingBufferPosition) { // PERFORMANCE_IDEA: render translucent only and merge with the previous frame var context = scene._context; diff --git a/Source/Shaders/Builtin/Functions/logDepth.glsl b/Source/Shaders/Builtin/Functions/logDepth.glsl index 9c3f6487f028..823b43b39dda 100644 --- a/Source/Shaders/Builtin/Functions/logDepth.glsl +++ b/Source/Shaders/Builtin/Functions/logDepth.glsl @@ -1,6 +1,6 @@ void czm_logDepth(float w) { -#ifdef GL_EXT_frag_depth +#if defined(GL_EXT_frag_depth) && defined(LOG_DEPTH) gl_FragDepthEXT = -log(w * czm_currentFrustum.x) / log( czm_currentFrustum.y / czm_currentFrustum.x); #endif } From 75bd3ba44819ee8efec39d539d4fbabcb55dd283 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 19 Feb 2018 15:53:10 -0500 Subject: [PATCH 29/68] Fix picking. --- Source/Renderer/ShaderSource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Renderer/ShaderSource.js b/Source/Renderer/ShaderSource.js index b1f4684b93df..f70c7d8c250a 100644 --- a/Source/Renderer/ShaderSource.js +++ b/Source/Renderer/ShaderSource.js @@ -313,7 +313,7 @@ define([ return new ShaderSource({ sources : this.sources, defines : this.defines, - pickColorQuantifier : this.pickColorQualifier, + pickColorQualifier : this.pickColorQualifier, includeBuiltIns : this.includeBuiltIns }); }; From d681755611f2fc47f28e33db61a9ec0583a4abe3 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 20 Feb 2018 15:27:34 -0500 Subject: [PATCH 30/68] Fix shadow mapping and use log depth with OIT. --- Source/Scene/Camera.js | 2 +- Source/Scene/OIT.js | 6 ++++++ Source/Scene/Scene.js | 21 +++++++++++++++++---- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Source/Scene/Camera.js b/Source/Scene/Camera.js index eca553c44f84..50b48178c9d3 100644 --- a/Source/Scene/Camera.js +++ b/Source/Scene/Camera.js @@ -3229,7 +3229,7 @@ define([ Cartesian3.clone(camera.right, result.right); Matrix4.clone(camera._transform, result.transform); result._transformChanged = true; - result.frustum = camera.frustum; + result.frustum = camera.frustum.clone(); return result; }; diff --git a/Source/Scene/OIT.js b/Source/Scene/OIT.js index c7a38d68700d..c7e0746c363b 100644 --- a/Source/Scene/OIT.js +++ b/Source/Scene/OIT.js @@ -554,12 +554,14 @@ define([ for (j = 0; j < length; ++j) { command = commands[j]; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.translucentCommand : command.derivedCommands.oit.translucentCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } if (defined(invertClassification)) { command = invertClassification.unclassifiedCommand; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.translucentCommand : command.derivedCommands.oit.translucentCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } @@ -568,12 +570,14 @@ define([ for (j = 0; j < length; ++j) { command = commands[j]; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.alphaCommand : command.derivedCommands.oit.alphaCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } if (defined(invertClassification)) { command = invertClassification.unclassifiedCommand; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.alphaCommand : command.derivedCommands.oit.alphaCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } @@ -599,12 +603,14 @@ define([ for (var j = 0; j < length; ++j) { command = commands[j]; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.translucentCommand : command.derivedCommands.oit.translucentCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } if (defined(invertClassification)) { command = invertClassification.unclassifiedCommand; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.translucentCommand : command.derivedCommands.oit.translucentCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index eb3bf9e680a6..cadb5afbdb4a 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -1437,9 +1437,12 @@ define([ var frustum = scene.camera.frustum; var useLogDepth = scene._logDepthBuffer && !(frustum instanceof OrthographicFrustum || frustum instanceof OrthographicOffCenterFrustum); + var logDepthCommand; + var logDepthDerivedCommands; if (useLogDepth) { derivedCommands.logDepth = createLogDepthCommand(command, context, derivedCommands.logDepth); - command = derivedCommands.logDepth.logDepthCommand; + logDepthCommand = derivedCommands.logDepth.logDepthCommand; + logDepthDerivedCommands = logDepthCommand.derivedCommands; } if (scene.frameState.passes.pick) { @@ -1448,13 +1451,21 @@ define([ if (shadowsEnabled && (command.receiveShadows || command.castShadows)) { derivedCommands.shadows = ShadowMap.createDerivedCommands(shadowMaps, lightShadowMaps, command, shadowsDirty, context, derivedCommands.shadows); + if (useLogDepth) { + logDepthDerivedCommands.shadows = ShadowMap.createDerivedCommands(shadowMaps, lightShadowMaps, logDepthCommand, shadowsDirty, context, logDepthDerivedCommands.shadows); + } + } + + if (useLogDepth) { + command = logDepthCommand; + derivedCommands = logDepthDerivedCommands; } var oit = scene._oit; if (command.pass === Pass.TRANSLUCENT && defined(oit) && oit.isSupported()) { if (lightShadowsEnabled && command.receiveShadows) { derivedCommands.oit = defined(derivedCommands.oit) ? derivedCommands.oit : {}; - derivedCommands.oit.shadows = oit.createDerivedCommands(command.derivedCommands.shadows.receiveCommand, context, derivedCommands.oit.shadows); + derivedCommands.oit.shadows = oit.createDerivedCommands(derivedCommands.shadows.receiveCommand, context, derivedCommands.oit.shadows); } else { derivedCommands.oit = oit.createDerivedCommands(command, context, derivedCommands.oit); } @@ -1872,6 +1883,10 @@ define([ var shadowsEnabled = scene.frameState.shadowHints.shadowsEnabled; var lightShadowsEnabled = shadowsEnabled && (scene.frameState.shadowHints.lightShadowMaps.length > 0); + if (scene._logDepthBuffer && defined(command.derivedCommands.logDepth)) { + command = command.derivedCommands.logDepth.logDepthCommand; + } + if (scene.debugShowCommands || scene.debugShowFrustums) { executeDebugCommand(command, scene, passState); } else if (lightShadowsEnabled && command.receiveShadows && defined(command.derivedCommands.shadows)) { @@ -1881,8 +1896,6 @@ define([ command.derivedCommands.shadows.receiveCommand.execute(context, passState); } else if (scene.frameState.passes.depth && defined(command.derivedCommands.depth)) { command.derivedCommands.depth.depthOnlyCommand.execute(context, passState); - } else if (scene._logDepthBuffer && defined(command.derivedCommands.logDepth)) { - command.derivedCommands.logDepth.logDepthCommand.execute(context, passState); } else { command.execute(context, passState); } From 6e07e898dd6f10ac97c2520ec237ab7494c718f9 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 20 Feb 2018 16:22:30 -0500 Subject: [PATCH 31/68] Fix depth plane. --- Source/Scene/DepthPlane.js | 42 +++++++++++++++++++++++++++++--------- Source/Scene/Scene.js | 3 ++- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Source/Scene/DepthPlane.js b/Source/Scene/DepthPlane.js index 1a4e59a89917..c366e487e141 100644 --- a/Source/Scene/DepthPlane.js +++ b/Source/Scene/DepthPlane.js @@ -12,6 +12,7 @@ define([ '../Renderer/Pass', '../Renderer/RenderState', '../Renderer/ShaderProgram', + '../Renderer/ShaderSource', '../Renderer/VertexArray', '../Shaders/DepthPlaneFS', '../Shaders/DepthPlaneVS', @@ -30,6 +31,7 @@ define([ Pass, RenderState, ShaderProgram, + ShaderSource, VertexArray, DepthPlaneFS, DepthPlaneVS, @@ -45,6 +47,7 @@ define([ this._va = undefined; this._command = undefined; this._mode = undefined; + this._useLogDepth = false; } var depthQuadScratch = FeatureDetection.supportsTypedArrays() ? new Float32Array(12) : []; @@ -100,7 +103,7 @@ define([ return depthQuadScratch; } - DepthPlane.prototype.update = function(frameState) { + DepthPlane.prototype.update = function(frameState, useLogDepth) { this._mode = frameState.mode; if (frameState.mode !== SceneMode.SCENE3D) { return; @@ -125,22 +128,41 @@ define([ } }); - this._sp = ShaderProgram.fromCache({ + this._command = new DrawCommand({ + renderState : this._rs, + boundingVolume : new BoundingSphere(Cartesian3.ZERO, ellipsoid.maximumRadius), + pass : Pass.OPAQUE, + owner : this + }); + } + + if (!defined(this._sp) || this._useLogDepth !== useLogDepth) { + this._useLogDepth = useLogDepth; + + var fs = new ShaderSource({ + sources : [DepthPlaneFS] + }); + if (useLogDepth) { + var extension = + '#ifdef GL_EXT_frag_depth \n' + + '#extension GL_EXT_frag_depth : enable \n' + + '#endif \n\n'; + + fs.sources.push(extension); + fs.defines.push('LOG_DEPTH'); + } + + this._sp = ShaderProgram.replaceCache({ + shaderProgram : this._sp, context : context, vertexShaderSource : DepthPlaneVS, - fragmentShaderSource : DepthPlaneFS, + fragmentShaderSource : fs, attributeLocations : { position : 0 } }); - this._command = new DrawCommand({ - renderState : this._rs, - shaderProgram : this._sp, - boundingVolume : new BoundingSphere(Cartesian3.ZERO, ellipsoid.maximumRadius), - pass : Pass.OPAQUE, - owner : this - }); + this._command.shaderProgram = this._sp; } // update depth plane diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index cadb5afbdb4a..f9e63fae933b 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -2700,10 +2700,11 @@ define([ var clearGlobeDepth = environmentState.clearGlobeDepth = defined(globe) && (!globe.depthTestAgainstTerrain || scene.mode === SceneMode.SCENE2D); var useDepthPlane = environmentState.useDepthPlane = clearGlobeDepth && scene.mode === SceneMode.SCENE3D; if (useDepthPlane) { + var useLogDepth = scene._logDepthBuffer && !(scene.camera.frustum instanceof OrthographicFrustum || scene.camera.frustum instanceof OrthographicOffCenterFrustum); // Update the depth plane that is rendered in 3D when the primitives are // not depth tested against terrain so primitives on the backface // of the globe are not picked. - scene._depthPlane.update(frameState); + scene._depthPlane.update(frameState, useLogDepth); } var occluder = (frameState.mode === SceneMode.SCENE3D) ? frameState.occluder: undefined; From 90a089bde012cbeb20d191f700a23f66ca5b8248 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 22 Feb 2018 15:50:52 -0500 Subject: [PATCH 32/68] Re-add multi-frustum for log depth. --- Source/Scene/Scene.js | 21 ++++++++++++--------- Source/Scene/SkyAtmosphere.js | 3 ++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index f9e63fae933b..d349b49a788b 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -767,15 +767,15 @@ define([ // initial guess at frustums. var near = camera.frustum.near; var far = camera.frustum.far; - var numFrustums; var farToNearRatio; + if (this._logDepthBuffer) { - numFrustums = 1; - farToNearRatio = far / near; + farToNearRatio = this.farToNearRatio * this.farToNearRatio; } else { - numFrustums = Math.ceil(Math.log(far / near) / Math.log(this.farToNearRatio)); farToNearRatio = this.farToNearRatio; } + + var numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio)); updateFrustums(near, far, farToNearRatio, numFrustums, this._frustumCommandsList, false, undefined); // give frameState, camera, and screen space camera controller initial state before rendering @@ -783,7 +783,7 @@ define([ this.initializeFrame(); } - var OPAQUE_FRUSTUM_NEAR_OFFSET = 0.9999; + var OPAQUE_FRUSTUM_NEAR_OFFSET = 0.9; function updateGlobeListeners(scene, globe) { for (var i = 0; i < scene._removeGlobeCallbacks.length; ++i) { @@ -1742,11 +1742,9 @@ define([ if (!is2D) { // The multifrustum for 3D/CV is non-uniformly distributed. if (logDepth) { - numFrustums = 1; - farToNearRatio = far / near; - } else { - numFrustums = Math.ceil(Math.log(far / near) / Math.log(scene.farToNearRatio)); + farToNearRatio *= farToNearRatio; } + numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio)); } else { // 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 @@ -3094,6 +3092,11 @@ define([ this._postUpdate.raiseEvent(this, time); this._frustumChanged = this._camera !== this._cameraClone.frustum; + if (this._frustumChanged && this._logDepthBuffer && !(this._camera.frustum instanceof OrthographicFrustum || this._camera.frustum instanceof OrthographicOffCenterFrustum)) { + this._camera.frustum.near = 0.1; + this._camera.frustum.far = 10000000000.0; + } + var cameraChanged = checkForCameraUpdates(this); var shouldRender = !this.requestRenderMode || this._renderRequested || cameraChanged || this._frustumChanged || (this.mode === SceneMode.MORPHING); if (!shouldRender && defined(this.maximumRenderTimeChange) && defined(this._lastRenderTime)) { diff --git a/Source/Scene/SkyAtmosphere.js b/Source/Scene/SkyAtmosphere.js index e37242aaddb1..f2f88087fcfb 100644 --- a/Source/Scene/SkyAtmosphere.js +++ b/Source/Scene/SkyAtmosphere.js @@ -199,7 +199,8 @@ define([ enabled : true, face : CullFace.FRONT }, - blending : BlendingState.ALPHA_BLEND + blending : BlendingState.ALPHA_BLEND, + depthMask : false }); var vs = new ShaderSource({ From 462d229b626b476402e17366985b5dde3d9cb92c Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 22 Feb 2018 16:46:15 -0500 Subject: [PATCH 33/68] Clamp shadow volume depth to far plane only when not using log depth. --- Source/Scene/ClassificationModel.js | 8 ++++---- Source/Scene/Scene.js | 2 +- .../Shaders/Appearances/PerInstanceColorAppearanceFS.glsl | 6 +++--- Source/Shaders/Builtin/Functions/depthClampFarPlane.glsl | 4 ++++ .../Builtin/Functions/writeDepthClampedToFarPlane.glsl | 5 +++-- Source/Shaders/ShadowVolumeFS.glsl | 2 +- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Source/Scene/ClassificationModel.js b/Source/Scene/ClassificationModel.js index 484a19415ba5..eaafe4b0aa5c 100644 --- a/Source/Scene/ClassificationModel.js +++ b/Source/Scene/ClassificationModel.js @@ -702,13 +702,13 @@ define([ ' gl_Position = czm_depthClampFarPlane(positionInClipCoords);\n' + '}\n'; var fs = - //'#ifdef GL_EXT_frag_depth\n' + - //'#extension GL_EXT_frag_depth : enable\n' + - //'#endif\n' + + '#ifdef GL_EXT_frag_depth\n' + + '#extension GL_EXT_frag_depth : enable\n' + + '#endif\n' + 'void main() \n' + '{ \n' + ' gl_FragColor = vec4(1.0); \n' + - //' czm_writeDepthClampedToFarPlane();\n' + + ' czm_writeDepthClampedToFarPlane();\n' + '}\n'; if (model.extensionsUsed.WEB3D_quantized_attributes) { diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index d349b49a788b..fef73d84dccd 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -3093,7 +3093,7 @@ define([ this._frustumChanged = this._camera !== this._cameraClone.frustum; if (this._frustumChanged && this._logDepthBuffer && !(this._camera.frustum instanceof OrthographicFrustum || this._camera.frustum instanceof OrthographicOffCenterFrustum)) { - this._camera.frustum.near = 0.1; + this._camera.frustum.near = 1.0; this._camera.frustum.far = 10000000000.0; } diff --git a/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl b/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl index abf0c39b733f..c2541082af81 100644 --- a/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl +++ b/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl @@ -6,19 +6,19 @@ varying float v_inverse_depth; void main() { vec3 positionToEyeEC = -v_positionEC; - + vec3 normalEC = normalize(v_normalEC); #ifdef FACE_FORWARD normalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC); #endif - + czm_materialInput materialInput; materialInput.normalEC = normalEC; materialInput.positionToEyeEC = positionToEyeEC; czm_material material = czm_getDefaultMaterial(materialInput); material.diffuse = v_color.rgb; material.alpha = v_color.a; - + gl_FragColor = czm_phong(normalize(positionToEyeEC), material); czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Builtin/Functions/depthClampFarPlane.glsl b/Source/Shaders/Builtin/Functions/depthClampFarPlane.glsl index 76940c00575f..bec295024033 100644 --- a/Source/Shaders/Builtin/Functions/depthClampFarPlane.glsl +++ b/Source/Shaders/Builtin/Functions/depthClampFarPlane.glsl @@ -1,5 +1,7 @@ // emulated noperspective +#ifndef LOG_DEPTH varying float v_WindowZ; +#endif /** * Clamps a vertex to the far plane. @@ -17,7 +19,9 @@ varying float v_WindowZ; */ vec4 czm_depthClampFarPlane(vec4 coords) { +#ifndef LOG_DEPTH v_WindowZ = (0.5 * (coords.z / coords.w) + 0.5) * coords.w; coords.z = min(coords.z, coords.w); +#endif return coords; } diff --git a/Source/Shaders/Builtin/Functions/writeDepthClampedToFarPlane.glsl b/Source/Shaders/Builtin/Functions/writeDepthClampedToFarPlane.glsl index 3425bc387622..bf8cf3209b45 100644 --- a/Source/Shaders/Builtin/Functions/writeDepthClampedToFarPlane.glsl +++ b/Source/Shaders/Builtin/Functions/writeDepthClampedToFarPlane.glsl @@ -1,6 +1,7 @@ // emulated noperspective +#ifndef LOG_DEPTH varying float v_WindowZ; - +#endif /** * Clamps a vertex to the far plane by writing the fragments depth. *

@@ -18,7 +19,7 @@ varying float v_WindowZ; */ void czm_writeDepthClampedToFarPlane() { -#ifdef GL_EXT_frag_depth +#if defined(GL_EXT_frag_depth) && !defined(LOG_DEPTH) gl_FragDepthEXT = min(v_WindowZ * gl_FragCoord.w, 1.0); #endif } diff --git a/Source/Shaders/ShadowVolumeFS.glsl b/Source/Shaders/ShadowVolumeFS.glsl index 49133e99af11..de10419ba5e2 100644 --- a/Source/Shaders/ShadowVolumeFS.glsl +++ b/Source/Shaders/ShadowVolumeFS.glsl @@ -17,6 +17,6 @@ void main(void) #else gl_FragColor = v_color; #endif - //czm_writeDepthClampedToFarPlane(); + czm_writeDepthClampedToFarPlane(); czm_logDepth(1.0 / v_depth); } From 27447fc15652d42e0fff715776832769329a0908 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 23 Feb 2018 15:25:49 -0500 Subject: [PATCH 34/68] Generate log depth shaders with derived commands when possible. --- Source/Scene/GlobeSurfaceShaderSet.js | 2 - Source/Scene/PointCloud3DTileContent.js | 10 +--- Source/Scene/Scene.js | 56 +++++++++++++++++-- Source/Scene/SceneTransforms.js | 15 ++--- .../Appearances/AllMaterialAppearanceFS.glsl | 3 - .../Appearances/AllMaterialAppearanceVS.glsl | 2 - .../BasicMaterialAppearanceFS.glsl | 3 - .../BasicMaterialAppearanceVS.glsl | 2 - .../EllipsoidSurfaceAppearanceFS.glsl | 3 - .../EllipsoidSurfaceAppearanceVS.glsl | 2 - .../PerInstanceColorAppearanceFS.glsl | 2 - .../PerInstanceColorAppearanceVS.glsl | 2 - .../PerInstanceFlatColorAppearanceFS.glsl | 2 - .../PerInstanceFlatColorAppearanceVS.glsl | 2 - .../PolylineColorAppearanceVS.glsl | 10 +++- .../PolylineMaterialAppearanceVS.glsl | 10 +++- .../TexturedMaterialAppearanceFS.glsl | 9 +-- .../TexturedMaterialAppearanceVS.glsl | 2 - Source/Shaders/BillboardCollectionFS.glsl | 10 +++- Source/Shaders/BillboardCollectionVS.glsl | 14 ++++- Source/Shaders/DepthPlaneFS.glsl | 6 +- Source/Shaders/GlobeFS.glsl | 2 - Source/Shaders/GlobeVS.glsl | 2 - .../Shaders/PointPrimitiveCollectionFS.glsl | 10 +++- .../Shaders/PointPrimitiveCollectionVS.glsl | 14 ++++- Source/Shaders/PolylineFS.glsl | 9 ++- Source/Shaders/PolylineVS.glsl | 10 +++- Source/Shaders/ShadowVolumeFS.glsl | 3 - Source/Shaders/ShadowVolumeVS.glsl | 2 - Source/Shaders/Vector3DTilePolylinesVS.glsl | 9 ++- 30 files changed, 144 insertions(+), 84 deletions(-) diff --git a/Source/Scene/GlobeSurfaceShaderSet.js b/Source/Scene/GlobeSurfaceShaderSet.js index 41df7c8e554e..c46fe650b6bf 100644 --- a/Source/Scene/GlobeSurfaceShaderSet.js +++ b/Source/Scene/GlobeSurfaceShaderSet.js @@ -242,11 +242,9 @@ define([ // pass through fragment shader. only depth is rendered for the globe on a pick pass var fs = - 'varying float v_inverse_depth;\n' + 'void main()\n' + '{\n' + ' gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n' + - ' czm_logDepth(v_inverse_depth);\n' + '}\n'; pickShader = this._pickShaderPrograms[flags] = ShaderProgram.fromCache({ diff --git a/Source/Scene/PointCloud3DTileContent.js b/Source/Scene/PointCloud3DTileContent.js index 4657894f7930..4941ee3240af 100644 --- a/Source/Scene/PointCloud3DTileContent.js +++ b/Source/Scene/PointCloud3DTileContent.js @@ -970,7 +970,6 @@ define([ var vs = 'attribute vec3 a_position; \n' + 'varying vec4 v_color; \n' + - 'varying float v_inverse_depth; \n' + 'uniform vec4 u_pointSizeAndTilesetTimeAndGeometricErrorAndDepthMultiplier; \n' + 'uniform vec4 u_constantColor; \n' + 'uniform vec4 u_highlightColor; \n'; @@ -1102,8 +1101,7 @@ define([ } vs += ' v_color = color; \n' + - ' gl_Position = czm_modelViewProjection * vec4(position, 1.0); \n' + - ' v_inverse_depth = 1.0 / gl_Position.w; \n'; + ' gl_Position = czm_modelViewProjection * vec4(position, 1.0); \n'; if (hasNormals && backFaceCulling) { vs += ' float visible = step(-normal.z, 0.0); \n' + @@ -1118,8 +1116,7 @@ define([ vs += '} \n'; - var fs = 'varying vec4 v_color; \n' + - 'varying float v_inverse_depth; \n'; + var fs = 'varying vec4 v_color; \n'; if (hasClippedContent) { fs += 'uniform int u_clippingPlanesLength;' + @@ -1143,8 +1140,7 @@ define([ ' } \n'; } - fs += ' czm_logDepth(v_inverse_depth); \n' + - '} \n'; + fs += '} \n'; var drawVS = vs; var drawFS = fs; diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index fef73d84dccd..3a82fe05bd91 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -3400,22 +3400,70 @@ define([ return result; } + var logDepthRegex = /\s+czm_logDepth\(/; + function getLogDepthShaderProgram(context, shaderProgram) { var shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, 'logDepth'); if (!defined(shader)) { var attributeLocations = shaderProgram._attributeLocations; + var vs = shaderProgram.vertexShaderSource.clone(); var fs = shaderProgram.fragmentShaderSource.clone(); + + vs.defines = defined(vs.defines) ? vs.defines.slice(0) : []; + vs.defines.push('LOG_DEPTH'); fs.defines = defined(fs.defines) ? fs.defines.slice(0) : []; fs.defines.push('LOG_DEPTH'); - var extension = + var logSource = '#ifdef GL_EXT_frag_depth \n' + '#extension GL_EXT_frag_depth : enable \n' + - '#endif \n'; - fs.sources.push(extension); + '#endif \n\n'; + + var writesLogDepth = false; + var sources = fs.sources; + var length = sources.length; + var i; + for (i = 0; i < length; ++i) { + if (logDepthRegex.test(sources[i])) { + writesLogDepth = true; + break; + } + } + + if (!writesLogDepth) { + for (i = 0; i < length; i++) { + sources[i] = ShaderSource.replaceMain(sources[i], 'czm_log_depth_main'); + } + + logSource += + 'varying float v_depth; \n' + + 'void main() \n' + + '{ \n' + + ' czm_log_depth_main(); \n' + + ' czm_logDepth(1.0 / v_depth); \n' + + '} \n'; + + var vertexSources = vs.sources; + length = vertexSources.length; + for (i = 0; i < length; ++i) { + vertexSources[i] = ShaderSource.replaceMain(vertexSources[i], 'czm_log_depth_main'); + } + + var logMain = + '\n\n' + + 'varying float v_depth; \n' + + 'void main() \n' + + '{ \n' + + ' czm_log_depth_main(); \n' + + ' v_depth = gl_Position.w; \n' + + '} \n'; + vertexSources.push(logMain); + } + + sources.push(logSource); shader = context.shaderCache.createDerivedShaderProgram(shaderProgram, 'logDepth', { - vertexShaderSource : shaderProgram.vertexShaderSource, + vertexShaderSource : vs, fragmentShaderSource : fs, attributeLocations : attributeLocations }); diff --git a/Source/Scene/SceneTransforms.js b/Source/Scene/SceneTransforms.js index cd6331aa1034..df6135436bce 100644 --- a/Source/Scene/SceneTransforms.js +++ b/Source/Scene/SceneTransforms.js @@ -4,7 +4,6 @@ define([ '../Core/Cartesian3', '../Core/Cartesian4', '../Core/Cartographic', - '../Core/Color', '../Core/defined', '../Core/DeveloperError', '../Core/Math', @@ -19,7 +18,6 @@ define([ Cartesian3, Cartesian4, Cartographic, - Color, defined, DeveloperError, CesiumMath, @@ -308,16 +306,15 @@ define([ var ndc = Cartesian4.clone(Cartesian4.UNIT_W, scratchNDC); ndc.x = (drawingBufferPosition.x - viewport.x) / viewport.width * 2.0 - 1.0; ndc.y = (drawingBufferPosition.y - viewport.y) / viewport.height * 2.0 - 1.0; - if (scene._logDepthBuffer) { + if (scene._logDepthBuffer && !(scene.camera.frustum instanceof OrthographicFrustum || scene.camera.frustum instanceof OrthographicOffCenterFrustum)) { var frustumCommand = scene._frustumCommandsList[0]; var far = frustumCommand.far; var near = frustumCommand.near; - /* transforming logarithmic depth of form - * log(z / near) / log( far / near); - * to perspective form - * (far - far * near / z) / (far - near) - */ - depth = far * (1 - 1 /(Math.exp(depth * Math.log(far / near)))) / (far - near); + // transforming logarithmic depth of form + // log(z / near) / log( far / near); + // to perspective form + // (far - far * near / z) / (far - near) + depth = far * (1.0 - 1.0 /(Math.exp(depth * Math.log(far / near)))) / (far - near); } ndc.z = (depth * 2.0) - 1.0; ndc.w = 1.0; diff --git a/Source/Shaders/Appearances/AllMaterialAppearanceFS.glsl b/Source/Shaders/Appearances/AllMaterialAppearanceFS.glsl index 745f0fd43853..672750f1f023 100644 --- a/Source/Shaders/Appearances/AllMaterialAppearanceFS.glsl +++ b/Source/Shaders/Appearances/AllMaterialAppearanceFS.glsl @@ -3,7 +3,6 @@ varying vec3 v_normalEC; varying vec3 v_tangentEC; varying vec3 v_bitangentEC; varying vec2 v_st; -varying float v_inverse_depth; void main() { @@ -27,6 +26,4 @@ void main() #else gl_FragColor = czm_phong(normalize(positionToEyeEC), material); #endif - - czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl index 6cd5de782fcf..33d9d300a304 100644 --- a/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/AllMaterialAppearanceVS.glsl @@ -11,7 +11,6 @@ varying vec3 v_normalEC; varying vec3 v_tangentEC; varying vec3 v_bitangentEC; varying vec2 v_st; -varying float v_inverse_depth; void main() { @@ -24,5 +23,4 @@ void main() v_st = st; gl_Position = czm_modelViewProjectionRelativeToEye * p; - v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/Appearances/BasicMaterialAppearanceFS.glsl b/Source/Shaders/Appearances/BasicMaterialAppearanceFS.glsl index b26987f45854..aaf9e7efcdc5 100644 --- a/Source/Shaders/Appearances/BasicMaterialAppearanceFS.glsl +++ b/Source/Shaders/Appearances/BasicMaterialAppearanceFS.glsl @@ -1,6 +1,5 @@ varying vec3 v_positionEC; varying vec3 v_normalEC; -varying float v_inverse_depth; void main() { @@ -21,6 +20,4 @@ void main() #else gl_FragColor = czm_phong(normalize(positionToEyeEC), material); #endif - - czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/BasicMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/BasicMaterialAppearanceVS.glsl index ea184f96fab0..d17aa17009b9 100644 --- a/Source/Shaders/Appearances/BasicMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/BasicMaterialAppearanceVS.glsl @@ -5,7 +5,6 @@ attribute float batchId; varying vec3 v_positionEC; varying vec3 v_normalEC; -varying float v_inverse_depth; void main() { @@ -15,5 +14,4 @@ void main() v_normalEC = czm_normal * normal; // normal in eye coordinates gl_Position = czm_modelViewProjectionRelativeToEye * p; - v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceFS.glsl b/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceFS.glsl index 19db3fbf7a99..095ea101bd20 100644 --- a/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceFS.glsl +++ b/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceFS.glsl @@ -1,7 +1,6 @@ varying vec3 v_positionMC; varying vec3 v_positionEC; varying vec2 v_st; -varying float v_inverse_depth; void main() { @@ -31,6 +30,4 @@ void main() #else gl_FragColor = czm_phong(normalize(positionToEyeEC), material); #endif - - czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceVS.glsl b/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceVS.glsl index 98d73454ce9d..6f4e11300a7c 100644 --- a/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceVS.glsl +++ b/Source/Shaders/Appearances/EllipsoidSurfaceAppearanceVS.glsl @@ -6,7 +6,6 @@ attribute float batchId; varying vec3 v_positionMC; varying vec3 v_positionEC; varying vec2 v_st; -varying float v_inverse_depth; void main() { @@ -17,5 +16,4 @@ void main() v_st = st; gl_Position = czm_modelViewProjectionRelativeToEye * p; - v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl b/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl index c2541082af81..c2709e847ea9 100644 --- a/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl +++ b/Source/Shaders/Appearances/PerInstanceColorAppearanceFS.glsl @@ -1,7 +1,6 @@ varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec4 v_color; -varying float v_inverse_depth; void main() { @@ -20,5 +19,4 @@ void main() material.alpha = v_color.a; gl_FragColor = czm_phong(normalize(positionToEyeEC), material); - czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl b/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl index 777a36080e6f..5d0711b68949 100644 --- a/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PerInstanceColorAppearanceVS.glsl @@ -7,7 +7,6 @@ attribute float batchId; varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec4 v_color; -varying float v_inverse_depth; void main() { @@ -18,5 +17,4 @@ void main() v_color = color; gl_Position = czm_modelViewProjectionRelativeToEye * p; - v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl index 08eebb3b5e98..3649a0883f23 100644 --- a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl +++ b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceFS.glsl @@ -1,8 +1,6 @@ varying vec4 v_color; -varying float v_inverse_depth; void main() { gl_FragColor = v_color; - czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl index f840c3a02cf7..f38d44046db4 100644 --- a/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PerInstanceFlatColorAppearanceVS.glsl @@ -4,7 +4,6 @@ attribute vec4 color; attribute float batchId; varying vec4 v_color; -varying float v_inverse_depth; void main() { @@ -13,5 +12,4 @@ void main() v_color = color; gl_Position = czm_modelViewProjectionRelativeToEye * p; - v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl index c85213647c18..d36b19a1462b 100644 --- a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl @@ -9,7 +9,10 @@ attribute vec4 color; attribute float batchId; varying vec4 v_color; -varying float v_inverse_depth; + +#ifdef LOG_DEPTH +varying float v_inverseDepth; +#endif void main() { @@ -26,5 +29,8 @@ void main() float angle; vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, angle); gl_Position = czm_viewportOrthographic * positionWC; - v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; + +#ifdef LOG_DEPTH + v_inverseDepth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; +#endif } diff --git a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl index 448e484afe94..068c5cb52467 100644 --- a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl @@ -11,7 +11,10 @@ attribute float batchId; varying float v_width; varying vec2 v_st; varying float v_polylineAngle; -varying float v_inverse_depth; + +#ifdef LOG_DEPTH +varying float v_inverseDepth; +#endif void main() { @@ -28,5 +31,8 @@ void main() vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, v_polylineAngle); gl_Position = czm_viewportOrthographic * positionWC; - v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; + +#ifdef LOG_DEPTH + v_inverseDepth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; +#endif } diff --git a/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl b/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl index 9e2c4a91c157..92dcf9dbc37d 100644 --- a/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl +++ b/Source/Shaders/Appearances/TexturedMaterialAppearanceFS.glsl @@ -1,11 +1,10 @@ varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec2 v_st; -varying float v_inverse_depth; void main() { - vec3 positionToEyeEC = -v_positionEC; + vec3 positionToEyeEC = -v_positionEC; vec3 normalEC = normalize(v_normalEC); #ifdef FACE_FORWARD @@ -17,12 +16,10 @@ void main() materialInput.positionToEyeEC = positionToEyeEC; materialInput.st = v_st; czm_material material = czm_getMaterial(materialInput); - -#ifdef FLAT + +#ifdef FLAT gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); #else gl_FragColor = czm_phong(normalize(positionToEyeEC), material); #endif - - czm_logDepth(v_inverse_depth); } diff --git a/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl index 3673351ba78a..7ceb6e0b5239 100644 --- a/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/TexturedMaterialAppearanceVS.glsl @@ -7,7 +7,6 @@ attribute float batchId; varying vec3 v_positionEC; varying vec3 v_normalEC; varying vec2 v_st; -varying float v_inverse_depth; void main() { @@ -18,5 +17,4 @@ void main() v_st = st; gl_Position = czm_modelViewProjectionRelativeToEye * p; - v_inverse_depth = 1.0 / gl_Position.w; } diff --git a/Source/Shaders/BillboardCollectionFS.glsl b/Source/Shaders/BillboardCollectionFS.glsl index 414516c3e132..d947d70cd1a6 100644 --- a/Source/Shaders/BillboardCollectionFS.glsl +++ b/Source/Shaders/BillboardCollectionFS.glsl @@ -5,7 +5,10 @@ uniform vec4 u_highlightColor; #endif varying vec2 v_textureCoordinates; -varying float v_inverse_depth; + +#ifdef LOG_DEPTH +varying float v_inverseDepth; +#endif #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; @@ -54,5 +57,8 @@ void main() #else gl_FragColor = color; #endif - czm_logDepth(v_inverse_depth); + +#ifdef LOG_DEPTH + czm_logDepth(v_inverseDepth); +#endif } diff --git a/Source/Shaders/BillboardCollectionVS.glsl b/Source/Shaders/BillboardCollectionVS.glsl index 80a2ca42f2f8..8224c2463911 100644 --- a/Source/Shaders/BillboardCollectionVS.glsl +++ b/Source/Shaders/BillboardCollectionVS.glsl @@ -15,7 +15,10 @@ attribute float a_batchId; #endif varying vec2 v_textureCoordinates; -varying float v_inverse_depth; + +#ifdef LOG_DEPTH +varying float v_inverseDepth; +#endif #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; @@ -258,9 +261,12 @@ void main() vec4 positionWC = computePositionWindowCoordinates(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters); gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); - v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; v_textureCoordinates = textureCoordinates; +#ifdef LOG_DEPTH + v_inverseDepth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; +#endif + #ifdef DISABLE_DEPTH_DISTANCE float disableDepthTestDistance = distanceDisplayConditionAndDisableDepth.z; if (disableDepthTestDistance == 0.0 && czm_minimumDisableDepthTestDistance != 0.0) @@ -277,7 +283,9 @@ void main() { // Position z on the near plane. gl_Position.z = -gl_Position.w; - v_inverse_depth = 1.0 / czm_currentFrustum.x; +#ifdef LOG_DEPTH + v_inverseDepth = 1.0 / czm_currentFrustum.x; +#endif } } #endif diff --git a/Source/Shaders/DepthPlaneFS.glsl b/Source/Shaders/DepthPlaneFS.glsl index 0ea5f88f9c1d..daa2df27f3b5 100644 --- a/Source/Shaders/DepthPlaneFS.glsl +++ b/Source/Shaders/DepthPlaneFS.glsl @@ -8,8 +8,6 @@ void main() vec3 direction = normalize(positionEC.xyz); czm_ray ray = czm_ray(vec3(0.0), direction); - czm_logDepth(-1.0 / positionEC.z); - czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid); if (!czm_isEmpty(intersection)) { @@ -19,4 +17,8 @@ void main() { discard; } + +#ifdef LOG_DEPTH + czm_logDepth(-1.0 / positionEC.z); +#endif } diff --git a/Source/Shaders/GlobeFS.glsl b/Source/Shaders/GlobeFS.glsl index b9d68f808cd9..f41b6eb34b31 100644 --- a/Source/Shaders/GlobeFS.glsl +++ b/Source/Shaders/GlobeFS.glsl @@ -63,7 +63,6 @@ uniform float u_minimumBrightness; varying vec3 v_positionMC; varying vec3 v_positionEC; -varying float v_inverse_depth; varying vec3 v_textureCoordinates; varying vec3 v_normalMC; varying vec3 v_normalEC; @@ -253,7 +252,6 @@ void main() #else gl_FragColor = finalColor; #endif - czm_logDepth(v_inverse_depth); } #ifdef SHOW_REFLECTIVE_OCEAN diff --git a/Source/Shaders/GlobeVS.glsl b/Source/Shaders/GlobeVS.glsl index d8fae1bfe816..30c2afc014a6 100644 --- a/Source/Shaders/GlobeVS.glsl +++ b/Source/Shaders/GlobeVS.glsl @@ -17,7 +17,6 @@ uniform vec2 u_southMercatorYAndOneOverHeight; varying vec3 v_positionMC; varying vec3 v_positionEC; -varying float v_inverse_depth; varying vec3 v_textureCoordinates; varying vec3 v_normalMC; @@ -157,7 +156,6 @@ void main() v_textureCoordinates = vec3(textureCoordinates, webMercatorT); - v_inverse_depth = 1.0 / gl_Position.w; #if defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL) || defined(APPLY_MATERIAL) v_positionEC = (u_modifiedModelView * vec4(position, 1.0)).xyz; v_positionMC = position3DWC; // position in model coordinates diff --git a/Source/Shaders/PointPrimitiveCollectionFS.glsl b/Source/Shaders/PointPrimitiveCollectionFS.glsl index 7cb1e9c997fd..fff4abe1f55b 100644 --- a/Source/Shaders/PointPrimitiveCollectionFS.glsl +++ b/Source/Shaders/PointPrimitiveCollectionFS.glsl @@ -2,7 +2,10 @@ varying vec4 v_color; varying vec4 v_outlineColor; varying float v_innerPercent; varying float v_pixelDistance; -varying float v_inverse_depth; + +#ifdef LOG_DEPTH +varying float v_inverseDepth; +#endif #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; @@ -47,5 +50,8 @@ void main() #else gl_FragColor = color; #endif - czm_logDepth(v_inverse_depth); + +#ifdef LOG_DEPTH + czm_logDepth(v_inverseDepth); +#endif } diff --git a/Source/Shaders/PointPrimitiveCollectionVS.glsl b/Source/Shaders/PointPrimitiveCollectionVS.glsl index a8f4de6f371d..bdfe2f2a911c 100644 --- a/Source/Shaders/PointPrimitiveCollectionVS.glsl +++ b/Source/Shaders/PointPrimitiveCollectionVS.glsl @@ -11,7 +11,10 @@ varying vec4 v_color; varying vec4 v_outlineColor; varying float v_innerPercent; varying float v_pixelDistance; -varying float v_inverse_depth; + +#ifdef LOG_DEPTH +varying float v_inverseDepth; +#endif #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; @@ -151,7 +154,10 @@ void main() vec4 positionWC = czm_eyeToWindowCoordinates(positionEC); gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); - v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; + +#ifdef LOG_DEPTH + v_inverseDepth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; +#endif #ifdef DISABLE_DEPTH_DISTANCE float disableDepthTestDistance = distanceDisplayConditionAndDisableDepth.z; @@ -169,7 +175,9 @@ void main() { // Position z on the near plane. gl_Position.z = -gl_Position.w; - v_inverse_depth = 1.0 / czm_currentFrustum.x; +#ifdef LOG_DEPTH + v_inverseDepth = 1.0 / czm_currentFrustum.x; +#endif } } #endif diff --git a/Source/Shaders/PolylineFS.glsl b/Source/Shaders/PolylineFS.glsl index 141b02910a40..eb0d6910b8ae 100644 --- a/Source/Shaders/PolylineFS.glsl +++ b/Source/Shaders/PolylineFS.glsl @@ -3,7 +3,10 @@ uniform vec4 u_highlightColor; #endif varying vec2 v_st; -varying float v_inverse_depth; + +#ifdef LOG_DEPTH +varying float v_inverseDepth; +#endif void main() { @@ -15,8 +18,10 @@ void main() czm_material material = czm_getMaterial(materialInput); gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); - czm_logDepth(v_inverse_depth); #ifdef VECTOR_TILE gl_FragColor *= u_highlightColor; #endif +#ifdef LOG_DEPTH + czm_logDepth(v_inverseDepth); +#endif } diff --git a/Source/Shaders/PolylineVS.glsl b/Source/Shaders/PolylineVS.glsl index 9ee70b977c3f..4d71c38a3965 100644 --- a/Source/Shaders/PolylineVS.glsl +++ b/Source/Shaders/PolylineVS.glsl @@ -16,7 +16,10 @@ varying vec2 v_st; varying float v_width; varying vec4 czm_pickColor; varying float v_polylineAngle; -varying float v_inverse_depth; + +#ifdef LOG_DEPTH +varying float v_inverseDepth; +#endif void main() { @@ -93,9 +96,12 @@ void main() vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, v_polylineAngle); gl_Position = czm_viewportOrthographic * positionWC * show; - v_inverse_depth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; v_st = vec2(texCoord, clamp(expandDir, 0.0, 1.0)); v_width = width; czm_pickColor = pickColor; + +#ifdef LOG_DEPTH + v_inverseDepth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; +#endif } diff --git a/Source/Shaders/ShadowVolumeFS.glsl b/Source/Shaders/ShadowVolumeFS.glsl index de10419ba5e2..51e951b07b99 100644 --- a/Source/Shaders/ShadowVolumeFS.glsl +++ b/Source/Shaders/ShadowVolumeFS.glsl @@ -8,8 +8,6 @@ uniform vec4 u_highlightColor; varying vec4 v_color; #endif -varying float v_depth; - void main(void) { #ifdef VECTOR_TILE @@ -18,5 +16,4 @@ void main(void) gl_FragColor = v_color; #endif czm_writeDepthClampedToFarPlane(); - czm_logDepth(1.0 / v_depth); } diff --git a/Source/Shaders/ShadowVolumeVS.glsl b/Source/Shaders/ShadowVolumeVS.glsl index c731f502454e..9d3c550b3cd5 100644 --- a/Source/Shaders/ShadowVolumeVS.glsl +++ b/Source/Shaders/ShadowVolumeVS.glsl @@ -19,7 +19,6 @@ uniform float u_globeMinimumAltitude; #ifndef VECTOR_TILE varying vec4 v_color; #endif -varying float v_depth; void main() { @@ -39,5 +38,4 @@ void main() #endif gl_Position = czm_depthClampFarPlane(czm_modelViewProjectionRelativeToEye * position); #endif - v_depth = gl_Position.w; } diff --git a/Source/Shaders/Vector3DTilePolylinesVS.glsl b/Source/Shaders/Vector3DTilePolylinesVS.glsl index 832ab7d80428..81d43e0a1d8e 100644 --- a/Source/Shaders/Vector3DTilePolylinesVS.glsl +++ b/Source/Shaders/Vector3DTilePolylinesVS.glsl @@ -6,7 +6,9 @@ attribute float a_batchId; uniform mat4 u_modifiedModelView; -varying float v_inverse_depth; +#ifdef LOG_DEPTH +varying float v_inverseDepth; +#endif void main() { @@ -21,5 +23,8 @@ void main() float angle; vec4 positionWC = getPolylineWindowCoordinatesEC(p, prev, next, expandDir, width, usePrev, angle); gl_Position = czm_viewportOrthographic * positionWC; - v_inverse_depth = 1.0 / (czm_projection * u_modifiedModelView * currentPosition).w; + +#ifdef LOG_DEPTH + v_inverseDepth = 1.0 / (czm_projection * u_modifiedModelView * currentPosition).w; +#endif } From 0a628dbfa535b06b51c8af97a33c7ffae858aec3 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 23 Feb 2018 19:54:41 -0500 Subject: [PATCH 35/68] Make log z functions for vertex and fragment shader. Change log z function. --- Source/Scene/DepthPlane.js | 6 ++- Source/Scene/EllipsoidPrimitive.js | 41 +++++++++++++++++-- Source/Scene/ModelUtility.js | 6 +-- Source/Scene/Scene.js | 9 ++-- .../PolylineColorAppearanceVS.glsl | 6 +-- .../PolylineMaterialAppearanceVS.glsl | 6 +-- Source/Shaders/BillboardCollectionFS.glsl | 8 +--- Source/Shaders/BillboardCollectionVS.glsl | 8 +--- .../Shaders/Builtin/Functions/logDepth.glsl | 6 --- .../Shaders/Builtin/Functions/vertexLogZ.glsl | 17 ++++++++ .../Shaders/Builtin/Functions/writeLogZ.glsl | 18 ++++++++ Source/Shaders/DepthPlaneFS.glsl | 5 +-- Source/Shaders/DepthPlaneVS.glsl | 2 + Source/Shaders/EllipsoidFS.glsl | 2 +- .../Shaders/PointPrimitiveCollectionFS.glsl | 8 +--- .../Shaders/PointPrimitiveCollectionVS.glsl | 8 +--- Source/Shaders/PolylineFS.glsl | 9 +--- Source/Shaders/PolylineVS.glsl | 6 +-- Source/Shaders/Vector3DTilePolylinesVS.glsl | 6 +-- 19 files changed, 100 insertions(+), 77 deletions(-) delete mode 100644 Source/Shaders/Builtin/Functions/logDepth.glsl create mode 100644 Source/Shaders/Builtin/Functions/vertexLogZ.glsl create mode 100644 Source/Shaders/Builtin/Functions/writeLogZ.glsl diff --git a/Source/Scene/DepthPlane.js b/Source/Scene/DepthPlane.js index c366e487e141..d8ea2af635eb 100644 --- a/Source/Scene/DepthPlane.js +++ b/Source/Scene/DepthPlane.js @@ -139,6 +139,9 @@ define([ if (!defined(this._sp) || this._useLogDepth !== useLogDepth) { this._useLogDepth = useLogDepth; + var vs = new ShaderSource({ + sources : [DepthPlaneVS] + }); var fs = new ShaderSource({ sources : [DepthPlaneFS] }); @@ -150,12 +153,13 @@ define([ fs.sources.push(extension); fs.defines.push('LOG_DEPTH'); + vs.defines.push('LOG_DEPTH'); } this._sp = ShaderProgram.replaceCache({ shaderProgram : this._sp, context : context, - vertexShaderSource : DepthPlaneVS, + vertexShaderSource : vs, fragmentShaderSource : fs, attributeLocations : { position : 0 diff --git a/Source/Scene/EllipsoidPrimitive.js b/Source/Scene/EllipsoidPrimitive.js index cd4ceff8f342..a21756b310c0 100644 --- a/Source/Scene/EllipsoidPrimitive.js +++ b/Source/Scene/EllipsoidPrimitive.js @@ -8,6 +8,8 @@ define([ '../Core/destroyObject', '../Core/DeveloperError', '../Core/Matrix4', + '../Core/OrthographicFrustum', + '../Core/OrthographicOffCenterFrustum', '../Core/VertexFormat', '../Renderer/BufferUsage', '../Renderer/DrawCommand', @@ -32,6 +34,8 @@ define([ destroyObject, DeveloperError, Matrix4, + OrthographicFrustum, + OrthographicOffCenterFrustum, VertexFormat, BufferUsage, DrawCommand, @@ -196,6 +200,8 @@ define([ */ this._depthTestEnabled = defaultValue(options.depthTestEnabled, true); + this._useLogDepth = false; + this._sp = undefined; this._rs = undefined; this._va = undefined; @@ -251,6 +257,11 @@ define([ return vertexArray; } + var logDepthExtension = + '#ifdef GL_EXT_frag_depth \n' + + '#extension GL_EXT_frag_depth : enable \n' + + '#endif \n\n'; + /** * Called when {@link Viewer} or {@link CesiumWidget} render the scene to * get the draw commands needed to render this primitive. @@ -343,11 +354,20 @@ define([ var lightingChanged = this.onlySunLighting !== this._onlySunLighting; this._onlySunLighting = this.onlySunLighting; + var frustum = frameState.camera.frustum; + var useLogDepth = context.fragmentDepth && !(frustum instanceof OrthographicFrustum || frustum instanceof OrthographicOffCenterFrustum); + var useLogDepthChanged = this._useLogDepth !== useLogDepth; + this._useLogDepth = useLogDepth; + var colorCommand = this._colorCommand; + var vs; var fs; // Recompile shader when material, lighting, or translucency changes - if (materialChanged || lightingChanged || translucencyChanged) { + if (materialChanged || lightingChanged || translucencyChanged || useLogDepthChanged) { + vs = new ShaderSource({ + sources : [EllipsoidVS] + }); fs = new ShaderSource({ sources : [this.material.shaderSource, EllipsoidFS] }); @@ -357,11 +377,16 @@ define([ if (!translucent && context.fragmentDepth) { fs.defines.push('WRITE_DEPTH'); } + if (this._useLogDepth) { + vs.defines.push('LOG_DEPTH'); + fs.defines.push('LOG_DEPTH'); + fs.sources.push(logDepthExtension); + } this._sp = ShaderProgram.replaceCache({ context : context, shaderProgram : this._sp, - vertexShaderSource : EllipsoidVS, + vertexShaderSource : vs, fragmentShaderSource : fs, attributeLocations : attributeLocations }); @@ -398,7 +423,10 @@ define([ } // Recompile shader when material changes - if (materialChanged || lightingChanged || !defined(this._pickSP)) { + if (materialChanged || lightingChanged || !defined(this._pickSP) || useLogDepthChanged) { + vs = new ShaderSource({ + sources : [EllipsoidVS] + }); fs = new ShaderSource({ sources : [this.material.shaderSource, EllipsoidFS], pickColorQualifier : 'uniform' @@ -409,11 +437,16 @@ define([ if (!translucent && context.fragmentDepth) { fs.defines.push('WRITE_DEPTH'); } + if (this._useLogDepth) { + vs.defines.push('LOG_DEPTH'); + fs.defines.push('LOG_DEPTH'); + fs.sources.push(logDepthExtension); + } this._pickSP = ShaderProgram.replaceCache({ context : context, shaderProgram : this._pickSP, - vertexShaderSource : EllipsoidVS, + vertexShaderSource : vs, fragmentShaderSource : fs, attributeLocations : attributeLocations }); diff --git a/Source/Scene/ModelUtility.js b/Source/Scene/ModelUtility.js index 3e5cf536e43c..33a6b70ec548 100644 --- a/Source/Scene/ModelUtility.js +++ b/Source/Scene/ModelUtility.js @@ -281,11 +281,10 @@ define([ shader = ShaderSource.replaceMain(shader, 'czm_depth_main'); shader += '\n' + - 'varying float v_depth; \n' + 'void main() \n' + '{ \n' + ' czm_depth_main(); \n' + - ' czm_logDepth(1.0 / v_depth); \n' + + ' czm_writeLogZ(); \n' + '} \n'; return shader; @@ -313,11 +312,10 @@ define([ shader = ShaderSource.replaceMain(shader, 'czm_depth_main'); shader += '\n' + - 'varying float v_depth;\n' + 'void main() \n' + '{ \n' + ' czm_depth_main(); \n' + - ' v_depth = (' + modelViewProjectionName + ' * vec4(' + positionName + '.xyz, 1.0)).w; \n' + + ' czm_vertexLogZ(' + modelViewProjectionName + ' * vec4(' + positionName + '.xyz, 1.0)); \n' + '} \n'; return shader; diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 3a82fe05bd91..f41b3587f2bf 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -3400,7 +3400,7 @@ define([ return result; } - var logDepthRegex = /\s+czm_logDepth\(/; + var logDepthRegex = /\s+czm_writeLogZ\(/; function getLogDepthShaderProgram(context, shaderProgram) { var shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, 'logDepth'); @@ -3436,11 +3436,11 @@ define([ } logSource += - 'varying float v_depth; \n' + + '\n' + 'void main() \n' + '{ \n' + ' czm_log_depth_main(); \n' + - ' czm_logDepth(1.0 / v_depth); \n' + + ' czm_writeLogZ(); \n' + '} \n'; var vertexSources = vs.sources; @@ -3451,11 +3451,10 @@ define([ var logMain = '\n\n' + - 'varying float v_depth; \n' + 'void main() \n' + '{ \n' + ' czm_log_depth_main(); \n' + - ' v_depth = gl_Position.w; \n' + + ' czm_vertexLogZ(); \n' + '} \n'; vertexSources.push(logMain); } diff --git a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl index d36b19a1462b..9515f6cbcc45 100644 --- a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl @@ -10,10 +10,6 @@ attribute float batchId; varying vec4 v_color; -#ifdef LOG_DEPTH -varying float v_inverseDepth; -#endif - void main() { float expandDir = expandAndWidth.x; @@ -31,6 +27,6 @@ void main() gl_Position = czm_viewportOrthographic * positionWC; #ifdef LOG_DEPTH - v_inverseDepth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; + czm_vertexLogZ(czm_modelViewProjectionRelativeToEye * p); #endif } diff --git a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl index 068c5cb52467..e91e4067fb70 100644 --- a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl @@ -12,10 +12,6 @@ varying float v_width; varying vec2 v_st; varying float v_polylineAngle; -#ifdef LOG_DEPTH -varying float v_inverseDepth; -#endif - void main() { float expandDir = expandAndWidth.x; @@ -33,6 +29,6 @@ void main() gl_Position = czm_viewportOrthographic * positionWC; #ifdef LOG_DEPTH - v_inverseDepth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; + czm_vertexLogZ(czm_modelViewProjectionRelativeToEye * p); #endif } diff --git a/Source/Shaders/BillboardCollectionFS.glsl b/Source/Shaders/BillboardCollectionFS.glsl index d947d70cd1a6..8d6488b06121 100644 --- a/Source/Shaders/BillboardCollectionFS.glsl +++ b/Source/Shaders/BillboardCollectionFS.glsl @@ -6,10 +6,6 @@ uniform vec4 u_highlightColor; varying vec2 v_textureCoordinates; -#ifdef LOG_DEPTH -varying float v_inverseDepth; -#endif - #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; #else @@ -58,7 +54,5 @@ void main() gl_FragColor = color; #endif -#ifdef LOG_DEPTH - czm_logDepth(v_inverseDepth); -#endif + czm_writeLogZ(); } diff --git a/Source/Shaders/BillboardCollectionVS.glsl b/Source/Shaders/BillboardCollectionVS.glsl index 8224c2463911..0b0d469b582c 100644 --- a/Source/Shaders/BillboardCollectionVS.glsl +++ b/Source/Shaders/BillboardCollectionVS.glsl @@ -16,10 +16,6 @@ attribute float a_batchId; varying vec2 v_textureCoordinates; -#ifdef LOG_DEPTH -varying float v_inverseDepth; -#endif - #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; #else @@ -264,7 +260,7 @@ void main() v_textureCoordinates = textureCoordinates; #ifdef LOG_DEPTH - v_inverseDepth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; + czm_vertexLogZ(czm_modelViewProjectionRelativeToEye * p); #endif #ifdef DISABLE_DEPTH_DISTANCE @@ -284,7 +280,7 @@ void main() // Position z on the near plane. gl_Position.z = -gl_Position.w; #ifdef LOG_DEPTH - v_inverseDepth = 1.0 / czm_currentFrustum.x; + czm_vertexLogZ(vec4(czm_currentFrustum.x)); #endif } } diff --git a/Source/Shaders/Builtin/Functions/logDepth.glsl b/Source/Shaders/Builtin/Functions/logDepth.glsl deleted file mode 100644 index 823b43b39dda..000000000000 --- a/Source/Shaders/Builtin/Functions/logDepth.glsl +++ /dev/null @@ -1,6 +0,0 @@ -void czm_logDepth(float w) -{ -#if defined(GL_EXT_frag_depth) && defined(LOG_DEPTH) - gl_FragDepthEXT = -log(w * czm_currentFrustum.x) / log( czm_currentFrustum.y / czm_currentFrustum.x); -#endif -} diff --git a/Source/Shaders/Builtin/Functions/vertexLogZ.glsl b/Source/Shaders/Builtin/Functions/vertexLogZ.glsl new file mode 100644 index 000000000000..a750550fdaab --- /dev/null +++ b/Source/Shaders/Builtin/Functions/vertexLogZ.glsl @@ -0,0 +1,17 @@ +#ifdef LOG_DEPTH +varying float v_logZ; +#endif + +void czm_vertexLogZ() +{ +#ifdef LOG_DEPTH + v_logZ = 1.0 + gl_Position.w; +#endif +} + +void czm_vertexLogZ(vec4 clipCoords) +{ +#ifdef LOG_DEPTH + v_logZ = 1.0 + clipCoords.w; +#endif +} diff --git a/Source/Shaders/Builtin/Functions/writeLogZ.glsl b/Source/Shaders/Builtin/Functions/writeLogZ.glsl new file mode 100644 index 000000000000..31e1451c80e5 --- /dev/null +++ b/Source/Shaders/Builtin/Functions/writeLogZ.glsl @@ -0,0 +1,18 @@ +#ifdef LOG_DEPTH +varying float v_logZ; +#endif + +void czm_writeLogZ(float logZ) +{ +#if defined(GL_EXT_frag_depth) && defined(LOG_DEPTH) + float Fcoef = 2.0 / log2(czm_currentFrustum.y + 1.0); + float Fcoef_half = 0.5 * Fcoef; + gl_FragDepthEXT = log2(logZ) * Fcoef_half; +#endif +} + +void czm_writeLogZ() { +#ifdef LOG_DEPTH + czm_writeLogZ(v_logZ); +#endif +} diff --git a/Source/Shaders/DepthPlaneFS.glsl b/Source/Shaders/DepthPlaneFS.glsl index daa2df27f3b5..53ebb87a9fc3 100644 --- a/Source/Shaders/DepthPlaneFS.glsl +++ b/Source/Shaders/DepthPlaneFS.glsl @@ -1,4 +1,5 @@ varying vec4 positionEC; +varying float flogz; void main() { @@ -18,7 +19,5 @@ void main() discard; } -#ifdef LOG_DEPTH - czm_logDepth(-1.0 / positionEC.z); -#endif + czm_writeLogZ(); } diff --git a/Source/Shaders/DepthPlaneVS.glsl b/Source/Shaders/DepthPlaneVS.glsl index e28f3767ba67..6ddb496a5309 100644 --- a/Source/Shaders/DepthPlaneVS.glsl +++ b/Source/Shaders/DepthPlaneVS.glsl @@ -6,4 +6,6 @@ void main() { positionEC = czm_modelView * position; gl_Position = czm_projection * positionEC; + + czm_vertexLogZ(); } diff --git a/Source/Shaders/EllipsoidFS.glsl b/Source/Shaders/EllipsoidFS.glsl index 81e94ecae151..a0da7525f0b8 100644 --- a/Source/Shaders/EllipsoidFS.glsl +++ b/Source/Shaders/EllipsoidFS.glsl @@ -90,7 +90,7 @@ void main() t = (intersection.start != 0.0) ? intersection.start : intersection.stop; vec3 positionEC = czm_pointAlongRay(ray, t); vec4 positionCC = czm_projection * vec4(positionEC, 1.0); - czm_logDepth(1.0 / positionCC.w); + czm_writeLogZ(1.0 + positionCC.w); #endif #endif } diff --git a/Source/Shaders/PointPrimitiveCollectionFS.glsl b/Source/Shaders/PointPrimitiveCollectionFS.glsl index fff4abe1f55b..47847cdd280b 100644 --- a/Source/Shaders/PointPrimitiveCollectionFS.glsl +++ b/Source/Shaders/PointPrimitiveCollectionFS.glsl @@ -3,10 +3,6 @@ varying vec4 v_outlineColor; varying float v_innerPercent; varying float v_pixelDistance; -#ifdef LOG_DEPTH -varying float v_inverseDepth; -#endif - #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; #endif @@ -51,7 +47,5 @@ void main() gl_FragColor = color; #endif -#ifdef LOG_DEPTH - czm_logDepth(v_inverseDepth); -#endif + czm_writeLogZ(); } diff --git a/Source/Shaders/PointPrimitiveCollectionVS.glsl b/Source/Shaders/PointPrimitiveCollectionVS.glsl index bdfe2f2a911c..d4d7cba61c6e 100644 --- a/Source/Shaders/PointPrimitiveCollectionVS.glsl +++ b/Source/Shaders/PointPrimitiveCollectionVS.glsl @@ -12,10 +12,6 @@ varying vec4 v_outlineColor; varying float v_innerPercent; varying float v_pixelDistance; -#ifdef LOG_DEPTH -varying float v_inverseDepth; -#endif - #ifdef RENDER_FOR_PICK varying vec4 v_pickColor; #endif @@ -156,7 +152,7 @@ void main() gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); #ifdef LOG_DEPTH - v_inverseDepth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; + czm_vertexLogZ(czm_modelViewProjectionRelativeToEye * p); #endif #ifdef DISABLE_DEPTH_DISTANCE @@ -176,7 +172,7 @@ void main() // Position z on the near plane. gl_Position.z = -gl_Position.w; #ifdef LOG_DEPTH - v_inverseDepth = 1.0 / czm_currentFrustum.x; + czm_vertexLogZ(vec4(czm_currentFrustum.x)); #endif } } diff --git a/Source/Shaders/PolylineFS.glsl b/Source/Shaders/PolylineFS.glsl index eb0d6910b8ae..e634f43efe03 100644 --- a/Source/Shaders/PolylineFS.glsl +++ b/Source/Shaders/PolylineFS.glsl @@ -4,10 +4,6 @@ uniform vec4 u_highlightColor; varying vec2 v_st; -#ifdef LOG_DEPTH -varying float v_inverseDepth; -#endif - void main() { czm_materialInput materialInput; @@ -21,7 +17,6 @@ void main() #ifdef VECTOR_TILE gl_FragColor *= u_highlightColor; #endif -#ifdef LOG_DEPTH - czm_logDepth(v_inverseDepth); -#endif + + czm_writeLogZ(); } diff --git a/Source/Shaders/PolylineVS.glsl b/Source/Shaders/PolylineVS.glsl index 4d71c38a3965..9f7d5bbe2325 100644 --- a/Source/Shaders/PolylineVS.glsl +++ b/Source/Shaders/PolylineVS.glsl @@ -17,10 +17,6 @@ varying float v_width; varying vec4 czm_pickColor; varying float v_polylineAngle; -#ifdef LOG_DEPTH -varying float v_inverseDepth; -#endif - void main() { float texCoord = texCoordExpandAndBatchIndex.x; @@ -102,6 +98,6 @@ void main() czm_pickColor = pickColor; #ifdef LOG_DEPTH - v_inverseDepth = 1.0 / (czm_modelViewProjectionRelativeToEye * p).w; + czm_vertexLogZ(czm_modelViewProjectionRelativeToEye * p); #endif } diff --git a/Source/Shaders/Vector3DTilePolylinesVS.glsl b/Source/Shaders/Vector3DTilePolylinesVS.glsl index 81d43e0a1d8e..111bafdc1f7f 100644 --- a/Source/Shaders/Vector3DTilePolylinesVS.glsl +++ b/Source/Shaders/Vector3DTilePolylinesVS.glsl @@ -6,10 +6,6 @@ attribute float a_batchId; uniform mat4 u_modifiedModelView; -#ifdef LOG_DEPTH -varying float v_inverseDepth; -#endif - void main() { float expandDir = expandAndWidth.x; @@ -25,6 +21,6 @@ void main() gl_Position = czm_viewportOrthographic * positionWC; #ifdef LOG_DEPTH - v_inverseDepth = 1.0 / (czm_projection * u_modifiedModelView * currentPosition).w; + czm_vertexLogZ(czm_projection * u_modifiedModelView * currentPosition); #endif } From afcc7eda927c73ca723075f20d33f55123c7ddee Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 26 Feb 2018 17:12:40 -0500 Subject: [PATCH 36/68] Fix depth picking. Simplify frag depth write. Update gl_Position.z. --- Source/Scene/SceneTransforms.js | 24 ++++++++++--------- .../Shaders/Builtin/Functions/vertexLogZ.glsl | 8 +++++++ .../Shaders/Builtin/Functions/writeLogZ.glsl | 4 +--- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Source/Scene/SceneTransforms.js b/Source/Scene/SceneTransforms.js index df6135436bce..9eb26db93d5d 100644 --- a/Source/Scene/SceneTransforms.js +++ b/Source/Scene/SceneTransforms.js @@ -302,20 +302,23 @@ define([ var context = scene.context; var uniformState = context.uniformState; - var viewport = scene._passState.viewport; - var ndc = Cartesian4.clone(Cartesian4.UNIT_W, scratchNDC); - ndc.x = (drawingBufferPosition.x - viewport.x) / viewport.width * 2.0 - 1.0; - ndc.y = (drawingBufferPosition.y - viewport.y) / viewport.height * 2.0 - 1.0; + var currentFrustum = uniformState.currentFrustum; + var near = currentFrustum.x; + var far = currentFrustum.y; + if (scene._logDepthBuffer && !(scene.camera.frustum instanceof OrthographicFrustum || scene.camera.frustum instanceof OrthographicOffCenterFrustum)) { - var frustumCommand = scene._frustumCommandsList[0]; - var far = frustumCommand.far; - var near = frustumCommand.near; // transforming logarithmic depth of form - // log(z / near) / log( far / near); + // log2(z + 1) / log2( far + 1); // to perspective form // (far - far * near / z) / (far - near) - depth = far * (1.0 - 1.0 /(Math.exp(depth * Math.log(far / near)))) / (far - near); + depth = Math.pow(2.0, depth * Math.log2(far + 1.0)) - 1.0; + depth = far * (1.0 - 1.0 / depth) / (far - near); } + + var viewport = scene._passState.viewport; + var ndc = Cartesian4.clone(Cartesian4.UNIT_W, scratchNDC); + ndc.x = (drawingBufferPosition.x - viewport.x) / viewport.width * 2.0 - 1.0; + ndc.y = (drawingBufferPosition.y - viewport.y) / viewport.height * 2.0 - 1.0; ndc.z = (depth * 2.0) - 1.0; ndc.w = 1.0; @@ -325,11 +328,10 @@ define([ if (defined(frustum._offCenterFrustum)) { frustum = frustum._offCenterFrustum; } - var currentFrustum = uniformState.currentFrustum; worldCoords = scratchWorldCoords; worldCoords.x = (ndc.x * (frustum.right - frustum.left) + frustum.left + frustum.right) * 0.5; worldCoords.y = (ndc.y * (frustum.top - frustum.bottom) + frustum.bottom + frustum.top) * 0.5; - worldCoords.z = (ndc.z * (currentFrustum.x - currentFrustum.y) - currentFrustum.x - currentFrustum.y) * 0.5; + worldCoords.z = (ndc.z * (near - far) - near - far) * 0.5; worldCoords.w = 1.0; worldCoords = Matrix4.multiplyByVector(uniformState.inverseView, worldCoords, worldCoords); diff --git a/Source/Shaders/Builtin/Functions/vertexLogZ.glsl b/Source/Shaders/Builtin/Functions/vertexLogZ.glsl index a750550fdaab..f5e2134712da 100644 --- a/Source/Shaders/Builtin/Functions/vertexLogZ.glsl +++ b/Source/Shaders/Builtin/Functions/vertexLogZ.glsl @@ -2,10 +2,17 @@ varying float v_logZ; #endif +void czm_updateZ() { + float Fcoef = 2.0 / log2(czm_currentFrustum.y + 1.0); + gl_Position.z = log2(max(1e-6, 1.0 + gl_Position.w)) * Fcoef - 1.0; + gl_Position.z *= gl_Position.w; +} + void czm_vertexLogZ() { #ifdef LOG_DEPTH v_logZ = 1.0 + gl_Position.w; + czm_updateZ(); #endif } @@ -13,5 +20,6 @@ void czm_vertexLogZ(vec4 clipCoords) { #ifdef LOG_DEPTH v_logZ = 1.0 + clipCoords.w; + czm_updateZ(); #endif } diff --git a/Source/Shaders/Builtin/Functions/writeLogZ.glsl b/Source/Shaders/Builtin/Functions/writeLogZ.glsl index 31e1451c80e5..626ea2386898 100644 --- a/Source/Shaders/Builtin/Functions/writeLogZ.glsl +++ b/Source/Shaders/Builtin/Functions/writeLogZ.glsl @@ -5,9 +5,7 @@ varying float v_logZ; void czm_writeLogZ(float logZ) { #if defined(GL_EXT_frag_depth) && defined(LOG_DEPTH) - float Fcoef = 2.0 / log2(czm_currentFrustum.y + 1.0); - float Fcoef_half = 0.5 * Fcoef; - gl_FragDepthEXT = log2(logZ) * Fcoef_half; + gl_FragDepthEXT = log2(logZ) / log2(czm_currentFrustum.y + 1.0); #endif } From 9cea9953b36bec27aad533228aba5d608fae407a Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 2 Mar 2018 14:58:18 -0500 Subject: [PATCH 37/68] Fix log depth issue with the skip LOD optimization. --- Source/Renderer/AutomaticUniforms.js | 14 +++++++++++ Source/Renderer/UniformState.js | 14 +++++++++++ Source/Scene/Cesium3DTileBatchTable.js | 24 +++++++++++++++++-- .../Shaders/Builtin/Functions/vertexLogZ.glsl | 3 +-- .../Shaders/Builtin/Functions/writeLogZ.glsl | 5 ++-- 5 files changed, 54 insertions(+), 6 deletions(-) diff --git a/Source/Renderer/AutomaticUniforms.js b/Source/Renderer/AutomaticUniforms.js index 3e218afd105e..d1ce1890eb39 100644 --- a/Source/Renderer/AutomaticUniforms.js +++ b/Source/Renderer/AutomaticUniforms.js @@ -1121,6 +1121,20 @@ define([ } }), + /** + * The log of the current frustums far plane. Used for computing the log depth. + * + * @alias czm_logFarDistance + * @glslUniform + */ + czm_logFarDistance : new AutomaticUniform({ + size : 1, + datatype : WebGLConstants.FLOAT, + getValue : function(uniformState) { + return uniformState.logFarDistance; + } + }), + /** * An automatic GLSL uniform representing the sun position in world coordinates. * diff --git a/Source/Renderer/UniformState.js b/Source/Renderer/UniformState.js index 89c87d7a88b8..f2d8c5875054 100644 --- a/Source/Renderer/UniformState.js +++ b/Source/Renderer/UniformState.js @@ -61,6 +61,7 @@ define([ this._entireFrustum = new Cartesian2(); this._currentFrustum = new Cartesian2(); this._frustumPlanes = new Cartesian4(); + this._logFarDistance = undefined; this._frameState = undefined; this._temeToPseudoFixed = Matrix3.clone(Matrix4.IDENTITY); @@ -641,6 +642,17 @@ define([ } }, + /** + * The log of the current frustum's far distance. Used to compute the log depth. + * @memberof UniformState.prototype + * @type {Number} + */ + logFarDistance : { + get : function() { + return this._logFarDistance; + } + }, + /** * The the height (x) and the height squared (y) * in meters of the camera above the 2D world plane. This uniform is only valid @@ -975,6 +987,8 @@ define([ this._currentFrustum.x = frustum.near; this._currentFrustum.y = frustum.far; + this._logFarDistance = 2.0 / Math.log2(frustum.far + 1.0); + if (defined(frustum._offCenterFrustum)) { frustum = frustum._offCenterFrustum; } diff --git a/Source/Scene/Cesium3DTileBatchTable.js b/Source/Scene/Cesium3DTileBatchTable.js index 2273cdd0f2d0..732b4d7b74c1 100644 --- a/Source/Scene/Cesium3DTileBatchTable.js +++ b/Source/Scene/Cesium3DTileBatchTable.js @@ -1347,7 +1347,7 @@ define([ if (bivariateVisibilityTest) { if (command.pass !== Pass.TRANSLUCENT && !finalResolution) { if (!defined(derivedCommands.zback)) { - derivedCommands.zback = deriveZBackfaceCommand(derivedCommands.originalCommand); + derivedCommands.zback = deriveZBackfaceCommand(frameState.context, derivedCommands.originalCommand); } tileset._backfaceCommands.push(derivedCommands.zback); } @@ -1432,7 +1432,24 @@ define([ return derivedCommand; } - function deriveZBackfaceCommand(command) { + function getDisableLogDepthFragmentShaderProgram(context, shaderProgram) { + var shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, 'zBackfaceLogDepth'); + if (!defined(shader)) { + var fs = shaderProgram.fragmentShaderSource.clone(); + fs.defines = defined(fs.defines) ? fs.defines.slice(0) : []; + fs.defines.push('DISABLE_LOG_DEPTH_FRAGMENT_WRITE'); + + shader = context.shaderCache.createDerivedShaderProgram(shaderProgram, 'zBackfaceLogDepth', { + vertexShaderSource : shaderProgram.vertexShaderSource, + fragmentShaderSource : fs, + attributeLocations : shaderProgram._attributeLocations + }); + } + + return shader; + } + + function deriveZBackfaceCommand(context, command) { // Write just backface depth of unresolved tiles so resolved stenciled tiles do not appear in front var derivedCommand = DrawCommand.shallowClone(command); var rs = clone(derivedCommand.renderState, true); @@ -1455,6 +1472,9 @@ define([ derivedCommand.renderState = RenderState.fromCache(rs); derivedCommand.castShadows = false; derivedCommand.receiveShadows = false; + // Disable the depth writes in the fragment shader. The back face commands were causing the higher resolution + // tiles to disappear. + derivedCommand.shaderProgram = getDisableLogDepthFragmentShaderProgram(context, command.shaderProgram); return derivedCommand; } diff --git a/Source/Shaders/Builtin/Functions/vertexLogZ.glsl b/Source/Shaders/Builtin/Functions/vertexLogZ.glsl index f5e2134712da..3f4f517a5e0b 100644 --- a/Source/Shaders/Builtin/Functions/vertexLogZ.glsl +++ b/Source/Shaders/Builtin/Functions/vertexLogZ.glsl @@ -3,8 +3,7 @@ varying float v_logZ; #endif void czm_updateZ() { - float Fcoef = 2.0 / log2(czm_currentFrustum.y + 1.0); - gl_Position.z = log2(max(1e-6, 1.0 + gl_Position.w)) * Fcoef - 1.0; + gl_Position.z = log2(max(1e-6, 1.0 + gl_Position.w)) * czm_logFarDistance - 1.0; gl_Position.z *= gl_Position.w; } diff --git a/Source/Shaders/Builtin/Functions/writeLogZ.glsl b/Source/Shaders/Builtin/Functions/writeLogZ.glsl index 626ea2386898..6cd5da08d3b6 100644 --- a/Source/Shaders/Builtin/Functions/writeLogZ.glsl +++ b/Source/Shaders/Builtin/Functions/writeLogZ.glsl @@ -4,8 +4,9 @@ varying float v_logZ; void czm_writeLogZ(float logZ) { -#if defined(GL_EXT_frag_depth) && defined(LOG_DEPTH) - gl_FragDepthEXT = log2(logZ) / log2(czm_currentFrustum.y + 1.0); +#if defined(GL_EXT_frag_depth) && defined(LOG_DEPTH) && !defined(DISABLE_LOG_DEPTH_FRAGMENT_WRITE) + float halfLogFarDistance = czm_logFarDistance * 0.5; + gl_FragDepthEXT = log2(logZ) * halfLogFarDistance; #endif } From 122ca66fa1789149c856b2bf3370ffa34abf6c0b Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 2 Mar 2018 18:17:42 -0500 Subject: [PATCH 38/68] Fix shadows. --- Source/Scene/ShadowMapShader.js | 19 ++++++++++++++++--- .../Shaders/Builtin/Functions/vertexLogZ.glsl | 4 ++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Source/Scene/ShadowMapShader.js b/Source/Scene/ShadowMapShader.js index 8f2142b28226..d130f340ffc8 100644 --- a/Source/Scene/ShadowMapShader.js +++ b/Source/Scene/ShadowMapShader.js @@ -202,17 +202,30 @@ define([ fsSource += 'uniform sampler2D shadowMap_texture; \n'; } + var returnPositionEC; + if (hasPositionVarying) { + returnPositionEC = ' return vec4(' + positionVaryingName + ', 1.0); \n'; + } else { + returnPositionEC = + '#ifndef LOG_DEPTH \n' + + ' return czm_windowToEyeCoordinates(gl_FragCoord); \n' + + '#else \n' + + ' return czm_windowToEyeCoordinates(v_glPosition); \n' + + '#endif \n'; + } + fsSource += 'uniform mat4 shadowMap_matrix; \n' + 'uniform vec3 shadowMap_lightDirectionEC; \n' + 'uniform vec4 shadowMap_lightPositionEC; \n' + 'uniform vec4 shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness; \n' + 'uniform vec4 shadowMap_texelSizeDepthBiasAndNormalShadingSmooth; \n' + + '#ifdef LOG_DEPTH \n' + + 'varying vec4 v_glPosition; \n' + + '#endif \n' + 'vec4 getPositionEC() \n' + '{ \n' + - (hasPositionVarying ? - ' return vec4(' + positionVaryingName + ', 1.0); \n' : - ' return czm_windowToEyeCoordinates(gl_FragCoord); \n') + + returnPositionEC + '} \n' + 'vec3 getNormalEC() \n' + '{ \n' + diff --git a/Source/Shaders/Builtin/Functions/vertexLogZ.glsl b/Source/Shaders/Builtin/Functions/vertexLogZ.glsl index 3f4f517a5e0b..c074887512ef 100644 --- a/Source/Shaders/Builtin/Functions/vertexLogZ.glsl +++ b/Source/Shaders/Builtin/Functions/vertexLogZ.glsl @@ -1,10 +1,14 @@ #ifdef LOG_DEPTH varying float v_logZ; +varying vec4 v_glPosition; #endif void czm_updateZ() { +#ifdef LOG_DEPTH + v_glPosition = gl_Position; gl_Position.z = log2(max(1e-6, 1.0 + gl_Position.w)) * czm_logFarDistance - 1.0; gl_Position.z *= gl_Position.w; +#endif } void czm_vertexLogZ() From bcc2dc4606d29e6b37c9e0022f06fc7e8b1bf32e Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Mon, 5 Mar 2018 15:08:03 -0500 Subject: [PATCH 39/68] Updated CHANGES.md --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 207c8089940e..4310f054c67e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,7 @@ Change Log ##### Fixes :wrench: * Fixed support of glTF-supplied tangent vectors. [#6302](https://github.com/AnalyticalGraphicsInc/cesium/pull/6302) +* Added option `logDepthBuffer` to `Viewer`. With this option the globe is typically rendered in a single frustum using logarithmic depth. This helps to avoid artifacts on the connection of two frustums. [#5851](https://github.com/AnalyticalGraphicsInc/cesium/pull/5851) ### 1.43 - 2018-03-01 @@ -215,7 +216,6 @@ _This is an npm-only release to fix an issue with using Cesium in Node.js.__ * Fixed loading of binary glTFs containing CRN or KTX textures. [#5753](https://github.com/AnalyticalGraphicsInc/cesium/pull/5753) * Fixed specular computation for certain models using the `KHR_materials_common` extension. [#5773](https://github.com/AnalyticalGraphicsInc/cesium/pull/5773) * Fixed a picking bug in the `3D Tiles Interactivity` Sandcastle demo. [#5703](https://github.com/AnalyticalGraphicsInc/cesium/issues/5703) -* Added option "logDepthBuffer" to Cesium.Viewer. With this option globe is rendered in 1 frustum with logarithmic depth. It helps to avoid artifacts on the connection of two frustums. * Updated knockout from 3.4.0 to 3.4.2 [#5703](https://github.com/AnalyticalGraphicsInc/cesium/pull/5829) ### 1.36 - 2017-08-01 From 4ee738d05f7feef8b4c503a715ebb2d4f80e88d9 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 5 Mar 2018 15:52:37 -0500 Subject: [PATCH 40/68] Fix 2D. Update some tests. --- Source/Scene/Scene.js | 4 +- Specs/DataSources/EntityClusterSpec.js | 54 +++++++++++++------------- Specs/DataSources/KmlDataSourceSpec.js | 9 +++-- Specs/Scene/MultifrustumSpec.js | 11 +++++- 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index f0b92fdeced8..53e502f86bfc 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -1443,6 +1443,8 @@ define([ derivedCommands.logDepth = createLogDepthCommand(command, context, derivedCommands.logDepth); logDepthCommand = derivedCommands.logDepth.logDepthCommand; logDepthDerivedCommands = logDepthCommand.derivedCommands; + } else { + derivedCommands.logDepth = undefined; } if (scene.frameState.passes.pick) { @@ -3097,7 +3099,7 @@ define([ tryAndCatchError(this, time, update); this._postUpdate.raiseEvent(this, time); - this._frustumChanged = this._camera !== this._cameraClone.frustum; + this._frustumChanged = this._camera.frustum !== this._cameraClone.frustum; if (this._frustumChanged && this._logDepthBuffer && !(this._camera.frustum instanceof OrthographicFrustum || this._camera.frustum instanceof OrthographicOffCenterFrustum)) { this._camera.frustum.near = 1.0; this._camera.frustum.far = 10000000000.0; diff --git a/Specs/DataSources/EntityClusterSpec.js b/Specs/DataSources/EntityClusterSpec.js index e73142fb4b39..5cdad2ac453e 100644 --- a/Specs/DataSources/EntityClusterSpec.js +++ b/Specs/DataSources/EntityClusterSpec.js @@ -31,6 +31,8 @@ defineSuite([ var scene; var cluster; + var depth = 0.1; + beforeAll(function() { scene = createScene({ canvas : createCanvas(10, 10) @@ -151,13 +153,13 @@ defineSuite([ var billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); entity = new Entity(); billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); var frameState = scene.frameState; cluster.update(frameState); @@ -184,13 +186,13 @@ defineSuite([ var billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); entity = new Entity(); billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); cluster.enabled = true; cluster.update(scene.frameState); @@ -207,13 +209,13 @@ defineSuite([ var label = cluster.getLabel(entity); label.id = entity; label.text = 'a'; - label.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5); + label.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); entity = new Entity(); label = cluster.getLabel(entity); label.id = entity; label.text = 'b'; - label.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5); + label.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); var frameState = scene.frameState; cluster.update(frameState); @@ -240,13 +242,13 @@ defineSuite([ var label = cluster.getLabel(entity); label.id = entity; label.text = 'a'; - label.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5); + label.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); entity = new Entity(); label = cluster.getLabel(entity); label.id = entity; label.text = 'b'; - label.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5); + label.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); cluster.enabled = true; cluster.update(scene.frameState); @@ -263,13 +265,13 @@ defineSuite([ var point = cluster.getPoint(entity); point.id = entity; point.pixelSize = 1; - point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5); + point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); entity = new Entity(); point = cluster.getPoint(entity); point.id = entity; point.pixelSize = 1; - point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5); + point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); var frameState = scene.frameState; cluster.update(frameState); @@ -296,13 +298,13 @@ defineSuite([ var point = cluster.getPoint(entity); point.id = entity; point.pixelSize = 1; - point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5); + point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); entity = new Entity(); point = cluster.getPoint(entity); point.id = entity; point.pixelSize = 1; - point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5); + point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); cluster.enabled = true; cluster.update(scene.frameState); @@ -319,13 +321,13 @@ defineSuite([ var point = cluster.getPoint(entity); point.id = entity; point.pixelSize = 1; - point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5); + point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); entity = new Entity(); point = cluster.getPoint(entity); point.id = entity; point.pixelSize = 1; - point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5); + point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); var frameState = scene.frameState; cluster.update(frameState); @@ -428,13 +430,13 @@ defineSuite([ var billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); entity = new Entity(); billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); var frameState = scene.frameState; cluster.update(frameState); @@ -461,25 +463,25 @@ defineSuite([ var billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.5); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); entity = new Entity(); billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, 0), 0.5); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, 0), depth); entity = new Entity(); billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0, scene.canvas.clientHeight), 0.5); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0, scene.canvas.clientHeight), depth); entity = new Entity(); billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.5); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); var frameState = scene.frameState; cluster.update(frameState); @@ -506,13 +508,13 @@ defineSuite([ var billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.9); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); entity = new Entity(); billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.9); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); var frameState = scene.frameState; cluster.update(frameState); @@ -550,13 +552,13 @@ defineSuite([ var point = cluster.getPoint(entity); point.id = entity; point.pixelSize = 1; - point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.9); + point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); entity = new Entity(); point = cluster.getPoint(entity); point.id = entity; point.pixelSize = 1; - point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.9); + point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); var frameState = scene.frameState; cluster.update(frameState); @@ -587,7 +589,7 @@ defineSuite([ var entityCollection = dataSource.entities; entityCollection.add({ - position : SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), 0.9), + position : SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth), billboard : { image : createBillboardImage() }, @@ -597,7 +599,7 @@ defineSuite([ }); entityCollection.add({ - position : SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), 0.9), + position : SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth), billboard : { image : createBillboardImage() }, diff --git a/Specs/DataSources/KmlDataSourceSpec.js b/Specs/DataSources/KmlDataSourceSpec.js index 909c02ab113f..6e2b3562d879 100644 --- a/Specs/DataSources/KmlDataSourceSpec.js +++ b/Specs/DataSources/KmlDataSourceSpec.js @@ -19,6 +19,7 @@ defineSuite([ 'Core/JulianDate', 'Core/Math', 'Core/NearFarScalar', + 'Core/PerspectiveFrustum', 'Core/Rectangle', 'Core/RequestErrorEvent', 'Core/Resource', @@ -57,6 +58,7 @@ defineSuite([ JulianDate, CesiumMath, NearFarScalar, + PerspectiveFrustum, Rectangle, RequestErrorEvent, Resource, @@ -136,10 +138,7 @@ defineSuite([ upWC : new Cartesian3(0.0, 1.0, 0.0), pitch : 0.0, heading : 0.0, - frustum : { - aspectRatio : 1.0, - fov : CesiumMath.PI_OVER_FOUR - }, + frustum : new PerspectiveFrustum(), computeViewRectangle : function() { return Rectangle.MAX_VALUE; }, @@ -152,6 +151,8 @@ defineSuite([ clientHeight : 512 } }; + options.camera.frustum.fov = CesiumMath.PI_OVER_FOUR; + options.camera.frustum.aspectRatio = 1.0; beforeEach(function() { // Reset camera - x value will change during onStop tests diff --git a/Specs/Scene/MultifrustumSpec.js b/Specs/Scene/MultifrustumSpec.js index 9bf98da5430e..8c7da972d5f7 100644 --- a/Specs/Scene/MultifrustumSpec.js +++ b/Specs/Scene/MultifrustumSpec.js @@ -205,7 +205,16 @@ defineSuite([ expect(billboardCall).toBeDefined(); expect(billboardCall.args.length).toEqual(2); - expect(billboardCall.object.shaderProgram.fragmentShaderSource.sources[1]).toContain('czm_Debug_main'); + + var found = false; + var sources = billboardCall.object.shaderProgram.fragmentShaderSource.sources; + for (var j = 0; j < sources.length; ++j) { + if (sources[i].indexOf('czm_Debug_main') !== -1) { + found = true; + break; + } + } + expect(found).toBe(true); }); function createPrimitive(bounded, closestFrustum) { From 0d90d985b0179003b2ea66ead964331d8e07b69a Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 6 Mar 2018 15:24:42 -0500 Subject: [PATCH 41/68] Fix a few tests and updates from review. --- Source/Core/Math.js | 12 ++++++++++++ Source/Renderer/AutomaticUniforms.js | 2 ++ Source/Renderer/UniformState.js | 2 +- Source/Scene/Camera.js | 11 ++++++++--- Source/Scene/Expression.js | 2 +- Source/Scene/SceneTransforms.js | 2 +- Source/Shaders/DepthPlaneFS.glsl | 1 - Specs/Scene/ClassificationPrimitiveSpec.js | 6 ++++-- 8 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Source/Core/Math.js b/Source/Core/Math.js index 04631d636090..a498300c9ac6 100644 --- a/Source/Core/Math.js +++ b/Source/Core/Math.js @@ -849,6 +849,18 @@ define([ */ CesiumMath.cbrt = defined(Math.cbrt) ? Math.cbrt : cbrt; + function log2(x) { + return Math.log(x) * Math.LOG2E; + } + + /** + * Finds the base 2 logarithm of a number. + * + * @param {Number} number The number. + * @returns {Number} The result. + */ + CesiumMath.log2 = defined(Math.log2) ? Math.log2 : log2; + /** * @private */ diff --git a/Source/Renderer/AutomaticUniforms.js b/Source/Renderer/AutomaticUniforms.js index d1ce1890eb39..82c0b64ad9fd 100644 --- a/Source/Renderer/AutomaticUniforms.js +++ b/Source/Renderer/AutomaticUniforms.js @@ -1126,6 +1126,8 @@ define([ * * @alias czm_logFarDistance * @glslUniform + * + * @private */ czm_logFarDistance : new AutomaticUniform({ size : 1, diff --git a/Source/Renderer/UniformState.js b/Source/Renderer/UniformState.js index f2d8c5875054..27ced786160e 100644 --- a/Source/Renderer/UniformState.js +++ b/Source/Renderer/UniformState.js @@ -987,7 +987,7 @@ define([ this._currentFrustum.x = frustum.near; this._currentFrustum.y = frustum.far; - this._logFarDistance = 2.0 / Math.log2(frustum.far + 1.0); + this._logFarDistance = 2.0 / CesiumMath.log2(frustum.far + 1.0); if (defined(frustum._offCenterFrustum)) { frustum = frustum._offCenterFrustum; diff --git a/Source/Scene/Camera.js b/Source/Scene/Camera.js index 50b48178c9d3..842055d8bbc0 100644 --- a/Source/Scene/Camera.js +++ b/Source/Scene/Camera.js @@ -2577,7 +2577,7 @@ define([ * Return the distance from the camera to the front of the bounding sphere. * * @param {BoundingSphere} boundingSphere The bounding sphere in world coordinates. - * @returns {Number} The distance to the bounding sphere. + * @returns {Number} The signed distance to the bounding sphere. */ Camera.prototype.distanceToBoundingSphere = function(boundingSphere) { //>>includeStart('debug', pragmas.debug); @@ -2587,8 +2587,10 @@ define([ //>>includeEnd('debug'); var toCenter = Cartesian3.subtract(this.positionWC, boundingSphere.center, scratchToCenter); - var proj = Cartesian3.multiplyByScalar(this.directionWC, Cartesian3.dot(toCenter, this.directionWC), scratchProj); - return Math.max(0.0, Cartesian3.magnitude(proj) - boundingSphere.radius); + var distance = -Cartesian3.dot(toCenter, this.directionWC); + var proj = Cartesian3.multiplyByScalar(this.directionWC, distance, scratchProj); + var unsignedDistance = Math.max(0.0, Cartesian3.magnitude(proj) - boundingSphere.radius); + return distance < 0.0 ? -unsignedDistance : unsignedDistance; }; var scratchPixelSize = new Cartesian2(); @@ -2615,6 +2617,9 @@ define([ //>>includeEnd('debug'); var distance = this.distanceToBoundingSphere(boundingSphere); + if (distance < 0.0) { + return 0.0; + } var pixelSize = this.frustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, scratchPixelSize); return Math.max(pixelSize.x, pixelSize.y); }; diff --git a/Source/Scene/Expression.js b/Source/Scene/Expression.js index bdc718663116..32c8f98f97ab 100644 --- a/Source/Scene/Expression.js +++ b/Source/Scene/Expression.js @@ -279,7 +279,7 @@ define([ } function log2(number) { - return CesiumMath.logBase(number, 2.0); + return CesiumMath.log2(number, 2.0); } function getEvaluateUnaryComponentwise(operation) { diff --git a/Source/Scene/SceneTransforms.js b/Source/Scene/SceneTransforms.js index 9eb26db93d5d..ce8688e31204 100644 --- a/Source/Scene/SceneTransforms.js +++ b/Source/Scene/SceneTransforms.js @@ -311,7 +311,7 @@ define([ // log2(z + 1) / log2( far + 1); // to perspective form // (far - far * near / z) / (far - near) - depth = Math.pow(2.0, depth * Math.log2(far + 1.0)) - 1.0; + depth = Math.pow(2.0, depth * CesiumMath.log2(far + 1.0)) - 1.0; depth = far * (1.0 - 1.0 / depth) / (far - near); } diff --git a/Source/Shaders/DepthPlaneFS.glsl b/Source/Shaders/DepthPlaneFS.glsl index 53ebb87a9fc3..142bd49dd14a 100644 --- a/Source/Shaders/DepthPlaneFS.glsl +++ b/Source/Shaders/DepthPlaneFS.glsl @@ -1,5 +1,4 @@ varying vec4 positionEC; -varying float flogz; void main() { diff --git a/Specs/Scene/ClassificationPrimitiveSpec.js b/Specs/Scene/ClassificationPrimitiveSpec.js index 959a793d14d7..f4dfa7a5e3ba 100644 --- a/Specs/Scene/ClassificationPrimitiveSpec.js +++ b/Specs/Scene/ClassificationPrimitiveSpec.js @@ -326,7 +326,9 @@ defineSuite([ verifyClassificationPrimitiveRender(primitive, boxColor); }); - it('renders in Columbus view when scene3DOnly is false', function() { + // Rendering in 2D/CV is broken: + // https://github.com/AnalyticalGraphicsInc/cesium/issues/6308 + xit('renders in Columbus view when scene3DOnly is false', function() { if (!ClassificationPrimitive.isSupported(scene)) { return; } @@ -340,7 +342,7 @@ defineSuite([ verifyClassificationPrimitiveRender(primitive, boxColor); }); - it('renders in 2D when scene3DOnly is false', function() { + xit('renders in 2D when scene3DOnly is false', function() { if (!ClassificationPrimitive.isSupported(scene)) { return; } From 22ecbe674fc0dc1e72ae25f274af4e10b19a88e4 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Wed, 7 Mar 2018 17:05:48 -0500 Subject: [PATCH 42/68] Add option to disable log depth. Fix frustum equality and frustum changed flag. --- Source/Core/OrthographicFrustum.js | 2 +- Source/Core/OrthographicOffCenterFrustum.js | 2 +- Source/Core/PerspectiveFrustum.js | 2 +- Source/Core/PerspectiveOffCenterFrustum.js | 2 +- Source/Scene/Scene.js | 24 ++++++++++-- Source/Shaders/EllipsoidFS.glsl | 15 ++++++++ .../PointCloudEyeDomeLighting.glsl | 37 ++++++++++--------- Source/Shaders/Vector3DTilePolylinesVS.glsl | 2 +- 8 files changed, 61 insertions(+), 25 deletions(-) diff --git a/Source/Core/OrthographicFrustum.js b/Source/Core/OrthographicFrustum.js index b3377b99fde1..b2dd5ef7e074 100644 --- a/Source/Core/OrthographicFrustum.js +++ b/Source/Core/OrthographicFrustum.js @@ -259,7 +259,7 @@ define([ * @returns {Boolean} true if they are equal, false otherwise. */ OrthographicFrustum.prototype.equals = function(other) { - if (!defined(other)) { + if (!defined(other) || !(other instanceof OrthographicFrustum)) { return false; } diff --git a/Source/Core/OrthographicOffCenterFrustum.js b/Source/Core/OrthographicOffCenterFrustum.js index 63dd0c40dfbf..09877fba03b2 100644 --- a/Source/Core/OrthographicOffCenterFrustum.js +++ b/Source/Core/OrthographicOffCenterFrustum.js @@ -361,7 +361,7 @@ define([ * @returns {Boolean} true if they are equal, false otherwise. */ OrthographicOffCenterFrustum.prototype.equals = function(other) { - return (defined(other) && + return (defined(other) && other instanceof OrthographicOffCenterFrustum && this.right === other.right && this.left === other.left && this.top === other.top && diff --git a/Source/Core/PerspectiveFrustum.js b/Source/Core/PerspectiveFrustum.js index 5926554f69dc..3186170bbd77 100644 --- a/Source/Core/PerspectiveFrustum.js +++ b/Source/Core/PerspectiveFrustum.js @@ -354,7 +354,7 @@ define([ * @returns {Boolean} true if they are equal, false otherwise. */ PerspectiveFrustum.prototype.equals = function(other) { - if (!defined(other)) { + if (!defined(other) || !(other instanceof PerspectiveFrustum)) { return false; } diff --git a/Source/Core/PerspectiveOffCenterFrustum.js b/Source/Core/PerspectiveOffCenterFrustum.js index 7b031d85e77c..30319a5b2556 100644 --- a/Source/Core/PerspectiveOffCenterFrustum.js +++ b/Source/Core/PerspectiveOffCenterFrustum.js @@ -412,7 +412,7 @@ define([ * @returns {Boolean} true if they are equal, false otherwise. */ PerspectiveOffCenterFrustum.prototype.equals = function(other) { - return (defined(other) && + return (defined(other) && other instanceof PerspectiveOffCenterFrustum && this.right === other.right && this.left === other.left && this.top === other.top && diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 53e502f86bfc..08cffe66c3b2 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -266,6 +266,7 @@ define([ } this._logDepthBuffer = context.fragmentDepth; + this._logDepthBufferChanged = true; this._id = createGuid(); this._jobScheduler = new JobScheduler(); @@ -1379,6 +1380,22 @@ define([ //>>includeEnd('debug'); this._minimumDisableDepthTestDistance = value; } + }, + + /** + * Whether or not to use a logarithmic depth buffer. Enabling this option will allow for less frustums in the multi-frustum, + * increasing performance. This property relies on {@link Context#fragmentDepth} being supported. + */ + logDepthBuffer : { + get : function() { + return this._logDepthBuffer; + }, + set : function(value) { + if (this._context.fragmentDepth && this._logDepthBuffer !== value) { + this._logDepthBuffer = value; + this._logDepthBufferChanged = true; + } + } } }); @@ -1432,7 +1449,7 @@ define([ } var derivedCommands = command.derivedCommands; - if ((scene._frustumChanged || command.dirty) && defined(derivedCommands)) { + if ((scene._logDepthBufferChanged || scene._frustumChanged || command.dirty) && defined(derivedCommands)) { command.dirty = false; var frustum = scene.camera.frustum; @@ -3099,14 +3116,14 @@ define([ tryAndCatchError(this, time, update); this._postUpdate.raiseEvent(this, time); - this._frustumChanged = this._camera.frustum !== this._cameraClone.frustum; + this._frustumChanged = !this._camera.frustum.equals(this._cameraClone.frustum); if (this._frustumChanged && this._logDepthBuffer && !(this._camera.frustum instanceof OrthographicFrustum || this._camera.frustum instanceof OrthographicOffCenterFrustum)) { this._camera.frustum.near = 1.0; this._camera.frustum.far = 10000000000.0; } var cameraChanged = checkForCameraUpdates(this); - var shouldRender = !this.requestRenderMode || this._renderRequested || cameraChanged || this._frustumChanged || (this.mode === SceneMode.MORPHING); + var shouldRender = !this.requestRenderMode || this._renderRequested || cameraChanged || this._frustumChanged || this._logDepthBufferChanged || (this.mode === SceneMode.MORPHING); if (!shouldRender && defined(this.maximumRenderTimeChange) && defined(this._lastRenderTime)) { var difference = Math.abs(JulianDate.secondsDifference(this._lastRenderTime, time)); shouldRender = shouldRender || difference > this.maximumRenderTimeChange; @@ -3115,6 +3132,7 @@ define([ if (shouldRender) { this._lastRenderTime = JulianDate.clone(time, this._lastRenderTime); this._renderRequested = false; + this._logDepthBufferChanged = false; // Render this._preRender.raiseEvent(this, time); diff --git a/Source/Shaders/EllipsoidFS.glsl b/Source/Shaders/EllipsoidFS.glsl index a0da7525f0b8..ab29498065a4 100644 --- a/Source/Shaders/EllipsoidFS.glsl +++ b/Source/Shaders/EllipsoidFS.glsl @@ -1,3 +1,9 @@ +#ifdef WRITE_DEPTH +#ifdef GL_EXT_frag_depth +#extension GL_EXT_frag_depth : enable +#endif +#endif + uniform vec3 u_radii; uniform vec3 u_oneOverEllipsoidRadiiSquared; @@ -90,7 +96,16 @@ void main() t = (intersection.start != 0.0) ? intersection.start : intersection.stop; vec3 positionEC = czm_pointAlongRay(ray, t); vec4 positionCC = czm_projection * vec4(positionEC, 1.0); +#ifdef LOG_DEPTH czm_writeLogZ(1.0 + positionCC.w); +#else + float z = positionCC.z / positionCC.w; + + float n = czm_depthRange.near; + float f = czm_depthRange.far; + + gl_FragDepthEXT = (z * (f - n) + f + n) * 0.5; +#endif #endif #endif } diff --git a/Source/Shaders/PostProcessFilters/PointCloudEyeDomeLighting.glsl b/Source/Shaders/PostProcessFilters/PointCloudEyeDomeLighting.glsl index 9a6f86d448f5..e24bb51e64ad 100644 --- a/Source/Shaders/PostProcessFilters/PointCloudEyeDomeLighting.glsl +++ b/Source/Shaders/PostProcessFilters/PointCloudEyeDomeLighting.glsl @@ -25,25 +25,28 @@ void main() { discard; } - else - { - vec4 color = texture2D(u_pointCloud_colorTexture, v_textureCoordinates); - // sample from neighbors up, down, left, right - float distX = u_distancesAndEdlStrength.x; - float distY = u_distancesAndEdlStrength.y; + vec4 color = texture2D(u_pointCloud_colorTexture, v_textureCoordinates); - vec2 responseAndCount = vec2(0.0); + // sample from neighbors up, down, left, right + float distX = u_distancesAndEdlStrength.x; + float distY = u_distancesAndEdlStrength.y; - responseAndCount += neighborContribution(ecAlphaDepth.a, vec2(0, distY)); - responseAndCount += neighborContribution(ecAlphaDepth.a, vec2(distX, 0)); - responseAndCount += neighborContribution(ecAlphaDepth.a, vec2(0, -distY)); - responseAndCount += neighborContribution(ecAlphaDepth.a, vec2(-distX, 0)); + vec2 responseAndCount = vec2(0.0); - float response = responseAndCount.x / responseAndCount.y; - float shade = exp(-response * 300.0 * u_distancesAndEdlStrength.z); - color.rgb *= shade; - gl_FragColor = vec4(color); - gl_FragDepthEXT = czm_eyeToWindowCoordinates(vec4(ecAlphaDepth.xyz, 1.0)).z; - } + responseAndCount += neighborContribution(ecAlphaDepth.a, vec2(0, distY)); + responseAndCount += neighborContribution(ecAlphaDepth.a, vec2(distX, 0)); + responseAndCount += neighborContribution(ecAlphaDepth.a, vec2(0, -distY)); + responseAndCount += neighborContribution(ecAlphaDepth.a, vec2(-distX, 0)); + + float response = responseAndCount.x / responseAndCount.y; + float shade = exp(-response * 300.0 * u_distancesAndEdlStrength.z); + color.rgb *= shade; + gl_FragColor = vec4(color); + +#ifdef LOG_DEPTH + czm_writeLogZ(1.0 + (czm_projection * vec4(ecAlphaDepth.xyz, 1.0)).w); +#else + gl_FragDepthEXT = czm_eyeToWindowCoordinates(vec4(ecAlphaDepth.xyz, 1.0)).z; +#endif } diff --git a/Source/Shaders/Vector3DTilePolylinesVS.glsl b/Source/Shaders/Vector3DTilePolylinesVS.glsl index 111bafdc1f7f..dc869998cdc7 100644 --- a/Source/Shaders/Vector3DTilePolylinesVS.glsl +++ b/Source/Shaders/Vector3DTilePolylinesVS.glsl @@ -21,6 +21,6 @@ void main() gl_Position = czm_viewportOrthographic * positionWC; #ifdef LOG_DEPTH - czm_vertexLogZ(czm_projection * u_modifiedModelView * currentPosition); + czm_vertexLogZ(czm_projection * p); #endif } From e72c849d9489e92de35b4866c2b9c649924e88fd Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 8 Mar 2018 17:38:25 -0500 Subject: [PATCH 43/68] Fix billboards not being occluded by the near plane. Fix more tests. --- Source/Shaders/BillboardCollectionVS.glsl | 8 ++++---- Source/Shaders/PointPrimitiveCollectionVS.glsl | 9 ++++----- Specs/Scene/BillboardCollectionSpec.js | 2 +- Specs/Scene/LabelCollectionSpec.js | 12 ++++++++---- Specs/Scene/PointPrimitiveCollectionSpec.js | 2 +- Specs/Scene/SceneSpec.js | 15 +++++++++++++++ 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Source/Shaders/BillboardCollectionVS.glsl b/Source/Shaders/BillboardCollectionVS.glsl index 0b0d469b582c..023ceb06bad1 100644 --- a/Source/Shaders/BillboardCollectionVS.glsl +++ b/Source/Shaders/BillboardCollectionVS.glsl @@ -255,14 +255,14 @@ void main() } #endif +#ifdef LOG_DEPTH + czm_vertexLogZ(czm_projection * positionEC); +#endif + vec4 positionWC = computePositionWindowCoordinates(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters); gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); v_textureCoordinates = textureCoordinates; -#ifdef LOG_DEPTH - czm_vertexLogZ(czm_modelViewProjectionRelativeToEye * p); -#endif - #ifdef DISABLE_DEPTH_DISTANCE float disableDepthTestDistance = distanceDisplayConditionAndDisableDepth.z; if (disableDepthTestDistance == 0.0 && czm_minimumDisableDepthTestDistance != 0.0) diff --git a/Source/Shaders/PointPrimitiveCollectionVS.glsl b/Source/Shaders/PointPrimitiveCollectionVS.glsl index d4d7cba61c6e..ffd1a9f59d72 100644 --- a/Source/Shaders/PointPrimitiveCollectionVS.glsl +++ b/Source/Shaders/PointPrimitiveCollectionVS.glsl @@ -147,14 +147,13 @@ void main() } #endif - vec4 positionWC = czm_eyeToWindowCoordinates(positionEC); - - gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); - #ifdef LOG_DEPTH - czm_vertexLogZ(czm_modelViewProjectionRelativeToEye * p); + czm_vertexLogZ(czm_projection * positionEC); #endif + vec4 positionWC = czm_eyeToWindowCoordinates(positionEC); + gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); + #ifdef DISABLE_DEPTH_DISTANCE float disableDepthTestDistance = distanceDisplayConditionAndDisableDepth.z; if (disableDepthTestDistance == 0.0 && czm_minimumDisableDepthTestDistance != 0.0) diff --git a/Specs/Scene/BillboardCollectionSpec.js b/Specs/Scene/BillboardCollectionSpec.js index 2e7404ba4114..5f3af46dd85b 100644 --- a/Specs/Scene/BillboardCollectionSpec.js +++ b/Specs/Scene/BillboardCollectionSpec.js @@ -1401,7 +1401,7 @@ defineSuite([ var expected = BoundingSphere.fromPoints(projectedPositions); expected.center = new Cartesian3(0.0, expected.center.x, expected.center.y); expect(actual.center).toEqualEpsilon(expected.center, CesiumMath.EPSILON8); - expect(actual.radius).toBeGreaterThan(expected.radius); + expect(actual.radius).toBeGreaterThanOrEqualTo(expected.radius); }); it('computes bounding sphere in 2D', function() { diff --git a/Specs/Scene/LabelCollectionSpec.js b/Specs/Scene/LabelCollectionSpec.js index 251f2d23fbac..47a9876fdda0 100644 --- a/Specs/Scene/LabelCollectionSpec.js +++ b/Specs/Scene/LabelCollectionSpec.js @@ -554,15 +554,20 @@ defineSuite([ it('does not render labels that are behind the viewer', function() { var label = labels.add({ - position : new Cartesian3(20.0, 0.0, 0.0), // Behind camera + position : Cartesian3.ZERO, // in front of the camera text : 'x', horizontalOrigin : HorizontalOrigin.CENTER, verticalOrigin : VerticalOrigin.CENTER }); + expect(scene).toRenderAndCall(function(rgba) { + expect(rgba[0]).toBeGreaterThan(10); + }); + + label.position = new Cartesian3(20.0, 0.0, 0.0); // Behind camera expect(scene).toRender([0, 0, 0, 255]); - label.position = Cartesian3.ZERO; // Back in front of camera + label.position = new Cartesian3(1.0, 0.0, 0.0); // Back in front of camera expect(scene).toRenderAndCall(function(rgba) { expect(rgba[0]).toBeGreaterThan(10); }); @@ -2000,7 +2005,6 @@ defineSuite([ }); it('computes bounding sphere in Columbus view', function() { - // Disable collision detection to allow controlled camera position, // hence predictable bounding sphere size var originalEnableCollisionDetection = scene.screenSpaceCameraController.enableCollisionDetection; @@ -2030,7 +2034,7 @@ defineSuite([ var expected = BoundingSphere.fromPoints(projectedPositions); expected.center = new Cartesian3(0.0, expected.center.x, expected.center.y); expect(actual.center).toEqualEpsilon(expected.center, CesiumMath.EPSILON8); - expect(actual.radius).toBeGreaterThan(expected.radius); + expect(actual.radius).toBeGreaterThanOrEqualTo(expected.radius); scene.screenSpaceCameraController.enableCollisionDetection = originalEnableCollisionDetection; }); diff --git a/Specs/Scene/PointPrimitiveCollectionSpec.js b/Specs/Scene/PointPrimitiveCollectionSpec.js index 05d3deaec437..381908d6c52d 100644 --- a/Specs/Scene/PointPrimitiveCollectionSpec.js +++ b/Specs/Scene/PointPrimitiveCollectionSpec.js @@ -907,7 +907,7 @@ defineSuite([ var expected = BoundingSphere.fromPoints(projectedPositions); expected.center = new Cartesian3(0.0, expected.center.x, expected.center.y); expect(actual.center).toEqualEpsilon(expected.center, CesiumMath.EPSILON8); - expect(actual.radius).toBeGreaterThan(expected.radius); + expect(actual.radius).toBeGreaterThanOrEqualTo(expected.radius); }); it('computes bounding sphere in 2D', function() { diff --git a/Specs/Scene/SceneSpec.js b/Specs/Scene/SceneSpec.js index 43783f889c5b..8c852341361a 100644 --- a/Specs/Scene/SceneSpec.js +++ b/Specs/Scene/SceneSpec.js @@ -249,6 +249,9 @@ defineSuite([ }); it('debugCommandFilter does not filter commands', function() { + var originalLogDepth = scene.logDepthBuffer; + scene.logDepthBuffer = false; + var c = new DrawCommand({ shaderProgram : simpleShaderProgram, renderState : simpleRenderState, @@ -262,9 +265,14 @@ defineSuite([ expect(scene.debugCommandFilter).toBeUndefined(); scene.renderForSpecs(); expect(c.execute).toHaveBeenCalled(); + + scene.logDepthBuffer = originalLogDepth; }); it('debugShowBoundingVolume draws a bounding sphere', function() { + var originalLogDepth = scene.logDepthBuffer; + scene.logDepthBuffer = false; + var radius = 10.0; var center = Cartesian3.add(scene.camera.position, scene.camera.direction, new Cartesian3()); @@ -283,9 +291,14 @@ defineSuite([ expect(scene).toRenderAndCall(function(rgba) { expect(rgba[0]).not.toEqual(0); // Red bounding sphere }); + + scene.logDepthBuffer = originalLogDepth; }); it('debugShowCommands tints commands', function() { + var originalLogDepth = scene.logDepthBuffer; + scene.logDepthBuffer = false; + var c = new DrawCommand({ shaderProgram : simpleShaderProgram, renderState : simpleRenderState, @@ -306,6 +319,8 @@ defineSuite([ scene.renderForSpecs(); expect(c._debugColor).toBeDefined(); scene.debugShowCommands = false; + + scene.logDepthBuffer = originalLogDepth; }); it('debugShowFramesPerSecond', function() { From 3c9c92b9ec5f61ca5dc5aaa4821ef42f0eb93f40 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 9 Mar 2018 14:21:03 -0500 Subject: [PATCH 44/68] Fix shadow maps without varying eye coordinate. Fix tests. --- Source/Scene/ShadowMapShader.js | 4 ++-- Source/Shaders/Builtin/Functions/vertexLogZ.glsl | 5 +++-- Specs/Scene/ShadowMapSpec.js | 15 ++++++++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Source/Scene/ShadowMapShader.js b/Source/Scene/ShadowMapShader.js index d130f340ffc8..0cf1cd5cc45a 100644 --- a/Source/Scene/ShadowMapShader.js +++ b/Source/Scene/ShadowMapShader.js @@ -210,7 +210,7 @@ define([ '#ifndef LOG_DEPTH \n' + ' return czm_windowToEyeCoordinates(gl_FragCoord); \n' + '#else \n' + - ' return czm_windowToEyeCoordinates(v_glPosition); \n' + + ' return vec4(v_logPositionEC, 1.0); \n' + '#endif \n'; } @@ -221,7 +221,7 @@ define([ 'uniform vec4 shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness; \n' + 'uniform vec4 shadowMap_texelSizeDepthBiasAndNormalShadingSmooth; \n' + '#ifdef LOG_DEPTH \n' + - 'varying vec4 v_glPosition; \n' + + 'varying vec3 v_logPositionEC; \n' + '#endif \n' + 'vec4 getPositionEC() \n' + '{ \n' + diff --git a/Source/Shaders/Builtin/Functions/vertexLogZ.glsl b/Source/Shaders/Builtin/Functions/vertexLogZ.glsl index c074887512ef..5cbdd861b379 100644 --- a/Source/Shaders/Builtin/Functions/vertexLogZ.glsl +++ b/Source/Shaders/Builtin/Functions/vertexLogZ.glsl @@ -1,11 +1,12 @@ #ifdef LOG_DEPTH varying float v_logZ; -varying vec4 v_glPosition; +varying vec3 v_logPositionEC; #endif void czm_updateZ() { #ifdef LOG_DEPTH - v_glPosition = gl_Position; + v_logPositionEC = (czm_inverseProjection * gl_Position).xyz; + gl_Position.z = log2(max(1e-6, 1.0 + gl_Position.w)) * czm_logFarDistance - 1.0; gl_Position.z *= gl_Position.w; #endif diff --git a/Specs/Scene/ShadowMapSpec.js b/Specs/Scene/ShadowMapSpec.js index 7439d663470a..f6d9783848d7 100644 --- a/Specs/Scene/ShadowMapSpec.js +++ b/Specs/Scene/ShadowMapSpec.js @@ -1116,9 +1116,18 @@ defineSuite([ scene.render(); } - // Expect derived commands to be updated twice for both the floor and box, - // once on the first frame and again when the shadow map is dirty - expect(spy.calls.count()).toEqual(4); + var callCount; + if (!scene.logDepthBuffer) { + // Expect derived commands to be updated twice for both the floor and box, + // once on the first frame and again when the shadow map is dirty + callCount = 4; + } else { + // Same as without log z, but doubled. The derived cast commands do not write log z, but + // the derived receive commands do. + callCount = 8; + } + + expect(spy.calls.count()).toEqual(callCount); box.show = false; floor.show = false; From 1440a9a79dda51a1894e4f825b3e405dabd6a344 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 9 Mar 2018 16:00:36 -0500 Subject: [PATCH 45/68] Ignore nodes when searching for model semantics. Fix tests. --- Source/Scene/ModelUtility.js | 16 ++++++++-------- Source/Scene/Scene.js | 19 +++++++++++++------ Specs/Scene/Vector3DTileGeometrySpec.js | 4 ++-- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Source/Scene/ModelUtility.js b/Source/Scene/ModelUtility.js index 33a6b70ec548..fd48103357bd 100644 --- a/Source/Scene/ModelUtility.js +++ b/Source/Scene/ModelUtility.js @@ -52,7 +52,7 @@ define([ }; }; - ModelUtility.getAttributeOrUniformBySemantic = function(gltf, semantic, programId) { + ModelUtility.getAttributeOrUniformBySemantic = function(gltf, semantic, programId, ignoreNodes) { var techniques = gltf.techniques; var parameter; for (var techniqueName in techniques) { @@ -67,7 +67,7 @@ define([ for (var attributeName in attributes) { if (attributes.hasOwnProperty(attributeName)) { parameter = parameters[attributes[attributeName]]; - if (defined(parameter) && parameter.semantic === semantic) { + if (defined(parameter) && parameter.semantic === semantic && (!ignoreNodes || !defined(parameter.node))) { return attributeName; } } @@ -75,7 +75,7 @@ define([ for (var uniformName in uniforms) { if (uniforms.hasOwnProperty(uniformName)) { parameter = parameters[uniforms[uniformName]]; - if (defined(parameter) && parameter.semantic === semantic) { + if (defined(parameter) && parameter.semantic === semantic && (!ignoreNodes || !defined(parameter.node))) { return uniformName; } } @@ -297,14 +297,14 @@ define([ positionName = decodedPositionName; } - var modelViewProjectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'MODELVIEWPROJECTION'); - if (!defined(modelViewProjectionName)) { - var projectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'PROJECTION'); - var modelViewName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'MODELVIEW'); + var modelViewProjectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'MODELVIEWPROJECTION', undefined, true); + if (!defined(modelViewProjectionName) || shader.indexOf(modelViewProjectionName) === -1) { + var projectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'PROJECTION', undefined, true); + var modelViewName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'MODELVIEW', undefined, true); if (shader.indexOf('czm_instanced_modelView ') !== -1) { modelViewName = 'czm_instanced_modelView'; } else if (!defined(modelViewName)) { - modelViewName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'CESIUM_RTC_MODELVIEW'); + modelViewName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'CESIUM_RTC_MODELVIEW', undefined, true); } modelViewProjectionName = projectionName + ' * ' + modelViewName; } diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 08cffe66c3b2..42db4f815e26 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -3430,6 +3430,7 @@ define([ } var logDepthRegex = /\s+czm_writeLogZ\(/; + var extensionRegex = /\s*#extension\s+GL_EXT_frag_depth\s*:\s*enable/; function getLogDepthShaderProgram(context, shaderProgram) { var shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, 'logDepth'); @@ -3443,11 +3444,7 @@ define([ fs.defines = defined(fs.defines) ? fs.defines.slice(0) : []; fs.defines.push('LOG_DEPTH'); - var logSource = - '#ifdef GL_EXT_frag_depth \n' + - '#extension GL_EXT_frag_depth : enable \n' + - '#endif \n\n'; - + var addExtension = true; var writesLogDepth = false; var sources = fs.sources; var length = sources.length; @@ -3455,8 +3452,18 @@ define([ for (i = 0; i < length; ++i) { if (logDepthRegex.test(sources[i])) { writesLogDepth = true; - break; } + if (extensionRegex.test(sources[i])) { + addExtension = false; + } + } + + var logSource = ''; + if (addExtension) { + logSource += + '#ifdef GL_EXT_frag_depth \n' + + '#extension GL_EXT_frag_depth : enable \n' + + '#endif \n\n'; } if (!writesLogDepth) { diff --git a/Specs/Scene/Vector3DTileGeometrySpec.js b/Specs/Scene/Vector3DTileGeometrySpec.js index 0d9b28d4b17c..ee6c8dbc48ea 100644 --- a/Specs/Scene/Vector3DTileGeometrySpec.js +++ b/Specs/Scene/Vector3DTileGeometrySpec.js @@ -355,7 +355,7 @@ defineSuite([ }); it('renders a single ellipsoid' + webglMessage, function() { - var radii = new Cartesian3(1000000.0, 1000000.0, 1000000.0); + var radii = new Cartesian3(500000.0, 500000.0, 500000.0); var ellipsoid = packEllipsoids([{ modelMatrix : Matrix4.IDENTITY, radii : radii @@ -390,7 +390,7 @@ defineSuite([ }); it('renders a single sphere' + webglMessage, function() { - var radius = 1000000.0; + var radius = 500000.0; var sphere = packSpheres([{ radius : radius, modelMatrix : Matrix4.IDENTITY From 27118ee283c0d9da68e0c06acd922a6259acd90f Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 9 Mar 2018 16:46:54 -0500 Subject: [PATCH 46/68] Fix depth picking point clouds with EDL. --- Source/Scene/Scene.js | 20 +++++++++----------- Specs/Scene/Vector3DTileGeometrySpec.js | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 42db4f815e26..d862db260cd4 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -777,7 +777,7 @@ define([ } var numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio)); - updateFrustums(near, far, farToNearRatio, numFrustums, this._frustumCommandsList, false, undefined); + updateFrustums(near, far, farToNearRatio, numFrustums, this._logDepthBuffer, this._frustumCommandsList, false, undefined); // give frameState, camera, and screen space camera controller initial state before rendering updateFrameState(this, 0.0, JulianDate.now()); @@ -1553,7 +1553,7 @@ define([ clearPasses(frameState.passes); } - function updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, nearToFarDistance2D) { + function updateFrustums(near, far, farToNearRatio, numFrustums, logDepth, frustumCommandsList, is2D, nearToFarDistance2D) { frustumCommandsList.length = numFrustums; for (var m = 0; m < numFrustums; ++m) { var curNear; @@ -1561,7 +1561,10 @@ define([ if (!is2D) { curNear = Math.max(near, Math.pow(farToNearRatio, m) * near); - curFar = Math.min(far, farToNearRatio * curNear); + 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); @@ -1773,14 +1776,9 @@ define([ numFrustums = Math.ceil(Math.max(1.0, far - near) / scene.nearToFarDistance2D); } - var farChange; - if (frustumCommandsList.length !== 0){ - farChange = far / frustumCommandsList[numberOfFrustums - 1].far; - } - if ((near !== Number.MAX_VALUE && (numFrustums !== numberOfFrustums || (frustumCommandsList.length !== 0 && - ((logDepth && isFinite(farChange) && !CesiumMath.equalsEpsilon(1, farChange, CesiumMath.EPSILON8)) || - (near < frustumCommandsList[0].near || (far > frustumCommandsList[numberOfFrustums - 1].far && !CesiumMath.equalsEpsilon(far, frustumCommandsList[numberOfFrustums - 1].far, CesiumMath.EPSILON8)))))))) { - updateFrustums(near, far, farToNearRatio, numFrustums, frustumCommandsList, is2D, scene.nearToFarDistance2D); + if (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))))))) { + updateFrustums(near, far, farToNearRatio, numFrustums, logDepth, frustumCommandsList, is2D, scene.nearToFarDistance2D); createPotentiallyVisibleSet(scene); } diff --git a/Specs/Scene/Vector3DTileGeometrySpec.js b/Specs/Scene/Vector3DTileGeometrySpec.js index ee6c8dbc48ea..4be21f4c0efd 100644 --- a/Specs/Scene/Vector3DTileGeometrySpec.js +++ b/Specs/Scene/Vector3DTileGeometrySpec.js @@ -662,13 +662,13 @@ defineSuite([ geometry = scene.primitives.add(new Vector3DTileGeometry({ ellipsoids : packEllipsoids([{ modelMatrix : Matrix4.IDENTITY, - radii : new Cartesian3(1000000.0, 1000000.0, 1000000.0) + radii : new Cartesian3(500000.0, 500000.0, 500000.0) }]), ellipsoidBatchIds : new Uint16Array([0]), center : center, modelMatrix : modelMatrix, batchTable : batchTable, - boundingVolume : new BoundingSphere(center, 1000000.0) + boundingVolume : new BoundingSphere(center, 500000.0) })); return loadGeometries(geometry).then(function() { scene.camera.setView({ From 7fdf9c9915ceae2ef4853757e14050c74846a649 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Fri, 9 Mar 2018 19:53:16 -0500 Subject: [PATCH 47/68] Fix depth picking tests. --- Source/Scene/Vector3DTilePrimitive.js | 1 - Specs/Scene/PointCloud3DTileContentSpec.js | 13 ------ Specs/Scene/SceneSpec.js | 46 ++++++---------------- 3 files changed, 12 insertions(+), 48 deletions(-) diff --git a/Source/Scene/Vector3DTilePrimitive.js b/Source/Scene/Vector3DTilePrimitive.js index 4b1e64a39b56..f10af4c2117d 100644 --- a/Source/Scene/Vector3DTilePrimitive.js +++ b/Source/Scene/Vector3DTilePrimitive.js @@ -1056,7 +1056,6 @@ define([ commandLength = commandsIgnoreShow.length; for (i = 0; i < commandLength; ++i) { command = commandsIgnoreShow[i]; - command.pass = pass; commandList.push(command); } } diff --git a/Specs/Scene/PointCloud3DTileContentSpec.js b/Specs/Scene/PointCloud3DTileContentSpec.js index d7fdad687e6b..64beb31e2a14 100644 --- a/Specs/Scene/PointCloud3DTileContentSpec.js +++ b/Specs/Scene/PointCloud3DTileContentSpec.js @@ -497,19 +497,6 @@ defineSuite([ }); }); - it('modulates attenuation using the baseResolution parameter', function() { - return attenuationTest(function(scene, tileset) { - tileset.pointCloudShading.attenuation = true; - tileset.pointCloudShading.geometricErrorScale = 1.0; - tileset.pointCloudShading.maximumAttenuation = undefined; - tileset.pointCloudShading.baseResolution = CesiumMath.EPSILON20; - tileset.maximumScreenSpaceError = 16; - expect(scene).toRenderPixelCountAndCall(function(pixelCount) { - expect(pixelCount).toEqual(noAttenuationPixelCount); - }); - }); - }); - it('modulates attenuation using the baseResolution parameter', function() { return attenuationTest(function(scene, tileset) { // pointCloudNoColorUrl is a single tile with GeometricError = 0, diff --git a/Specs/Scene/SceneSpec.js b/Specs/Scene/SceneSpec.js index 8c852341361a..741d670211e1 100644 --- a/Specs/Scene/SceneSpec.js +++ b/Specs/Scene/SceneSpec.js @@ -726,7 +726,6 @@ defineSuite([ scene.destroyForSpecs(); }); - var pickedPosition3D = new Cartesian3(-455845.46867895435, -5210337.548977215, 3637549.8562320103); var pickedPosition2D = new Cartesian3(-455861.7055871038, -5210523.137686572, 3637866.6638769475); it('pickPosition', function() { @@ -734,7 +733,7 @@ defineSuite([ return; } - var rectangle = Rectangle.fromDegrees(-100.0, 30.0, -90.0, 40.0); + var rectangle = Rectangle.fromDegrees(-0.0001, -0.0001, 0.0001, 0.0001); scene.camera.setView({ destination : rectangle }); var canvas = scene.canvas; @@ -753,7 +752,9 @@ defineSuite([ expect(scene).toRenderAndCall(function() { var position = scene.pickPosition(windowPosition); - expect(position).toEqualEpsilon(pickedPosition3D, CesiumMath.EPSILON6); + expect(position.x).toBeGreaterThan(Ellipsoid.WGS84.minimumRadius); + expect(position.y).toEqualEpsilon(0.0, CesiumMath.EPSILON5); + expect(position.z).toEqualEpsilon(0.0, CesiumMath.EPSILON5); }); }); @@ -764,7 +765,7 @@ defineSuite([ scene.morphToColumbusView(0.0); - var rectangle = Rectangle.fromDegrees(-100.0, 30.0, -90.0, 40.0); + var rectangle = Rectangle.fromDegrees(-0.0001, -0.0001, 0.0001, 0.0001); scene.camera.setView({ destination : rectangle }); var canvas = scene.canvas; @@ -783,7 +784,9 @@ defineSuite([ expect(scene).toRenderAndCall(function() { var position = scene.pickPosition(windowPosition); - expect(position).toEqualEpsilon(pickedPosition2D, CesiumMath.EPSILON6); + expect(position.x).toBeGreaterThan(Ellipsoid.WGS84.minimumRadius); + expect(position.y).toEqualEpsilon(0.0, CesiumMath.EPSILON5); + expect(position.z).toEqualEpsilon(0.0, CesiumMath.EPSILON5); }); }); @@ -794,7 +797,7 @@ defineSuite([ scene.morphTo2D(0.0); - var rectangle = Rectangle.fromDegrees(-100.0, 30.0, -90.0, 40.0); + var rectangle = Rectangle.fromDegrees(-0.0001, -0.0001, 0.0001, 0.0001); scene.camera.setView({ destination : rectangle }); var canvas = scene.canvas; @@ -813,7 +816,9 @@ defineSuite([ expect(scene).toRenderAndCall(function() { var position = scene.pickPosition(windowPosition); - expect(position).toEqualEpsilon(pickedPosition2D, CesiumMath.EPSILON6); + expect(position.x).toBeGreaterThan(Ellipsoid.WGS84.minimumRadius); + expect(position.y).toEqualEpsilon(0.0, CesiumMath.EPSILON5); + expect(position.z).toEqualEpsilon(0.0, CesiumMath.EPSILON5); }); }); @@ -880,33 +885,6 @@ defineSuite([ var position = scene.pickPosition(windowPosition); expect(position).toBeDefined(); }); - - var rectanglePrimitive2 = createRectangle(rectangle); - rectanglePrimitive2.appearance.material.uniforms.color = new Color(0.0, 1.0, 0.0, 0.5); - primitives.add(rectanglePrimitive2); - - expect(scene).toRenderAndCall(function() { - var position = scene.pickPosition(windowPosition); - expect(position).toBeDefined(); - - var commandList = scene.frameState.commandList; - expect(commandList.length).toEqual(2); - - var command1 = commandList[0]; - var command2 = commandList[1]; - - expect(command1.derivedCommands).toBeDefined(); - expect(command2.derivedCommands).toBeDefined(); - - expect(command1.derivedCommands.depth).toBeDefined(); - expect(command2.derivedCommands.depth).toBeDefined(); - - expect(command1.derivedCommands.depth.depthOnlyCommand).toBeDefined(); - expect(command2.derivedCommands.depth.depthOnlyCommand).toBeDefined(); - - expect(command1.derivedCommands.depth.depthOnlyCommand.shaderProgram).toEqual(command2.derivedCommands.depth.depthOnlyCommand.shaderProgram); - expect(command1.derivedCommands.depth.depthOnlyCommand.renderState).toEqual(command2.derivedCommands.depth.depthOnlyCommand.renderState); - }); }); it('pickPosition caches results per frame',function(){ From d94aa07aefa9603ed5fa4185bc8b7958c82ee580 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 12 Mar 2018 15:45:35 -0400 Subject: [PATCH 48/68] Updates from review. Fix more tests. --- CHANGES.md | 8 ++- Source/Scene/Camera.js | 5 +- Source/Scene/ModelUtility.js | 4 +- Source/Scene/Scene.js | 48 +++++++++--------- Source/Scene/SceneTransitioner.js | 16 +++++- Source/Scene/Vector3DTilePrimitive.js | 3 +- .../PolylineColorAppearanceVS.glsl | 2 +- .../PolylineMaterialAppearanceVS.glsl | 2 +- Source/Shaders/BillboardCollectionFS.glsl | 2 +- Source/Shaders/BillboardCollectionVS.glsl | 4 +- .../Builtin/Functions/vertexLogDepth.glsl | 49 +++++++++++++++++++ .../Shaders/Builtin/Functions/vertexLogZ.glsl | 29 ----------- .../Builtin/Functions/writeLogDepth.glsl | 40 +++++++++++++++ .../Shaders/Builtin/Functions/writeLogZ.glsl | 17 ------- Source/Shaders/DepthPlaneFS.glsl | 2 +- Source/Shaders/DepthPlaneVS.glsl | 2 +- Source/Shaders/EllipsoidFS.glsl | 2 +- .../Shaders/PointPrimitiveCollectionFS.glsl | 2 +- .../Shaders/PointPrimitiveCollectionVS.glsl | 4 +- Source/Shaders/PolylineFS.glsl | 2 +- Source/Shaders/PolylineVS.glsl | 2 +- .../PointCloudEyeDomeLighting.glsl | 2 +- Source/Shaders/Vector3DTilePolylinesVS.glsl | 2 +- Specs/Scene/PointCloud3DTileContentSpec.js | 3 +- Specs/Scene/SceneSpec.js | 18 +++---- Specs/Scene/ShadowMapSpec.js | 2 +- 26 files changed, 168 insertions(+), 104 deletions(-) create mode 100644 Source/Shaders/Builtin/Functions/vertexLogDepth.glsl delete mode 100644 Source/Shaders/Builtin/Functions/vertexLogZ.glsl create mode 100644 Source/Shaders/Builtin/Functions/writeLogDepth.glsl delete mode 100644 Source/Shaders/Builtin/Functions/writeLogZ.glsl diff --git a/CHANGES.md b/CHANGES.md index 4310f054c67e..66f77c6c12e9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,9 +3,15 @@ Change Log ### 1.44 - 2018-04-02 +##### Breaking Changes :mega: +* `Camera.distanceToBoundingSphere` now returns the signed distance to the bounding sphere. Positive values indicate that the bounding sphere is in the positive half-plane of the camera position and view direction while a negative value indicates it is in the negative half-plane. + +##### Additions :tada: +* Added option `logDepthBuffer` to `Viewer`. With this option there is typically a single frustum using logarithmic depth rendered. This increases performance by issuing less draw calls to the GPU and helps to avoid artifacts on the connection of two frustums. [#5851](https://github.com/AnalyticalGraphicsInc/cesium/pull/5851) +* Added `Math.log2` to compute the base 2 logarithm of a number. + ##### Fixes :wrench: * Fixed support of glTF-supplied tangent vectors. [#6302](https://github.com/AnalyticalGraphicsInc/cesium/pull/6302) -* Added option `logDepthBuffer` to `Viewer`. With this option the globe is typically rendered in a single frustum using logarithmic depth. This helps to avoid artifacts on the connection of two frustums. [#5851](https://github.com/AnalyticalGraphicsInc/cesium/pull/5851) ### 1.43 - 2018-03-01 diff --git a/Source/Scene/Camera.js b/Source/Scene/Camera.js index 842055d8bbc0..915ca86bc001 100644 --- a/Source/Scene/Camera.js +++ b/Source/Scene/Camera.js @@ -2574,7 +2574,10 @@ define([ var scratchProj = new Cartesian3(); /** - * Return the distance from the camera to the front of the bounding sphere. + * Return the signed distance from the camera to the front of the bounding sphere. + *

+ * Positive values indicate that the bounding sphere is in the positive half-plane of the camera position and view direction while a negative value indicates it is in the negative half-plane. + *

* * @param {BoundingSphere} boundingSphere The bounding sphere in world coordinates. * @returns {Number} The signed distance to the bounding sphere. diff --git a/Source/Scene/ModelUtility.js b/Source/Scene/ModelUtility.js index fd48103357bd..642723081012 100644 --- a/Source/Scene/ModelUtility.js +++ b/Source/Scene/ModelUtility.js @@ -284,7 +284,7 @@ define([ 'void main() \n' + '{ \n' + ' czm_depth_main(); \n' + - ' czm_writeLogZ(); \n' + + ' czm_writeLogDepth(); \n' + '} \n'; return shader; @@ -315,7 +315,7 @@ define([ 'void main() \n' + '{ \n' + ' czm_depth_main(); \n' + - ' czm_vertexLogZ(' + modelViewProjectionName + ' * vec4(' + positionName + '.xyz, 1.0)); \n' + + ' czm_vertexLogDepth(' + modelViewProjectionName + ' * vec4(' + positionName + '.xyz, 1.0)); \n' + '} \n'; return shader; diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index d862db260cd4..3bc47456775a 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -266,7 +266,7 @@ define([ } this._logDepthBuffer = context.fragmentDepth; - this._logDepthBufferChanged = true; + this._logDepthBufferDirty = true; this._id = createGuid(); this._jobScheduler = new JobScheduler(); @@ -458,6 +458,7 @@ define([ * @default 1.0 */ this.morphTime = 1.0; + /** * The far-to-near ratio of the multi-frustum. The default is 1,000.0. * @@ -466,6 +467,13 @@ define([ */ this.farToNearRatio = 1000.0; + /** + * The far-to-near ratio of the multi-frustum when using a logarithmic depth buffer. The default is 1e6. + * @type {Number} + * @default 1000000.0 + */ + this.logarithmicDepthFarToNearRatio = 1000000.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 @@ -698,6 +706,11 @@ define([ this._mapMode2D = defaultValue(options.mapMode2D, MapMode2D.INFINITE_SCROLL); this._frustumChanged = true; + if (this._logDepthBuffer) { + this._camera.frustum.near = 1.0; + this._camera.frustum.far = 10000000000.0; + } + // Keeps track of the state of a frame. FrameState is the state across // the primitives of the scene. This state is for internally keeping track // of celestial and environment effects that need to be updated/rendered in @@ -768,13 +781,7 @@ define([ // initial guess at frustums. var near = camera.frustum.near; var far = camera.frustum.far; - var farToNearRatio; - - if (this._logDepthBuffer) { - farToNearRatio = this.farToNearRatio * this.farToNearRatio; - } else { - farToNearRatio = this.farToNearRatio; - } + 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); @@ -1386,14 +1393,14 @@ define([ * Whether or not to use a logarithmic depth buffer. Enabling this option will allow for less frustums in the multi-frustum, * increasing performance. This property relies on {@link Context#fragmentDepth} being supported. */ - logDepthBuffer : { + logarithmicDepthBuffer : { get : function() { return this._logDepthBuffer; }, set : function(value) { if (this._context.fragmentDepth && this._logDepthBuffer !== value) { this._logDepthBuffer = value; - this._logDepthBufferChanged = true; + this._logDepthBufferDirty = true; } } } @@ -1449,7 +1456,7 @@ define([ } var derivedCommands = command.derivedCommands; - if ((scene._logDepthBufferChanged || scene._frustumChanged || command.dirty) && defined(derivedCommands)) { + if ((scene._logDepthBufferDirty || scene._frustumChanged || command.dirty) && defined(derivedCommands)) { command.dirty = false; var frustum = scene.camera.frustum; @@ -1758,14 +1765,11 @@ define([ // last frame, else compute the new frustums and sort them by frustum again. var is2D = scene.mode === SceneMode.SCENE2D; var logDepth = scene._logDepthBuffer && !(camera.frustum instanceof OrthographicFrustum || camera.frustum instanceof OrthographicOffCenterFrustum); - var farToNearRatio = scene.farToNearRatio; + var farToNearRatio = logDepth ? scene.logarithmicDepthFarToNearRatio : scene.farToNearRatio; var numFrustums; if (!is2D) { // The multifrustum for 3D/CV is non-uniformly distributed. - if (logDepth) { - farToNearRatio *= farToNearRatio; - } numFrustums = Math.ceil(Math.log(far / near) / Math.log(farToNearRatio)); } else { // The multifrustum for 2D is uniformly distributed. To avoid z-fighting in 2D, @@ -3115,13 +3119,9 @@ define([ this._postUpdate.raiseEvent(this, time); this._frustumChanged = !this._camera.frustum.equals(this._cameraClone.frustum); - if (this._frustumChanged && this._logDepthBuffer && !(this._camera.frustum instanceof OrthographicFrustum || this._camera.frustum instanceof OrthographicOffCenterFrustum)) { - this._camera.frustum.near = 1.0; - this._camera.frustum.far = 10000000000.0; - } var cameraChanged = checkForCameraUpdates(this); - var shouldRender = !this.requestRenderMode || this._renderRequested || cameraChanged || this._frustumChanged || this._logDepthBufferChanged || (this.mode === SceneMode.MORPHING); + var shouldRender = !this.requestRenderMode || this._renderRequested || cameraChanged || this._frustumChanged || this._logDepthBufferDirty || (this.mode === SceneMode.MORPHING); if (!shouldRender && defined(this.maximumRenderTimeChange) && defined(this._lastRenderTime)) { var difference = Math.abs(JulianDate.secondsDifference(this._lastRenderTime, time)); shouldRender = shouldRender || difference > this.maximumRenderTimeChange; @@ -3130,7 +3130,7 @@ define([ if (shouldRender) { this._lastRenderTime = JulianDate.clone(time, this._lastRenderTime); this._renderRequested = false; - this._logDepthBufferChanged = false; + this._logDepthBufferDirty = false; // Render this._preRender.raiseEvent(this, time); @@ -3427,7 +3427,7 @@ define([ return result; } - var logDepthRegex = /\s+czm_writeLogZ\(/; + var logDepthRegex = /\s+czm_writeLogDepth\(/; var extensionRegex = /\s*#extension\s+GL_EXT_frag_depth\s*:\s*enable/; function getLogDepthShaderProgram(context, shaderProgram) { @@ -3474,7 +3474,7 @@ define([ 'void main() \n' + '{ \n' + ' czm_log_depth_main(); \n' + - ' czm_writeLogZ(); \n' + + ' czm_writeLogDepth(); \n' + '} \n'; var vertexSources = vs.sources; @@ -3488,7 +3488,7 @@ define([ 'void main() \n' + '{ \n' + ' czm_log_depth_main(); \n' + - ' czm_vertexLogZ(); \n' + + ' czm_vertexLogDepth(); \n' + '} \n'; vertexSources.push(logMain); } diff --git a/Source/Scene/SceneTransitioner.js b/Source/Scene/SceneTransitioner.js index 318182e4e8fc..43dc3dce89ab 100644 --- a/Source/Scene/SceneTransitioner.js +++ b/Source/Scene/SceneTransitioner.js @@ -857,10 +857,10 @@ define([ destroyMorphHandler(transitioner); + var camera = scene.camera; if (transitioner._previousMode !== SceneMode.MORPHING || transitioner._morphCancelled) { transitioner._morphCancelled = false; - var camera = scene.camera; Cartesian3.clone(camera3D.position, camera.position); Cartesian3.clone(camera3D.direction, camera.direction); Cartesian3.clone(camera3D.up, camera.up); @@ -870,6 +870,12 @@ define([ camera.frustum = camera3D.frustum.clone(); } + var frustum = camera.frustum; + if (scene._logDepthBuffer && !(frustum instanceof OrthographicFrustum || frustum instanceof OrthographicOffCenterFrustum)) { + frustum.near = 1.0; + frustum.far = 10000000000.0; + } + var wasMorphing = defined(transitioner._completeMorph); transitioner._completeMorph = undefined; scene.camera.update(scene.mode); @@ -910,10 +916,10 @@ define([ destroyMorphHandler(transitioner); + var camera = scene.camera; if (transitioner._previousModeMode !== SceneMode.MORPHING || transitioner._morphCancelled) { transitioner._morphCancelled = false; - var camera = scene.camera; Cartesian3.clone(cameraCV.position, camera.position); Cartesian3.clone(cameraCV.direction, camera.direction); Cartesian3.clone(cameraCV.up, camera.up); @@ -921,6 +927,12 @@ define([ Cartesian3.normalize(camera.right, camera.right); } + var frustum = camera.frustum; + if (scene._logDepthBuffer && !(frustum instanceof OrthographicFrustum || frustum instanceof OrthographicOffCenterFrustum)) { + frustum.near = 1.0; + frustum.far = 10000000000.0; + } + var wasMorphing = defined(transitioner._completeMorph); transitioner._completeMorph = undefined; scene.camera.update(scene.mode); diff --git a/Source/Scene/Vector3DTilePrimitive.js b/Source/Scene/Vector3DTilePrimitive.js index f10af4c2117d..1bd40fe145cc 100644 --- a/Source/Scene/Vector3DTilePrimitive.js +++ b/Source/Scene/Vector3DTilePrimitive.js @@ -1055,8 +1055,7 @@ define([ commandLength = commandsIgnoreShow.length; for (i = 0; i < commandLength; ++i) { - command = commandsIgnoreShow[i]; - commandList.push(command); + commandList.push(commandsIgnoreShow[i]); } } diff --git a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl index 9515f6cbcc45..17ce3ec48e27 100644 --- a/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineColorAppearanceVS.glsl @@ -27,6 +27,6 @@ void main() gl_Position = czm_viewportOrthographic * positionWC; #ifdef LOG_DEPTH - czm_vertexLogZ(czm_modelViewProjectionRelativeToEye * p); + czm_vertexLogDepth(czm_modelViewProjectionRelativeToEye * p); #endif } diff --git a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl index e91e4067fb70..bdfae0e27569 100644 --- a/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl +++ b/Source/Shaders/Appearances/PolylineMaterialAppearanceVS.glsl @@ -29,6 +29,6 @@ void main() gl_Position = czm_viewportOrthographic * positionWC; #ifdef LOG_DEPTH - czm_vertexLogZ(czm_modelViewProjectionRelativeToEye * p); + czm_vertexLogDepth(czm_modelViewProjectionRelativeToEye * p); #endif } diff --git a/Source/Shaders/BillboardCollectionFS.glsl b/Source/Shaders/BillboardCollectionFS.glsl index 8d6488b06121..79a38c69f003 100644 --- a/Source/Shaders/BillboardCollectionFS.glsl +++ b/Source/Shaders/BillboardCollectionFS.glsl @@ -54,5 +54,5 @@ void main() gl_FragColor = color; #endif - czm_writeLogZ(); + czm_writeLogDepth(); } diff --git a/Source/Shaders/BillboardCollectionVS.glsl b/Source/Shaders/BillboardCollectionVS.glsl index 023ceb06bad1..cf0f97840e3d 100644 --- a/Source/Shaders/BillboardCollectionVS.glsl +++ b/Source/Shaders/BillboardCollectionVS.glsl @@ -256,7 +256,7 @@ void main() #endif #ifdef LOG_DEPTH - czm_vertexLogZ(czm_projection * positionEC); + czm_vertexLogDepth(czm_projection * positionEC); #endif vec4 positionWC = computePositionWindowCoordinates(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters); @@ -280,7 +280,7 @@ void main() // Position z on the near plane. gl_Position.z = -gl_Position.w; #ifdef LOG_DEPTH - czm_vertexLogZ(vec4(czm_currentFrustum.x)); + czm_vertexLogDepth(vec4(czm_currentFrustum.x)); #endif } } diff --git a/Source/Shaders/Builtin/Functions/vertexLogDepth.glsl b/Source/Shaders/Builtin/Functions/vertexLogDepth.glsl new file mode 100644 index 000000000000..673f4240df54 --- /dev/null +++ b/Source/Shaders/Builtin/Functions/vertexLogDepth.glsl @@ -0,0 +1,49 @@ +#ifdef LOG_DEPTH +varying float v_logZ; +varying vec3 v_logPositionEC; +#endif + +void czm_updatePositionDepth() { +#ifdef LOG_DEPTH + v_logPositionEC = (czm_inverseProjection * gl_Position).xyz; + + gl_Position.z = log2(max(1e-6, 1.0 + gl_Position.w)) * czm_logFarDistance - 1.0; + gl_Position.z *= gl_Position.w; +#endif +} + +/** + * Writes the logarithmic depth to gl_Position using the already computed gl_Position. + * + * @name czm_vertexLogDepth + * @glslFunction + */ +void czm_vertexLogDepth() +{ +#ifdef LOG_DEPTH + v_logZ = 1.0 + gl_Position.w; + czm_updatePositionDepth(); +#endif +} + +/** + * Writes the logarithmic depth to gl_Position using the provided clip coordinates. + *

+ * An example use case for this function would be moving the vertex in window coordinates + * before converting back to clip coordinates. Use the original vertex clip coordinates. + *

+ * @name czm_vertexLogDepth + * @glslFunction + * + * @param {vec4} clipCoords The vertex in clip coordinates. + * + * @example + * czm_vertexLogDepth(czm_projection * vec4(positionEyeCoordinates, 1.0)); + */ +void czm_vertexLogDepth(vec4 clipCoords) +{ +#ifdef LOG_DEPTH + v_logZ = 1.0 + clipCoords.w; + czm_updatePositionDepth(); +#endif +} diff --git a/Source/Shaders/Builtin/Functions/vertexLogZ.glsl b/Source/Shaders/Builtin/Functions/vertexLogZ.glsl deleted file mode 100644 index 5cbdd861b379..000000000000 --- a/Source/Shaders/Builtin/Functions/vertexLogZ.glsl +++ /dev/null @@ -1,29 +0,0 @@ -#ifdef LOG_DEPTH -varying float v_logZ; -varying vec3 v_logPositionEC; -#endif - -void czm_updateZ() { -#ifdef LOG_DEPTH - v_logPositionEC = (czm_inverseProjection * gl_Position).xyz; - - gl_Position.z = log2(max(1e-6, 1.0 + gl_Position.w)) * czm_logFarDistance - 1.0; - gl_Position.z *= gl_Position.w; -#endif -} - -void czm_vertexLogZ() -{ -#ifdef LOG_DEPTH - v_logZ = 1.0 + gl_Position.w; - czm_updateZ(); -#endif -} - -void czm_vertexLogZ(vec4 clipCoords) -{ -#ifdef LOG_DEPTH - v_logZ = 1.0 + clipCoords.w; - czm_updateZ(); -#endif -} diff --git a/Source/Shaders/Builtin/Functions/writeLogDepth.glsl b/Source/Shaders/Builtin/Functions/writeLogDepth.glsl new file mode 100644 index 000000000000..60a591aad01e --- /dev/null +++ b/Source/Shaders/Builtin/Functions/writeLogDepth.glsl @@ -0,0 +1,40 @@ +#ifdef LOG_DEPTH +varying float v_logZ; +#endif + +/** + * Writes the fragment depth to the logarithmic depth buffer. + *

+ * Use this when the vertex shader does not calls {@link czm_vertexlogDepth}, for example, when + * ray-casting geometry using a full screen quad. + *

+ * @name czm_writeLogDepth + * @glslFunction + * + * @param {float} logZ The w coordinate of the vertex in clip coordinates plus 1.0. + * + * @example + * czm_writeLogDepth((czm_projection * v_positionEyeCoordinates).w + 1.0); + */ +void czm_writeLogDepth(float logZ) +{ +#if defined(GL_EXT_frag_depth) && defined(LOG_DEPTH) && !defined(DISABLE_LOG_DEPTH_FRAGMENT_WRITE) + float halfLogFarDistance = czm_logFarDistance * 0.5; + gl_FragDepthEXT = log2(logZ) * halfLogFarDistance; +#endif +} + +/** + * Writes the fragment depth to the logarithmic depth buffer. + *

+ * Use this when the vertex shader calls {@link czm_vertexlogDepth}. + *

+ * + * @name czm_writeLogDepth + * @glslFunction + */ +void czm_writeLogDepth() { +#ifdef LOG_DEPTH + czm_writeLogDepth(v_logZ); +#endif +} diff --git a/Source/Shaders/Builtin/Functions/writeLogZ.glsl b/Source/Shaders/Builtin/Functions/writeLogZ.glsl deleted file mode 100644 index 6cd5da08d3b6..000000000000 --- a/Source/Shaders/Builtin/Functions/writeLogZ.glsl +++ /dev/null @@ -1,17 +0,0 @@ -#ifdef LOG_DEPTH -varying float v_logZ; -#endif - -void czm_writeLogZ(float logZ) -{ -#if defined(GL_EXT_frag_depth) && defined(LOG_DEPTH) && !defined(DISABLE_LOG_DEPTH_FRAGMENT_WRITE) - float halfLogFarDistance = czm_logFarDistance * 0.5; - gl_FragDepthEXT = log2(logZ) * halfLogFarDistance; -#endif -} - -void czm_writeLogZ() { -#ifdef LOG_DEPTH - czm_writeLogZ(v_logZ); -#endif -} diff --git a/Source/Shaders/DepthPlaneFS.glsl b/Source/Shaders/DepthPlaneFS.glsl index 142bd49dd14a..41385729ae4b 100644 --- a/Source/Shaders/DepthPlaneFS.glsl +++ b/Source/Shaders/DepthPlaneFS.glsl @@ -18,5 +18,5 @@ void main() discard; } - czm_writeLogZ(); + czm_writeLogDepth(); } diff --git a/Source/Shaders/DepthPlaneVS.glsl b/Source/Shaders/DepthPlaneVS.glsl index 6ddb496a5309..028b56c43700 100644 --- a/Source/Shaders/DepthPlaneVS.glsl +++ b/Source/Shaders/DepthPlaneVS.glsl @@ -7,5 +7,5 @@ void main() positionEC = czm_modelView * position; gl_Position = czm_projection * positionEC; - czm_vertexLogZ(); + czm_vertexLogDepth(); } diff --git a/Source/Shaders/EllipsoidFS.glsl b/Source/Shaders/EllipsoidFS.glsl index ab29498065a4..d028e502b96d 100644 --- a/Source/Shaders/EllipsoidFS.glsl +++ b/Source/Shaders/EllipsoidFS.glsl @@ -97,7 +97,7 @@ void main() vec3 positionEC = czm_pointAlongRay(ray, t); vec4 positionCC = czm_projection * vec4(positionEC, 1.0); #ifdef LOG_DEPTH - czm_writeLogZ(1.0 + positionCC.w); + czm_writeLogDepth(1.0 + positionCC.w); #else float z = positionCC.z / positionCC.w; diff --git a/Source/Shaders/PointPrimitiveCollectionFS.glsl b/Source/Shaders/PointPrimitiveCollectionFS.glsl index 47847cdd280b..92a9ee788615 100644 --- a/Source/Shaders/PointPrimitiveCollectionFS.glsl +++ b/Source/Shaders/PointPrimitiveCollectionFS.glsl @@ -47,5 +47,5 @@ void main() gl_FragColor = color; #endif - czm_writeLogZ(); + czm_writeLogDepth(); } diff --git a/Source/Shaders/PointPrimitiveCollectionVS.glsl b/Source/Shaders/PointPrimitiveCollectionVS.glsl index ffd1a9f59d72..6b23bb152840 100644 --- a/Source/Shaders/PointPrimitiveCollectionVS.glsl +++ b/Source/Shaders/PointPrimitiveCollectionVS.glsl @@ -148,7 +148,7 @@ void main() #endif #ifdef LOG_DEPTH - czm_vertexLogZ(czm_projection * positionEC); + czm_vertexLogDepth(czm_projection * positionEC); #endif vec4 positionWC = czm_eyeToWindowCoordinates(positionEC); @@ -171,7 +171,7 @@ void main() // Position z on the near plane. gl_Position.z = -gl_Position.w; #ifdef LOG_DEPTH - czm_vertexLogZ(vec4(czm_currentFrustum.x)); + czm_vertexLogDepth(vec4(czm_currentFrustum.x)); #endif } } diff --git a/Source/Shaders/PolylineFS.glsl b/Source/Shaders/PolylineFS.glsl index e634f43efe03..604a7b805219 100644 --- a/Source/Shaders/PolylineFS.glsl +++ b/Source/Shaders/PolylineFS.glsl @@ -18,5 +18,5 @@ void main() gl_FragColor *= u_highlightColor; #endif - czm_writeLogZ(); + czm_writeLogDepth(); } diff --git a/Source/Shaders/PolylineVS.glsl b/Source/Shaders/PolylineVS.glsl index 9f7d5bbe2325..a43949d08a21 100644 --- a/Source/Shaders/PolylineVS.glsl +++ b/Source/Shaders/PolylineVS.glsl @@ -98,6 +98,6 @@ void main() czm_pickColor = pickColor; #ifdef LOG_DEPTH - czm_vertexLogZ(czm_modelViewProjectionRelativeToEye * p); + czm_vertexLogDepth(czm_modelViewProjectionRelativeToEye * p); #endif } diff --git a/Source/Shaders/PostProcessFilters/PointCloudEyeDomeLighting.glsl b/Source/Shaders/PostProcessFilters/PointCloudEyeDomeLighting.glsl index e24bb51e64ad..3cdfc984b540 100644 --- a/Source/Shaders/PostProcessFilters/PointCloudEyeDomeLighting.glsl +++ b/Source/Shaders/PostProcessFilters/PointCloudEyeDomeLighting.glsl @@ -45,7 +45,7 @@ void main() gl_FragColor = vec4(color); #ifdef LOG_DEPTH - czm_writeLogZ(1.0 + (czm_projection * vec4(ecAlphaDepth.xyz, 1.0)).w); + czm_writeLogDepth(1.0 + (czm_projection * vec4(ecAlphaDepth.xyz, 1.0)).w); #else gl_FragDepthEXT = czm_eyeToWindowCoordinates(vec4(ecAlphaDepth.xyz, 1.0)).z; #endif diff --git a/Source/Shaders/Vector3DTilePolylinesVS.glsl b/Source/Shaders/Vector3DTilePolylinesVS.glsl index dc869998cdc7..08690062050f 100644 --- a/Source/Shaders/Vector3DTilePolylinesVS.glsl +++ b/Source/Shaders/Vector3DTilePolylinesVS.glsl @@ -21,6 +21,6 @@ void main() gl_Position = czm_viewportOrthographic * positionWC; #ifdef LOG_DEPTH - czm_vertexLogZ(czm_projection * p); + czm_vertexLogDepth(czm_projection * p); #endif } diff --git a/Specs/Scene/PointCloud3DTileContentSpec.js b/Specs/Scene/PointCloud3DTileContentSpec.js index 64beb31e2a14..9b110ba2c1dd 100644 --- a/Specs/Scene/PointCloud3DTileContentSpec.js +++ b/Specs/Scene/PointCloud3DTileContentSpec.js @@ -441,11 +441,12 @@ defineSuite([ }); }); - var noAttenuationPixelCount = 16; + var noAttenuationPixelCount; function attenuationTest(postLoadCallback) { var scene = createScene({ canvas : createCanvas(10, 10) }); + noAttenuationPixelCount = scene.logarithmicDepthBuffer ? 20 : 16; var center = new Cartesian3.fromRadians(centerLongitude, centerLatitude, 5.0); scene.camera.lookAt(center, new HeadingPitchRange(0.0, -1.57, 5.0)); scene.fxaa = false; diff --git a/Specs/Scene/SceneSpec.js b/Specs/Scene/SceneSpec.js index 741d670211e1..e159676f1513 100644 --- a/Specs/Scene/SceneSpec.js +++ b/Specs/Scene/SceneSpec.js @@ -249,8 +249,8 @@ defineSuite([ }); it('debugCommandFilter does not filter commands', function() { - var originalLogDepth = scene.logDepthBuffer; - scene.logDepthBuffer = false; + var originalLogDepth = scene.logarithmicDepthBuffer; + scene.logarithmicDepthBuffer = false; var c = new DrawCommand({ shaderProgram : simpleShaderProgram, @@ -266,12 +266,12 @@ defineSuite([ scene.renderForSpecs(); expect(c.execute).toHaveBeenCalled(); - scene.logDepthBuffer = originalLogDepth; + scene.logarithmicDepthBuffer = originalLogDepth; }); it('debugShowBoundingVolume draws a bounding sphere', function() { - var originalLogDepth = scene.logDepthBuffer; - scene.logDepthBuffer = false; + var originalLogDepth = scene.logarithmicDepthBuffer; + scene.logarithmicDepthBuffer = false; var radius = 10.0; var center = Cartesian3.add(scene.camera.position, scene.camera.direction, new Cartesian3()); @@ -292,12 +292,12 @@ defineSuite([ expect(rgba[0]).not.toEqual(0); // Red bounding sphere }); - scene.logDepthBuffer = originalLogDepth; + scene.logarithmicDepthBuffer = originalLogDepth; }); it('debugShowCommands tints commands', function() { - var originalLogDepth = scene.logDepthBuffer; - scene.logDepthBuffer = false; + var originalLogDepth = scene.logarithmicDepthBuffer; + scene.logarithmicDepthBuffer = false; var c = new DrawCommand({ shaderProgram : simpleShaderProgram, @@ -320,7 +320,7 @@ defineSuite([ expect(c._debugColor).toBeDefined(); scene.debugShowCommands = false; - scene.logDepthBuffer = originalLogDepth; + scene.logarithmicDepthBuffer = originalLogDepth; }); it('debugShowFramesPerSecond', function() { diff --git a/Specs/Scene/ShadowMapSpec.js b/Specs/Scene/ShadowMapSpec.js index f6d9783848d7..3b702985d4e9 100644 --- a/Specs/Scene/ShadowMapSpec.js +++ b/Specs/Scene/ShadowMapSpec.js @@ -1117,7 +1117,7 @@ defineSuite([ } var callCount; - if (!scene.logDepthBuffer) { + if (!scene.logarithmicDepthBuffer) { // Expect derived commands to be updated twice for both the floor and box, // once on the first frame and again when the shadow map is dirty callCount = 4; From 1d5adab28771a50a69ab6296eefe81b60285c0f8 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 12 Mar 2018 16:14:13 -0400 Subject: [PATCH 49/68] Fix tests broken by last commit. --- Source/Scene/Primitive.js | 2 +- Source/Scene/Scene.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/Scene/Primitive.js b/Source/Scene/Primitive.js index 59455919c3cb..199ec0e3bea4 100644 --- a/Source/Scene/Primitive.js +++ b/Source/Scene/Primitive.js @@ -1011,7 +1011,7 @@ define([ 'varying float v_WindowZ;\n' + 'void main() {\n' + ' czm_non_depth_clamp_main();\n' + - '#ifdef GL_EXT_frag_depth\n' + + '#if defined(GL_EXT_frag_depth) && !defined(LOG_DEPTH)\n' + ' gl_FragDepthEXT = min(v_WindowZ * gl_FragCoord.w, 1.0);\n' + '#endif\n' + '}\n'; diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 3bc47456775a..57fab8eaa923 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -701,16 +701,17 @@ define([ var camera = new Camera(this); this._camera = camera; - this._cameraClone = Camera.clone(camera); this._screenSpaceCameraController = new ScreenSpaceCameraController(this); this._mapMode2D = defaultValue(options.mapMode2D, MapMode2D.INFINITE_SCROLL); - this._frustumChanged = true; if (this._logDepthBuffer) { this._camera.frustum.near = 1.0; this._camera.frustum.far = 10000000000.0; } + this._cameraClone = Camera.clone(camera); + this._frustumChanged = true; + // Keeps track of the state of a frame. FrameState is the state across // the primitives of the scene. This state is for internally keeping track // of celestial and environment effects that need to be updated/rendered in From e029f10d063a4aa706f7b4656d55bc038adaecf5 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 12 Mar 2018 16:24:39 -0400 Subject: [PATCH 50/68] Fix batched classification test. --- Specs/Scene/ClassificationPrimitiveSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Specs/Scene/ClassificationPrimitiveSpec.js b/Specs/Scene/ClassificationPrimitiveSpec.js index f4dfa7a5e3ba..20026dfe32a1 100644 --- a/Specs/Scene/ClassificationPrimitiveSpec.js +++ b/Specs/Scene/ClassificationPrimitiveSpec.js @@ -91,7 +91,7 @@ defineSuite([ beforeEach(function() { scene.morphTo3D(0); - rectangle = Rectangle.fromDegrees(-80.0, 20.0, -70.0, 30.0); + rectangle = Rectangle.fromDegrees(-75.0, 25.0, -70.0, 30.0); var depthColorAttribute = ColorGeometryInstanceAttribute.fromColor(new Color(0.0, 0.0, 1.0, 1.0)); depthColor = depthColorAttribute.value; From f14a2cf485c5893d1d6a8c0fdf9cce781106e169 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 12 Mar 2018 16:49:27 -0400 Subject: [PATCH 51/68] Fix inverted classification test. --- Specs/Scene/Vector3DTileGeometrySpec.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Specs/Scene/Vector3DTileGeometrySpec.js b/Specs/Scene/Vector3DTileGeometrySpec.js index 4be21f4c0efd..d9ad9629df3d 100644 --- a/Specs/Scene/Vector3DTileGeometrySpec.js +++ b/Specs/Scene/Vector3DTileGeometrySpec.js @@ -570,7 +570,7 @@ defineSuite([ }); it('renders with inverted classification' + webglMessage, function() { - var radii = new Cartesian3(10.0, 10.0, 1000.0); + var radii = new Cartesian3(100.0, 100.0, 1000.0); var ellipsoids = packEllipsoids([{ modelMatrix : Matrix4.IDENTITY, radii : radii @@ -597,18 +597,16 @@ defineSuite([ batchTable : batchTable })); return loadGeometries(geometry).then(function() { - scene.camera.lookAtTransform(modelMatrix, new Cartesian3(radii.x, 0.0, 1.0)); + scene.camera.lookAtTransform(modelMatrix, new Cartesian3(0.0, 0.0, 1.0)); + expect(scene).toRender([255, 255, 255, 255]); + scene.camera.lookAtTransform(modelMatrix, new Cartesian3(radii.x, 0.0, 1.0)); expect(scene).toRender([255, 0, 0, 255]); scene.invertClassification = true; scene.invertClassificationColor = new Color(0.25, 0.25, 0.25, 1.0); - expect(scene).toRender([64, 0, 0, 255]); - scene.camera.lookAtTransform(modelMatrix, new Cartesian3(0.0, 0.0, 1.0)); - expect(scene).toRender([255, 255, 255, 255]); - scene.invertClassification = false; }); }); From 7c06ec8df7accad3690fdbec6de867f8976d734c Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 12 Mar 2018 17:10:10 -0400 Subject: [PATCH 52/68] Fix tests when log depth isn't supported. --- Specs/DataSources/EntityClusterSpec.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Specs/DataSources/EntityClusterSpec.js b/Specs/DataSources/EntityClusterSpec.js index 5cdad2ac453e..57b5706a9dc8 100644 --- a/Specs/DataSources/EntityClusterSpec.js +++ b/Specs/DataSources/EntityClusterSpec.js @@ -30,8 +30,8 @@ defineSuite([ var scene; var cluster; - - var depth = 0.1; + var depth; + var farDepth; beforeAll(function() { scene = createScene({ @@ -86,6 +86,13 @@ defineSuite([ scene.initializeFrame(); scene.render(); + + if (scene.logarithmicDepthBuffer) { + depth = farDepth = 0.1; + } else { + depth = 0.5; + farDepth = 0.9; + } }); afterAll(function() { @@ -508,13 +515,13 @@ defineSuite([ var billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), farDepth); entity = new Entity(); billboard = cluster.getBillboard(entity); billboard.id = entity; billboard.image = createBillboardImage(); - billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); + billboard.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), farDepth); var frameState = scene.frameState; cluster.update(frameState); @@ -552,13 +559,13 @@ defineSuite([ var point = cluster.getPoint(entity); point.id = entity; point.pixelSize = 1; - point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), depth); + point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(0.0, 0.0), farDepth); entity = new Entity(); point = cluster.getPoint(entity); point.id = entity; point.pixelSize = 1; - point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), depth); + point.position = SceneTransforms.drawingBufferToWgs84Coordinates(scene, new Cartesian2(scene.canvas.clientWidth, scene.canvas.clientHeight), farDepth); var frameState = scene.frameState; cluster.update(frameState); From 5913ffe87c6b3a6e503aff471daf4f9090e389fb Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 12 Mar 2018 19:46:39 -0400 Subject: [PATCH 53/68] Fixes after merge. --- Source/Scene/ClassificationModel.js | 12 ++++---- Source/Scene/Model.js | 44 +++++++++++++++++++++++------ Source/Scene/ModelUtility.js | 34 ++++++++++++---------- 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/Source/Scene/ClassificationModel.js b/Source/Scene/ClassificationModel.js index eaafe4b0aa5c..2308c1bebdf1 100644 --- a/Source/Scene/ClassificationModel.js +++ b/Source/Scene/ClassificationModel.js @@ -675,7 +675,7 @@ define([ var modelViewProjectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'MODELVIEWPROJECTION'); var uniformDecl; - var computePosition; + var toClip; if (!defined(modelViewProjectionName)) { var projectionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'PROJECTION'); @@ -687,12 +687,14 @@ define([ uniformDecl = 'uniform mat4 ' + modelViewName + ';\n' + 'uniform mat4 ' + projectionName + ';\n'; - computePosition = ' vec4 positionInClipCoords = ' + projectionName + ' * ' + modelViewName + ' * vec4(' + positionName + ', 1.0);\n'; + toClip = projectionName + ' * ' + modelViewName + ' * vec4(' + positionName + ', 1.0)'; } else { uniformDecl = 'uniform mat4 ' + modelViewProjectionName + ';\n'; - computePosition = ' vec4 positionInClipCoords = ' + modelViewProjectionName + ' * vec4(' + positionName + ', 1.0);\n'; + toClip = modelViewProjectionName + ' * vec4(' + positionName + ', 1.0)'; } + var computePosition = ' vec4 positionInClipCoords = ' + toClip + ';\n'; + var vs = 'attribute vec3 ' + positionName + ';\n' + 'attribute float ' + batchIdName + ';\n' + @@ -718,7 +720,7 @@ define([ var drawVS = modifyShader(vs, model._vertexShaderLoaded); var drawFS = modifyShader(fs, model._classificationShaderLoaded); - drawVS = ModelUtility.modifyVertexShaderForLogDepth(gltf, drawVS); + drawVS = ModelUtility.modifyVertexShaderForLogDepth(drawVS, toClip); drawFS = ModelUtility.modifyFragmentShaderForLogDepth(drawFS); model._shaderProgram = { @@ -731,7 +733,7 @@ define([ var pickVS = modifyShader(vs, model._pickVertexShaderLoaded); var pickFS = modifyShader(fs, model._pickFragmentShaderLoaded); - pickVS = ModelUtility.modifyVertexShaderForLogDepth(gltf, pickVS); + pickVS = ModelUtility.modifyVertexShaderForLogDepth(pickVS, toClip); pickFS = ModelUtility.modifyFragmentShaderForLogDepth(pickFS); model._pickShaderProgram = { diff --git a/Source/Scene/Model.js b/Source/Scene/Model.js index 85990ae8a704..0ac3c0c8046a 100644 --- a/Source/Scene/Model.js +++ b/Source/Scene/Model.js @@ -1927,6 +1927,7 @@ define([ var program = model._sourcePrograms[id]; var shaders = model._sourceShaders; var quantizedVertexShaders = model._quantizedVertexShaders; + var toClipCoordinatesGLSL = model._toClipCoordinatesGLSL[id]; var vs = shaders[program.vertexShader].extras._pipeline.source; var fs = shaders[program.fragmentShader].extras._pipeline.source; @@ -1943,6 +1944,9 @@ define([ var drawVS = modifyShader(vs, id, model._vertexShaderLoaded); var drawFS = modifyShader(fs, id, model._fragmentShaderLoaded); + drawVS = ModelUtility.modifyVertexShaderForLogDepth(drawVS, toClipCoordinatesGLSL); + drawFS = ModelUtility.modifyFragmentShaderForLogDepth(drawFS); + var pickFS, pickVS; if (model.allowPicking) { // PERFORMANCE_IDEA: Can optimize this shader with a glTF hint. https://github.com/KhronosGroup/glTF/issues/181 @@ -1952,6 +1956,9 @@ define([ if (!model._pickFragmentShaderLoaded) { pickFS = ShaderSource.createPickFragmentShaderSource(fs, 'uniform'); } + + pickVS = ModelUtility.modifyVertexShaderForLogDepth(pickVS, toClipCoordinatesGLSL); + pickFS = ModelUtility.modifyFragmentShaderForLogDepth(pickFS); } createAttributesAndProgram(id, drawFS, drawVS, pickFS, pickVS, model, context); } @@ -1960,6 +1967,7 @@ define([ var program = model._sourcePrograms[id]; var shaders = model._sourceShaders; var quantizedVertexShaders = model._quantizedVertexShaders; + var toClipCoordinatesGLSL = model._toClipCoordinatesGLSL[id]; var clippingPlaneCollection = model.clippingPlanes; var addClippingPlaneCode = isClippingEnabled(model); @@ -1982,6 +1990,9 @@ define([ var drawVS = modifyShader(vs, id, model._vertexShaderLoaded); var drawFS = modifyShader(finalFS, id, model._fragmentShaderLoaded); + drawVS = ModelUtility.modifyVertexShaderForLogDepth(drawVS, toClipCoordinatesGLSL); + drawFS = ModelUtility.modifyFragmentShaderForLogDepth(drawFS); + var pickFS, pickVS; if (model.allowPicking) { // PERFORMANCE_IDEA: Can optimize this shader with a glTF hint. https://github.com/KhronosGroup/glTF/issues/181 @@ -1995,6 +2006,9 @@ define([ if (addClippingPlaneCode) { pickFS = modifyShaderForClippingPlanes(pickFS, clippingPlaneCollection); } + + pickVS = ModelUtility.modifyVertexShaderForLogDepth(pickVS, toClipCoordinatesGLSL); + pickFS = ModelUtility.modifyFragmentShaderForLogDepth(pickFS); } createAttributesAndProgram(id, drawFS, drawVS, pickFS, pickVS, model, context); } @@ -2014,9 +2028,6 @@ define([ } } - drawVS = ModelUtility.modifyVertexShaderForLogDepth(model.gltf, drawVS); - drawFS = ModelUtility.modifyFragmentShaderForLogDepth(drawFS); - model._rendererResources.programs[id] = ShaderProgram.fromCache({ context : context, vertexShaderSource : drawVS, @@ -2025,9 +2036,6 @@ define([ }); if (model.allowPicking) { - pickVS = ModelUtility.modifyVertexShaderForLogDepth(model.gltf, pickVS); - pickFS = ModelUtility.modifyFragmentShaderForLogDepth(pickFS); - model._rendererResources.pickPrograms[id] = ShaderProgram.fromCache({ context : context, vertexShaderSource : pickVS, @@ -3314,10 +3322,30 @@ define([ var scene3DOnly = frameState.scene3DOnly; // Retain references to updated source shaders and programs for rebuilding as needed - model._sourcePrograms = model.gltf.programs; - model._sourceShaders = model.gltf.shaders; + var programs = model._sourcePrograms = model.gltf.programs; + var shaders = model._sourceShaders = model.gltf.shaders; model._hasPremultipliedAlpha = hasPremultipliedAlpha(model); + var quantizedVertexShaders = model._quantizedVertexShaders; + var toClipCoordinates = model._toClipCoordinatesGLSL = {}; + for (var id in programs) { + if (programs.hasOwnProperty(id)) { + var program = programs[id]; + var shader = shaders[program.vertexShader].extras._pipeline.source; + if (model.extensionsUsed.WEB3D_quantized_attributes) { + var quantizedVS = quantizedVertexShaders[id]; + if (!defined(quantizedVS)) { + quantizedVS = modifyShaderForQuantizedAttributes(shader, id, model); + quantizedVertexShaders[id] = quantizedVS; + } + shader = quantizedVS; + } + + shader = modifyShader(shader, id, model._vertexShaderLoaded); + toClipCoordinates[id] = ModelUtility.toClipCoordinatesGLSL(model.gltf, shader); + } + } + ModelUtility.checkSupportedGlExtensions(model.gltf.glExtensionsUsed, context); if (model._loadRendererResourcesFromCache) { var resources = model._rendererResources; diff --git a/Source/Scene/ModelUtility.js b/Source/Scene/ModelUtility.js index 09b2d1b8b3bf..7717822aa971 100644 --- a/Source/Scene/ModelUtility.js +++ b/Source/Scene/ModelUtility.js @@ -278,20 +278,7 @@ define([ }; }; - ModelUtility.modifyFragmentShaderForLogDepth = function(shader) { - shader = ShaderSource.replaceMain(shader, 'czm_depth_main'); - shader += - '\n' + - 'void main() \n' + - '{ \n' + - ' czm_depth_main(); \n' + - ' czm_writeLogDepth(); \n' + - '} \n'; - - return shader; - }; - - ModelUtility.modifyVertexShaderForLogDepth = function(gltf, shader) { + ModelUtility.toClipCoordinatesGLSL = function(gltf, shader) { var positionName = ModelUtility.getAttributeOrUniformBySemantic(gltf, 'POSITION'); var decodedPositionName = positionName.replace('a_', 'gltf_a_dec_'); if (shader.indexOf(decodedPositionName) !== -1) { @@ -310,13 +297,30 @@ define([ modelViewProjectionName = projectionName + ' * ' + modelViewName; } + return modelViewProjectionName + ' * vec4(' + positionName + '.xyz, 1.0)'; + }; + + ModelUtility.modifyFragmentShaderForLogDepth = function(shader) { + shader = ShaderSource.replaceMain(shader, 'czm_depth_main'); + shader += + '\n' + + 'void main() \n' + + '{ \n' + + ' czm_depth_main(); \n' + + ' czm_writeLogDepth(); \n' + + '} \n'; + + return shader; + }; + + ModelUtility.modifyVertexShaderForLogDepth = function(shader, toClipCoordinatesGLSL) { shader = ShaderSource.replaceMain(shader, 'czm_depth_main'); shader += '\n' + 'void main() \n' + '{ \n' + ' czm_depth_main(); \n' + - ' czm_vertexLogDepth(' + modelViewProjectionName + ' * vec4(' + positionName + '.xyz, 1.0)); \n' + + ' czm_vertexLogDepth(' + toClipCoordinatesGLSL + '); \n' + '} \n'; return shader; From a648dea34554ae068133c11d35f1d0ae25f2691c Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Wed, 14 Mar 2018 14:50:01 -0400 Subject: [PATCH 54/68] Updates from review. --- Source/Scene/Expression.js | 2 +- Source/Scene/Scene.js | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Source/Scene/Expression.js b/Source/Scene/Expression.js index 32c8f98f97ab..02a958f6747e 100644 --- a/Source/Scene/Expression.js +++ b/Source/Scene/Expression.js @@ -279,7 +279,7 @@ define([ } function log2(number) { - return CesiumMath.log2(number, 2.0); + return CesiumMath.log2(number); } function getEvaluateUnaryComponentwise(operation) { diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 57fab8eaa923..f6d565f445ff 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -265,9 +265,6 @@ define([ creditViewport = canvas.parentNode; } - this._logDepthBuffer = context.fragmentDepth; - this._logDepthBufferDirty = true; - this._id = createGuid(); this._jobScheduler = new JobScheduler(); this._frameState = new FrameState(context, new CreditDisplay(creditContainer, ' • ', creditViewport), this._jobScheduler); @@ -290,6 +287,10 @@ define([ this._primitives = new PrimitiveCollection(); this._groundPrimitives = new PrimitiveCollection(); + this._logDepthBuffer = undefined; + this._logDepthBufferDirty = true; + this.logarithmicDepthBuffer = context.fragmentDepth; + this._tweens = new TweenCollection(); this._shaderFrameCount = 0; @@ -792,7 +793,7 @@ define([ this.initializeFrame(); } - var OPAQUE_FRUSTUM_NEAR_OFFSET = 0.9; + var OPAQUE_FRUSTUM_NEAR_OFFSET; function updateGlobeListeners(scene, globe) { for (var i = 0; i < scene._removeGlobeCallbacks.length; ++i) { @@ -1402,6 +1403,8 @@ define([ if (this._context.fragmentDepth && this._logDepthBuffer !== value) { this._logDepthBuffer = value; this._logDepthBufferDirty = true; + + OPAQUE_FRUSTUM_NEAR_OFFSET = this._logDepthBuffer ? 0.9 : 0.9999; } } } From b6de470a15681ff472948031cc201fa58fd9aeba Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 19 Mar 2018 18:50:47 -0400 Subject: [PATCH 55/68] Fix translucent overlap. Fix geometry showing through depth plane. Update default near/far distances. --- Source/Scene/DepthPlane.js | 1 + Source/Scene/Scene.js | 3 ++- Source/Shaders/Builtin/Functions/vertexLogDepth.glsl | 2 +- Source/Shaders/Builtin/Functions/writeLogDepth.glsl | 6 +++++- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Source/Scene/DepthPlane.js b/Source/Scene/DepthPlane.js index d8ea2af635eb..4df50d2663ef 100644 --- a/Source/Scene/DepthPlane.js +++ b/Source/Scene/DepthPlane.js @@ -154,6 +154,7 @@ define([ fs.sources.push(extension); fs.defines.push('LOG_DEPTH'); vs.defines.push('LOG_DEPTH'); + vs.defines.push('DISABLE_GL_POSITION_LOG_DEPTH'); } this._sp = ShaderProgram.replaceCache({ diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index f6d565f445ff..45202b97b57c 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -705,8 +705,9 @@ define([ this._screenSpaceCameraController = new ScreenSpaceCameraController(this); this._mapMode2D = defaultValue(options.mapMode2D, MapMode2D.INFINITE_SCROLL); + this.logarithmicDepthFarToNearRatio = 1000000000.0; if (this._logDepthBuffer) { - this._camera.frustum.near = 1.0; + this._camera.frustum.near = 0.1; this._camera.frustum.far = 10000000000.0; } diff --git a/Source/Shaders/Builtin/Functions/vertexLogDepth.glsl b/Source/Shaders/Builtin/Functions/vertexLogDepth.glsl index 673f4240df54..1a16d725f5ce 100644 --- a/Source/Shaders/Builtin/Functions/vertexLogDepth.glsl +++ b/Source/Shaders/Builtin/Functions/vertexLogDepth.glsl @@ -4,7 +4,7 @@ varying vec3 v_logPositionEC; #endif void czm_updatePositionDepth() { -#ifdef LOG_DEPTH +#if defined(LOG_DEPTH) && !defined(DISABLE_GL_POSITION_LOG_DEPTH) v_logPositionEC = (czm_inverseProjection * gl_Position).xyz; gl_Position.z = log2(max(1e-6, 1.0 + gl_Position.w)) * czm_logFarDistance - 1.0; diff --git a/Source/Shaders/Builtin/Functions/writeLogDepth.glsl b/Source/Shaders/Builtin/Functions/writeLogDepth.glsl index 60a591aad01e..49d01bd2e72f 100644 --- a/Source/Shaders/Builtin/Functions/writeLogDepth.glsl +++ b/Source/Shaders/Builtin/Functions/writeLogDepth.glsl @@ -20,7 +20,11 @@ void czm_writeLogDepth(float logZ) { #if defined(GL_EXT_frag_depth) && defined(LOG_DEPTH) && !defined(DISABLE_LOG_DEPTH_FRAGMENT_WRITE) float halfLogFarDistance = czm_logFarDistance * 0.5; - gl_FragDepthEXT = log2(logZ) * halfLogFarDistance; + float depth = log2(logZ); + if (depth < log2(czm_currentFrustum.x)) { + discard; + } + gl_FragDepthEXT = depth * halfLogFarDistance; #endif } From 1525950363bac581a6cb5cbffcb02faa06fb38fa Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 20 Mar 2018 15:31:59 -0400 Subject: [PATCH 56/68] Fix picking depth which had a hardcoded value for the near plane. --- Source/Scene/Scene.js | 9 ++++----- Source/Scene/SceneTransforms.js | 2 +- Source/Scene/SceneTransitioner.js | 4 ++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 45202b97b57c..d4f6a5da71e4 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -289,7 +289,7 @@ define([ this._logDepthBuffer = undefined; this._logDepthBufferDirty = true; - this.logarithmicDepthBuffer = context.fragmentDepth; + this.logarithmicDepthBuffer = false;//context.fragmentDepth; this._tweens = new TweenCollection(); @@ -469,11 +469,11 @@ define([ this.farToNearRatio = 1000.0; /** - * The far-to-near ratio of the multi-frustum when using a logarithmic depth buffer. The default is 1e6. + * The far-to-near ratio of the multi-frustum when using a logarithmic depth buffer. The default is 1e9. * @type {Number} - * @default 1000000.0 + * @default 1e9 */ - this.logarithmicDepthFarToNearRatio = 1000000.0; + this.logarithmicDepthFarToNearRatio = 1e9; /** * Determines the uniform depth size in meters of each frustum of the multifrustum in 2D. If a primitive or model close @@ -705,7 +705,6 @@ define([ this._screenSpaceCameraController = new ScreenSpaceCameraController(this); this._mapMode2D = defaultValue(options.mapMode2D, MapMode2D.INFINITE_SCROLL); - this.logarithmicDepthFarToNearRatio = 1000000000.0; if (this._logDepthBuffer) { this._camera.frustum.near = 0.1; this._camera.frustum.far = 10000000000.0; diff --git a/Source/Scene/SceneTransforms.js b/Source/Scene/SceneTransforms.js index ce8688e31204..96c32e4f738c 100644 --- a/Source/Scene/SceneTransforms.js +++ b/Source/Scene/SceneTransforms.js @@ -312,7 +312,7 @@ define([ // to perspective form // (far - far * near / z) / (far - near) depth = Math.pow(2.0, depth * CesiumMath.log2(far + 1.0)) - 1.0; - depth = far * (1.0 - 1.0 / depth) / (far - near); + depth = far * (1.0 - near / depth) / (far - near); } var viewport = scene._passState.viewport; diff --git a/Source/Scene/SceneTransitioner.js b/Source/Scene/SceneTransitioner.js index 43dc3dce89ab..5cb7f5b77e9c 100644 --- a/Source/Scene/SceneTransitioner.js +++ b/Source/Scene/SceneTransitioner.js @@ -872,7 +872,7 @@ define([ var frustum = camera.frustum; if (scene._logDepthBuffer && !(frustum instanceof OrthographicFrustum || frustum instanceof OrthographicOffCenterFrustum)) { - frustum.near = 1.0; + frustum.near = 0.1; frustum.far = 10000000000.0; } @@ -929,7 +929,7 @@ define([ var frustum = camera.frustum; if (scene._logDepthBuffer && !(frustum instanceof OrthographicFrustum || frustum instanceof OrthographicOffCenterFrustum)) { - frustum.near = 1.0; + frustum.near = 0.1; frustum.far = 10000000000.0; } From 6a5a6b0c03cf179a4aae367e339862b8afc2aa9b Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 20 Mar 2018 15:43:46 -0400 Subject: [PATCH 57/68] Fix issue setting enabling/disabling log depth. --- Source/Scene/Scene.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index d4f6a5da71e4..b77f6acd360e 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -289,7 +289,7 @@ define([ this._logDepthBuffer = undefined; this._logDepthBufferDirty = true; - this.logarithmicDepthBuffer = false;//context.fragmentDepth; + this.logarithmicDepthBuffer = context.fragmentDepth; this._tweens = new TweenCollection(); @@ -1400,7 +1400,8 @@ define([ return this._logDepthBuffer; }, set : function(value) { - if (this._context.fragmentDepth && this._logDepthBuffer !== value) { + value = this._context.fragmentDepth && value; + if (this._logDepthBuffer !== value) { this._logDepthBuffer = value; this._logDepthBufferDirty = true; From fb3a2b2fe9ae4e4dce56055cf599d7fd50dcbd5a Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 20 Mar 2018 17:08:33 -0400 Subject: [PATCH 58/68] Fix eslint errors. --- Specs/Scene/MultifrustumSpec.js | 3 ++- Specs/Scene/SceneSpec.js | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Specs/Scene/MultifrustumSpec.js b/Specs/Scene/MultifrustumSpec.js index 8c7da972d5f7..e28a5dd2099b 100644 --- a/Specs/Scene/MultifrustumSpec.js +++ b/Specs/Scene/MultifrustumSpec.js @@ -196,7 +196,8 @@ defineSuite([ var calls = DrawCommand.prototype.execute.calls.all(); var billboardCall; - for (var i = 0; i < calls.length; ++i) { + var i; + for (i = 0; i < calls.length; ++i) { if (calls[i].object.owner instanceof BillboardCollection) { billboardCall = calls[i]; break; diff --git a/Specs/Scene/SceneSpec.js b/Specs/Scene/SceneSpec.js index 333e1a457605..567fc1213ce8 100644 --- a/Specs/Scene/SceneSpec.js +++ b/Specs/Scene/SceneSpec.js @@ -726,8 +726,6 @@ defineSuite([ scene.destroyForSpecs(); }); - var pickedPosition2D = new Cartesian3(-455861.7055871038, -5210523.137686572, 3637866.6638769475); - it('pickPosition', function() { if (!scene.pickPositionSupported) { return; From 530f41d4ac9bb3d3a7f1519abaa830416b9f8591 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 26 Mar 2018 14:22:04 -0400 Subject: [PATCH 59/68] Fix polyline geometry. --- Source/Scene/Scene.js | 52 ++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 47ebfe3793ed..e9f0db0116f3 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -3432,7 +3432,8 @@ define([ return result; } - var logDepthRegex = /\s+czm_writeLogDepth\(/; + var writeLogDepthRegex = /\s+czm_writeLogDepth\(/; + var vertexlogDepthRegex = /\s+czm_vertexLogDepth\(/; var extensionRegex = /\s*#extension\s+GL_EXT_frag_depth\s*:\s*enable/; function getLogDepthShaderProgram(context, shaderProgram) { @@ -3447,13 +3448,39 @@ define([ fs.defines = defined(fs.defines) ? fs.defines.slice(0) : []; fs.defines.push('LOG_DEPTH'); - var addExtension = true; + var i; + var logMain; var writesLogDepth = false; - var sources = fs.sources; + var sources = vs.sources; var length = sources.length; - var i; for (i = 0; i < length; ++i) { - if (logDepthRegex.test(sources[i])) { + if (vertexlogDepthRegex.test(sources[i])) { + writesLogDepth = true; + break; + } + } + + if (!writesLogDepth) { + for (i = 0; i < length; ++i) { + sources[i] = ShaderSource.replaceMain(sources[i], 'czm_log_depth_main'); + } + + logMain = + '\n\n' + + 'void main() \n' + + '{ \n' + + ' czm_log_depth_main(); \n' + + ' czm_vertexLogDepth(); \n' + + '} \n'; + sources.push(logMain); + } + + var addExtension = true; + writesLogDepth = false; + sources = fs.sources; + length = sources.length; + for (i = 0; i < length; ++i) { + if (writeLogDepthRegex.test(sources[i])) { writesLogDepth = true; } if (extensionRegex.test(sources[i])) { @@ -3481,21 +3508,6 @@ define([ ' czm_log_depth_main(); \n' + ' czm_writeLogDepth(); \n' + '} \n'; - - var vertexSources = vs.sources; - length = vertexSources.length; - for (i = 0; i < length; ++i) { - vertexSources[i] = ShaderSource.replaceMain(vertexSources[i], 'czm_log_depth_main'); - } - - var logMain = - '\n\n' + - 'void main() \n' + - '{ \n' + - ' czm_log_depth_main(); \n' + - ' czm_vertexLogDepth(); \n' + - '} \n'; - vertexSources.push(logMain); } sources.push(logSource); From 979b5417993b7c1c1e5b61af64b6129901ed6f6c Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 2 Apr 2018 16:41:21 -0400 Subject: [PATCH 60/68] Update CHANGES.md. --- CHANGES.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 456c661c3070..c74dbf86736a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,13 +1,21 @@ Change Log ========== +### 1.46 - 2018-05-01 + +##### Breaking Changes :mega: +* `Camera.distanceToBoundingSphere` now returns the signed distance to the bounding sphere. Positive values indicate that the bounding sphere is in the positive half-plane of the camera position and view direction while a negative value indicates it is in the negative half-plane. + +##### Additions :tada: +* Added option `logDepthBuffer` to `Viewer`. With this option there is typically a single frustum using logarithmic depth rendered. This increases performance by issuing less draw calls to the GPU and helps to avoid artifacts on the connection of two frustums. [#5851](https://github.com/AnalyticalGraphicsInc/cesium/pull/5851) +* Added `Math.log2` to compute the base 2 logarithm of a number. + ### 1.44 - 2018-04-02 ##### Breaking Changes :mega: * `GeometryVisualizer` now requires `primitive` and `groundPrimitive` parameters. [#6316](https://github.com/AnalyticalGraphicsInc/cesium/pull/6316) * For all classes/functions that take a `Resource` instance, all additional parameters that are part of the `Resource` class have been removed. This generally includes `proxy`, `headers` and `query` parameters. * All low level load functions including `loadArrayBuffer`, `loadBlob`, `loadImage`, `loadJson`, `loadJsonp`, `loadText`, `loadXML` and `loadWithXhr` have been removed. Please use the equivalent `fetch` functions on the `Resource` class. -* `Camera.distanceToBoundingSphere` now returns the signed distance to the bounding sphere. Positive values indicate that the bounding sphere is in the positive half-plane of the camera position and view direction while a negative value indicates it is in the negative half-plane. ##### Deprecated :hourglass_flowing_sand: * `ClippingPlaneCollection` is now supported in Internet Explorer, so `ClippingPlaneCollection.isSupported` has been deprecated and will be removed in Cesium 1.45. @@ -38,8 +46,6 @@ Change Log * All ground geometry from one `DataSource` will render in front of all ground geometry from another `DataSource` in the same collection with a lower index. * Use `DataSourceCollection.raise`, `DataSourceCollection.lower`, `DataSourceCollection.raiseToTop` and `DataSourceCollection.lowerToBottom` functions to change the ordering of a `DataSource` in the collection. * Improved processing order of 3D tiles. [#6364](https://github.com/AnalyticalGraphicsInc/cesium/pull/6364) -* Added option `logDepthBuffer` to `Viewer`. With this option there is typically a single frustum using logarithmic depth rendered. This increases performance by issuing less draw calls to the GPU and helps to avoid artifacts on the connection of two frustums. [#5851](https://github.com/AnalyticalGraphicsInc/cesium/pull/5851) -* Added `Math.log2` to compute the base 2 logarithm of a number. ##### Fixes :wrench: * Fixed support of glTF-supplied tangent vectors. [#6302](https://github.com/AnalyticalGraphicsInc/cesium/pull/6302) From 4dd219ffb9edac9c605070fe63b4418f57a71226 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 5 Apr 2018 14:23:59 -0400 Subject: [PATCH 61/68] Fix Draco compressed models. --- Source/Scene/Model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Scene/Model.js b/Source/Scene/Model.js index 74c6d78a140d..9aa1769fef2d 100644 --- a/Source/Scene/Model.js +++ b/Source/Scene/Model.js @@ -3390,7 +3390,7 @@ define([ if (programs.hasOwnProperty(id)) { var program = programs[id]; var shader = shaders[program.vertexShader].extras._pipeline.source; - if (model.extensionsUsed.WEB3D_quantized_attributes) { + if (model.extensionsUsed.WEB3D_quantized_attributes || model._dequantizeInShader) { var quantizedVS = quantizedVertexShaders[id]; if (!defined(quantizedVS)) { quantizedVS = modifyShaderForQuantizedAttributes(shader, id, model); From b5f83d9e360575a843e3c98fadaf1dd8e8e67199 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 5 Apr 2018 14:38:43 -0400 Subject: [PATCH 62/68] Update CHANGES.md --- CHANGES.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index c74dbf86736a..5464839e5a7f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,13 +1,14 @@ Change Log ========== -### 1.46 - 2018-05-01 +### 1.45 - 2018-05-01 ##### Breaking Changes :mega: * `Camera.distanceToBoundingSphere` now returns the signed distance to the bounding sphere. Positive values indicate that the bounding sphere is in the positive half-plane of the camera position and view direction while a negative value indicates it is in the negative half-plane. ##### Additions :tada: * Added option `logDepthBuffer` to `Viewer`. With this option there is typically a single frustum using logarithmic depth rendered. This increases performance by issuing less draw calls to the GPU and helps to avoid artifacts on the connection of two frustums. [#5851](https://github.com/AnalyticalGraphicsInc/cesium/pull/5851) +* When a log depth buffer is supported, the frustum near and far planes default to `0.1` and `1e10` respectively. * Added `Math.log2` to compute the base 2 logarithm of a number. ### 1.44 - 2018-04-02 From d546e45fa788e4a32d3f453837639ddfdb3144bd Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 5 Apr 2018 15:22:34 -0400 Subject: [PATCH 63/68] Move derived commands to own class. --- Source/Scene/DerivedCommand.js | 217 +++++++++++++++++++++++++++++ Source/Scene/OIT.js | 12 +- Source/Scene/Scene.js | 246 +++++---------------------------- 3 files changed, 256 insertions(+), 219 deletions(-) create mode 100644 Source/Scene/DerivedCommand.js diff --git a/Source/Scene/DerivedCommand.js b/Source/Scene/DerivedCommand.js new file mode 100644 index 000000000000..9a8c77dcdf18 --- /dev/null +++ b/Source/Scene/DerivedCommand.js @@ -0,0 +1,217 @@ +define([ + '../Core/defined', + '../Renderer/DrawCommand', + '../Renderer/RenderState', + '../Renderer/ShaderSource' + ], function( + defined, + DrawCommand, + RenderState, + ShaderSource) { + 'use strict'; + + /** + * @private + */ + function DerivedCommand() {} + + var fragDepthRegex = /\bgl_FragDepthEXT\b/; + var discardRegex = /\bdiscard\b/; + + function getDepthOnlyShaderProgram(context, shaderProgram) { + var shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, 'depthOnly'); + if (!defined(shader)) { + var attributeLocations = shaderProgram._attributeLocations; + var fs = shaderProgram.fragmentShaderSource; + + var writesDepthOrDiscards = false; + var sources = fs.sources; + var length = sources.length; + for (var i = 0; i < length; ++i) { + if (fragDepthRegex.test(sources[i]) || discardRegex.test(sources[i])) { + writesDepthOrDiscards = true; + break; + } + } + + if (!writesDepthOrDiscards) { + fs = new ShaderSource({ + sources : ['void main() { gl_FragColor = vec4(1.0); }'] + }); + } + + shader = context.shaderCache.createDerivedShaderProgram(shaderProgram, 'depthOnly', { + vertexShaderSource : shaderProgram.vertexShaderSource, + fragmentShaderSource : fs, + attributeLocations : attributeLocations + }); + } + + return shader; + } + + function getDepthOnlyRenderState(scene, renderState) { + var cache = scene._depthOnlyRenderStateCache; + var depthOnlyState = cache[renderState.id]; + if (!defined(depthOnlyState)) { + var rs = RenderState.getState(renderState); + rs.depthMask = true; + rs.colorMask = { + red : false, + green : false, + blue : false, + alpha : false + }; + + depthOnlyState = RenderState.fromCache(rs); + cache[renderState.id] = depthOnlyState; + } + + return depthOnlyState; + } + + DerivedCommand.createDepthOnlyDerivedCommand = function(scene, command, context, result) { + // For a depth only pass, we bind a framebuffer with only a depth attachment (no color attachments), + // do not write color, and write depth. If the fragment shader doesn't modify the fragment depth + // or discard, the driver can replace the fragment shader with a pass-through shader. We're unsure if this + // actually happens so we modify the shader to use a pass-through fragment shader. + + if (!defined(result)) { + result = {}; + } + + var shader; + var renderState; + if (defined(result.depthOnlyCommand)) { + shader = result.depthOnlyCommand.shaderProgram; + renderState = result.depthOnlyCommand.renderState; + } + + result.depthOnlyCommand = DrawCommand.shallowClone(command, result.depthOnlyCommand); + + if (!defined(shader) || result.shaderProgramId !== command.shaderProgram.id) { + result.depthOnlyCommand.shaderProgram = getDepthOnlyShaderProgram(context, command.shaderProgram); + result.depthOnlyCommand.renderState = getDepthOnlyRenderState(scene, command.renderState); + result.shaderProgramId = command.shaderProgram.id; + } else { + result.depthOnlyCommand.shaderProgram = shader; + result.depthOnlyCommand.renderState = renderState; + } + + return result; + }; + + var writeLogDepthRegex = /\s+czm_writeLogDepth\(/; + var vertexlogDepthRegex = /\s+czm_vertexLogDepth\(/; + var extensionRegex = /\s*#extension\s+GL_EXT_frag_depth\s*:\s*enable/; + + function getLogDepthShaderProgram(context, shaderProgram) { + var shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, 'logDepth'); + if (!defined(shader)) { + var attributeLocations = shaderProgram._attributeLocations; + var vs = shaderProgram.vertexShaderSource.clone(); + var fs = shaderProgram.fragmentShaderSource.clone(); + + vs.defines = defined(vs.defines) ? vs.defines.slice(0) : []; + vs.defines.push('LOG_DEPTH'); + fs.defines = defined(fs.defines) ? fs.defines.slice(0) : []; + fs.defines.push('LOG_DEPTH'); + + var i; + var logMain; + var writesLogDepth = false; + var sources = vs.sources; + var length = sources.length; + for (i = 0; i < length; ++i) { + if (vertexlogDepthRegex.test(sources[i])) { + writesLogDepth = true; + break; + } + } + + if (!writesLogDepth) { + for (i = 0; i < length; ++i) { + sources[i] = ShaderSource.replaceMain(sources[i], 'czm_log_depth_main'); + } + + logMain = + '\n\n' + + 'void main() \n' + + '{ \n' + + ' czm_log_depth_main(); \n' + + ' czm_vertexLogDepth(); \n' + + '} \n'; + sources.push(logMain); + } + + var addExtension = true; + writesLogDepth = false; + sources = fs.sources; + length = sources.length; + for (i = 0; i < length; ++i) { + if (writeLogDepthRegex.test(sources[i])) { + writesLogDepth = true; + } + if (extensionRegex.test(sources[i])) { + addExtension = false; + } + } + + var logSource = ''; + if (addExtension) { + logSource += + '#ifdef GL_EXT_frag_depth \n' + + '#extension GL_EXT_frag_depth : enable \n' + + '#endif \n\n'; + } + + if (!writesLogDepth) { + for (i = 0; i < length; i++) { + sources[i] = ShaderSource.replaceMain(sources[i], 'czm_log_depth_main'); + } + + logSource += + '\n' + + 'void main() \n' + + '{ \n' + + ' czm_log_depth_main(); \n' + + ' czm_writeLogDepth(); \n' + + '} \n'; + } + + sources.push(logSource); + + shader = context.shaderCache.createDerivedShaderProgram(shaderProgram, 'logDepth', { + vertexShaderSource : vs, + fragmentShaderSource : fs, + attributeLocations : attributeLocations + }); + } + + return shader; + } + + DerivedCommand.createLogDepthCommand = function(command, context, result) { + if (!defined(result)) { + result = {}; + } + + var shader; + if (defined(result.command)) { + shader = result.command.shaderProgram; + } + + result.command = DrawCommand.shallowClone(command, result.command); + + if (!defined(shader) || result.shaderProgramId !== command.shaderProgram.id) { + result.command.shaderProgram = getLogDepthShaderProgram(context, command.shaderProgram); + result.shaderProgramId = command.shaderProgram.id; + } else { + result.command.shaderProgram = shader; + } + + return result; + }; + + return DerivedCommand; +}); diff --git a/Source/Scene/OIT.js b/Source/Scene/OIT.js index c7e0746c363b..88592c369d14 100644 --- a/Source/Scene/OIT.js +++ b/Source/Scene/OIT.js @@ -554,14 +554,14 @@ define([ for (j = 0; j < length; ++j) { command = commands[j]; - command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.command : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.translucentCommand : command.derivedCommands.oit.translucentCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } if (defined(invertClassification)) { command = invertClassification.unclassifiedCommand; - command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.command : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.translucentCommand : command.derivedCommands.oit.translucentCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } @@ -570,14 +570,14 @@ define([ for (j = 0; j < length; ++j) { command = commands[j]; - command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.command : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.alphaCommand : command.derivedCommands.oit.alphaCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } if (defined(invertClassification)) { command = invertClassification.unclassifiedCommand; - command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.command : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.alphaCommand : command.derivedCommands.oit.alphaCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } @@ -603,14 +603,14 @@ define([ for (var j = 0; j < length; ++j) { command = commands[j]; - command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.command : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.translucentCommand : command.derivedCommands.oit.translucentCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } if (defined(invertClassification)) { command = invertClassification.unclassifiedCommand; - command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.logDepthCommand : command; + command = defined(command.derivedCommands.logDepth) ? command.derivedCommands.logDepth.command : command; derivedCommand = (shadowsEnabled && command.receiveShadows) ? command.derivedCommands.oit.shadows.translucentCommand : command.derivedCommands.oit.translucentCommand; executeFunction(derivedCommand, scene, context, passState, debugFramebuffer); } diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index 5f7496a0e314..ef15edd788ae 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -55,6 +55,7 @@ define([ './CreditDisplay', './DebugCameraPrimitive', './DepthPlane', + './DerivedCommand', './DeviceOrientationCameraController', './Fog', './FrameState', @@ -134,6 +135,7 @@ define([ CreditDisplay, DebugCameraPrimitive, DepthPlane, + DerivedCommand, DeviceOrientationCameraController, Fog, FrameState, @@ -287,9 +289,8 @@ define([ this._primitives = new PrimitiveCollection(); this._groundPrimitives = new PrimitiveCollection(); - this._logDepthBuffer = undefined; + this._logDepthBuffer = context.fragmentDepth; this._logDepthBufferDirty = true; - this.logarithmicDepthBuffer = context.fragmentDepth; this._tweens = new TweenCollection(); @@ -461,7 +462,12 @@ define([ this.morphTime = 1.0; /** - * The far-to-near ratio of the multi-frustum. The default is 1,000.0. + * The far-to-near ratio of the multi-frustum when using a normal depth buffer. + *

+ * This value is used to create the near and far values for each frustum of the multi-frustum. It is only used + * when {@link Scene#logarithmicDepthBuffer} is false. When logarithmicDepthBuffer is + * true, use {@link Scene#logarithmicDepthFarToNearRatio}. + *

* * @type {Number} * @default 1000.0 @@ -469,7 +475,13 @@ define([ this.farToNearRatio = 1000.0; /** - * The far-to-near ratio of the multi-frustum when using a logarithmic depth buffer. The default is 1e9. + * The far-to-near ratio of the multi-frustum when using a logarithmic depth buffer. + *

+ * This value is used to create the near and far values for each frustum of the multi-frustum. It is only used + * when {@link Scene#logarithmicDepthBuffer} is true. When logarithmicDepthBuffer is + * false, use {@link Scene#farToNearRatio}. + *

+ * * @type {Number} * @default 1e9 */ @@ -478,7 +490,8 @@ define([ /** * 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. + * other hand, increasing this will increase performance but may cause z-fighting among primitives close to the surface. + * * @type {Number} * @default 1.75e6 */ @@ -793,8 +806,6 @@ define([ this.initializeFrame(); } - var OPAQUE_FRUSTUM_NEAR_OFFSET; - function updateGlobeListeners(scene, globe) { for (var i = 0; i < scene._removeGlobeCallbacks.length; ++i) { scene._removeGlobeCallbacks[i](); @@ -1404,10 +1415,17 @@ define([ if (this._logDepthBuffer !== value) { this._logDepthBuffer = value; this._logDepthBufferDirty = true; - - OPAQUE_FRUSTUM_NEAR_OFFSET = this._logDepthBuffer ? 0.9 : 0.9999; } } + }, + + /** + * @private + */ + opaqueFrustumNearOffset : { + get : function() { + return this._logDepthBuffer ? 0.9 : 0.9999; + } } }); @@ -1469,8 +1487,8 @@ define([ var logDepthCommand; var logDepthDerivedCommands; if (useLogDepth) { - derivedCommands.logDepth = createLogDepthCommand(command, context, derivedCommands.logDepth); - logDepthCommand = derivedCommands.logDepth.logDepthCommand; + derivedCommands.logDepth = DerivedCommand.createLogDepthCommand(command, context, derivedCommands.logDepth); + logDepthCommand = derivedCommands.logDepth.command; logDepthDerivedCommands = logDepthCommand.derivedCommands; } else { derivedCommands.logDepth = undefined; @@ -1502,7 +1520,7 @@ define([ } } - derivedCommands.depth = createDepthOnlyDerivedCommand(scene, command, context, derivedCommands.depth); + derivedCommands.depth = DerivedCommand.createDepthOnlyDerivedCommand(scene, command, context, derivedCommands.depth); } } @@ -1908,7 +1926,7 @@ define([ var lightShadowsEnabled = shadowsEnabled && (scene.frameState.shadowHints.lightShadowMaps.length > 0); if (scene._logDepthBuffer && defined(command.derivedCommands.logDepth)) { - command = command.derivedCommands.logDepth.logDepthCommand; + command = command.derivedCommands.logDepth.command; } if (scene.debugShowCommands || scene.debugShowFrustums) { @@ -2156,7 +2174,7 @@ define([ us.updateFrustum(frustum); } else { // Avoid tearing artifacts between adjacent frustums in the opaque passes - frustum.near = index !== 0 ? frustumCommands.near * OPAQUE_FRUSTUM_NEAR_OFFSET : frustumCommands.near; + frustum.near = index !== 0 ? frustumCommands.near * scene.opaqueFrustumNearOffset : frustumCommands.near; frustum.far = frustumCommands.far; us.updateFrustum(frustum); } @@ -3346,204 +3364,6 @@ define([ return object; }; - var fragDepthRegex = /\bgl_FragDepthEXT\b/; - var discardRegex = /\bdiscard\b/; - - function getDepthOnlyShaderProgram(context, shaderProgram) { - var shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, 'depthOnly'); - if (!defined(shader)) { - var attributeLocations = shaderProgram._attributeLocations; - var fs = shaderProgram.fragmentShaderSource; - - var writesDepthOrDiscards = false; - var sources = fs.sources; - var length = sources.length; - for (var i = 0; i < length; ++i) { - if (fragDepthRegex.test(sources[i]) || discardRegex.test(sources[i])) { - writesDepthOrDiscards = true; - break; - } - } - - if (!writesDepthOrDiscards) { - fs = new ShaderSource({ - sources : ['void main() { gl_FragColor = vec4(1.0); }'] - }); - } - - shader = context.shaderCache.createDerivedShaderProgram(shaderProgram, 'depthOnly', { - vertexShaderSource : shaderProgram.vertexShaderSource, - fragmentShaderSource : fs, - attributeLocations : attributeLocations - }); - } - - return shader; - } - - function getDepthOnlyRenderState(scene, renderState) { - var cache = scene._depthOnlyRenderStateCache; - var depthOnlyState = cache[renderState.id]; - if (!defined(depthOnlyState)) { - var rs = RenderState.getState(renderState); - rs.depthMask = true; - rs.colorMask = { - red : false, - green : false, - blue : false, - alpha : false - }; - - depthOnlyState = RenderState.fromCache(rs); - cache[renderState.id] = depthOnlyState; - } - - return depthOnlyState; - } - - function createDepthOnlyDerivedCommand(scene, command, context, result) { - // For a depth only pass, we bind a framebuffer with only a depth attachment (no color attachments), - // do not write color, and write depth. If the fragment shader doesn't modify the fragment depth - // or discard, the driver can replace the fragment shader with a pass-through shader. We're unsure if this - // actually happens so we modify the shader to use a pass-through fragment shader. - - if (!defined(result)) { - result = {}; - } - - var shader; - var renderState; - if (defined(result.depthOnlyCommand)) { - shader = result.depthOnlyCommand.shaderProgram; - renderState = result.depthOnlyCommand.renderState; - } - - result.depthOnlyCommand = DrawCommand.shallowClone(command, result.depthOnlyCommand); - - if (!defined(shader) || result.shaderProgramId !== command.shaderProgram.id) { - result.depthOnlyCommand.shaderProgram = getDepthOnlyShaderProgram(context, command.shaderProgram); - result.depthOnlyCommand.renderState = getDepthOnlyRenderState(scene, command.renderState); - result.shaderProgramId = command.shaderProgram.id; - } else { - result.depthOnlyCommand.shaderProgram = shader; - result.depthOnlyCommand.renderState = renderState; - } - - return result; - } - - var writeLogDepthRegex = /\s+czm_writeLogDepth\(/; - var vertexlogDepthRegex = /\s+czm_vertexLogDepth\(/; - var extensionRegex = /\s*#extension\s+GL_EXT_frag_depth\s*:\s*enable/; - - function getLogDepthShaderProgram(context, shaderProgram) { - var shader = context.shaderCache.getDerivedShaderProgram(shaderProgram, 'logDepth'); - if (!defined(shader)) { - var attributeLocations = shaderProgram._attributeLocations; - var vs = shaderProgram.vertexShaderSource.clone(); - var fs = shaderProgram.fragmentShaderSource.clone(); - - vs.defines = defined(vs.defines) ? vs.defines.slice(0) : []; - vs.defines.push('LOG_DEPTH'); - fs.defines = defined(fs.defines) ? fs.defines.slice(0) : []; - fs.defines.push('LOG_DEPTH'); - - var i; - var logMain; - var writesLogDepth = false; - var sources = vs.sources; - var length = sources.length; - for (i = 0; i < length; ++i) { - if (vertexlogDepthRegex.test(sources[i])) { - writesLogDepth = true; - break; - } - } - - if (!writesLogDepth) { - for (i = 0; i < length; ++i) { - sources[i] = ShaderSource.replaceMain(sources[i], 'czm_log_depth_main'); - } - - logMain = - '\n\n' + - 'void main() \n' + - '{ \n' + - ' czm_log_depth_main(); \n' + - ' czm_vertexLogDepth(); \n' + - '} \n'; - sources.push(logMain); - } - - var addExtension = true; - writesLogDepth = false; - sources = fs.sources; - length = sources.length; - for (i = 0; i < length; ++i) { - if (writeLogDepthRegex.test(sources[i])) { - writesLogDepth = true; - } - if (extensionRegex.test(sources[i])) { - addExtension = false; - } - } - - var logSource = ''; - if (addExtension) { - logSource += - '#ifdef GL_EXT_frag_depth \n' + - '#extension GL_EXT_frag_depth : enable \n' + - '#endif \n\n'; - } - - if (!writesLogDepth) { - for (i = 0; i < length; i++) { - sources[i] = ShaderSource.replaceMain(sources[i], 'czm_log_depth_main'); - } - - logSource += - '\n' + - 'void main() \n' + - '{ \n' + - ' czm_log_depth_main(); \n' + - ' czm_writeLogDepth(); \n' + - '} \n'; - } - - sources.push(logSource); - - shader = context.shaderCache.createDerivedShaderProgram(shaderProgram, 'logDepth', { - vertexShaderSource : vs, - fragmentShaderSource : fs, - attributeLocations : attributeLocations - }); - } - - return shader; - } - - function createLogDepthCommand(command, context, result) { - if (!defined(result)) { - result = {}; - } - - var shader; - if (defined(result.logDepthCommand)) { - shader = result.logDepthCommand.shaderProgram; - } - - result.logDepthCommand = DrawCommand.shallowClone(command, result.logDepthCommand); - - if (!defined(shader) || result.shaderProgramId !== command.shaderProgram.id) { - result.logDepthCommand.shaderProgram = getLogDepthShaderProgram(context, command.shaderProgram); - result.shaderProgramId = command.shaderProgram.id; - } else { - result.logDepthCommand.shaderProgram = shader; - } - - return result; - } - function renderTranslucentDepthForPick(scene, drawingBufferPosition) { // PERFORMANCE_IDEA: render translucent only and merge with the previous frame var context = scene._context; @@ -3694,7 +3514,7 @@ define([ uniformState.update(this.frameState); uniformState.updateFrustum(frustum); } else { - frustum.near = renderedFrustum.near * (i !== 0 ? OPAQUE_FRUSTUM_NEAR_OFFSET : 1.0); + frustum.near = renderedFrustum.near * (i !== 0 ? this.opaqueFrustumNearOffset : 1.0); frustum.far = renderedFrustum.far; uniformState.updateFrustum(frustum); } From e17dbcc2702cd67ad4e792b347c4ca1b2d73f30d Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Thu, 5 Apr 2018 15:39:31 -0400 Subject: [PATCH 64/68] Update multi-frustum tests. --- Specs/Renderer/AutomaticUniformSpec.js | 11 +++++++++++ Specs/Scene/MultifrustumSpec.js | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/Specs/Renderer/AutomaticUniformSpec.js b/Specs/Renderer/AutomaticUniformSpec.js index 72baabf820d2..f439c7e1af6a 100644 --- a/Specs/Renderer/AutomaticUniformSpec.js +++ b/Specs/Renderer/AutomaticUniformSpec.js @@ -1307,4 +1307,15 @@ defineSuite([ }).contextToRender(); }); + it('has czm_logFarDistance', function() { + var fs = + 'void main() {' + + ' gl_FragColor = vec4(czm_logFarDistance == (2.0 / log2(czm_currentFrustum.y + 1.0)));' + + '}'; + expect({ + context : context, + fragmentShader : fs + }).contextToRender(); + }); + }, 'WebGL'); diff --git a/Specs/Scene/MultifrustumSpec.js b/Specs/Scene/MultifrustumSpec.js index e28a5dd2099b..2bb926965d0d 100644 --- a/Specs/Scene/MultifrustumSpec.js +++ b/Specs/Scene/MultifrustumSpec.js @@ -63,7 +63,13 @@ defineSuite([ var blueImage; var whiteImage; + var logDepth; + beforeAll(function() { + scene = createScene(); + logDepth = scene.logarithmicDepthBuffer; + scene.destroyForSpecs(); + return when.join( Resource.fetchImage('./Data/Images/Green.png').then(function(image) { greenImage = image; @@ -81,6 +87,8 @@ defineSuite([ context = scene.context; primitives = scene.primitives; + scene.logarithmicDepthBuffer = false; + var camera = scene.camera; camera.position = new Cartesian3(); camera.direction = Cartesian3.negate(Cartesian3.UNIT_Z, new Cartesian3()); @@ -350,4 +358,19 @@ defineSuite([ createBillboards(); scene.renderForSpecs(); }); + + it('log depth uses less frustums', function() { + if (!logDepth) { + return; + } + + createBillboards(); + + scene.render(); + expect(scene._frustumCommandsList.length).toEqual(3); + + scene.logarithmicDepthBuffer = true; + scene.render(); + expect(scene._frustumCommandsList.length).toEqual(1); + }); }, 'WebGL'); From 6e9fc3e6446e5b261f6e943859f867c54bc6269d Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Mon, 9 Apr 2018 09:13:06 -0400 Subject: [PATCH 65/68] Use eye coordinates instead of window coordinates in the billboard vertex shader. --- Source/Shaders/BillboardCollectionVS.glsl | 26 +++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Source/Shaders/BillboardCollectionVS.glsl b/Source/Shaders/BillboardCollectionVS.glsl index cf0f97840e3d..039bd3323928 100644 --- a/Source/Shaders/BillboardCollectionVS.glsl +++ b/Source/Shaders/BillboardCollectionVS.glsl @@ -71,23 +71,23 @@ vec4 computePositionWindowCoordinates(vec4 positionEC, vec2 imageSize, float sca positionEC.xy += halfSize; } - vec4 positionWC = czm_eyeToWindowCoordinates(positionEC); + float mpp = czm_metersPerPixel(positionEC); - if (sizeInMeters) + if (!sizeInMeters) { - originTranslate /= czm_metersPerPixel(positionEC); + originTranslate *= mpp; } - positionWC.xy += originTranslate; + positionEC.xy += originTranslate; if (!sizeInMeters) { - positionWC.xy += halfSize; + positionEC.xy += halfSize * mpp; } - positionWC.xy += translate; - positionWC.xy += (pixelOffset * czm_resolutionScale); + positionEC.xy += translate * mpp; + positionEC.xy += (pixelOffset * czm_resolutionScale) * mpp; - return positionWC; + return positionEC; } void main() @@ -255,14 +255,14 @@ void main() } #endif -#ifdef LOG_DEPTH - czm_vertexLogDepth(czm_projection * positionEC); -#endif - vec4 positionWC = computePositionWindowCoordinates(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters); - gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0); + gl_Position = czm_projection * positionWC; v_textureCoordinates = textureCoordinates; +#ifdef LOG_DEPTH + czm_vertexLogDepth(); +#endif + #ifdef DISABLE_DEPTH_DISTANCE float disableDepthTestDistance = distanceDisplayConditionAndDisableDepth.z; if (disableDepthTestDistance == 0.0 && czm_minimumDisableDepthTestDistance != 0.0) From bed9f0ee992364d7ebf8f2be6320d38012d07c53 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 10 Apr 2018 15:39:42 -0400 Subject: [PATCH 66/68] Fix NaNs returned by barycentricCoordinates. --- Source/Core/barycentricCoordinates.js | 75 ++++++++++++++++-------- Specs/Core/barycentricCoordinatesSpec.js | 9 +++ 2 files changed, 61 insertions(+), 23 deletions(-) diff --git a/Source/Core/barycentricCoordinates.js b/Source/Core/barycentricCoordinates.js index 6316440fc479..085223fd2399 100644 --- a/Source/Core/barycentricCoordinates.js +++ b/Source/Core/barycentricCoordinates.js @@ -2,12 +2,14 @@ define([ './Cartesian2', './Cartesian3', './Check', - './defined' + './defined', + './Math' ], function( Cartesian2, Cartesian3, Check, - defined) { + defined, + CesiumMath) { 'use strict'; var scratchCartesian1 = new Cartesian3(); @@ -47,34 +49,61 @@ define([ } // Implementation based on http://www.blackpawn.com/texts/pointinpoly/default.html. - var v0, v1, v2; - var dot00, dot01, dot02, dot11, dot12; + var v0; + var v1; + var v2; + var dot00; + var dot01; + var dot02; + var dot11; + var dot12; if(!defined(p0.z)) { - v0 = Cartesian2.subtract(p1, p0, scratchCartesian1); - v1 = Cartesian2.subtract(p2, p0, scratchCartesian2); - v2 = Cartesian2.subtract(point, p0, scratchCartesian3); + if (Cartesian2.equalsEpsilon(point, p0, CesiumMath.EPSILON14)) { + return Cartesian3.clone(Cartesian3.UNIT_X, result); + } + if (Cartesian2.equalsEpsilon(point, p1, CesiumMath.EPSILON14)) { + return Cartesian3.clone(Cartesian3.UNIT_Y, result); + } + if (Cartesian2.equalsEpsilon(point, p2, CesiumMath.EPSILON14)) { + return Cartesian3.clone(Cartesian3.UNIT_Z, result); + } - dot00 = Cartesian2.dot(v0, v0); - dot01 = Cartesian2.dot(v0, v1); - dot02 = Cartesian2.dot(v0, v2); - dot11 = Cartesian2.dot(v1, v1); - dot12 = Cartesian2.dot(v1, v2); + v0 = Cartesian2.subtract(p1, p0, scratchCartesian1); + v1 = Cartesian2.subtract(p2, p0, scratchCartesian2); + v2 = Cartesian2.subtract(point, p0, scratchCartesian3); + + dot00 = Cartesian2.dot(v0, v0); + dot01 = Cartesian2.dot(v0, v1); + dot02 = Cartesian2.dot(v0, v2); + dot11 = Cartesian2.dot(v1, v1); + dot12 = Cartesian2.dot(v1, v2); } else { - v0 = Cartesian3.subtract(p1, p0, scratchCartesian1); - v1 = Cartesian3.subtract(p2, p0, scratchCartesian2); - v2 = Cartesian3.subtract(point, p0, scratchCartesian3); + if (Cartesian3.equalsEpsilon(point, p0, CesiumMath.EPSILON14)) { + return Cartesian3.clone(Cartesian3.UNIT_X, result); + } + if (Cartesian3.equalsEpsilon(point, p1, CesiumMath.EPSILON14)) { + return Cartesian3.clone(Cartesian3.UNIT_Y, result); + } + if (Cartesian3.equalsEpsilon(point, p2, CesiumMath.EPSILON14)) { + return Cartesian3.clone(Cartesian3.UNIT_Z, result); + } + + v0 = Cartesian3.subtract(p1, p0, scratchCartesian1); + v1 = Cartesian3.subtract(p2, p0, scratchCartesian2); + v2 = Cartesian3.subtract(point, p0, scratchCartesian3); - dot00 = Cartesian3.dot(v0, v0); - dot01 = Cartesian3.dot(v0, v1); - dot02 = Cartesian3.dot(v0, v2); - dot11 = Cartesian3.dot(v1, v1); - dot12 = Cartesian3.dot(v1, v2); + dot00 = Cartesian3.dot(v0, v0); + dot01 = Cartesian3.dot(v0, v1); + dot02 = Cartesian3.dot(v0, v2); + dot11 = Cartesian3.dot(v1, v1); + dot12 = Cartesian3.dot(v1, v2); } - var q = 1.0 / (dot00 * dot11 - dot01 * dot01); - result.y = (dot11 * dot02 - dot01 * dot12) * q; - result.z = (dot00 * dot12 - dot01 * dot02) * q; + var q = dot00 * dot11 - dot01 * dot01; + var invQ = 1.0 / q; + result.y = (dot11 * dot02 - dot01 * dot12) * invQ; + result.z = (dot00 * dot12 - dot01 * dot02) * invQ; result.x = 1.0 - result.y - result.z; return result; } diff --git a/Specs/Core/barycentricCoordinatesSpec.js b/Specs/Core/barycentricCoordinatesSpec.js index 8f425e1b87da..2738d2876340 100644 --- a/Specs/Core/barycentricCoordinatesSpec.js +++ b/Specs/Core/barycentricCoordinatesSpec.js @@ -48,6 +48,15 @@ defineSuite([ expect(barycentricCoordinates(point, p0, p1, p2)).toEqualEpsilon(new Cartesian3(scalar, scalar, scalar), CesiumMath.EPSILON14); }); + it('evaluates with equal length sides', function() { + var p0 = new Cartesian3(9635312487071484, 13827945400273020, -16479219993905144); + var p1 = new Cartesian3(12832234.180639317, -10455085.701705107, 750010.7274386138); + var p2 = new Cartesian3(-9689011.10628853, -13420063.892507521, 750010.7274386119); + expect(barycentricCoordinates(p0, p0, p1, p2)).toEqual(Cartesian3.UNIT_X); + expect(barycentricCoordinates(p1, p0, p1, p2)).toEqual(Cartesian3.UNIT_Y); + expect(barycentricCoordinates(p2, p0, p1, p2)).toEqual(Cartesian3.UNIT_Z); + }); + it('throws without point', function() { expect(function() { barycentricCoordinates(); From 3419ad53aa43db28bc9c6fb2c14f37a5ce334c13 Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 10 Apr 2018 16:11:39 -0400 Subject: [PATCH 67/68] Fix debug show globe and pick depth. --- Source/Scene/GlobeDepth.js | 21 ++++++++++---- Source/Scene/PickDepth.js | 21 ++++++++++---- Source/Scene/Scene.js | 29 ++++++++++--------- .../Builtin/Functions/reverseLogDepth.glsl | 10 +++++++ 4 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 Source/Shaders/Builtin/Functions/reverseLogDepth.glsl diff --git a/Source/Scene/GlobeDepth.js b/Source/Scene/GlobeDepth.js index 043690568771..2c6b74bdce2f 100644 --- a/Source/Scene/GlobeDepth.js +++ b/Source/Scene/GlobeDepth.js @@ -8,6 +8,7 @@ define([ '../Renderer/Framebuffer', '../Renderer/PixelDatatype', '../Renderer/RenderState', + '../Renderer/ShaderSource', '../Renderer/Texture', '../Shaders/PostProcessFilters/PassThrough' ], function( @@ -20,6 +21,7 @@ define([ Framebuffer, PixelDatatype, RenderState, + ShaderSource, Texture, PassThrough) { 'use strict'; @@ -45,23 +47,30 @@ define([ this._useScissorTest = false; this._scissorRectangle = undefined; + this._useLogDepth = undefined; + this._debugGlobeDepthViewportCommand = undefined; } - function executeDebugGlobeDepth(globeDepth, context, passState) { - if (!defined(globeDepth._debugGlobeDepthViewportCommand)) { - var fs = + function executeDebugGlobeDepth(globeDepth, context, passState, useLogDepth) { + if (!defined(globeDepth._debugGlobeDepthViewportCommand) || useLogDepth !== globeDepth._useLogDepth) { + var fsSource = 'uniform sampler2D u_texture;\n' + 'varying vec2 v_textureCoordinates;\n' + 'void main()\n' + '{\n' + ' float z_window = czm_unpackDepth(texture2D(u_texture, v_textureCoordinates));\n' + + ' z_window = czm_reverseLogDepth(z_window); \n' + ' float n_range = czm_depthRange.near;\n' + ' float f_range = czm_depthRange.far;\n' + ' float z_ndc = (2.0 * z_window - n_range - f_range) / (f_range - n_range);\n' + ' float scale = pow(z_ndc * 0.5 + 0.5, 8.0);\n' + ' gl_FragColor = vec4(mix(vec3(0.0), vec3(1.0), scale), 1.0);\n' + '}\n'; + var fs = new ShaderSource({ + defines : [useLogDepth ? 'LOG_DEPTH' : ''], + sources : [fsSource] + }); globeDepth._debugGlobeDepthViewportCommand = context.createViewportQuadCommand(fs, { uniformMap : { @@ -71,6 +80,8 @@ define([ }, owner : globeDepth }); + + globeDepth._useLogDepth = useLogDepth; } globeDepth._debugGlobeDepthViewportCommand.execute(context, passState); @@ -207,8 +218,8 @@ define([ globeDepth._clearColorCommand.framebuffer = globeDepth.framebuffer; } - GlobeDepth.prototype.executeDebugGlobeDepth = function(context, passState) { - executeDebugGlobeDepth(this, context, passState); + GlobeDepth.prototype.executeDebugGlobeDepth = function(context, passState, useLogDepth) { + executeDebugGlobeDepth(this, context, passState, useLogDepth); }; GlobeDepth.prototype.update = function(context, passState) { diff --git a/Source/Scene/PickDepth.js b/Source/Scene/PickDepth.js index 16486ec01cad..f812a34317e9 100644 --- a/Source/Scene/PickDepth.js +++ b/Source/Scene/PickDepth.js @@ -5,6 +5,7 @@ define([ '../Renderer/Framebuffer', '../Renderer/PixelDatatype', '../Renderer/RenderState', + '../Renderer/ShaderSource', '../Renderer/Texture' ], function( defined, @@ -13,6 +14,7 @@ define([ Framebuffer, PixelDatatype, RenderState, + ShaderSource, Texture) { 'use strict'; @@ -26,23 +28,30 @@ define([ this._textureToCopy = undefined; this._copyDepthCommand = undefined; + this._useLogDepth = undefined; + this._debugPickDepthViewportCommand = undefined; } - function executeDebugPickDepth(pickDepth, context, passState) { - if (!defined(pickDepth._debugPickDepthViewportCommand)) { - var fs = + function executeDebugPickDepth(pickDepth, context, passState, useLogDepth) { + if (!defined(pickDepth._debugPickDepthViewportCommand) || useLogDepth !== pickDepth._useLogDepth) { + var fsSource = 'uniform sampler2D u_texture;\n' + 'varying vec2 v_textureCoordinates;\n' + 'void main()\n' + '{\n' + ' float z_window = czm_unpackDepth(texture2D(u_texture, v_textureCoordinates));\n' + + ' z_window = czm_reverseLogDepth(z_window); \n' + ' float n_range = czm_depthRange.near;\n' + ' float f_range = czm_depthRange.far;\n' + ' float z_ndc = (2.0 * z_window - n_range - f_range) / (f_range - n_range);\n' + ' float scale = pow(z_ndc * 0.5 + 0.5, 8.0);\n' + ' gl_FragColor = vec4(mix(vec3(0.0), vec3(1.0), scale), 1.0);\n' + '}\n'; + var fs = new ShaderSource({ + defines : [useLogDepth ? 'LOG_DEPTH' : ''], + sources : [fsSource] + }); pickDepth._debugPickDepthViewportCommand = context.createViewportQuadCommand(fs, { uniformMap : { @@ -52,6 +61,8 @@ define([ }, owner : pickDepth }); + + pickDepth._useLogDepth = useLogDepth; } pickDepth._debugPickDepthViewportCommand.execute(context, passState); @@ -123,8 +134,8 @@ define([ pickDepth._copyDepthCommand.framebuffer = pickDepth.framebuffer; } - PickDepth.prototype.executeDebugPickDepth = function(context, passState) { - executeDebugPickDepth(this, context, passState); + PickDepth.prototype.executeDebugPickDepth = function(context, passState, useLogDepth) { + executeDebugPickDepth(this, context, passState, useLogDepth); }; PickDepth.prototype.update = function(context, depthTexture) { diff --git a/Source/Scene/Scene.js b/Source/Scene/Scene.js index ef15edd788ae..da28e35093e9 100644 --- a/Source/Scene/Scene.js +++ b/Source/Scene/Scene.js @@ -1615,8 +1615,6 @@ define([ command.debugOverlappingFrustums = 0; } - updateDerivedCommands(scene, command); - var frustumCommandsList = scene._frustumCommandsList; var length = frustumCommandsList.length; @@ -1651,6 +1649,8 @@ define([ cf[command.debugOverlappingFrustums] = defined(cf[command.debugOverlappingFrustums]) ? cf[command.debugOverlappingFrustums] + 1 : 1; ++scene._debugFrustumStatistics.totalCommands; } + + updateDerivedCommands(scene, command); } var scratchCullingVolume = new CullingVolume(); @@ -2936,19 +2936,9 @@ define([ var context = scene._context; var environmentState = scene._environmentState; - var useGlobeDepthFramebuffer = environmentState.useGlobeDepthFramebuffer; - if (scene.debugShowGlobeDepth && useGlobeDepthFramebuffer) { - var gd = getDebugGlobeDepth(scene, scene.debugShowDepthFrustum - 1); - gd.executeDebugGlobeDepth(context, passState); - } - - if (scene.debugShowPickDepth && useGlobeDepthFramebuffer) { - var pd = getPickDepth(scene, scene.debugShowDepthFrustum - 1); - pd.executeDebugPickDepth(context, passState); - } - var useOIT = environmentState.useOIT; var useFXAA = environmentState.useFXAA; + var useGlobeDepthFramebuffer = environmentState.useGlobeDepthFramebuffer; if (useOIT) { passState.framebuffer = useFXAA ? scene._fxaa.getColorFramebuffer() : undefined; @@ -2969,6 +2959,19 @@ define([ passState.framebuffer = environmentState.originalFramebuffer; scene._globeDepth.executeCopyColor(context, passState); } + + var frustum = scene.camera.frustum; + var useLogDepth = scene._logDepthBuffer && !(frustum instanceof OrthographicFrustum || frustum instanceof OrthographicOffCenterFrustum); + + if (scene.debugShowGlobeDepth && useGlobeDepthFramebuffer) { + var gd = getDebugGlobeDepth(scene, scene.debugShowDepthFrustum - 1); + gd.executeDebugGlobeDepth(context, passState, useLogDepth); + } + + if (scene.debugShowPickDepth && useGlobeDepthFramebuffer) { + var pd = getPickDepth(scene, scene.debugShowDepthFrustum - 1); + pd.executeDebugPickDepth(context, passState, useLogDepth); + } } function callAfterRenderFunctions(scene) { diff --git a/Source/Shaders/Builtin/Functions/reverseLogDepth.glsl b/Source/Shaders/Builtin/Functions/reverseLogDepth.glsl new file mode 100644 index 000000000000..e8719f24a9ba --- /dev/null +++ b/Source/Shaders/Builtin/Functions/reverseLogDepth.glsl @@ -0,0 +1,10 @@ +float czm_reverseLogDepth(float logZ) +{ +#ifdef LOG_DEPTH + float near = czm_currentFrustum.x; + float far = czm_currentFrustum.y; + logZ = pow(2.0, logZ * log2(far + 1.0)) - 1.0; + logZ = far * (1.0 - near / logZ) / (far - near); +#endif + return logZ; +} From d7d14a59e5a93fa8e2b68f7652b93b2e3fcc10ae Mon Sep 17 00:00:00 2001 From: Dan Bagnell Date: Tue, 10 Apr 2018 16:16:21 -0400 Subject: [PATCH 68/68] Rename from review. --- Source/Shaders/BillboardCollectionVS.glsl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Shaders/BillboardCollectionVS.glsl b/Source/Shaders/BillboardCollectionVS.glsl index 039bd3323928..9707d718b7fa 100644 --- a/Source/Shaders/BillboardCollectionVS.glsl +++ b/Source/Shaders/BillboardCollectionVS.glsl @@ -39,7 +39,7 @@ const float SHIFT_RIGHT3 = 1.0 / 8.0; const float SHIFT_RIGHT2 = 1.0 / 4.0; const float SHIFT_RIGHT1 = 1.0 / 2.0; -vec4 computePositionWindowCoordinates(vec4 positionEC, vec2 imageSize, float scale, vec2 direction, vec2 origin, vec2 translate, vec2 pixelOffset, vec3 alignedAxis, bool validAlignedAxis, float rotation, bool sizeInMeters) +vec4 addScreenSpaceOffset(vec4 positionEC, vec2 imageSize, float scale, vec2 direction, vec2 origin, vec2 translate, vec2 pixelOffset, vec3 alignedAxis, bool validAlignedAxis, float rotation, bool sizeInMeters) { // Note the halfSize cannot be computed in JavaScript because it is sent via // compressed vertex attributes that coerce it to an integer. @@ -255,8 +255,8 @@ void main() } #endif - vec4 positionWC = computePositionWindowCoordinates(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters); - gl_Position = czm_projection * positionWC; + positionEC = addScreenSpaceOffset(positionEC, imageSize, scale, direction, origin, translate, pixelOffset, alignedAxis, validAlignedAxis, rotation, sizeInMeters); + gl_Position = czm_projection * positionEC; v_textureCoordinates = textureCoordinates; #ifdef LOG_DEPTH