-
Notifications
You must be signed in to change notification settings - Fork 65
/
util.ts
216 lines (178 loc) · 5.99 KB
/
util.ts
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
import {
Vector2,
Vector3,
Matrix4,
Mesh,
MeshBasicMaterial,
EdgesGeometry,
LineSegments,
LineBasicMaterial,
DoubleSide,
BoxGeometry,
PlaneGeometry,
Camera,
Frustum,
CanvasTexture,
LinearFilter,
RepeatWrapping,
Group,
ArrowHelper,
Color,
Texture,
BufferGeometry
} from 'three';
import { Tile3D } from '@loaders.gl/tiles';
import { Plane as MathGLPlane } from '@math.gl/culling';
import { Matrix3 as MathGLMatrix3 } from '@math.gl/core';
import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
import { Gradient } from './gradients'
// From https://github.com/potree/potree/blob/master/src/materials/PointCloudMaterial.js
function generateGradientTexture(gradient: Gradient): CanvasTexture {
const size = 64;
// create canvas
const canvas = document.createElement('canvas');
canvas.width = size;
canvas.height = size;
// get context
const context = canvas.getContext('2d');
// draw gradient
context.rect(0, 0, size, size);
const ctxGradient = context.createLinearGradient(0, 0, size, size);
for (let i = 0; i < gradient.length; i++) {
const step = gradient[i];
ctxGradient.addColorStop(step[0], '#' + step[1].getHexString());
}
context.fillStyle = ctxGradient;
context.fill();
//let texture = new THREE.Texture(canvas);
const texture = new CanvasTexture(canvas);
texture.needsUpdate = true;
texture.minFilter = LinearFilter;
texture.wrapS = RepeatWrapping;
texture.wrapT = RepeatWrapping;
texture.repeat.set(2, 2);
// textureImage = texture.image;
return texture;
}
function getCameraFrustum(camera: Camera): Frustum {
camera.updateMatrix(); // make sure camera's local matrix is updated
camera.updateMatrixWorld(); // make sure camera's world matrix is updated
camera.matrixWorldInverse.copy(camera.matrixWorld).invert();
const frustum = new Frustum();
frustum.setFromProjectionMatrix(new Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse));
return frustum;
}
function loadersPlaneToMesh(plane: MathGLPlane): Group {
const group = new Group();
// Create a basic rectangle geometry from math.gl plane
const planeGeometry = new PlaneGeometry(10, 5);
// Align the geometry to the plane
const coplanarPoint = new Vector3(...plane.projectPointOntoPlane([0, 0, 0]));
const normal = new Vector3(plane.normal.x, plane.normal.y, plane.normal.z);
const focalPoint = new Vector3().copy(coplanarPoint).add(normal);
planeGeometry.lookAt(focalPoint);
planeGeometry.translate(coplanarPoint.x, coplanarPoint.y, coplanarPoint.z);
// Edges
/*
const edges = new EdgesGeometry(planeGeometry)
var dispPlane = new LineSegments(edges, new LineBasicMaterial({ color: 0x00ffff }))*/
//plane
const material = new MeshBasicMaterial({ color: 0x00ffff, side: DoubleSide });
const mesh = new Mesh(planeGeometry, material);
const arrowHelper = new ArrowHelper(normal, coplanarPoint, 5, 0xffff00);
group.add(arrowHelper);
group.add(mesh);
return group;
}
function loadersBoundingBoxToMesh(tile: Tile3D): LineSegments {
// Create a basic rectangle geometry from math.gl half-axes
const { boundingVolume } = tile;
let redColor = 0;
if (tile.content) {
redColor = Math.min(tile.content.byteLength / 500000, 1.0);
}
const boxColor = new Color(redColor, 1.0, 0.0);
const boxGeometry = new BoxGeometry(1, 1, 1);
const boxTransform = new Matrix4();
if (boundingVolume.halfAxes) {
boxTransform.copy(getMatrix4FromHalfAxes(boundingVolume.halfAxes));
} else if (boundingVolume.radius) {
boxGeometry.scale(boundingVolume.radius * 2, boundingVolume.radius * 2, boundingVolume.radius * 2);
}
boxGeometry.applyMatrix4(boxTransform);
const edges = new EdgesGeometry(boxGeometry);
const dispPlane = new LineSegments(edges, new LineBasicMaterial({ color: boxColor }));
dispPlane.position.copy(new Vector3(...boundingVolume.center));
return dispPlane;
}
function getMatrix4FromHalfAxes(halfAxes: MathGLMatrix3): Matrix4 {
const m = halfAxes;
const rotateMatrix = new Matrix4().fromArray([
m[0] * 2,
m[1] * 2,
m[2] * 2,
0,
m[3] * 2,
m[4] * 2,
m[5] * 2,
0,
m[6] * 2,
m[7] * 2,
m[8] * 2,
0,
0,
0,
0,
1,
]);
return rotateMatrix;
}
/*
* from https://github.com/tentone/geo-three
* Tree-shaking did not work, probably due to static class methods
*/
function datumsToSpherical(latitude:number, longitude:number): Vector2 {
const EARTH_RADIUS = 6378137;
const EARTH_PERIMETER = 2 * Math.PI * EARTH_RADIUS;
const EARTH_ORIGIN = EARTH_PERIMETER / 2.0;
const x = longitude * EARTH_ORIGIN / 180.0;
let y = Math.log(Math.tan((90 + latitude) * Math.PI / 360.0)) / (Math.PI / 180.0);
y = y * EARTH_ORIGIN / 180.0;
return new Vector2(x, y);
}
function getTextureVRAMByteLength(texture: Texture): number | undefined {
// Reference: https://github.com/donmccurdy/glTF-Transform/blob/main/packages/core/src/utils/image-utils.ts
let uncompressedBytes = 0;
if (texture.userData.mimeType == "image/ktx2" && texture.mipmaps) {
for (let i = 0; i < texture.mipmaps.length; i++) {
uncompressedBytes += texture.mipmaps[i].data.byteLength;
}
return uncompressedBytes;
} else if (texture.image) {
const { image } = texture;
const channels = 4;
let resolution = [image.width, image.height];
while (resolution[0] > 1 || resolution[1] > 1) {
uncompressedBytes += resolution[0] * resolution[1] * channels;
resolution[0] = Math.max(Math.floor(resolution[0] / 2), 1);
resolution[1] = Math.max(Math.floor(resolution[1] / 2), 1);
}
uncompressedBytes += 1 * 1 * channels;
return uncompressedBytes
} else {
return undefined;
}
}
function getGeometryVRAMByteLength(geometry: BufferGeometry) {
return BufferGeometryUtils.estimateBytesUsed(geometry);
}
export {
getCameraFrustum,
loadersPlaneToMesh,
loadersBoundingBoxToMesh,
generateGradientTexture,
getMatrix4FromHalfAxes,
datumsToSpherical,
getTextureVRAMByteLength,
getGeometryVRAMByteLength
};