-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathBufferPolygonCollection.js
More file actions
360 lines (317 loc) · 10.1 KB
/
BufferPolygonCollection.js
File metadata and controls
360 lines (317 loc) · 10.1 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// @ts-check
import defined from "../Core/defined.js";
import BufferPrimitiveCollection from "./BufferPrimitiveCollection.js";
import BufferPolygon from "./BufferPolygon.js";
import Frozen from "../Core/Frozen.js";
import assert from "../Core/assert.js";
import IndexDatatype from "../Core/IndexDatatype.js";
import renderPolygons from "./renderBufferPolygonCollection.js";
/** @import { TypedArray } from "../Core/globalTypes.js"; */
/** @import Color from "../Core/Color.js"; */
/** @import Matrix4 from "../Core/Matrix4.js"; */
/** @import FrameState from "./FrameState.js" */
/** @import ComponentDatatype from "../Core/ComponentDatatype.js"; */
const { ERR_CAPACITY } = BufferPrimitiveCollection.Error;
/**
* @typedef {object} BufferPolygonOptions
* @property {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] Transforms geometry from model to world coordinates.
* @property {boolean} [show=true]
* @property {Color} [color=Color.WHITE]
* @property {TypedArray} [positions]
* @property {TypedArray} [holes]
* @property {TypedArray} [triangles]
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
*/
/**
* Collection of polygons held in ArrayBuffer storage for performance and memory optimization.
*
* <p>Default buffer memory allocation is arbitrary, and collections cannot be resized,
* so specific per-buffer capacities should be provided in the collection
* constructor when available.</p>
*
* @example
* import earcut from "earcut";
*
* const collection = new BufferPolygonCollection({
* primitiveCountMax: 1024,
* vertexCountMax: 4096,
* holeCountMax: 1024,
* triangleCountMax: 2048,
* });
*
* const polygon = new BufferPolygon();
* const positions = [ ... ];
* const holes = [ ... ];
*
* // Create a new polygon, temporarily bound to 'polygon' local variable.
* collection.add({
* positions: new Float64Array(positions),
* holes: new Uint32Array(holes),
* triangles: new Uint32Array(earcut(positions, holes, 3)),
* color: Color.WHITE,
* }, polygon);
*
* // Iterate over all polygons in collection, temporarily binding 'polygon'
* // local variable to each, and updating polygon color.
* for (let i = 0; i < collection.primitiveCount; i++) {
* collection.get(i, polygon);
* polygon.setColor(Color.RED);
* }
*
* @see BufferPolygon
* @see BufferPrimitiveCollection
* @extends BufferPrimitiveCollection<BufferPolygon>
* @experimental This feature is not final and is subject to change without Cesium's standard deprecation policy.
*/
class BufferPolygonCollection extends BufferPrimitiveCollection {
/**
* @param {object} options
* @param {number} [options.primitiveCountMax=BufferPrimitiveCollection.DEFAULT_CAPACITY]
* @param {number} [options.vertexCountMax=BufferPrimitiveCollection.DEFAULT_CAPACITY]
* @param {number} [options.holeCountMax=BufferPrimitiveCollection.DEFAULT_CAPACITY]
* @param {number} [options.triangleCountMax=BufferPrimitiveCollection.DEFAULT_CAPACITY]
* @param {ComponentDatatype} [options.positionDatatype=ComponentDatatype.DOUBLE]
* @param {boolean} [options.show=true]
* @param {boolean} [options.debugShowBoundingVolume=false]
*/
constructor(options = Frozen.EMPTY_OBJECT) {
super(options);
/**
* @type {number}
* @ignore
*/
this._holeCount = 0;
/**
* @type {number}
* @protected
* @ignore
*/
this._holeCountMax =
options.holeCountMax ?? BufferPrimitiveCollection.DEFAULT_CAPACITY;
/**
* @type {TypedArray}
* @ignore
*/
this._holeIndexView = null;
/**
* @type {number}
* @ignore
*/
this._triangleCount = 0;
/**
* @type {number}
* @protected
* @ignore
*/
this._triangleCountMax =
options.triangleCountMax ?? BufferPrimitiveCollection.DEFAULT_CAPACITY;
/**
* @type {TypedArray}
* @ignore
*/
this._triangleIndexView = null;
this._allocateHoleIndexBuffer();
this._allocateTriangleIndexBuffer();
}
_getCollectionClass() {
return BufferPolygonCollection;
}
_getPrimitiveClass() {
return BufferPolygon;
}
/////////////////////////////////////////////////////////////////////////////
// COLLECTION LIFECYCLE
/**
* @private
* @ignore
*/
_allocateHoleIndexBuffer() {
// @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203.
this._holeIndexView = IndexDatatype.createTypedArray(
this._positionCountMax,
this._holeCountMax,
);
}
/**
* @private
* @ignore
*/
_allocateTriangleIndexBuffer() {
// @ts-expect-error Requires https://github.com/CesiumGS/cesium/pull/13203.
this._triangleIndexView = IndexDatatype.createTypedArray(
this._positionCountMax,
this._triangleCountMax * 3,
);
}
/**
* Duplicates the contents of this collection into the result collection.
* Result collection is not resized, and must contain enough space for all
* primitives in the source collection. Existing polygons in the result
* collection will be overwritten.
*
* <p>Useful when allocating more space for a collection that has reached its
* capacity, and efficiently transferring polygons to the new collection.</p>
*
* @example
* const result = new BufferPolygonCollection({ ... }); // allocate larger 'result' collection
* BufferPolygonCollection.clone(collection, result); // copy polygons from 'collection' into 'result'
*
* @param {BufferPolygonCollection} collection
* @param {BufferPolygonCollection} result
* @returns {BufferPolygonCollection}
*/
static clone(collection, result) {
super.clone(collection, result);
//>>includeStart('debug', pragmas.debug);
assert(collection.holeCount <= result.holeCountMax, ERR_CAPACITY);
assert(collection.triangleCount <= result.triangleCountMax, ERR_CAPACITY);
//>>includeEnd('debug');
this._copySubArray(
collection._holeIndexView,
result._holeIndexView,
collection.holeCount,
);
this._copySubArray(
collection._triangleIndexView,
result._triangleIndexView,
collection._triangleCount * 3,
);
result._holeCount = collection._holeCount;
result._triangleCount = collection._triangleCount;
return result;
}
/**
* @param {BufferPolygonCollection} collection
* @returns {BufferPolygonCollection}
* @override
* @ignore
*/
static _cloneEmpty(collection) {
return new BufferPolygonCollection({
primitiveCountMax: collection.primitiveCountMax,
vertexCountMax: collection.vertexCountMax,
holeCountMax: collection.holeCountMax,
triangleCountMax: collection.triangleCountMax,
});
}
/**
* @param {BufferPolygonCollection} src
* @param {BufferPolygonCollection} dst
* @override
* @ignore
*/
static _replaceBuffers(src, dst) {
super._replaceBuffers(src, dst);
dst._holeIndexView = src._holeIndexView;
dst._triangleIndexView = src._triangleIndexView;
}
/////////////////////////////////////////////////////////////////////////////
// PRIMITIVE LIFECYCLE
/**
* Adds a new polygon to the collection, with the specified options. A
* {@link BufferPolygon} instance is linked to the new polygon, using
* the 'result' argument if given, or a new instance if not. For repeated
* calls, prefer to reuse a single BufferPolygon instance rather than
* allocating a new instance on each call.
*
* @param {BufferPolygonOptions} options
* @param {BufferPolygon} result
* @returns {BufferPolygon}
* @override
*/
add(options, result = new BufferPolygon()) {
super.add(options, result);
const vertexOffset = this._positionCount;
result._setUint32(BufferPolygon.Layout.POSITION_OFFSET_U32, vertexOffset);
result._setUint32(BufferPolygon.Layout.POSITION_COUNT_U32, 0);
const holeOffset = this._holeCount;
result._setUint32(BufferPolygon.Layout.HOLE_OFFSET_U32, holeOffset);
result._setUint32(BufferPolygon.Layout.HOLE_COUNT_U32, 0);
const triangleOffset = this._triangleCount;
result._setUint32(BufferPolygon.Layout.TRIANGLE_OFFSET_U32, triangleOffset);
result._setUint32(BufferPolygon.Layout.TRIANGLE_COUNT_U32, 0);
if (defined(options.positions)) {
result.setPositions(options.positions);
}
if (defined(options.holes)) {
result.setHoles(options.holes);
}
if (defined(options.triangles)) {
result.setTriangles(options.triangles);
}
return result;
}
/////////////////////////////////////////////////////////////////////////////
// RENDER
/**
* @param {FrameState} frameState
* @ignore
*/
update(frameState) {
super.update(frameState);
if (this.show) {
this._renderContext = renderPolygons(
this,
frameState,
this._renderContext,
);
}
}
/////////////////////////////////////////////////////////////////////////////
// ACCESSORS
/**
* Total byte length of buffers owned by this collection. Includes any unused
* space allocated by {@link primitiveCountMax}, even if no polygons have
* yet been added in that space.
*
* @type {number}
* @readonly
* @override
*/
get byteLength() {
return (
super.byteLength +
this._holeIndexView.byteLength +
this._triangleIndexView.byteLength
);
}
/**
* Number of holes in collection. Must be <= {@link holeCountMax}.
*
* @type {number}
* @readonly
*/
get holeCount() {
return this._holeCount;
}
/**
* Maximum number of holes in collection. Must be >= {@link holeCount}.
*
* @type {number}
* @readonly
* @default {@link BufferPrimitiveCollection.DEFAULT_CAPACITY}
*/
get holeCountMax() {
return this._holeCountMax;
}
/**
* Number of triangles in collection. Must be <= {@link triangleCountMax}.
*
* @type {number}
* @readonly
*/
get triangleCount() {
return this._triangleCount;
}
/**
* Maximum number of triangles in collection. Must be >= {@link triangleCount}.
*
* @type {number}
* @readonly
* @default {@link BufferPrimitiveCollection.DEFAULT_CAPACITY}
*/
get triangleCountMax() {
return this._triangleCountMax;
}
}
export default BufferPolygonCollection;