/
SkyAtmosphere.js
238 lines (217 loc) · 7.75 KB
/
SkyAtmosphere.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*global define*/
define([
'../Core/Cartesian3',
'../Core/defaultValue',
'../Core/defined',
'../Core/defineProperties',
'../Core/destroyObject',
'../Core/Ellipsoid',
'../Core/EllipsoidGeometry',
'../Core/GeometryPipeline',
'../Core/VertexFormat',
'../Renderer/BufferUsage',
'../Renderer/createShaderSource',
'../Renderer/DrawCommand',
'../Shaders/SkyAtmosphereFS',
'../Shaders/SkyAtmosphereVS',
'./BlendingState',
'./CullFace',
'./SceneMode'
], function(
Cartesian3,
defaultValue,
defined,
defineProperties,
destroyObject,
Ellipsoid,
EllipsoidGeometry,
GeometryPipeline,
VertexFormat,
BufferUsage,
createShaderSource,
DrawCommand,
SkyAtmosphereFS,
SkyAtmosphereVS,
BlendingState,
CullFace,
SceneMode) {
"use strict";
/**
* An atmosphere drawn around the limb of the provided ellipsoid. Based on
* {@link http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter16.html|Accurate Atmospheric Scattering}
* in GPU Gems 2.
* <p>
* This is only supported in 3D. atmosphere is faded out when morphing to 2D or Columbus view.
* </p>
*
* @alias SkyAtmosphere
* @constructor
*
* @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid that the atmosphere is drawn around.
*
* @example
* scene.skyAtmosphere = new Cesium.SkyAtmosphere();
*
* @see Scene.skyAtmosphere
*/
var SkyAtmosphere = function(ellipsoid) {
ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
/**
* Determines if the atmosphere is shown.
*
* @type {Boolean}
* @default true
*/
this.show = true;
this._ellipsoid = ellipsoid;
this._command = new DrawCommand({
owner : this
});
this._spSkyFromSpace = undefined;
this._spSkyFromAtmosphere = undefined;
this._fCameraHeight = undefined;
this._fCameraHeight2 = undefined;
this._outerRadius = Cartesian3.getMaximumComponent(Cartesian3.multiplyByScalar(ellipsoid.radii, 1.025));
var innerRadius = ellipsoid.maximumRadius;
var rayleighScaleDepth = 0.25;
var that = this;
this._command.uniformMap = {
fCameraHeight : function() {
return that._fCameraHeight;
},
fCameraHeight2 : function() {
return that._fCameraHeight2;
},
fOuterRadius : function() {
return that._outerRadius;
},
fOuterRadius2 : function() {
return that._outerRadius * that._outerRadius;
},
fInnerRadius : function() {
return innerRadius;
},
fScale : function() {
return 1.0 / (that._outerRadius - innerRadius);
},
fScaleDepth : function() {
return rayleighScaleDepth;
},
fScaleOverScaleDepth : function() {
return (1.0 / (that._outerRadius - innerRadius)) / rayleighScaleDepth;
}
};
};
defineProperties(SkyAtmosphere.prototype, {
/**
* Gets the ellipsoid the atmosphere is drawn around.
* @memberof SkyAtmosphere.prototype
* @type {Ellipsoid}
*/
ellipsoid : {
get : function() {
return this._ellipsoid;
}
}
});
/**
* @private
*/
SkyAtmosphere.prototype.update = function(context, frameState) {
if (!this.show) {
return undefined;
}
if ((frameState.mode !== SceneMode.SCENE3D) &&
(frameState.mode !== SceneMode.MORPHING)) {
return undefined;
}
// The atmosphere is only rendered during the render pass; it is not pickable, it doesn't cast shadows, etc.
if (!frameState.passes.render) {
return undefined;
}
var command = this._command;
if (!defined(command.vertexArray)) {
var geometry = EllipsoidGeometry.createGeometry(new EllipsoidGeometry({
radii : Cartesian3.multiplyByScalar(this._ellipsoid.radii, 1.025),
slicePartitions : 256,
stackPartitions : 256,
vertexFormat : VertexFormat.POSITION_ONLY
}));
command.vertexArray = context.createVertexArrayFromGeometry({
geometry : geometry,
attributeLocations : GeometryPipeline.createAttributeLocations(geometry),
bufferUsage : BufferUsage.STATIC_DRAW
});
command.renderState = context.createRenderState({
cull : {
enabled : true,
face : CullFace.FRONT
},
blending : BlendingState.ALPHA_BLEND
});
var vs = createShaderSource({
defines : ['SKY_FROM_SPACE'],
sources : [SkyAtmosphereVS]
});
this._spSkyFromSpace = context.createShaderProgram(vs, SkyAtmosphereFS);
vs = createShaderSource({
defines : ['SKY_FROM_ATMOSPHERE'],
sources : [SkyAtmosphereVS]
});
this._spSkyFromAtmosphere = context.createShaderProgram(vs, SkyAtmosphereFS);
}
var cameraPosition = frameState.camera.positionWC;
this._fCameraHeight2 = Cartesian3.magnitudeSquared(cameraPosition);
this._fCameraHeight = Math.sqrt(this._fCameraHeight2);
if (this._fCameraHeight > this._outerRadius) {
// Camera in space
command.shaderProgram = this._spSkyFromSpace;
} else {
// Camera in atmosphere
command.shaderProgram = this._spSkyFromAtmosphere;
}
return command;
};
/**
* Returns true if this object was destroyed; otherwise, false.
* <br /><br />
* If this object was destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
*
* @memberof SkyAtmosphere
*
* @returns {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
*
* @see SkyAtmosphere#destroy
*/
SkyAtmosphere.prototype.isDestroyed = function() {
return false;
};
/**
* Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
* release of WebGL resources, instead of relying on the garbage collector to destroy this object.
* <br /><br />
* Once an object is destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
* assign the return value (<code>undefined</code>) to the object as done in the example.
*
* @memberof SkyAtmosphere
*
* @returns {undefined}
*
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*
* @see SkyAtmosphere#isDestroyed
*
* @example
* skyAtmosphere = skyAtmosphere && skyAtmosphere.destroy();
*/
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);
};
return SkyAtmosphere;
});