/
Sun.js
180 lines (157 loc) · 5.45 KB
/
Sun.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
/*global define*/
define([
'../Core/BoundingSphere',
'../Core/Cartesian3',
'../Core/ComponentDatatype',
'../Core/destroyObject',
'../Core/Math',
'../Core/PrimitiveType',
'../Renderer/BlendingState',
'../Renderer/BufferUsage',
'../Renderer/DrawCommand',
'./SceneMode',
'../Shaders/SunVS',
'../Shaders/SunFS'
], function(
BoundingSphere,
Cartesian3,
ComponentDatatype,
destroyObject,
CesiumMath,
PrimitiveType,
BlendingState,
BufferUsage,
DrawCommand,
SceneMode,
SunVS,
SunFS) {
"use strict";
/**
* Draws a sun billboard.
* <p>This is only supported in 3D and Columbus view.</p>
*
* @alias Sun
* @constructor
*
* @example
* scene.sun = new Sun();
*
* @see Scene.sun
*/
var Sun = function() {
this._command = new DrawCommand();
this._boundingVolume = new BoundingSphere();
this._boundingVolume.radius = CesiumMath.SOLAR_RADIUS * 30.0;
this._boundingVolume2D = new BoundingSphere();
this._boundingVolume2D.radius = this._boundingVolume.radius;
/**
* Determines if the sun will be shown.
* <p>
* The default is <code>true</code>.
* </p>
*
* @type Boolean
*/
this.show = true;
};
/**
* @private
*/
Sun.prototype.update = function(context, frameState) {
if (!this.show) {
return undefined;
}
var mode = frameState.mode;
if (mode === SceneMode.SCENE2D || mode === SceneMode.MORPHING) {
return undefined;
}
if (!frameState.passes.color) {
return undefined;
}
var command = this._command;
if (typeof command.vertexArray === 'undefined') {
var attributeIndices = {
direction : 0
};
var directions = new Uint8Array(4 * 2);
directions[0] = 0;
directions[1] = 0;
directions[2] = 255;
directions[3] = 0.0;
directions[4] = 255;
directions[5] = 255;
directions[6] = 0.0;
directions[7] = 255;
var vertexBuffer = context.createVertexBuffer(directions, BufferUsage.STATIC_DRAW);
var attributes = [{
index : attributeIndices.direction,
vertexBuffer : vertexBuffer,
componentsPerAttribute : 2,
normalize : true,
componentDatatype : ComponentDatatype.UNSIGNED_BYTE
}];
command.vertexArray = context.createVertexArray(attributes);
command.primitiveType = PrimitiveType.TRIANGLE_FAN;
command.shaderProgram = context.getShaderCache().getShaderProgram(SunVS, SunFS, attributeIndices);
command.renderState = context.createRenderState({
blending : BlendingState.ALPHA_BLEND
});
command.boundingVolume = new BoundingSphere();
}
var sunPosition = context.getUniformState().getSunPositionWC();
var sunPositionCV = context.getUniformState().getSunPositionColumbusView();
var boundingVolume = this._boundingVolume;
var boundingVolume2D = this._boundingVolume2D;
Cartesian3.clone(sunPosition, boundingVolume.center);
boundingVolume2D.center.x = sunPositionCV.z;
boundingVolume2D.center.y = sunPositionCV.x;
boundingVolume2D.center.z = sunPositionCV.y;
if (mode === SceneMode.SCENE3D) {
BoundingSphere.clone(boundingVolume, command.boundingVolume);
} else if (mode === SceneMode.COLUMBUS_VIEW) {
BoundingSphere.clone(boundingVolume2D, command.boundingVolume);
}
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 Sun
*
* @return {Boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
*
* @see Sun#destroy
*/
Sun.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 Sun
*
* @return {undefined}
*
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*
* @see Sun#isDestroyed
*
* @example
* sun = sun && sun.destroy();
*/
Sun.prototype.destroy = function() {
var command = this._command;
command.vertexArray = command.vertexArray && command.vertexArray.destroy();
command.shaderProgram = command.shaderProgram && command.shaderProgram.release();
return destroyObject(this);
};
return Sun;
});