@loaders.gl/math 4.2.0-alpha.4 → 4.2.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/geometry/attributes/compute-bounding-box.js +35 -23
- package/dist/geometry/attributes/compute-bounding-sphere.js +29 -1
- package/dist/geometry/attributes/compute-tangents.js +145 -1
- package/dist/geometry/attributes/compute-vertex-normals.js +36 -33
- package/dist/geometry/attributes/convert-to-non-indexed.js +27 -25
- package/dist/geometry/attributes/get-attribute-from-geometry.js +23 -20
- package/dist/geometry/attributes/normalize.js +15 -12
- package/dist/geometry/colors/rgb565.js +23 -14
- package/dist/geometry/compression/attribute-compression.js +290 -114
- package/dist/geometry/constants.js +23 -21
- package/dist/geometry/gl/gl-type.js +96 -59
- package/dist/geometry/is-geometry.js +9 -2
- package/dist/geometry/iterators/attribute-iterator.js +12 -8
- package/dist/geometry/iterators/primitive-iterator.js +70 -62
- package/dist/geometry/primitives/modes.js +58 -41
- package/dist/geometry/typed-arrays/typed-array-utils.js +19 -16
- package/dist/geometry/utils/assert.js +8 -4
- package/dist/geometry/utils/coordinates.js +5 -2
- package/dist/index.cjs +19 -18
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +11 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/utils/assert.js +6 -4
- package/package.json +7 -4
- package/dist/geometry/attributes/compute-bounding-box.js.map +0 -1
- package/dist/geometry/attributes/compute-bounding-sphere.js.map +0 -1
- package/dist/geometry/attributes/compute-tangents.js.map +0 -1
- package/dist/geometry/attributes/compute-vertex-normals.js.map +0 -1
- package/dist/geometry/attributes/convert-to-non-indexed.js.map +0 -1
- package/dist/geometry/attributes/get-attribute-from-geometry.js.map +0 -1
- package/dist/geometry/attributes/normalize.js.map +0 -1
- package/dist/geometry/colors/rgb565.js.map +0 -1
- package/dist/geometry/compression/attribute-compression.js.map +0 -1
- package/dist/geometry/constants.js.map +0 -1
- package/dist/geometry/gl/gl-type.js.map +0 -1
- package/dist/geometry/is-geometry.js.map +0 -1
- package/dist/geometry/iterators/attribute-iterator.js.map +0 -1
- package/dist/geometry/iterators/primitive-iterator.js.map +0 -1
- package/dist/geometry/primitives/modes.js.map +0 -1
- package/dist/geometry/typed-arrays/typed-array-utils.js.map +0 -1
- package/dist/geometry/utils/assert.js.map +0 -1
- package/dist/geometry/utils/coordinates.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/utils/assert.js.map +0 -1
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// This file is derived from the Cesium code base under Apache 2 license
|
|
2
|
+
// See LICENSE.md and https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md
|
|
3
|
+
// Attribute compression and decompression functions.
|
|
1
4
|
import { Vector2, Vector3, clamp, _MathUtils } from '@math.gl/core';
|
|
2
5
|
import { assert } from "../utils/assert.js";
|
|
3
6
|
const RIGHT_SHIFT = 1.0 / 256.0;
|
|
@@ -7,153 +10,326 @@ const scratchVector3 = new Vector3();
|
|
|
7
10
|
const scratchEncodeVector2 = new Vector2();
|
|
8
11
|
const octEncodeScratch = new Vector2();
|
|
9
12
|
const uint8ForceArray = new Uint8Array(1);
|
|
13
|
+
/**
|
|
14
|
+
* Force a value to Uint8
|
|
15
|
+
*
|
|
16
|
+
* @param value
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
10
19
|
function forceUint8(value) {
|
|
11
|
-
|
|
12
|
-
|
|
20
|
+
uint8ForceArray[0] = value;
|
|
21
|
+
return uint8ForceArray[0];
|
|
13
22
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Converts a SNORM value in the range [0, rangeMaximum] to a scalar in the range [-1.0, 1.0].
|
|
25
|
+
*
|
|
26
|
+
* @param value SNORM value in the range [0, rangeMaximum]
|
|
27
|
+
* @param [rangeMaximum=255] The maximum value in the SNORM range, 255 by default.
|
|
28
|
+
* @returns Scalar in the range [-1.0, 1.0].
|
|
29
|
+
*
|
|
30
|
+
* @see CesiumMath.toSNorm
|
|
31
|
+
*/
|
|
32
|
+
function fromSNorm(value, rangeMaximum = 255) {
|
|
33
|
+
return (clamp(value, 0.0, rangeMaximum) / rangeMaximum) * 2.0 - 1.0;
|
|
17
34
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Converts a scalar value in the range [-1.0, 1.0] to a SNORM in the range [0, rangeMaximum].
|
|
37
|
+
*
|
|
38
|
+
* @param value The scalar value in the range [-1.0, 1.0]
|
|
39
|
+
* @param [rangeMaximum=255] The maximum value in the mapped range, 255 by default.
|
|
40
|
+
* @returns A SNORM value, where 0 maps to -1.0 and rangeMaximum maps to 1.0.
|
|
41
|
+
*
|
|
42
|
+
* @see CesiumMath.fromSNorm
|
|
43
|
+
*/
|
|
44
|
+
function toSNorm(value, rangeMaximum = 255) {
|
|
45
|
+
return Math.round((clamp(value, -1.0, 1.0) * 0.5 + 0.5) * rangeMaximum);
|
|
21
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Returns 1.0 if the given value is positive or zero, and -1.0 if it is negative.
|
|
49
|
+
* This is similar to `Math.sign` except that returns 1.0 instead of
|
|
50
|
+
* 0.0 when the input value is 0.0.
|
|
51
|
+
*
|
|
52
|
+
* @param value The value to return the sign of.
|
|
53
|
+
* @returns The sign of value.
|
|
54
|
+
*/
|
|
22
55
|
function signNotZero(value) {
|
|
23
|
-
|
|
56
|
+
return value < 0.0 ? -1.0 : 1.0;
|
|
24
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Encodes a normalized vector into 2 SNORM values in the range of [0-rangeMax] following the 'oct' encoding.
|
|
60
|
+
*
|
|
61
|
+
* Oct encoding is a compact representation of unit length vectors.
|
|
62
|
+
* The 'oct' encoding is described in "A Survey of Efficient Representations of Independent Unit Vectors",
|
|
63
|
+
* Cigolle et al 2014: {@link http://jcgt.org/published/0003/02/01/}
|
|
64
|
+
*
|
|
65
|
+
* @param vector The normalized vector to be compressed into 2 component 'oct' encoding.
|
|
66
|
+
* @param result The 2 component oct-encoded unit length vector.
|
|
67
|
+
* @param rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
|
|
68
|
+
* @returns The 2 component oct-encoded unit length vector.
|
|
69
|
+
*
|
|
70
|
+
* @exception vector must be normalized.
|
|
71
|
+
*
|
|
72
|
+
* @see octDecodeInRange
|
|
73
|
+
*/
|
|
25
74
|
export function octEncodeInRange(vector, rangeMax, result) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
75
|
+
assert(vector);
|
|
76
|
+
assert(result);
|
|
77
|
+
const vector3 = scratchVector3.from(vector);
|
|
78
|
+
assert(Math.abs(vector3.magnitudeSquared() - 1.0) <= _MathUtils.EPSILON6);
|
|
79
|
+
result.x = vector.x / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
|
|
80
|
+
result.y = vector.y / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
|
|
81
|
+
if (vector.z < 0) {
|
|
82
|
+
const x = result.x;
|
|
83
|
+
const y = result.y;
|
|
84
|
+
result.x = (1.0 - Math.abs(y)) * signNotZero(x);
|
|
85
|
+
result.y = (1.0 - Math.abs(x)) * signNotZero(y);
|
|
86
|
+
}
|
|
87
|
+
result.x = toSNorm(result.x, rangeMax);
|
|
88
|
+
result.y = toSNorm(result.y, rangeMax);
|
|
89
|
+
return result;
|
|
41
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding.
|
|
93
|
+
*
|
|
94
|
+
* @param vector The normalized vector to be compressed into 2 byte 'oct' encoding.
|
|
95
|
+
* @param result The 2 byte oct-encoded unit length vector.
|
|
96
|
+
* @returns he 2 byte oct-encoded unit length vector.
|
|
97
|
+
*
|
|
98
|
+
* @exception vector must be normalized.
|
|
99
|
+
*
|
|
100
|
+
* @see octEncodeInRange
|
|
101
|
+
* @see octDecode
|
|
102
|
+
*/
|
|
42
103
|
export function octEncode(vector, result) {
|
|
43
|
-
|
|
104
|
+
return octEncodeInRange(vector, 255, result);
|
|
44
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Encodes a normalized vector into 4-byte vector
|
|
108
|
+
* @param vector The normalized vector to be compressed into 4 byte 'oct' encoding.
|
|
109
|
+
* @param result The 4 byte oct-encoded unit length vector.
|
|
110
|
+
* @returns The 4 byte oct-encoded unit length vector.
|
|
111
|
+
*
|
|
112
|
+
* @exception vector must be normalized.
|
|
113
|
+
*
|
|
114
|
+
* @see octEncodeInRange
|
|
115
|
+
* @see octDecodeFromVector4
|
|
116
|
+
*/
|
|
45
117
|
export function octEncodeToVector4(vector, result) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
118
|
+
octEncodeInRange(vector, 65535, octEncodeScratch);
|
|
119
|
+
result.x = forceUint8(octEncodeScratch.x * RIGHT_SHIFT);
|
|
120
|
+
result.y = forceUint8(octEncodeScratch.x);
|
|
121
|
+
result.z = forceUint8(octEncodeScratch.y * RIGHT_SHIFT);
|
|
122
|
+
result.w = forceUint8(octEncodeScratch.y);
|
|
123
|
+
return result;
|
|
52
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Decodes a unit-length vector in 'oct' encoding to a normalized 3-component vector.
|
|
127
|
+
*
|
|
128
|
+
* @param x The x component of the oct-encoded unit length vector.
|
|
129
|
+
* @param y The y component of the oct-encoded unit length vector.
|
|
130
|
+
* @param rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
|
|
131
|
+
* @param result The decoded and normalized vector
|
|
132
|
+
* @returns The decoded and normalized vector.
|
|
133
|
+
*
|
|
134
|
+
* @exception x and y must be unsigned normalized integers between 0 and rangeMax.
|
|
135
|
+
*
|
|
136
|
+
* @see octEncodeInRange
|
|
137
|
+
*/
|
|
53
138
|
export function octDecodeInRange(x, y, rangeMax, result) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
139
|
+
assert(result);
|
|
140
|
+
if (x < 0 || x > rangeMax || y < 0 || y > rangeMax) {
|
|
141
|
+
throw new Error(`x and y must be unsigned normalized integers between 0 and ${rangeMax}`);
|
|
142
|
+
}
|
|
143
|
+
result.x = fromSNorm(x, rangeMax);
|
|
144
|
+
result.y = fromSNorm(y, rangeMax);
|
|
145
|
+
result.z = 1.0 - (Math.abs(result.x) + Math.abs(result.y));
|
|
146
|
+
if (result.z < 0.0) {
|
|
147
|
+
const oldVX = result.x;
|
|
148
|
+
result.x = (1.0 - Math.abs(result.y)) * signNotZero(oldVX);
|
|
149
|
+
result.y = (1.0 - Math.abs(oldVX)) * signNotZero(result.y);
|
|
150
|
+
}
|
|
151
|
+
return result.normalize();
|
|
67
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Decodes a unit-length vector in 2 byte 'oct' encoding to a normalized 3-component vector.
|
|
155
|
+
*
|
|
156
|
+
* @param x The x component of the oct-encoded unit length vector.
|
|
157
|
+
* @param y The y component of the oct-encoded unit length vector.
|
|
158
|
+
* @param result The decoded and normalized vector.
|
|
159
|
+
* @returns he decoded and normalized vector.
|
|
160
|
+
*
|
|
161
|
+
* @exception x and y must be an unsigned normalized integer between 0 and 255.
|
|
162
|
+
*
|
|
163
|
+
* @see octDecodeInRange
|
|
164
|
+
*/
|
|
68
165
|
export function octDecode(x, y, result) {
|
|
69
|
-
|
|
166
|
+
return octDecodeInRange(x, y, 255, result);
|
|
70
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Decodes a unit-length vector in 4 byte 'oct' encoding to a normalized 3-component vector.
|
|
170
|
+
*
|
|
171
|
+
* @param encoded The oct-encoded unit length vector.
|
|
172
|
+
* @param esult The decoded and normalized vector.
|
|
173
|
+
* @returns The decoded and normalized vector.
|
|
174
|
+
*
|
|
175
|
+
* @exception x, y, z, and w must be unsigned normalized integers between 0 and 255.
|
|
176
|
+
*
|
|
177
|
+
* @see octDecodeInRange
|
|
178
|
+
* @see octEncodeToVector4
|
|
179
|
+
*/
|
|
71
180
|
export function octDecodeFromVector4(encoded, result) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
181
|
+
assert(encoded);
|
|
182
|
+
assert(result);
|
|
183
|
+
const x = encoded.x;
|
|
184
|
+
const y = encoded.y;
|
|
185
|
+
const z = encoded.z;
|
|
186
|
+
const w = encoded.w;
|
|
187
|
+
if (x < 0 || x > 255 || y < 0 || y > 255 || z < 0 || z > 255 || w < 0 || w > 255) {
|
|
188
|
+
throw new Error('x, y, z, and w must be unsigned normalized integers between 0 and 255');
|
|
189
|
+
}
|
|
190
|
+
const xOct16 = x * LEFT_SHIFT + y;
|
|
191
|
+
const yOct16 = z * LEFT_SHIFT + w;
|
|
192
|
+
return octDecodeInRange(xOct16, yOct16, 65535, result);
|
|
84
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* Packs an oct encoded vector into a single floating-point number.
|
|
196
|
+
*
|
|
197
|
+
* @param encoded The oct encoded vector.
|
|
198
|
+
* @returns The oct encoded vector packed into a single float.
|
|
199
|
+
*
|
|
200
|
+
*/
|
|
85
201
|
export function octPackFloat(encoded) {
|
|
86
|
-
|
|
87
|
-
|
|
202
|
+
const vector2 = scratchVector2.from(encoded);
|
|
203
|
+
return 256.0 * vector2.x + vector2.y;
|
|
88
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding and
|
|
207
|
+
* stores those values in a single float-point number.
|
|
208
|
+
*
|
|
209
|
+
* @param vector The normalized vector to be compressed into 2 byte 'oct' encoding.
|
|
210
|
+
* @returns The 2 byte oct-encoded unit length vector.
|
|
211
|
+
*
|
|
212
|
+
* @exception vector must be normalized.
|
|
213
|
+
*/
|
|
89
214
|
export function octEncodeFloat(vector) {
|
|
90
|
-
|
|
91
|
-
|
|
215
|
+
octEncode(vector, scratchEncodeVector2);
|
|
216
|
+
return octPackFloat(scratchEncodeVector2);
|
|
92
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Decodes a unit-length vector in 'oct' encoding packed in a floating-point number to a normalized 3-component vector.
|
|
220
|
+
*
|
|
221
|
+
* @param value The oct-encoded unit length vector stored as a single floating-point number.
|
|
222
|
+
* @param result The decoded and normalized vector
|
|
223
|
+
* @returns The decoded and normalized vector.
|
|
224
|
+
*
|
|
225
|
+
*/
|
|
93
226
|
export function octDecodeFloat(value, result) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
227
|
+
assert(Number.isFinite(value));
|
|
228
|
+
const temp = value / 256.0;
|
|
229
|
+
const x = Math.floor(temp);
|
|
230
|
+
const y = (temp - x) * 256.0;
|
|
231
|
+
return octDecode(x, y, result);
|
|
99
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* Encodes three normalized vectors into 6 SNORM values in the range of [0-255] following the 'oct' encoding and
|
|
235
|
+
* packs those into two floating-point numbers.
|
|
236
|
+
*
|
|
237
|
+
* @param v1 A normalized vector to be compressed.
|
|
238
|
+
* @param v2 A normalized vector to be compressed.
|
|
239
|
+
* @param v3 A normalized vector to be compressed.
|
|
240
|
+
* @param result The 'oct' encoded vectors packed into two floating-point numbers.
|
|
241
|
+
* @returns The 'oct' encoded vectors packed into two floating-point numbers.
|
|
242
|
+
*
|
|
243
|
+
*/
|
|
100
244
|
export function octPack(v1, v2, v3, result) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
245
|
+
assert(v1);
|
|
246
|
+
assert(v2);
|
|
247
|
+
assert(v3);
|
|
248
|
+
assert(result);
|
|
249
|
+
const encoded1 = octEncodeFloat(v1);
|
|
250
|
+
const encoded2 = octEncodeFloat(v2);
|
|
251
|
+
const encoded3 = octEncode(v3, scratchEncodeVector2);
|
|
252
|
+
result.x = 65536.0 * encoded3.x + encoded1;
|
|
253
|
+
result.y = 65536.0 * encoded3.y + encoded2;
|
|
254
|
+
return result;
|
|
111
255
|
}
|
|
256
|
+
/**
|
|
257
|
+
* Decodes three unit-length vectors in 'oct' encoding packed into a floating-point number to a normalized 3-component vector.
|
|
258
|
+
*
|
|
259
|
+
* @param packed The three oct-encoded unit length vectors stored as two floating-point number.
|
|
260
|
+
* @param v1 One decoded and normalized vector.
|
|
261
|
+
* @param v2 One decoded and normalized vector.
|
|
262
|
+
* @param v3 One decoded and normalized vector.
|
|
263
|
+
*/
|
|
112
264
|
export function octUnpack(packed, v1, v2, v3) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
265
|
+
let temp = packed.x / 65536.0;
|
|
266
|
+
const x = Math.floor(temp);
|
|
267
|
+
const encodedFloat1 = (temp - x) * 65536.0;
|
|
268
|
+
temp = packed.y / 65536.0;
|
|
269
|
+
const y = Math.floor(temp);
|
|
270
|
+
const encodedFloat2 = (temp - y) * 65536.0;
|
|
271
|
+
octDecodeFloat(encodedFloat1, v1);
|
|
272
|
+
octDecodeFloat(encodedFloat2, v2);
|
|
273
|
+
octDecode(x, y, v3);
|
|
122
274
|
}
|
|
275
|
+
/**
|
|
276
|
+
* Pack texture coordinates into a single float. The texture coordinates will only preserve 12 bits of precision.
|
|
277
|
+
*
|
|
278
|
+
* @param textureCoordinates The texture coordinates to compress. Both coordinates must be in the range 0.0-1.0.
|
|
279
|
+
* @returns The packed texture coordinates.
|
|
280
|
+
*
|
|
281
|
+
*/
|
|
123
282
|
export function compressTextureCoordinates(textureCoordinates) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
283
|
+
// Move x and y to the range 0-4095;
|
|
284
|
+
const x = (textureCoordinates.x * 4095.0) | 0;
|
|
285
|
+
const y = (textureCoordinates.y * 4095.0) | 0;
|
|
286
|
+
return 4096.0 * x + y;
|
|
127
287
|
}
|
|
288
|
+
/**
|
|
289
|
+
* Decompresses texture coordinates that were packed into a single float.
|
|
290
|
+
*
|
|
291
|
+
* @param compressed The compressed texture coordinates.
|
|
292
|
+
* @param result The decompressed texture coordinates.
|
|
293
|
+
* @returns The modified result parameter.
|
|
294
|
+
*
|
|
295
|
+
*/
|
|
128
296
|
export function decompressTextureCoordinates(compressed, result) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
297
|
+
const temp = compressed / 4096.0;
|
|
298
|
+
const xZeroTo4095 = Math.floor(temp);
|
|
299
|
+
result.x = xZeroTo4095 / 4095.0;
|
|
300
|
+
result.y = (compressed - xZeroTo4095 * 4096) / 4095;
|
|
301
|
+
return result;
|
|
134
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Decodes delta and ZigZag encoded vertices. This modifies the buffers in place.
|
|
305
|
+
*
|
|
306
|
+
* @param uBuffer The buffer view of u values.
|
|
307
|
+
* @param vBuffer The buffer view of v values.
|
|
308
|
+
* @param [heightBuffer] The buffer view of height values.
|
|
309
|
+
*
|
|
310
|
+
* @link https://github.com/AnalyticalGraphicsInc/quantized-mesh|quantized-mesh-1.0 terrain format
|
|
311
|
+
*/
|
|
135
312
|
export function zigZagDeltaDecode(uBuffer, vBuffer, heightBuffer) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (heightBuffer) {
|
|
140
|
-
assert(uBuffer.length === heightBuffer.length);
|
|
141
|
-
}
|
|
142
|
-
function zigZagDecode(value) {
|
|
143
|
-
return value >> 1 ^ -(value & 1);
|
|
144
|
-
}
|
|
145
|
-
let u = 0;
|
|
146
|
-
let v = 0;
|
|
147
|
-
let height = 0;
|
|
148
|
-
for (let i = 0; i < uBuffer.length; ++i) {
|
|
149
|
-
u += zigZagDecode(uBuffer[i]);
|
|
150
|
-
v += zigZagDecode(vBuffer[i]);
|
|
151
|
-
uBuffer[i] = u;
|
|
152
|
-
vBuffer[i] = v;
|
|
313
|
+
assert(uBuffer);
|
|
314
|
+
assert(vBuffer);
|
|
315
|
+
assert(uBuffer.length === vBuffer.length);
|
|
153
316
|
if (heightBuffer) {
|
|
154
|
-
|
|
155
|
-
|
|
317
|
+
assert(uBuffer.length === heightBuffer.length);
|
|
318
|
+
}
|
|
319
|
+
function zigZagDecode(value) {
|
|
320
|
+
return (value >> 1) ^ -(value & 1);
|
|
321
|
+
}
|
|
322
|
+
let u = 0;
|
|
323
|
+
let v = 0;
|
|
324
|
+
let height = 0;
|
|
325
|
+
for (let i = 0; i < uBuffer.length; ++i) {
|
|
326
|
+
u += zigZagDecode(uBuffer[i]);
|
|
327
|
+
v += zigZagDecode(vBuffer[i]);
|
|
328
|
+
uBuffer[i] = u;
|
|
329
|
+
vBuffer[i] = v;
|
|
330
|
+
if (heightBuffer) {
|
|
331
|
+
height += zigZagDecode(heightBuffer[i]);
|
|
332
|
+
heightBuffer[i] = height;
|
|
333
|
+
}
|
|
156
334
|
}
|
|
157
|
-
}
|
|
158
335
|
}
|
|
159
|
-
//# sourceMappingURL=attribute-compression.js.map
|
|
@@ -1,29 +1,31 @@
|
|
|
1
|
+
// Subset of WebGL constants
|
|
1
2
|
export const GL_PRIMITIVE = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
POINTS: 0x0000, // Points. single points.
|
|
4
|
+
LINES: 0x0001, // Lines. Each vertex connects to the one after it.
|
|
5
|
+
TRIANGLES: 0x0004 // Triangles. Each set of three vertices creates a separate triangle.
|
|
5
6
|
};
|
|
7
|
+
// Primitive modes
|
|
6
8
|
export const GL_PRIMITIVE_MODE = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
POINTS: 0x0000, // Points. single points.
|
|
10
|
+
LINES: 0x0001, // Lines. Each vertex connects to the one after it.
|
|
11
|
+
LINE_LOOP: 0x0002, // Lines. Each set of two vertices is treated as a separate line segment.
|
|
12
|
+
LINE_STRIP: 0x0003, // Lines/ a connected group of line segments from the first vertex to the last
|
|
13
|
+
TRIANGLES: 0x0004, // Triangles. Each set of three vertices creates a separate triangle.
|
|
14
|
+
TRIANGLE_STRIP: 0x0005, // Triangles. A connected group of triangles.
|
|
15
|
+
TRIANGLE_FAN: 0x0006 // Triangles. A connected group of triangles.
|
|
16
|
+
// Each vertex connects to the previous and the first vertex in the fan.
|
|
14
17
|
};
|
|
15
18
|
export const GL_TYPE = {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
BYTE: 5120,
|
|
20
|
+
UNSIGNED_BYTE: 5121,
|
|
21
|
+
SHORT: 5122,
|
|
22
|
+
UNSIGNED_SHORT: 5123,
|
|
23
|
+
INT: 5124,
|
|
24
|
+
UNSIGNED_INT: 5125,
|
|
25
|
+
FLOAT: 5126,
|
|
26
|
+
DOUBLE: 5130
|
|
24
27
|
};
|
|
25
28
|
export const GL = {
|
|
26
|
-
|
|
27
|
-
|
|
29
|
+
...GL_PRIMITIVE_MODE,
|
|
30
|
+
...GL_TYPE
|
|
28
31
|
};
|
|
29
|
-
//# sourceMappingURL=constants.js.map
|
|
@@ -1,72 +1,109 @@
|
|
|
1
1
|
import { GL_TYPE as GL } from "../constants.js";
|
|
2
2
|
const GL_TYPE_TO_ARRAY_TYPE = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
[GL.DOUBLE]: Float64Array,
|
|
4
|
+
[GL.FLOAT]: Float32Array,
|
|
5
|
+
[GL.UNSIGNED_SHORT]: Uint16Array,
|
|
6
|
+
[GL.UNSIGNED_INT]: Uint32Array,
|
|
7
|
+
[GL.UNSIGNED_BYTE]: Uint8Array,
|
|
8
|
+
[GL.BYTE]: Int8Array,
|
|
9
|
+
[GL.SHORT]: Int16Array,
|
|
10
|
+
[GL.INT]: Int32Array
|
|
11
11
|
};
|
|
12
12
|
const NAME_TO_GL_TYPE = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
DOUBLE: GL.DOUBLE,
|
|
14
|
+
FLOAT: GL.FLOAT,
|
|
15
|
+
UNSIGNED_SHORT: GL.UNSIGNED_SHORT,
|
|
16
|
+
UNSIGNED_INT: GL.UNSIGNED_INT,
|
|
17
|
+
UNSIGNED_BYTE: GL.UNSIGNED_BYTE,
|
|
18
|
+
BYTE: GL.BYTE,
|
|
19
|
+
SHORT: GL.SHORT,
|
|
20
|
+
INT: GL.INT
|
|
21
21
|
};
|
|
22
22
|
const ERR_TYPE_CONVERSION = 'Failed to convert GL type';
|
|
23
|
+
// Converts TYPED ARRAYS to corresponding GL constant
|
|
24
|
+
// Used to auto deduce gl parameter types
|
|
23
25
|
export default class GLType {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
// Signature: fromTypedArray(new Uint8Array())
|
|
27
|
+
// Signature: fromTypedArray(Uint8Array)
|
|
28
|
+
/**
|
|
29
|
+
* Returns the size, in bytes, of the corresponding datatype
|
|
30
|
+
* @param arrayOrType
|
|
31
|
+
* @returns glType a a string
|
|
32
|
+
*/
|
|
33
|
+
static fromTypedArray(arrayOrType) {
|
|
34
|
+
// If typed array, look up constructor
|
|
35
|
+
arrayOrType = ArrayBuffer.isView(arrayOrType) ? arrayOrType.constructor : arrayOrType;
|
|
36
|
+
for (const glType in GL_TYPE_TO_ARRAY_TYPE) {
|
|
37
|
+
const ArrayType = GL_TYPE_TO_ARRAY_TYPE[glType];
|
|
38
|
+
if (ArrayType === arrayOrType) {
|
|
39
|
+
return glType;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
throw new Error(ERR_TYPE_CONVERSION);
|
|
31
43
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Extracts name for glType from array NAME_TO_GL_TYPE
|
|
46
|
+
* @param name
|
|
47
|
+
* @returns glType as a number
|
|
48
|
+
*/
|
|
49
|
+
static fromName(name) {
|
|
50
|
+
const glType = NAME_TO_GL_TYPE[name];
|
|
51
|
+
if (!glType) {
|
|
52
|
+
throw new Error(ERR_TYPE_CONVERSION);
|
|
53
|
+
}
|
|
54
|
+
return glType;
|
|
38
55
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
// Converts GL constant to corresponding typed array type
|
|
57
|
+
// eslint-disable-next-line complexity
|
|
58
|
+
static getArrayType(glType) {
|
|
59
|
+
switch (glType) {
|
|
60
|
+
/*eslint-disable*/
|
|
61
|
+
// @ts-ignore
|
|
62
|
+
case GL.UNSIGNED_SHORT_5_6_5:
|
|
63
|
+
// @ts-ignore
|
|
64
|
+
case GL.UNSIGNED_SHORT_4_4_4_4:
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
case GL.UNSIGNED_SHORT_5_5_5_1:
|
|
67
|
+
/* eslint-enable*/
|
|
68
|
+
return Uint16Array;
|
|
69
|
+
default:
|
|
70
|
+
const ArrayType = GL_TYPE_TO_ARRAY_TYPE[glType];
|
|
71
|
+
if (!ArrayType) {
|
|
72
|
+
throw new Error(ERR_TYPE_CONVERSION);
|
|
73
|
+
}
|
|
74
|
+
return ArrayType;
|
|
51
75
|
}
|
|
52
|
-
return ArrayType;
|
|
53
76
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Returns the size in bytes of one element of the provided WebGL type
|
|
79
|
+
* @param glType
|
|
80
|
+
* @returns size of glType
|
|
81
|
+
*/
|
|
82
|
+
static getByteSize(glType) {
|
|
83
|
+
const ArrayType = GLType.getArrayType(glType);
|
|
84
|
+
return ArrayType.BYTES_PER_ELEMENT;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Returns `true` if `glType` is a valid WebGL data type.
|
|
88
|
+
* @param glType
|
|
89
|
+
* @returns boolean
|
|
90
|
+
*/
|
|
91
|
+
static validate(glType) {
|
|
92
|
+
return Boolean(GLType.getArrayType(glType));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Creates a typed view of an array of bytes
|
|
96
|
+
* @param glType The type of typed array (ArrayBuffer view) to create
|
|
97
|
+
* @param buffer The buffer storage to use for the view.
|
|
98
|
+
* @param byteOffset The offset, in bytes, to the first element in the view
|
|
99
|
+
* @param length The number of elements in the view. Defaults to buffer length
|
|
100
|
+
* @returns A typed array view of the buffer
|
|
101
|
+
*/
|
|
102
|
+
static createTypedArray(glType, buffer, byteOffset = 0, length) {
|
|
103
|
+
if (length === undefined) {
|
|
104
|
+
length = (buffer.byteLength - byteOffset) / GLType.getByteSize(glType);
|
|
105
|
+
}
|
|
106
|
+
const ArrayType = GLType.getArrayType(glType);
|
|
107
|
+
return new ArrayType(buffer, byteOffset, length);
|
|
67
108
|
}
|
|
68
|
-
const ArrayType = GLType.getArrayType(glType);
|
|
69
|
-
return new ArrayType(buffer, byteOffset, length);
|
|
70
|
-
}
|
|
71
109
|
}
|
|
72
|
-
//# sourceMappingURL=gl-type.js.map
|