123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- import Cartesian3 from "../Core/Cartesian3.js";
- import defined from "../Core/defined.js";
- import CesiumMath from "../Core/Math.js";
- import SceneMode from "./SceneMode.js";
- function Fog() {
-
- this.enabled = true;
-
- this.density = 2.0e-4;
-
- this.screenSpaceErrorFactor = 2.0;
-
- this.minimumBrightness = 0.03;
- }
- var heightsTable = [
- 359.393,
- 800.749,
- 1275.6501,
- 2151.1192,
- 3141.7763,
- 4777.5198,
- 6281.2493,
- 12364.307,
- 15900.765,
- 49889.0549,
- 78026.8259,
- 99260.7344,
- 120036.3873,
- 151011.0158,
- 156091.1953,
- 203849.3112,
- 274866.9803,
- 319916.3149,
- 493552.0528,
- 628733.5874,
- ];
- var densityTable = [
- 2.0e-5,
- 2.0e-4,
- 1.0e-4,
- 7.0e-5,
- 5.0e-5,
- 4.0e-5,
- 3.0e-5,
- 1.9e-5,
- 1.0e-5,
- 8.5e-6,
- 6.2e-6,
- 5.8e-6,
- 5.3e-6,
- 5.2e-6,
- 5.1e-6,
- 4.2e-6,
- 4.0e-6,
- 3.4e-6,
- 2.6e-6,
- 2.2e-6,
- ];
- for (var i = 0; i < densityTable.length; ++i) {
- densityTable[i] *= 1.0e6;
- }
- var tableStartDensity = densityTable[1];
- var tableEndDensity = densityTable[densityTable.length - 1];
- for (var j = 0; j < densityTable.length; ++j) {
- densityTable[j] =
- (densityTable[j] - tableEndDensity) / (tableStartDensity - tableEndDensity);
- }
- var tableLastIndex = 0;
- function findInterval(height) {
- var heights = heightsTable;
- var length = heights.length;
- if (height < heights[0]) {
- tableLastIndex = 0;
- return tableLastIndex;
- } else if (height > heights[length - 1]) {
- tableLastIndex = length - 2;
- return tableLastIndex;
- }
-
-
- if (height >= heights[tableLastIndex]) {
- if (tableLastIndex + 1 < length && height < heights[tableLastIndex + 1]) {
- return tableLastIndex;
- } else if (
- tableLastIndex + 2 < length &&
- height < heights[tableLastIndex + 2]
- ) {
- ++tableLastIndex;
- return tableLastIndex;
- }
- } else if (tableLastIndex - 1 >= 0 && height >= heights[tableLastIndex - 1]) {
- --tableLastIndex;
- return tableLastIndex;
- }
-
- var i;
- for (i = 0; i < length - 2; ++i) {
- if (height >= heights[i] && height < heights[i + 1]) {
- break;
- }
- }
- tableLastIndex = i;
- return tableLastIndex;
- }
- var scratchPositionNormal = new Cartesian3();
- Fog.prototype.update = function (frameState) {
- var enabled = (frameState.fog.enabled = this.enabled);
- if (!enabled) {
- return;
- }
- var camera = frameState.camera;
- var positionCartographic = camera.positionCartographic;
-
- if (
- !defined(positionCartographic) ||
- positionCartographic.height > 800000.0 ||
- frameState.mode !== SceneMode.SCENE3D
- ) {
- frameState.fog.enabled = false;
- return;
- }
- var height = positionCartographic.height;
- var i = findInterval(height);
- var t = CesiumMath.clamp(
- (height - heightsTable[i]) / (heightsTable[i + 1] - heightsTable[i]),
- 0.0,
- 1.0
- );
- var density = CesiumMath.lerp(densityTable[i], densityTable[i + 1], t);
-
- var startDensity = this.density * 1.0e6;
- var endDensity = (startDensity / tableStartDensity) * tableEndDensity;
- density = density * (startDensity - endDensity) * 1.0e-6;
-
- var positionNormal = Cartesian3.normalize(
- camera.positionWC,
- scratchPositionNormal
- );
- var dot = Math.abs(Cartesian3.dot(camera.directionWC, positionNormal));
- density *= 1.0 - dot;
- frameState.fog.density = density;
- frameState.fog.sse = this.screenSpaceErrorFactor;
- frameState.fog.minimumBrightness = this.minimumBrightness;
- };
- export default Fog;
|