/
ViewportQuad.js
260 lines (233 loc) · 8.51 KB
/
ViewportQuad.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*global define*/
define([
'../Core/Color',
'../Core/combine',
'../Core/destroyObject',
'../Core/defaultValue',
'../Core/defined',
'../Core/DeveloperError',
'../Core/BoundingRectangle',
'../Core/ComponentDatatype',
'../Core/PrimitiveType',
'../Core/Geometry',
'../Core/GeometryAttribute',
'./Material',
'../Renderer/BufferUsage',
'../Renderer/BlendingState',
'../Renderer/CommandLists',
'../Renderer/DrawCommand',
'../Renderer/createShaderSource',
'../Shaders/ViewportQuadVS',
'../Shaders/ViewportQuadFS'
], function(
Color,
combine,
destroyObject,
defaultValue,
defined,
DeveloperError,
BoundingRectangle,
ComponentDatatype,
PrimitiveType,
Geometry,
GeometryAttribute,
Material,
BufferUsage,
BlendingState,
CommandLists,
DrawCommand,
createShaderSource,
ViewportQuadVS,
ViewportQuadFS) {
"use strict";
/**
* A viewport aligned quad.
*
* @alias ViewportQuad
* @constructor
*
* @param {BoundingRectangle} [rectangle] The {@link BoundingRectangle} defining the quad's position within the viewport.
* @param {Material} [material] The {@link Material} defining the surface appearance of the viewport quad.
*
* @example
* var viewportQuad = new ViewportQuad(new BoundingRectangle(0, 0, 80, 40));
* viewportQuad.material.uniforms.color = new Color(1.0, 0.0, 0.0, 1.0);
*/
var ViewportQuad = function(rectangle, material) {
this._va = undefined;
this._overlayCommand = new DrawCommand();
this._overlayCommand.primitiveType = PrimitiveType.TRIANGLE_FAN;
this._overlayCommand.owner = this;
this._commandLists = new CommandLists();
this._commandLists.overlayList.push(this._overlayCommand);
/**
* Determines if the viewport quad primitive will be shown.
*
* @type {Boolean}
* @default true
*/
this.show = true;
if (!defined(rectangle)) {
rectangle = new BoundingRectangle();
}
/**
* The BoundingRectangle defining the quad's position within the viewport.
*
* @type {BoundingRectangle}
*
* @example
* viewportQuad.rectangle = new BoundingRectangle(0, 0, 80, 40);
*/
this.rectangle = BoundingRectangle.clone(rectangle);
if (!defined(material)) {
material = Material.fromType(Material.ColorType);
material.uniforms.color = new Color(1.0, 1.0, 1.0, 1.0);
}
/**
* The surface appearance of the viewport quad. This can be one of several built-in {@link Material} objects or a custom material, scripted with
* <a href='https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric'>Fabric</a>.
* <p>
* The default material is <code>Material.ColorType</code>.
* </p>
*
* @type Material
*
* @example
* // 1. Change the color of the default material to yellow
* viewportQuad.material.uniforms.color = new Color(1.0, 1.0, 0.0, 1.0);
*
* // 2. Change material to horizontal stripes
* viewportQuad.material = Material.fromType(Material.StripeType);
*
* @see <a href='https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric'>Fabric</a>
*/
this.material = material;
this._material = undefined;
};
var attributeIndices = {
position : 0,
textureCoordinates : 1
};
function getVertexArray(context) {
// Per-context cache for viewport quads
var vertexArray = context.cache.viewportQuad_vertexArray;
if (defined(vertexArray)) {
return vertexArray;
}
var geometry = new Geometry({
attributes : {
position : new GeometryAttribute({
componentDatatype : ComponentDatatype.FLOAT,
componentsPerAttribute : 2,
values : [
-1.0, -1.0,
1.0, -1.0,
1.0, 1.0,
-1.0, 1.0
]
}),
textureCoordinates : new GeometryAttribute({
componentDatatype : ComponentDatatype.FLOAT,
componentsPerAttribute : 2,
values : [
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 1.0
]
})
},
primitiveType : PrimitiveType.TRIANGLES
});
vertexArray = context.createVertexArrayFromGeometry({
geometry : geometry,
attributeIndices : attributeIndices,
bufferUsage : BufferUsage.STATIC_DRAW
});
context.cache.viewportQuad_vertexArray = vertexArray;
return vertexArray;
}
/**
* Commits changes to properties before rendering by updating the object's WebGL resources.
*
* @memberof ViewportQuad
*
* @exception {DeveloperError} this.material must be defined.
* @exception {DeveloperError} this.rectangle must be defined.
*/
ViewportQuad.prototype.update = function(context, frameState, commandList) {
if (!this.show)
{
return;
}
if (!defined(this.material)) {
throw new DeveloperError('this.material must be defined.');
}
if (!defined(this.rectangle)) {
throw new DeveloperError('this.rectangle must be defined.');
}
if (!defined(this._va)) {
this._va = getVertexArray(context);
this._overlayCommand.vertexArray = this._va;
}
var rs = this._overlayCommand.renderState;
if ((!defined(rs)) || !BoundingRectangle.equals(rs.viewport, this.rectangle)) {
this._overlayCommand.renderState = context.createRenderState({
blending : BlendingState.ALPHA_BLEND,
viewport : this.rectangle
});
}
var pass = frameState.passes;
if (pass.overlay) {
if (this._material !== this.material) {
// Recompile shader when material changes
this._material = this.material;
var fsSource = createShaderSource({ sources : [this._material.shaderSource, ViewportQuadFS] });
this._overlayCommand.shaderProgram = context.getShaderCache().replaceShaderProgram(
this._overlayCommand.shaderProgram, ViewportQuadVS, fsSource, attributeIndices);
}
this._material.update(context);
this._overlayCommand.uniformMap = this._material._uniforms;
commandList.push(this._commandLists);
}
};
/**
* 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 ViewportQuad
*
* @returns {Boolean} True if this object was destroyed; otherwise, false.
*
* @see ViewportQuad#destroy
*/
ViewportQuad.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 ViewportQuad
*
* @returns {undefined}
*
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*
* @see ViewportQuad#isDestroyed
*
* @example
* quad = quad && quad.destroy();
*/
ViewportQuad.prototype.destroy = function() {
this._overlayCommand.shaderProgram = this._overlayCommand.shaderProgram && this._overlayCommand.shaderProgram.release();
return destroyObject(this);
};
return ViewportQuad;
});