@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.
Files changed (45) hide show
  1. package/dist/geometry/attributes/compute-bounding-box.js +35 -23
  2. package/dist/geometry/attributes/compute-bounding-sphere.js +29 -1
  3. package/dist/geometry/attributes/compute-tangents.js +145 -1
  4. package/dist/geometry/attributes/compute-vertex-normals.js +36 -33
  5. package/dist/geometry/attributes/convert-to-non-indexed.js +27 -25
  6. package/dist/geometry/attributes/get-attribute-from-geometry.js +23 -20
  7. package/dist/geometry/attributes/normalize.js +15 -12
  8. package/dist/geometry/colors/rgb565.js +23 -14
  9. package/dist/geometry/compression/attribute-compression.js +290 -114
  10. package/dist/geometry/constants.js +23 -21
  11. package/dist/geometry/gl/gl-type.js +96 -59
  12. package/dist/geometry/is-geometry.js +9 -2
  13. package/dist/geometry/iterators/attribute-iterator.js +12 -8
  14. package/dist/geometry/iterators/primitive-iterator.js +70 -62
  15. package/dist/geometry/primitives/modes.js +58 -41
  16. package/dist/geometry/typed-arrays/typed-array-utils.js +19 -16
  17. package/dist/geometry/utils/assert.js +8 -4
  18. package/dist/geometry/utils/coordinates.js +5 -2
  19. package/dist/index.cjs +19 -18
  20. package/dist/index.cjs.map +7 -0
  21. package/dist/index.d.ts +11 -11
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +6 -1
  24. package/dist/utils/assert.js +6 -4
  25. package/package.json +7 -4
  26. package/dist/geometry/attributes/compute-bounding-box.js.map +0 -1
  27. package/dist/geometry/attributes/compute-bounding-sphere.js.map +0 -1
  28. package/dist/geometry/attributes/compute-tangents.js.map +0 -1
  29. package/dist/geometry/attributes/compute-vertex-normals.js.map +0 -1
  30. package/dist/geometry/attributes/convert-to-non-indexed.js.map +0 -1
  31. package/dist/geometry/attributes/get-attribute-from-geometry.js.map +0 -1
  32. package/dist/geometry/attributes/normalize.js.map +0 -1
  33. package/dist/geometry/colors/rgb565.js.map +0 -1
  34. package/dist/geometry/compression/attribute-compression.js.map +0 -1
  35. package/dist/geometry/constants.js.map +0 -1
  36. package/dist/geometry/gl/gl-type.js.map +0 -1
  37. package/dist/geometry/is-geometry.js.map +0 -1
  38. package/dist/geometry/iterators/attribute-iterator.js.map +0 -1
  39. package/dist/geometry/iterators/primitive-iterator.js.map +0 -1
  40. package/dist/geometry/primitives/modes.js.map +0 -1
  41. package/dist/geometry/typed-arrays/typed-array-utils.js.map +0 -1
  42. package/dist/geometry/utils/assert.js.map +0 -1
  43. package/dist/geometry/utils/coordinates.js.map +0 -1
  44. package/dist/index.js.map +0 -1
  45. package/dist/utils/assert.js.map +0 -1
@@ -1,28 +1,40 @@
1
1
  import { makeAttributeIterator } from "../iterators/attribute-iterator.js";
2
2
  import { assert } from "../utils/assert.js";
3
- export function computeBoundingBox() {
4
- let positions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
5
- const min = [Number(Infinity), Number(Infinity), Number(Infinity)];
6
- const max = [-Infinity, -Infinity, -Infinity];
7
- for (const position of makeAttributeIterator(positions)) {
8
- const x = position[0];
9
- const y = position[1];
10
- const z = position[2];
11
- if (x < min[0]) min[0] = x;
12
- if (y < min[1]) min[1] = y;
13
- if (z < min[2]) min[2] = z;
14
- if (x > max[0]) max[0] = x;
15
- if (y > max[1]) max[1] = y;
16
- if (z > max[2]) max[2] = z;
17
- }
18
- const boundingBox = {
19
- min,
20
- max
21
- };
22
- validateBoundingBox(boundingBox);
23
- return boundingBox;
3
+ /**
4
+ * Getting bounding box geometry according to positions parameters
5
+ * @param positions
6
+ * @returns Bounding Box
7
+ */
8
+ export function computeBoundingBox(positions = []) {
9
+ const min = [Number(Infinity), Number(Infinity), Number(Infinity)];
10
+ const max = [-Infinity, -Infinity, -Infinity];
11
+ // @ts-ignore
12
+ for (const position of makeAttributeIterator(positions)) {
13
+ const x = position[0];
14
+ const y = position[1];
15
+ const z = position[2];
16
+ if (x < min[0])
17
+ min[0] = x;
18
+ if (y < min[1])
19
+ min[1] = y;
20
+ if (z < min[2])
21
+ min[2] = z;
22
+ if (x > max[0])
23
+ max[0] = x;
24
+ if (y > max[1])
25
+ max[1] = y;
26
+ if (z > max[2])
27
+ max[2] = z;
28
+ }
29
+ const boundingBox = { min, max };
30
+ validateBoundingBox(boundingBox);
31
+ return boundingBox;
24
32
  }
25
33
  function validateBoundingBox(boundingBox) {
26
- assert(Number.isFinite(boundingBox.min[0]) && Number.isFinite(boundingBox.min[1]) && Number.isFinite(boundingBox.min[2]) && Number.isFinite(boundingBox.max[0]) && Number.isFinite(boundingBox.max[1]) && Number.isFinite(boundingBox.max[2]));
34
+ assert(Number.isFinite(boundingBox.min[0]) &&
35
+ Number.isFinite(boundingBox.min[1]) &&
36
+ Number.isFinite(boundingBox.min[2]) &&
37
+ Number.isFinite(boundingBox.max[0]) &&
38
+ Number.isFinite(boundingBox.max[1]) &&
39
+ Number.isFinite(boundingBox.max[2]));
27
40
  }
28
- //# sourceMappingURL=compute-bounding-box.js.map
@@ -1,2 +1,30 @@
1
+ "use strict";
2
+ /* eslint-disable */
3
+ /**
4
+ import {getPositions} from './get-attribute-from-geometry';
1
5
 
2
- //# sourceMappingURL=compute-bounding-sphere.js.map
6
+ export function computeBoundingSphere(geometry: any, boundingBox: object, vector: Vector3 ) {
7
+ const positions = getPositions(geometry);
8
+
9
+ const center = getBoundingBox(center);
10
+ box.setFromBufferAttribute(position);
11
+ box.getCenter(center);
12
+
13
+ // hoping to find a boundingSphere with a radius smaller than the
14
+ // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
15
+
16
+ var maxRadiusSq = 0;
17
+
18
+ for (const position of makeAttributeIterator(positions)) {
19
+ vector.x = position[0];
20
+ vector.y = position[1];
21
+ vector.z = position[2];
22
+ maxRadiusSq = Math.max(maxRadiusSq, center.distanceToSquared(vector));
23
+ }
24
+
25
+ const radius = Math.sqrt(maxRadiusSq);
26
+ assert(Number.isFinite(radius));
27
+
28
+ return {center, radius};
29
+ }
30
+ */
@@ -1,2 +1,146 @@
1
+ "use strict";
2
+ /*
3
+ export function computeTangents({indices, positions, normals, uvs}) {
4
+ var index = geometry.index;
5
+ var attributes = geometry.attributes;
1
6
 
2
- //# sourceMappingURL=compute-tangents.js.map
7
+ // based on http://www.terathon.com/code/tangent.html
8
+ // (per vertex tangents)
9
+
10
+ if (
11
+ index === null ||
12
+ attributes.position === undefined ||
13
+ attributes.normal === undefined ||
14
+ attributes.uv === undefined
15
+ ) {
16
+ console.warn(
17
+ 'THREE.BufferGeometry: Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()'
18
+ );
19
+ return;
20
+ }
21
+
22
+ var nVertices = positions.length / 3;
23
+
24
+ var tangents = new Float32Array(4 * nVertices); // size: 4
25
+
26
+ var tan1 = [],
27
+ tan2 = [];
28
+
29
+ for (var k = 0; k < nVertices; k++) {
30
+ tan1[k] = new THREE.Vector3();
31
+ tan2[k] = new THREE.Vector3();
32
+ }
33
+
34
+ var vA = new THREE.Vector3(),
35
+ vB = new THREE.Vector3(),
36
+ vC = new THREE.Vector3(),
37
+ uvA = new THREE.Vector2(),
38
+ uvB = new THREE.Vector2(),
39
+ uvC = new THREE.Vector2(),
40
+ sdir = new THREE.Vector3(),
41
+ tdir = new THREE.Vector3();
42
+
43
+ function handleTriangle(a, b, c) {
44
+ vA.fromArray(positions, a * 3);
45
+ vB.fromArray(positions, b * 3);
46
+ vC.fromArray(positions, c * 3);
47
+
48
+ uvA.fromArray(uvs, a * 2);
49
+ uvB.fromArray(uvs, b * 2);
50
+ uvC.fromArray(uvs, c * 2);
51
+
52
+ var x1 = vB.x - vA.x;
53
+ var x2 = vC.x - vA.x;
54
+
55
+ var y1 = vB.y - vA.y;
56
+ var y2 = vC.y - vA.y;
57
+
58
+ var z1 = vB.z - vA.z;
59
+ var z2 = vC.z - vA.z;
60
+
61
+ var s1 = uvB.x - uvA.x;
62
+ var s2 = uvC.x - uvA.x;
63
+
64
+ var t1 = uvB.y - uvA.y;
65
+ var t2 = uvC.y - uvA.y;
66
+
67
+ var r = 1.0 / (s1 * t2 - s2 * t1);
68
+
69
+ sdir.set((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
70
+
71
+ tdir.set((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
72
+
73
+ tan1[a].add(sdir);
74
+ tan1[b].add(sdir);
75
+ tan1[c].add(sdir);
76
+
77
+ tan2[a].add(tdir);
78
+ tan2[b].add(tdir);
79
+ tan2[c].add(tdir);
80
+ }
81
+
82
+ var groups = geometry.groups;
83
+
84
+ if (groups.length === 0) {
85
+ groups = [
86
+ {
87
+ start: 0,
88
+ count: indices.length
89
+ }
90
+ ];
91
+ }
92
+
93
+ for (var j = 0, jl = groups.length; j < jl; ++j) {
94
+ var group = groups[j];
95
+
96
+ var start = group.start;
97
+ var count = group.count;
98
+
99
+ for (var i = start, il = start + count; i < il; i += 3) {
100
+ handleTriangle(indices[i + 0], indices[i + 1], indices[i + 2]);
101
+ }
102
+ }
103
+
104
+ var tmp = new THREE.Vector3(),
105
+ tmp2 = new THREE.Vector3();
106
+ var n = new THREE.Vector3(),
107
+ n2 = new THREE.Vector3();
108
+ var w, t, test;
109
+
110
+ function handleVertex(v) {
111
+ n.fromArray(normals, v * 3);
112
+ n2.copy(n);
113
+
114
+ t = tan1[v];
115
+
116
+ // Gram-Schmidt orthogonalize
117
+
118
+ tmp.copy(t);
119
+ tmp.sub(n.multiplyScalar(n.dot(t))).normalize();
120
+
121
+ // Calculate handedness
122
+
123
+ tmp2.crossVectors(n2, t);
124
+ test = tmp2.dot(tan2[v]);
125
+ w = test < 0.0 ? -1.0 : 1.0;
126
+
127
+ tangents[v * 4] = tmp.x;
128
+ tangents[v * 4 + 1] = tmp.y;
129
+ tangents[v * 4 + 2] = tmp.z;
130
+ tangents[v * 4 + 3] = w;
131
+ }
132
+
133
+ for (var j = 0, jl = groups.length; j < jl; ++j) {
134
+ var group = groups[j];
135
+
136
+ var start = group.start;
137
+ var count = group.count;
138
+
139
+ for (var i = start, il = start + count; i < il; i += 3) {
140
+ handleVertex(indices[i + 0]);
141
+ handleVertex(indices[i + 1]);
142
+ handleVertex(indices[i + 2]);
143
+ }
144
+ }
145
+ }
146
+ */
@@ -4,38 +4,41 @@ import { assert } from "../utils/assert.js";
4
4
  import { makePrimitiveIterator } from "../iterators/primitive-iterator.js";
5
5
  import { getPrimitiveModeType } from "../primitives/modes.js";
6
6
  import { getPositions } from "./get-attribute-from-geometry.js";
7
+ /**
8
+ * Computes vertex normals for a geometry
9
+ * @param param0
10
+ * @returns
11
+ */
12
+ // eslint-disable-next-line max-statements
7
13
  export function computeVertexNormals(geometry) {
8
- assert(getPrimitiveModeType(geometry.mode) === GL.TRIANGLES, 'TRIANGLES required');
9
- const {
10
- values: positions
11
- } = getPositions(geometry);
12
- const normals = new Float32Array(positions.length);
13
- const vectorA = new Vector3();
14
- const vectorB = new Vector3();
15
- const vectorC = new Vector3();
16
- const vectorCB = new Vector3();
17
- const vectorAB = new Vector3();
18
- for (const primitive of makePrimitiveIterator(geometry)) {
19
- vectorA.fromArray(positions, primitive.i1 * 3);
20
- vectorB.fromArray(positions, primitive.i2 * 3 + 3);
21
- vectorC.fromArray(positions, primitive.i3 * 3 + 6);
22
- vectorCB.subVectors(vectorC, vectorB);
23
- vectorAB.subVectors(vectorA, vectorB);
24
- const normal = vectorCB.cross(vectorAB);
25
- normal.normalize();
26
- const {
27
- primitiveIndex
28
- } = primitive;
29
- normals[primitiveIndex * 9 + 0] = normal.x;
30
- normals[primitiveIndex * 9 + 1] = normal.y;
31
- normals[primitiveIndex * 9 + 2] = normal.z;
32
- normals[primitiveIndex * 9 + 3] = normal.x;
33
- normals[primitiveIndex * 9 + 4] = normal.y;
34
- normals[primitiveIndex * 9 + 5] = normal.z;
35
- normals[primitiveIndex * 9 + 6] = normal.x;
36
- normals[primitiveIndex * 9 + 7] = normal.y;
37
- normals[primitiveIndex * 9 + 8] = normal.z;
38
- }
39
- return normals;
14
+ // Only support GL.TRIANGLES, GL.TRIANGLE_STRIP, GL.TRIANGLE_FAN
15
+ assert(getPrimitiveModeType(geometry.mode) === GL.TRIANGLES, 'TRIANGLES required');
16
+ const { values: positions } = getPositions(geometry);
17
+ const normals = new Float32Array(positions.length);
18
+ const vectorA = new Vector3();
19
+ const vectorB = new Vector3();
20
+ const vectorC = new Vector3();
21
+ const vectorCB = new Vector3();
22
+ const vectorAB = new Vector3();
23
+ for (const primitive of makePrimitiveIterator(geometry)) {
24
+ vectorA.fromArray(positions, primitive.i1 * 3);
25
+ vectorB.fromArray(positions, primitive.i2 * 3 + 3);
26
+ vectorC.fromArray(positions, primitive.i3 * 3 + 6);
27
+ vectorCB.subVectors(vectorC, vectorB);
28
+ vectorAB.subVectors(vectorA, vectorB);
29
+ const normal = vectorCB.cross(vectorAB);
30
+ normal.normalize();
31
+ // @ts-ignore
32
+ const { primitiveIndex } = primitive;
33
+ normals[primitiveIndex * 9 + 0] = normal.x;
34
+ normals[primitiveIndex * 9 + 1] = normal.y;
35
+ normals[primitiveIndex * 9 + 2] = normal.z;
36
+ normals[primitiveIndex * 9 + 3] = normal.x;
37
+ normals[primitiveIndex * 9 + 4] = normal.y;
38
+ normals[primitiveIndex * 9 + 5] = normal.z;
39
+ normals[primitiveIndex * 9 + 6] = normal.x;
40
+ normals[primitiveIndex * 9 + 7] = normal.y;
41
+ normals[primitiveIndex * 9 + 8] = normal.z;
42
+ }
43
+ return normals;
40
44
  }
41
- //# sourceMappingURL=compute-vertex-normals.js.map
@@ -1,27 +1,29 @@
1
- export function convertBuffersToNonIndexed(_ref) {
2
- let {
3
- indices,
4
- attributes
5
- } = _ref;
6
- const geometry2 = new BufferGeometry();
7
- for (const name in attributes) {
8
- const attribute = attributes[name];
9
- const array = attribute.array;
10
- const itemSize = attribute.itemSize;
11
- const array2 = new array.constructor(indices.length * itemSize);
12
- let index = 0,
13
- index2 = 0;
14
- for (var i = 0, l = indices.length; i < l; i++) {
15
- index = indices[i] * itemSize;
16
- for (var j = 0; j < itemSize; j++) {
17
- array2[index2++] = array[index++];
18
- }
1
+ /* eslint-disable */
2
+ // @ts-nocheck
3
+ /**
4
+ * Converts indices of geometry.
5
+ *
6
+ * @param param0
7
+ * @returns no indexed geometry
8
+ */
9
+ export function convertBuffersToNonIndexed({ indices, attributes }) {
10
+ const geometry2 = new BufferGeometry();
11
+ for (const name in attributes) {
12
+ const attribute = attributes[name];
13
+ const array = attribute.array;
14
+ const itemSize = attribute.itemSize;
15
+ const array2 = new array.constructor(indices.length * itemSize);
16
+ let index = 0, index2 = 0;
17
+ for (var i = 0, l = indices.length; i < l; i++) {
18
+ index = indices[i] * itemSize;
19
+ for (var j = 0; j < itemSize; j++) {
20
+ array2[index2++] = array[index++];
21
+ }
22
+ }
23
+ geometry2.addAttribute(name, new BufferAttribute(array2, itemSize));
19
24
  }
20
- geometry2.addAttribute(name, new BufferAttribute(array2, itemSize));
21
- }
22
- for (const group of this.groups) {
23
- geometry2.addGroup(group.start, group.count, group.materialIndex);
24
- }
25
- return geometry2;
25
+ for (const group of this.groups) {
26
+ geometry2.addGroup(group.start, group.count, group.materialIndex);
27
+ }
28
+ return geometry2;
26
29
  }
27
- //# sourceMappingURL=convert-to-non-indexed.js.map
@@ -1,24 +1,27 @@
1
1
  import isGeometry from "../is-geometry.js";
2
2
  import { assert } from "../utils/assert.js";
3
+ /**
4
+ * analyze positions of geometry
5
+ *
6
+ * @param geometry
7
+ * @returns Position| New geometry |assert
8
+ */
3
9
  export function getPositions(geometry) {
4
- if (isGeometry(geometry)) {
5
- const {
6
- attributes
7
- } = geometry;
8
- const position = attributes.POSITION || attributes.positions;
9
- assert(position);
10
- return position;
11
- }
12
- if (ArrayBuffer.isView(geometry)) {
13
- return {
14
- values: geometry,
15
- size: 3
16
- };
17
- }
18
- if (geometry) {
19
- assert(geometry.values);
20
- return geometry;
21
- }
22
- return assert(false);
10
+ // If geometry, extract positions
11
+ if (isGeometry(geometry)) {
12
+ const { attributes } = geometry;
13
+ const position = attributes.POSITION || attributes.positions;
14
+ assert(position);
15
+ return position;
16
+ }
17
+ // If arraybuffer, assume 3 components
18
+ if (ArrayBuffer.isView(geometry)) {
19
+ return { values: geometry, size: 3 };
20
+ }
21
+ // Else assume accessor object
22
+ if (geometry) {
23
+ assert(geometry.values);
24
+ return geometry;
25
+ }
26
+ return assert(false);
23
27
  }
24
- //# sourceMappingURL=get-attribute-from-geometry.js.map
@@ -1,13 +1,16 @@
1
- export function normalize() {
2
- let normals = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
3
- let vector = arguments.length > 1 ? arguments[1] : undefined;
4
- normals = this.attributes.normal;
5
- for (let i = 0, il = normals.count; i < il; i++) {
6
- vector.x = normals.getX(i);
7
- vector.y = normals.getY(i);
8
- vector.z = normals.getZ(i);
9
- vector.normalize();
10
- normals.setXYZ(i, vector.x, vector.y, vector.z);
11
- }
1
+ /**
2
+ * Setting X, Y, Z for Vector
3
+ * @param normals
4
+ * @param vector
5
+ */
6
+ export function normalize(normals = {}, vector) {
7
+ //@ts-ignore
8
+ normals = this.attributes.normal;
9
+ for (let i = 0, il = normals.count; i < il; i++) {
10
+ vector.x = normals.getX(i);
11
+ vector.y = normals.getY(i);
12
+ vector.z = normals.getZ(i);
13
+ vector.normalize();
14
+ normals.setXYZ(i, vector.x, vector.y, vector.z);
15
+ }
12
16
  }
13
- //# sourceMappingURL=normalize.js.map
@@ -1,17 +1,26 @@
1
- export function decodeRGB565(rgb565) {
2
- let target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [0, 0, 0];
3
- const r5 = rgb565 >> 11 & 31;
4
- const g6 = rgb565 >> 5 & 63;
5
- const b5 = rgb565 & 31;
6
- target[0] = r5 << 3;
7
- target[1] = g6 << 2;
8
- target[2] = b5 << 3;
9
- return target;
1
+ /**
2
+ * Decode color values
3
+ * @param rgb565
4
+ * @param target
5
+ * @returns target
6
+ */
7
+ export function decodeRGB565(rgb565, target = [0, 0, 0]) {
8
+ const r5 = (rgb565 >> 11) & 31;
9
+ const g6 = (rgb565 >> 5) & 63;
10
+ const b5 = rgb565 & 31;
11
+ target[0] = r5 << 3;
12
+ target[1] = g6 << 2;
13
+ target[2] = b5 << 3;
14
+ return target;
10
15
  }
16
+ /**
17
+ * Encode color values
18
+ * @param rgb
19
+ * @returns color
20
+ */
11
21
  export function encodeRGB565(rgb) {
12
- const r5 = Math.floor(rgb[0] / 8) + 4;
13
- const g6 = Math.floor(rgb[1] / 4) + 2;
14
- const b5 = Math.floor(rgb[2] / 8) + 4;
15
- return r5 + (g6 << 5) + (b5 << 11);
22
+ const r5 = Math.floor(rgb[0] / 8) + 4;
23
+ const g6 = Math.floor(rgb[1] / 4) + 2;
24
+ const b5 = Math.floor(rgb[2] / 8) + 4;
25
+ return r5 + (g6 << 5) + (b5 << 11);
16
26
  }
17
- //# sourceMappingURL=rgb565.js.map