123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- import Cartesian3 from "../Core/Cartesian3.js";
- import defaultValue from "../Core/defaultValue.js";
- import defined from "../Core/defined.js";
- import destroyObject from "../Core/destroyObject.js";
- import Ellipsoid from "../Core/Ellipsoid.js";
- import EllipsoidGeometry from "../Core/EllipsoidGeometry.js";
- import GeometryPipeline from "../Core/GeometryPipeline.js";
- import CesiumMath from "../Core/Math.js";
- import Matrix4 from "../Core/Matrix4.js";
- import VertexFormat from "../Core/VertexFormat.js";
- import BufferUsage from "../Renderer/BufferUsage.js";
- import DrawCommand from "../Renderer/DrawCommand.js";
- import RenderState from "../Renderer/RenderState.js";
- import ShaderProgram from "../Renderer/ShaderProgram.js";
- import ShaderSource from "../Renderer/ShaderSource.js";
- import VertexArray from "../Renderer/VertexArray.js";
- import SkyAtmosphereCommon from "../Shaders/SkyAtmosphereCommon.js";
- import SkyAtmosphereFS from "../Shaders/SkyAtmosphereFS.js";
- import SkyAtmosphereVS from "../Shaders/SkyAtmosphereVS.js";
- import Axis from "./Axis.js";
- import BlendingState from "./BlendingState.js";
- import CullFace from "./CullFace.js";
- import SceneMode from "./SceneMode.js";
- function SkyAtmosphere(ellipsoid) {
- ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
-
- this.show = true;
-
- this.perFragmentAtmosphere = false;
- this._ellipsoid = ellipsoid;
- var outerEllipsoidScale = 1.025;
- var scaleVector = Cartesian3.multiplyByScalar(
- ellipsoid.radii,
- outerEllipsoidScale,
- new Cartesian3()
- );
- this._scaleMatrix = Matrix4.fromScale(scaleVector);
- this._modelMatrix = new Matrix4();
- this._command = new DrawCommand({
- owner: this,
- modelMatrix: this._modelMatrix,
- });
- this._spSkyFromSpace = undefined;
- this._spSkyFromAtmosphere = undefined;
- this._flags = undefined;
-
- this.hueShift = 0.0;
-
- this.saturationShift = 0.0;
-
- this.brightnessShift = 0.0;
- this._hueSaturationBrightness = new Cartesian3();
-
- var radiiAndDynamicAtmosphereColor = new Cartesian3();
- radiiAndDynamicAtmosphereColor.x =
- ellipsoid.maximumRadius * outerEllipsoidScale;
- radiiAndDynamicAtmosphereColor.y = ellipsoid.maximumRadius;
-
- radiiAndDynamicAtmosphereColor.z = 0;
- this._radiiAndDynamicAtmosphereColor = radiiAndDynamicAtmosphereColor;
- var that = this;
- this._command.uniformMap = {
- u_radiiAndDynamicAtmosphereColor: function () {
- return that._radiiAndDynamicAtmosphereColor;
- },
- u_hsbShift: function () {
- that._hueSaturationBrightness.x = that.hueShift;
- that._hueSaturationBrightness.y = that.saturationShift;
- that._hueSaturationBrightness.z = that.brightnessShift;
- return that._hueSaturationBrightness;
- },
- };
- }
- Object.defineProperties(SkyAtmosphere.prototype, {
-
- ellipsoid: {
- get: function () {
- return this._ellipsoid;
- },
- },
- });
- SkyAtmosphere.prototype.setDynamicAtmosphereColor = function (
- enableLighting,
- useSunDirection
- ) {
- var lightEnum = enableLighting ? (useSunDirection ? 2.0 : 1.0) : 0.0;
- this._radiiAndDynamicAtmosphereColor.z = lightEnum;
- };
- var scratchModelMatrix = new Matrix4();
- SkyAtmosphere.prototype.update = function (frameState, globe) {
- if (!this.show) {
- return undefined;
- }
- var mode = frameState.mode;
- if (mode !== SceneMode.SCENE3D && mode !== SceneMode.MORPHING) {
- return undefined;
- }
-
- if (!frameState.passes.render) {
- return undefined;
- }
-
-
- var rotationMatrix = Matrix4.fromRotationTranslation(
- frameState.context.uniformState.inverseViewRotation,
- Cartesian3.ZERO,
- scratchModelMatrix
- );
- var rotationOffsetMatrix = Matrix4.multiplyTransformation(
- rotationMatrix,
- Axis.Y_UP_TO_Z_UP,
- scratchModelMatrix
- );
- var modelMatrix = Matrix4.multiply(
- this._scaleMatrix,
- rotationOffsetMatrix,
- scratchModelMatrix
- );
- Matrix4.clone(modelMatrix, this._modelMatrix);
- var context = frameState.context;
- var colorCorrect = hasColorCorrection(this);
- var translucent = frameState.globeTranslucencyState.translucent;
- var perFragmentAtmosphere =
- this.perFragmentAtmosphere || translucent || !defined(globe) || !globe.show;
- var command = this._command;
- if (!defined(command.vertexArray)) {
- var geometry = EllipsoidGeometry.createGeometry(
- new EllipsoidGeometry({
- radii: new Cartesian3(1.0, 1.0, 1.0),
- slicePartitions: 256,
- stackPartitions: 256,
- vertexFormat: VertexFormat.POSITION_ONLY,
- })
- );
- command.vertexArray = VertexArray.fromGeometry({
- context: context,
- geometry: geometry,
- attributeLocations: GeometryPipeline.createAttributeLocations(geometry),
- bufferUsage: BufferUsage.STATIC_DRAW,
- });
- command.renderState = RenderState.fromCache({
- cull: {
- enabled: true,
- face: CullFace.FRONT,
- },
- blending: BlendingState.ALPHA_BLEND,
- depthMask: false,
- });
- }
- var flags = colorCorrect | (perFragmentAtmosphere << 2) | (translucent << 3);
- if (flags !== this._flags) {
- this._flags = flags;
- var defines = [];
- if (colorCorrect) {
- defines.push("COLOR_CORRECT");
- }
- if (perFragmentAtmosphere) {
- defines.push("PER_FRAGMENT_ATMOSPHERE");
- }
- if (translucent) {
- defines.push("GLOBE_TRANSLUCENT");
- }
- var vs = new ShaderSource({
- defines: defines.concat("SKY_FROM_SPACE"),
- sources: [SkyAtmosphereCommon, SkyAtmosphereVS],
- });
- var fs = new ShaderSource({
- defines: defines.concat("SKY_FROM_SPACE"),
- sources: [SkyAtmosphereCommon, SkyAtmosphereFS],
- });
- this._spSkyFromSpace = ShaderProgram.fromCache({
- context: context,
- vertexShaderSource: vs,
- fragmentShaderSource: fs,
- });
- vs = new ShaderSource({
- defines: defines.concat("SKY_FROM_ATMOSPHERE"),
- sources: [SkyAtmosphereCommon, SkyAtmosphereVS],
- });
- fs = new ShaderSource({
- defines: defines.concat("SKY_FROM_ATMOSPHERE"),
- sources: [SkyAtmosphereCommon, SkyAtmosphereFS],
- });
- this._spSkyFromAtmosphere = ShaderProgram.fromCache({
- context: context,
- vertexShaderSource: vs,
- fragmentShaderSource: fs,
- });
- }
- var cameraPosition = frameState.camera.positionWC;
- var cameraHeight = Cartesian3.magnitude(cameraPosition);
- if (cameraHeight > this._radiiAndDynamicAtmosphereColor.x) {
-
- command.shaderProgram = this._spSkyFromSpace;
- } else {
-
- command.shaderProgram = this._spSkyFromAtmosphere;
- }
- return command;
- };
- function hasColorCorrection(skyAtmosphere) {
- return !(
- CesiumMath.equalsEpsilon(
- skyAtmosphere.hueShift,
- 0.0,
- CesiumMath.EPSILON7
- ) &&
- CesiumMath.equalsEpsilon(
- skyAtmosphere.saturationShift,
- 0.0,
- CesiumMath.EPSILON7
- ) &&
- CesiumMath.equalsEpsilon(
- skyAtmosphere.brightnessShift,
- 0.0,
- CesiumMath.EPSILON7
- )
- );
- }
- SkyAtmosphere.prototype.isDestroyed = function () {
- return false;
- };
- SkyAtmosphere.prototype.destroy = function () {
- var command = this._command;
- command.vertexArray = command.vertexArray && command.vertexArray.destroy();
- this._spSkyFromSpace = this._spSkyFromSpace && this._spSkyFromSpace.destroy();
- this._spSkyFromAtmosphere =
- this._spSkyFromAtmosphere && this._spSkyFromAtmosphere.destroy();
- return destroyObject(this);
- };
- export default SkyAtmosphere;
|