/
VertexFormat.js
309 lines (283 loc) · 9.42 KB
/
VertexFormat.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
define([
'./defaultValue',
'./defined',
'./DeveloperError',
'./freezeObject'
], function(
defaultValue,
defined,
DeveloperError,
freezeObject) {
'use strict';
/**
* A vertex format defines what attributes make up a vertex. A VertexFormat can be provided
* to a {@link Geometry} to request that certain properties be computed, e.g., just position,
* position and normal, etc.
*
* @param {Object} [options] An object with boolean properties corresponding to VertexFormat properties as shown in the code example.
*
* @alias VertexFormat
* @constructor
*
* @example
* // Create a vertex format with position and 2D texture coordinate attributes.
* var format = new Cesium.VertexFormat({
* position : true,
* st : true
* });
*
* @see Geometry#attributes
* @see Packable
*/
function VertexFormat(options) {
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
/**
* When <code>true</code>, the vertex has a 3D position attribute.
* <p>
* 64-bit floating-point (for precision). 3 components per attribute.
* </p>
*
* @type Boolean
*
* @default false
*/
this.position = defaultValue(options.position, false);
/**
* When <code>true</code>, the vertex has a normal attribute (normalized), which is commonly used for lighting.
* <p>
* 32-bit floating-point. 3 components per attribute.
* </p>
*
* @type Boolean
*
* @default false
*/
this.normal = defaultValue(options.normal, false);
/**
* When <code>true</code>, the vertex has a 2D texture coordinate attribute.
* <p>
* 32-bit floating-point. 2 components per attribute
* </p>
*
* @type Boolean
*
* @default false
*/
this.st = defaultValue(options.st, false);
/**
* When <code>true</code>, the vertex has a bitangent attribute (normalized), which is used for tangent-space effects like bump mapping.
* <p>
* 32-bit floating-point. 3 components per attribute.
* </p>
*
* @type Boolean
*
* @default false
*/
this.bitangent = defaultValue(options.bitangent, false);
/**
* When <code>true</code>, the vertex has a tangent attribute (normalized), which is used for tangent-space effects like bump mapping.
* <p>
* 32-bit floating-point. 3 components per attribute.
* </p>
*
* @type Boolean
*
* @default false
*/
this.tangent = defaultValue(options.tangent, false);
/**
* When <code>true</code>, the vertex has an RGB color attribute.
* <p>
* 8-bit unsigned byte. 3 components per attribute.
* </p>
*
* @type Boolean
*
* @default false
*/
this.color = defaultValue(options.color, false);
}
/**
* An immutable vertex format with only a position attribute.
*
* @type {VertexFormat}
* @constant
*
* @see VertexFormat#position
*/
VertexFormat.POSITION_ONLY = freezeObject(new VertexFormat({
position : true
}));
/**
* An immutable vertex format with position and normal attributes.
* This is compatible with per-instance color appearances like {@link PerInstanceColorAppearance}.
*
* @type {VertexFormat}
* @constant
*
* @see VertexFormat#position
* @see VertexFormat#normal
*/
VertexFormat.POSITION_AND_NORMAL = freezeObject(new VertexFormat({
position : true,
normal : true
}));
/**
* An immutable vertex format with position, normal, and st attributes.
* This is compatible with {@link MaterialAppearance} when {@link MaterialAppearance#materialSupport}
* is <code>TEXTURED/code>.
*
* @type {VertexFormat}
* @constant
*
* @see VertexFormat#position
* @see VertexFormat#normal
* @see VertexFormat#st
*/
VertexFormat.POSITION_NORMAL_AND_ST = freezeObject(new VertexFormat({
position : true,
normal : true,
st : true
}));
/**
* An immutable vertex format with position and st attributes.
* This is compatible with {@link EllipsoidSurfaceAppearance}.
*
* @type {VertexFormat}
* @constant
*
* @see VertexFormat#position
* @see VertexFormat#st
*/
VertexFormat.POSITION_AND_ST = freezeObject(new VertexFormat({
position : true,
st : true
}));
/**
* An immutable vertex format with position and color attributes.
*
* @type {VertexFormat}
* @constant
*
* @see VertexFormat#position
* @see VertexFormat#color
*/
VertexFormat.POSITION_AND_COLOR = freezeObject(new VertexFormat({
position : true,
color : true
}));
/**
* An immutable vertex format with well-known attributes: position, normal, st, tangent, and bitangent.
*
* @type {VertexFormat}
* @constant
*
* @see VertexFormat#position
* @see VertexFormat#normal
* @see VertexFormat#st
* @see VertexFormat#tangent
* @see VertexFormat#bitangent
*/
VertexFormat.ALL = freezeObject(new VertexFormat({
position : true,
normal : true,
st : true,
tangent : true,
bitangent : true
}));
/**
* An immutable vertex format with position, normal, and st attributes.
* This is compatible with most appearances and materials; however
* normal and st attributes are not always required. When this is
* known in advance, another <code>VertexFormat</code> should be used.
*
* @type {VertexFormat}
* @constant
*
* @see VertexFormat#position
* @see VertexFormat#normal
*/
VertexFormat.DEFAULT = VertexFormat.POSITION_NORMAL_AND_ST;
/**
* The number of elements used to pack the object into an array.
* @type {Number}
*/
VertexFormat.packedLength = 6;
/**
* Stores the provided instance into the provided array.
*
* @param {VertexFormat} value The value to pack.
* @param {Number[]} array The array to pack into.
* @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
*
* @returns {Number[]} The array that was packed into
*/
VertexFormat.pack = function(value, array, startingIndex) {
//>>includeStart('debug', pragmas.debug);
if (!defined(value)) {
throw new DeveloperError('value is required');
}
if (!defined(array)) {
throw new DeveloperError('array is required');
}
//>>includeEnd('debug');
startingIndex = defaultValue(startingIndex, 0);
array[startingIndex++] = value.position ? 1.0 : 0.0;
array[startingIndex++] = value.normal ? 1.0 : 0.0;
array[startingIndex++] = value.st ? 1.0 : 0.0;
array[startingIndex++] = value.tangent ? 1.0 : 0.0;
array[startingIndex++] = value.bitangent ? 1.0 : 0.0;
array[startingIndex] = value.color ? 1.0 : 0.0;
return array;
};
/**
* Retrieves an instance from a packed array.
*
* @param {Number[]} array The packed array.
* @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
* @param {VertexFormat} [result] The object into which to store the result.
* @returns {VertexFormat} The modified result parameter or a new VertexFormat instance if one was not provided.
*/
VertexFormat.unpack = function(array, startingIndex, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(array)) {
throw new DeveloperError('array is required');
}
//>>includeEnd('debug');
startingIndex = defaultValue(startingIndex, 0);
if (!defined(result)) {
result = new VertexFormat();
}
result.position = array[startingIndex++] === 1.0;
result.normal = array[startingIndex++] === 1.0;
result.st = array[startingIndex++] === 1.0;
result.tangent = array[startingIndex++] === 1.0;
result.bitangent = array[startingIndex++] === 1.0;
result.color = array[startingIndex] === 1.0;
return result;
};
/**
* Duplicates a VertexFormat instance.
*
* @param {VertexFormat} vertexFormat The vertex format to duplicate.
* @param {VertexFormat} [result] The object onto which to store the result.
* @returns {VertexFormat} The modified result parameter or a new VertexFormat instance if one was not provided. (Returns undefined if vertexFormat is undefined)
*/
VertexFormat.clone = function(vertexFormat, result) {
if (!defined(vertexFormat)) {
return undefined;
}
if (!defined(result)) {
result = new VertexFormat();
}
result.position = vertexFormat.position;
result.normal = vertexFormat.normal;
result.st = vertexFormat.st;
result.tangent = vertexFormat.tangent;
result.bitangent = vertexFormat.bitangent;
result.color = vertexFormat.color;
return result;
};
return VertexFormat;
});