-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.tsx
78 lines (69 loc) · 1.87 KB
/
util.tsx
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
/**
* _____ __
* / ___// /_ _____
* \__ \/ / / / / _ \
* ___/ / / /_/ / __/
* /____/_/\__, /\___/
* /____/
* Copyright 2018 Parsa Ghadimi. All Rights Reserved.
* Licence: MIT License
*/
import * as THREE from "three";
import { FontLoader } from "three/examples/jsm/loaders/FontLoader";
import * as types from "./types";
export function createResolvable() {
let methods;
const promise = new Promise((resolve, reject) => {
methods = { resolve, reject };
});
return Object.assign(promise, methods);
}
const fontCache = new Map<string, any>();
const loader = new FontLoader();
export async function loadFontAsync(src: string) {
const resolvable = createResolvable();
let font = fontCache.get(src);
if (font) {
resolvable.resolve();
} else {
loader.load(src, (f) => {
font = f;
fontCache.set(src, f);
resolvable.resolve();
});
}
await resolvable;
return font;
}
export function toThreeVec3(vec: types.Vec3): THREE.Vector3 {
return new THREE.Vector3(vec.x, vec.y, vec.z);
}
export function cloneStep(step: types.Step): types.Step {
return {
text: step.text,
position: { ...step.position },
orientation: { ...step.orientation },
};
}
export function stepDeg2Rad(step: types.Step): types.Step {
step = cloneStep(step);
step.orientation.x = THREE.MathUtils.degToRad(step.orientation.x);
step.orientation.y = THREE.MathUtils.degToRad(step.orientation.y);
step.orientation.z = THREE.MathUtils.degToRad(step.orientation.z);
return step;
}
export function emptyStep(text = ""): types.Step {
return {
text,
position: { x: 0, y: 0, z: 0 },
orientation: { x: 0, y: 0, z: 0 },
};
}
export function randomString(): string {
return Math.random().toString(36).substring(7);
}
export function delay(t: number): Promise<void> {
return new Promise((r) => {
setTimeout(r, t);
});
}