cesium/packages/engine/Source/Scene/SunPostProcess.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

289 lines
8.1 KiB
JavaScript
Raw Permalink Normal View History

Migrate Cesium to ES6 Modules See https://github.com/AnalyticalGraphicsInc/cesium/pull/8224 for details. eslint There are a handful of new .eslintrc.json files, mostly to identify the files that are still AMD modules (Sandcastle/Workers). These are needed because you can't change the parser type with a comment directive (since the parser is the thing reading the file). We can finally detect unusued modules! So those have all been cleaned up as well. requirejs -> rollup & clean-css requirejs, almond, and karma-requirejs have all been removed. We now use rollup for building and minifying (via uglify) JS code and clean-css for css. These changes are fairly straight-forward and just involve calling rollup instead of requirejs in the build process. Overall build time is significantly faster. CI is ~11 minutes compared to ~17 in master. Running makeZipFile on my machine takes 69 seconds compared to 112 seconds in master. There's probably plenty of room for additional optimization here too. We wrote an published a small npm module, rollup-plugin-strip-pragma, for stripping the requirejs pragmas we use out of the release builds. This is maintained in the Tools/rollup-plugin-strip-pragma directory. As for what we produce. The built version of Cesium is now a UMD module. So it should work anywhere that hasn't made the jump to ES6 yet. For users that were already using the "legacy" combined/minified approach, nothing changes. One awesome thing about roll-up is that it compiles all of the workers at once and automatically detects shared codes and generates separate bundles under the hood. This means the size of our worker modules shrink dramatically and Cesium itself will load them much faster. The total minified/gzipped size of all workers in master is 2.6 MB compared to 225 KB in this branch! This should be most noticeable on demos like Geometry & Appearances which load lots of workers for the various geometry typs. roll-up is also used to build Cesium Viewer, which is now an ES6 app. We use clean-css via gulp and it is also a straightforward change from requirejs that requires no special mention. Workers While the spec allows for ES6 Web Workers, no browser actually supports them yet. That means we needed a way to get our workers into non-ES6 form. Thankfully, roll-up can generate AMD modules, which means we now have a build step to compile our Worker source code back into AMD and use the existing TaskProcessor to load and execute them. This build step is part of the standard build task and is called createWorkers. During development, these "built" workers are un-optimized so you can still debug them and read the code. Since there is a build step, that means if you are changing code that affects a worker, you need to re-run build, or you can use the build-watch task to do it automatically. The ES6 versions of Worker code has moved into Source/WorkersES6 and we build the workers into their "old home" of Source/Workers. cesiumWorkerBootstrapper and transferTypedArrayTest which were already non-AMD ES5 scripts remain living in the Workers directory. Surprisingly little was changed about TaskProcessor or the worker system in general, especially considering that I thought this would be one of the major hurdles. ThirdParty A lot of our ThirdParty either already had a hand-written wrapper for AMD (which I updated to ES6) or had UMD which created problems when importing the same code in both Node and the browser. I basically had to update the wrapper of every third-party library to fix these problems. In some cases I updated the library version itself (Autolinker, topojson). Nothing to be too concerned about, but future clean-up would be using npm versions of these libraries and auto-generating the wrappers as needed so we don't hand-edit things. Sandcastle Sandcastle is eternal and manages to live another day in it's ancient requirejs/dojo 1.x form. Sandcastle now automatically uses the ES6 version of Cesium if it is available and fallsback to the ES5 unminified version if it is now. The built version of Sandcastle always uses CesiumUnminified, just like master. This means Sandcastle still works in IE11 if you run the combine step first (or use the relase zip) Removed Cesium usage from Sandcastle proper, since it wasn't really needed Generate a VERSION propertyin the gallery index since Cesium is no longer being included. Remove requirejs from Sandcastle bucket Update bucket to use the built version of Cesium if it is available by fallbackto the ES6 version during development. Standalone.html was also updated There's a bit of room for further clean-up here, but I think this gets us into master. I did not rename bucket-requirejs.html because I'm pretty sure it would break previously shared demos. We can put in some backwards compatible code later on if we want. (But I'd rather just see a full Sandcastle rewrite). Specs Specs are now all ES6, except for TestWorkers, which remain standard JS worker modules. This means you can no longer run the unbuilt unit tests in IE11. No changes for Chrome and Firefox. Since the specs use ES6 modules and built Cesium is an ES5 UMD, I added a build-specs build step which generates a combined ES5 version of the specs which rely on Cesium as a global variable. We then inject these files into jasmine instead of the standard specs and everything works exactly as it did before. SpecRunner.html has been updated to inject the correct version of the script depending on the build/release query parameters. The Specs must always use Cesium by importing Source/Cesium.js, this is so we can replace it with the built Cesium as describe above. There's a bunch of room for clean-up here, such as unifying our two copies of jasmine into a single helper file, but I didn't want to start doing that clean-up as part of this already overly big PR. The important thing is that we can still test the built version and still test on IE/Edge as needed. I also found and fixed two bugs that were causing failing unit tests, one in BingMapsImageryProviderSpec.js (which was overwriting createImage andnot setting it back) and ShadowVolumeAppearance.js (which had a module level caching bug). I think these may have been the cause of random CI failures in master as well, but only time will tell. For coverage, we had to switch to karma-coverage-istanbul-instrumenter for native ES6 support, but that's it. Finally, I updated appveryor to build Cesium and run the built tests under IE. We still don't fail the build for IE, but we should probably fix that if we want to keep it going. NodeJS When NODE_ENV is production, we now require in the minified CesiumJS directly, which works great because it's now a UMD module. Otherwise, we use the excellant esmpackage to load individual modules, it was a fairly straightforward swap from our old requirejs usage. We could probably drop esm too if we don't care about debugging or if we provie source maps at some point.
2019-10-03 23:51:23 +08:00
import BoundingRectangle from "../Core/BoundingRectangle.js";
import Cartesian2 from "../Core/Cartesian2.js";
import Cartesian4 from "../Core/Cartesian4.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import CesiumMath from "../Core/Math.js";
import Matrix4 from "../Core/Matrix4.js";
import Transforms from "../Core/Transforms.js";
import AdditiveBlend from "../Shaders/PostProcessStages/AdditiveBlend.js";
import BrightPass from "../Shaders/PostProcessStages/BrightPass.js";
import GaussianBlur1D from "../Shaders/PostProcessStages/GaussianBlur1D.js";
import PassThrough from "../Shaders/PostProcessStages/PassThrough.js";
import PostProcessStage from "./PostProcessStage.js";
import PostProcessStageComposite from "./PostProcessStageComposite.js";
import PostProcessStageSampleMode from "./PostProcessStageSampleMode.js";
import PostProcessStageTextureCache from "./PostProcessStageTextureCache.js";
import SceneFramebuffer from "./SceneFramebuffer.js";
2020-04-17 08:31:36 +08:00
function SunPostProcess() {
this._sceneFramebuffer = new SceneFramebuffer();
2020-04-17 08:31:36 +08:00
const scale = 0.125;
const stages = new Array(6);
2020-04-17 08:31:36 +08:00
stages[0] = new PostProcessStage({
fragmentShader: PassThrough,
textureScale: scale,
forcePowerOfTwo: true,
2018-01-30 08:40:58 +08:00
sampleMode: PostProcessStageSampleMode.LINEAR,
});
2020-04-17 08:31:36 +08:00
const brightPass = (stages[1] = new PostProcessStage({
fragmentShader: BrightPass,
uniforms: {
avgLuminance: 0.5, // A guess at the average luminance across the entire scene
threshold: 0.25,
offset: 0.1,
},
textureScale: scale,
forcePowerOfTwo: true,
}));
2020-04-17 08:31:36 +08:00
const that = this;
this._delta = 1.0;
this._sigma = 2.0;
this._blurStep = new Cartesian2();
2020-04-17 08:31:36 +08:00
stages[2] = new PostProcessStage({
fragmentShader: GaussianBlur1D,
uniforms: {
step: function () {
that._blurStep.x = that._blurStep.y =
1.0 / brightPass.outputTexture.width;
return that._blurStep;
},
delta: function () {
return that._delta;
},
sigma: function () {
return that._sigma;
},
direction: 0.0,
},
textureScale: scale,
forcePowerOfTwo: true,
});
2020-04-17 08:31:36 +08:00
stages[3] = new PostProcessStage({
fragmentShader: GaussianBlur1D,
uniforms: {
step: function () {
that._blurStep.x = that._blurStep.y =
1.0 / brightPass.outputTexture.width;
return that._blurStep;
},
delta: function () {
return that._delta;
},
sigma: function () {
return that._sigma;
},
direction: 1.0,
},
textureScale: scale,
forcePowerOfTwo: true,
});
2020-04-17 08:31:36 +08:00
stages[4] = new PostProcessStage({
fragmentShader: PassThrough,
2018-01-30 08:40:58 +08:00
sampleMode: PostProcessStageSampleMode.LINEAR,
});
2020-04-17 08:31:36 +08:00
this._uCenter = new Cartesian2();
this._uRadius = undefined;
2020-04-17 08:31:36 +08:00
stages[5] = new PostProcessStage({
fragmentShader: AdditiveBlend,
uniforms: {
center: function () {
return that._uCenter;
},
radius: function () {
return that._uRadius;
},
colorTexture2: function () {
2021-12-10 06:53:55 +08:00
return that._sceneFramebuffer.framebuffer.getColorTexture(0);
2020-04-17 08:31:36 +08:00
},
},
});
2020-04-17 08:31:36 +08:00
this._stages = new PostProcessStageComposite({
stages: stages,
});
2020-04-17 08:31:36 +08:00
2018-01-26 06:30:02 +08:00
const textureCache = new PostProcessStageTextureCache(this);
const length = stages.length;
for (let i = 0; i < length; ++i) {
2018-01-26 06:30:02 +08:00
stages[i]._textureCache = textureCache;
2020-04-17 08:31:36 +08:00
}
this._textureCache = textureCache;
this.length = stages.length;
}
2020-04-17 08:31:36 +08:00
2018-01-26 06:30:02 +08:00
SunPostProcess.prototype.get = function (index) {
return this._stages.get(index);
2020-04-17 08:31:36 +08:00
};
2018-01-26 06:30:02 +08:00
SunPostProcess.prototype.getStageByName = function (name) {
const length = this._stages.length;
for (let i = 0; i < length; ++i) {
const stage = this._stages.get(i);
if (stage.name === name) {
return stage;
}
2020-04-17 08:31:36 +08:00
}
return undefined;
};
2020-04-17 08:31:36 +08:00
2018-01-26 04:18:30 +08:00
const sunPositionECScratch = new Cartesian4();
const sunPositionWCScratch = new Cartesian2();
const sizeScratch = new Cartesian2();
const postProcessMatrix4Scratch = new Matrix4();
2020-04-17 08:31:36 +08:00
function updateSunPosition(postProcess, context, viewport) {
const us = context.uniformState;
const sunPosition = us.sunPositionWC;
const viewMatrix = us.view;
const viewProjectionMatrix = us.viewProjection;
2014-03-28 01:13:05 +08:00
const projectionMatrix = us.projection;
2020-04-17 08:31:36 +08:00
// create up sampled render state
let viewportTransformation = Matrix4.computeViewportTransformation(
viewport,
2020-04-17 08:31:36 +08:00
0.0,
1.0,
postProcessMatrix4Scratch,
);
2013-12-24 04:39:54 +08:00
const sunPositionEC = Matrix4.multiplyByPoint(
viewMatrix,
sunPosition,
sunPositionECScratch,
);
let sunPositionWC = Transforms.pointToGLWindowCoordinates(
viewProjectionMatrix,
viewportTransformation,
sunPosition,
sunPositionWCScratch,
);
2020-04-17 08:31:36 +08:00
sunPositionEC.x += CesiumMath.SOLAR_RADIUS;
const limbWC = Transforms.pointToGLWindowCoordinates(
projectionMatrix,
viewportTransformation,
sunPositionEC,
sunPositionEC,
);
const sunSize =
Cartesian2.magnitude(Cartesian2.subtract(limbWC, sunPositionWC, limbWC)) *
30.0 *
2.0;
2020-04-17 08:31:36 +08:00
const size = sizeScratch;
size.x = sunSize;
size.y = sunSize;
2020-04-17 08:31:36 +08:00
postProcess._uCenter = Cartesian2.clone(sunPositionWC, postProcess._uCenter);
postProcess._uRadius = Math.max(size.x, size.y) * 0.15;
2020-04-17 08:31:36 +08:00
const width = context.drawingBufferWidth;
const height = context.drawingBufferHeight;
2020-04-17 08:31:36 +08:00
const stages = postProcess._stages;
const firstStage = stages.get(0);
2020-04-17 08:31:36 +08:00
const downSampleWidth = firstStage.outputTexture.width;
const downSampleHeight = firstStage.outputTexture.height;
2020-04-17 08:31:36 +08:00
const downSampleViewport = new BoundingRectangle();
downSampleViewport.width = downSampleWidth;
downSampleViewport.height = downSampleHeight;
2020-04-17 08:31:36 +08:00
// create down sampled render state
viewportTransformation = Matrix4.computeViewportTransformation(
downSampleViewport,
0.0,
1.0,
postProcessMatrix4Scratch,
);
sunPositionWC = Transforms.pointToGLWindowCoordinates(
viewProjectionMatrix,
viewportTransformation,
sunPosition,
sunPositionWCScratch,
2020-04-17 08:31:36 +08:00
);
size.x *= downSampleWidth / width;
size.y *= downSampleHeight / height;
2020-04-17 08:31:36 +08:00
const scissorRectangle = firstStage.scissorRectangle;
scissorRectangle.x = Math.max(sunPositionWC.x - size.x * 0.5, 0.0);
scissorRectangle.y = Math.max(sunPositionWC.y - size.y * 0.5, 0.0);
scissorRectangle.width = Math.min(size.x, width);
scissorRectangle.height = Math.min(size.y, height);
2020-04-17 08:31:36 +08:00
for (let i = 1; i < 4; ++i) {
BoundingRectangle.clone(scissorRectangle, stages.get(i).scissorRectangle);
}
2020-04-17 08:31:36 +08:00
}
SunPostProcess.prototype.clear = function (context, passState, clearColor) {
this._sceneFramebuffer.clear(context, passState, clearColor);
this._textureCache.clear(context);
};
2020-04-17 08:31:36 +08:00
SunPostProcess.prototype.update = function (passState) {
const context = passState.context;
2018-08-16 04:43:52 +08:00
const viewport = passState.viewport;
2020-04-17 08:31:36 +08:00
const sceneFramebuffer = this._sceneFramebuffer;
2018-08-16 04:43:52 +08:00
sceneFramebuffer.update(context, viewport);
2021-12-10 06:53:55 +08:00
const framebuffer = sceneFramebuffer.framebuffer;
2020-04-17 08:31:36 +08:00
this._textureCache.update(context);
2018-07-12 01:32:13 +08:00
this._stages.update(context, false);
2020-04-17 08:31:36 +08:00
updateSunPosition(this, context, viewport);
2020-04-17 08:31:36 +08:00
return framebuffer;
};
2020-04-17 08:31:36 +08:00
SunPostProcess.prototype.execute = function (context) {
2021-12-10 06:53:55 +08:00
const colorTexture = this._sceneFramebuffer.framebuffer.getColorTexture(0);
const stages = this._stages;
const length = stages.length;
stages.get(0).execute(context, colorTexture);
for (let i = 1; i < length; ++i) {
stages.get(i).execute(context, stages.get(i - 1).outputTexture);
}
};
2020-04-17 08:31:36 +08:00
SunPostProcess.prototype.copy = function (context, framebuffer) {
if (!defined(this._copyColorCommand)) {
const that = this;
this._copyColorCommand = context.createViewportQuadCommand(PassThrough, {
uniformMap: {
colorTexture: function () {
return that._stages.get(that._stages.length - 1).outputTexture;
},
2020-04-17 08:31:36 +08:00
},
owner: this,
});
}
2020-04-17 08:31:36 +08:00
this._copyColorCommand.framebuffer = framebuffer;
this._copyColorCommand.execute(context);
};
2020-04-17 08:31:36 +08:00
SunPostProcess.prototype.isDestroyed = function () {
return false;
};
2020-04-17 08:31:36 +08:00
SunPostProcess.prototype.destroy = function () {
2018-01-17 08:07:28 +08:00
this._textureCache.destroy();
this._stages.destroy();
return destroyObject(this);
};
Migrate Cesium to ES6 Modules See https://github.com/AnalyticalGraphicsInc/cesium/pull/8224 for details. eslint There are a handful of new .eslintrc.json files, mostly to identify the files that are still AMD modules (Sandcastle/Workers). These are needed because you can't change the parser type with a comment directive (since the parser is the thing reading the file). We can finally detect unusued modules! So those have all been cleaned up as well. requirejs -> rollup & clean-css requirejs, almond, and karma-requirejs have all been removed. We now use rollup for building and minifying (via uglify) JS code and clean-css for css. These changes are fairly straight-forward and just involve calling rollup instead of requirejs in the build process. Overall build time is significantly faster. CI is ~11 minutes compared to ~17 in master. Running makeZipFile on my machine takes 69 seconds compared to 112 seconds in master. There's probably plenty of room for additional optimization here too. We wrote an published a small npm module, rollup-plugin-strip-pragma, for stripping the requirejs pragmas we use out of the release builds. This is maintained in the Tools/rollup-plugin-strip-pragma directory. As for what we produce. The built version of Cesium is now a UMD module. So it should work anywhere that hasn't made the jump to ES6 yet. For users that were already using the "legacy" combined/minified approach, nothing changes. One awesome thing about roll-up is that it compiles all of the workers at once and automatically detects shared codes and generates separate bundles under the hood. This means the size of our worker modules shrink dramatically and Cesium itself will load them much faster. The total minified/gzipped size of all workers in master is 2.6 MB compared to 225 KB in this branch! This should be most noticeable on demos like Geometry & Appearances which load lots of workers for the various geometry typs. roll-up is also used to build Cesium Viewer, which is now an ES6 app. We use clean-css via gulp and it is also a straightforward change from requirejs that requires no special mention. Workers While the spec allows for ES6 Web Workers, no browser actually supports them yet. That means we needed a way to get our workers into non-ES6 form. Thankfully, roll-up can generate AMD modules, which means we now have a build step to compile our Worker source code back into AMD and use the existing TaskProcessor to load and execute them. This build step is part of the standard build task and is called createWorkers. During development, these "built" workers are un-optimized so you can still debug them and read the code. Since there is a build step, that means if you are changing code that affects a worker, you need to re-run build, or you can use the build-watch task to do it automatically. The ES6 versions of Worker code has moved into Source/WorkersES6 and we build the workers into their "old home" of Source/Workers. cesiumWorkerBootstrapper and transferTypedArrayTest which were already non-AMD ES5 scripts remain living in the Workers directory. Surprisingly little was changed about TaskProcessor or the worker system in general, especially considering that I thought this would be one of the major hurdles. ThirdParty A lot of our ThirdParty either already had a hand-written wrapper for AMD (which I updated to ES6) or had UMD which created problems when importing the same code in both Node and the browser. I basically had to update the wrapper of every third-party library to fix these problems. In some cases I updated the library version itself (Autolinker, topojson). Nothing to be too concerned about, but future clean-up would be using npm versions of these libraries and auto-generating the wrappers as needed so we don't hand-edit things. Sandcastle Sandcastle is eternal and manages to live another day in it's ancient requirejs/dojo 1.x form. Sandcastle now automatically uses the ES6 version of Cesium if it is available and fallsback to the ES5 unminified version if it is now. The built version of Sandcastle always uses CesiumUnminified, just like master. This means Sandcastle still works in IE11 if you run the combine step first (or use the relase zip) Removed Cesium usage from Sandcastle proper, since it wasn't really needed Generate a VERSION propertyin the gallery index since Cesium is no longer being included. Remove requirejs from Sandcastle bucket Update bucket to use the built version of Cesium if it is available by fallbackto the ES6 version during development. Standalone.html was also updated There's a bit of room for further clean-up here, but I think this gets us into master. I did not rename bucket-requirejs.html because I'm pretty sure it would break previously shared demos. We can put in some backwards compatible code later on if we want. (But I'd rather just see a full Sandcastle rewrite). Specs Specs are now all ES6, except for TestWorkers, which remain standard JS worker modules. This means you can no longer run the unbuilt unit tests in IE11. No changes for Chrome and Firefox. Since the specs use ES6 modules and built Cesium is an ES5 UMD, I added a build-specs build step which generates a combined ES5 version of the specs which rely on Cesium as a global variable. We then inject these files into jasmine instead of the standard specs and everything works exactly as it did before. SpecRunner.html has been updated to inject the correct version of the script depending on the build/release query parameters. The Specs must always use Cesium by importing Source/Cesium.js, this is so we can replace it with the built Cesium as describe above. There's a bunch of room for clean-up here, such as unifying our two copies of jasmine into a single helper file, but I didn't want to start doing that clean-up as part of this already overly big PR. The important thing is that we can still test the built version and still test on IE/Edge as needed. I also found and fixed two bugs that were causing failing unit tests, one in BingMapsImageryProviderSpec.js (which was overwriting createImage andnot setting it back) and ShadowVolumeAppearance.js (which had a module level caching bug). I think these may have been the cause of random CI failures in master as well, but only time will tell. For coverage, we had to switch to karma-coverage-istanbul-instrumenter for native ES6 support, but that's it. Finally, I updated appveryor to build Cesium and run the built tests under IE. We still don't fail the build for IE, but we should probably fix that if we want to keep it going. NodeJS When NODE_ENV is production, we now require in the minified CesiumJS directly, which works great because it's now a UMD module. Otherwise, we use the excellant esmpackage to load individual modules, it was a fairly straightforward swap from our old requirejs usage. We could probably drop esm too if we don't care about debugging or if we provie source maps at some point.
2019-10-03 23:51:23 +08:00
export default SunPostProcess;