summaryrefslogtreecommitdiff
path: root/client/splash/face/util.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2019-01-28 00:12:14 +0100
committerJules Laplace <julescarbon@gmail.com>2019-01-28 00:12:14 +0100
commitbd0bb81ee5d1856f3b3e86b43ac7ff7312472dfe (patch)
tree95c7154838296385d2bda1235c3ae66d0aa8b351 /client/splash/face/util.js
parent93a7fae69e5c3a45515a309cfcd96a0fa90a602b (diff)
moar constants etc
Diffstat (limited to 'client/splash/face/util.js')
-rw-r--r--client/splash/face/util.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/client/splash/face/util.js b/client/splash/face/util.js
new file mode 100644
index 00000000..e6237316
--- /dev/null
+++ b/client/splash/face/util.js
@@ -0,0 +1,86 @@
+
+import { Vector3, Geometry } from 'three'
+import { FACE_SCALE, LINE_THICKNESS } from '../constants'
+
+export function getLineGeometry(points) {
+ return [
+ points.slice(0, 17),
+ points.slice(17, 22),
+ points.slice(22, 27),
+ points.slice(27, 31),
+ points.slice(31, 36),
+ points.slice(36, 42),
+ points.slice(42, 48),
+ points.slice(48)
+ ].map((a, i) => {
+ const geometry = new Geometry()
+ a.forEach(p => geometry.vertices.push(p))
+ if (i > 4) {
+ geometry.vertices.push(a[0])
+ }
+ return geometry
+ })
+}
+
+export function updateLineGeometry(points, meshes) {
+ getLineGeometry(points).forEach((geometry, i) => {
+ const [line, mesh] = meshes[i]
+ line.setGeometry(geometry, () => LINE_THICKNESS)
+ mesh.geometry.vertices = line.geometry.vertices
+ mesh.geometry.verticesNeedUpdate = true
+ })
+}
+
+export function lerp(n, a, b) {
+ return (b - a) * n + a
+}
+
+export function lerpPoint(n, A, B, C) {
+ C.x = lerp(n, A.x, B.x)
+ C.y = lerp(n, A.y, B.y)
+ C.z = lerp(n, A.z, B.z)
+}
+
+export function lerpPoints(n, A, B, C) {
+ for (let i = 0, len = A.length; i < len; i++) {
+ lerpPoint(n, A[i], B[i], C[i])
+ }
+}
+
+export function updateCubeGeometry(points, cubes) {
+ cubes.forEach((cube, i) => {
+ const p = points[i]
+ cube.position.set(p.x, p.y, p.z)
+ })
+}
+
+export function updateFace(buf, cubes, meshes) {
+ updateCubeGeometry(buf, cubes)
+ updateLineGeometry(buf, meshes)
+}
+
+export function getBounds(obj) {
+ return obj.reduce((a, p) => {
+ return [
+ Math.min(a[0], p[0]),
+ Math.max(a[1], p[0]),
+ Math.min(a[2], p[1]),
+ Math.max(a[3], p[1]),
+ Math.min(a[4], p[2]),
+ Math.max(a[5], p[2]),
+ ]
+ }, [Infinity, -Infinity, Infinity, -Infinity, Infinity, -Infinity])
+}
+
+export function recenter(obj) {
+ const bounds = getBounds(obj)
+ const xWidth = (bounds[1] - bounds[0]) / 2
+ const yWidth = (bounds[3] - bounds[2]) / -3
+ const zWidth = (bounds[5] - bounds[4]) / 2
+ return obj.map(p => {
+ p[0] = p[0] - bounds[0] - xWidth
+ p[1] = -p[1] + bounds[1] + yWidth
+ p[2] = p[2] - bounds[2] + zWidth
+ return new Vector3(p[0] * FACE_SCALE, p[1] * FACE_SCALE, p[2] * FACE_SCALE)
+ })
+}