Skip to content

Commit

Permalink
Add a uniform for whether the tile is in fog
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrgags committed Jan 9, 2024
1 parent 138e4e2 commit e0314cd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
4 changes: 3 additions & 1 deletion packages/engine/Source/Scene/GlobeSurfaceTileProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,9 @@ function addDrawCommandsForTile(tileProvider, tile, frameState) {
uniformMapProperties.localizedTranslucencyRectangle
);

// For performance, use fog in the shader only when the tile is in fog.
// For performance, render fog only when fog is enabled and the effect of
// fog would be non-negligible. This prevents the shader from running when
// the camera is in space, for example.
const applyFog =
enableFog &&
CesiumMath.fog(tile._distance, frameState.fog.density) >
Expand Down
22 changes: 21 additions & 1 deletion packages/engine/Source/Scene/Model/FogPipelineStage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import FogStageFS from "../../Shaders/Model/FogStageFS.js";
import Cartesian3 from "../../Core/Cartesian3.js";
import CesiumMath from "../../Core/Math.js";
import ShaderDestination from "../../Renderer/ShaderDestination.js";
import FogStageFS from "../../Shaders/Model/FogStageFS.js";

/**
* The fog color pipeline stage is responsible for applying fog to tiles in the distance in horizon views.
Expand All @@ -17,6 +19,24 @@ FogPipelineStage.process = function (renderResources, model, frameState) {

shaderBuilder.addDefine("HAS_FOG", undefined, ShaderDestination.FRAGMENT);
shaderBuilder.addFragmentLines([FogStageFS]);

// Add a uniform so fog is only calculated when the effect would
// be non-negligible For example when the camera is in space, fog density decreases
// to 0 so fog shouldn't be rendered. Since this state may change rapidly if
// the camera is moving, this is implemented as a uniform, not a define.
shaderBuilder.addUniform("bool", "u_isInFog", ShaderDestination.FRAGMENT);
renderResources.uniformMap.u_isInFog = function () {
// We only need a rough measure of distance to the model, so measure
// from the camera to the bounding sphere center.
const distance = Cartesian3.distance(
frameState.camera.position,
model.boundingSphere.center
);

return (
CesiumMath.fog(distance, frameState.fog.density) > CesiumMath.EPSILON3
);
};
};

export default FogPipelineStage;
6 changes: 0 additions & 6 deletions packages/engine/Source/Scene/Model/ModelRuntimePrimitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import CustomShaderMode from "./CustomShaderMode.js";
import CustomShaderPipelineStage from "./CustomShaderPipelineStage.js";
import DequantizationPipelineStage from "./DequantizationPipelineStage.js";
import FeatureIdPipelineStage from "./FeatureIdPipelineStage.js";
import FogPipelineStage from "./FogPipelineStage.js";
import GeometryPipelineStage from "./GeometryPipelineStage.js";
import LightingPipelineStage from "./LightingPipelineStage.js";
import MaterialPipelineStage from "./MaterialPipelineStage.js";
Expand Down Expand Up @@ -200,7 +199,6 @@ ModelRuntimePrimitive.prototype.configurePipeline = function (frameState) {
const mode = frameState.mode;
const use2D =
mode !== SceneMode.SCENE3D && !frameState.scene3DOnly && model._projectTo2D;
const fogRenderable = frameState.fog.enabled && frameState.fog.renderable;
const exaggerateTerrain = frameState.verticalExaggeration !== 1.0;

const hasMorphTargets =
Expand Down Expand Up @@ -305,10 +303,6 @@ ModelRuntimePrimitive.prototype.configurePipeline = function (frameState) {

pipelineStages.push(AlphaPipelineStage);

if (fogRenderable) {
pipelineStages.push(FogPipelineStage);
}

pipelineStages.push(PrimitiveStatisticsPipelineStage);

return;
Expand Down
6 changes: 6 additions & 0 deletions packages/engine/Source/Scene/Model/ModelSceneGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SceneMode from "../SceneMode.js";
import SplitDirection from "../SplitDirection.js";
import buildDrawCommand from "./buildDrawCommand.js";
import TilesetPipelineStage from "./TilesetPipelineStage.js";
import FogPipelineStage from "./FogPipelineStage.js";
import ImageBasedLightingPipelineStage from "./ImageBasedLightingPipelineStage.js";
import ModelArticulation from "./ModelArticulation.js";
import ModelColorPipelineStage from "./ModelColorPipelineStage.js";
Expand Down Expand Up @@ -606,6 +607,7 @@ ModelSceneGraph.prototype.configurePipeline = function (frameState) {
modelPipelineStages.length = 0;

const model = this._model;
const fogRenderable = frameState.fog.enabled && frameState.fog.renderable;

if (defined(model.color)) {
modelPipelineStages.push(ModelColorPipelineStage);
Expand Down Expand Up @@ -638,6 +640,10 @@ ModelSceneGraph.prototype.configurePipeline = function (frameState) {
if (ModelType.is3DTiles(model.type)) {
modelPipelineStages.push(TilesetPipelineStage);
}

if (fogRenderable) {
modelPipelineStages.push(FogPipelineStage);
}
};

ModelSceneGraph.prototype.update = function (frameState, updateForAnimations) {
Expand Down
2 changes: 0 additions & 2 deletions packages/engine/Source/Shaders/GlobeFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,6 @@ void main()

finalColor.rgb = mix(finalColor.rgb, finalAtmosphereColor.rgb, fade);
#endif

//finalColor.rgb = computeEllipsoidPosition() / 1e7;
}
#endif

Expand Down
6 changes: 6 additions & 0 deletions packages/engine/Source/Shaders/Model/FogStageFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ vec3 computeFogColor(vec3 positionMC) {
}

void fogStage(inout vec4 color, in ProcessedAttributes attributes) {
if (!u_isInFog) {
// Debugging
//color.rgb = vec3(1.0, 1.0, 0.0);
return;
}

vec3 fogColor = computeFogColor(attributes.positionMC);

// Note: camera is far away (distance > nightFadeOutDistance), scattering is computed in the fragment shader.
Expand Down

0 comments on commit e0314cd

Please sign in to comment.