@realsee/dnalogel
Version:
52 lines (51 loc) • 2.4 kB
JavaScript
import * as n from "three";
import { Vector3 as x } from "three";
import { polylineNormals as G } from "../../vendor/polyline-normals/index.js";
import "../../vendor/polyline-miter-util/index.js";
import "../../vendor/gl-vec2/add.js";
import "../../vendor/gl-vec2/set.js";
import "../../vendor/gl-vec2/normalize.js";
import "../../vendor/gl-vec2/subtract.js";
import "../../vendor/gl-vec2/dot.js";
const z = 2, M = 3, X = 3, k = 2;
function Q(e) {
const i = e.width, g = e.unitLength / i, c = i / 2, E = e.path;
if (E.length === 0)
return { geometry: new n.BufferGeometry(), maxU: 0, curvePath: null, curvePoints: null };
const u = new n.CurvePath();
E.forEach((t) => {
const l = I(t);
l && u.add(l);
});
const w = Math.ceil(u.getLength() * 100);
u.arcLengthDivisions = w;
const o = u.getPoints(w), y = o.length, L = G(o.map((t) => [t.x, t.z])), B = (y - 1) * z * M, d = 2 * y, C = d * X, U = d * k, r = new Uint32Array(B), s = new Float32Array(C), a = new Float32Array(U);
let N = 0, P = 0;
for (let t = 0; t < o.length; t++) {
const [l, R] = L[t][0], T = o[t], { x: A, y: _, z: b } = T, v = new x(A + l * c, _ + 0.05, b + R * c);
s[t * 6 + 0] = v.x, s[t * 6 + 1] = v.y, s[t * 6 + 2] = v.z;
const f = new x(A - l * c, _ + 0.05, b - R * c);
s[t * 6 + 3] = f.x, s[t * 6 + 4] = f.y, s[t * 6 + 5] = f.z, N += t > 0 ? o[t - 1].clone().sub(o[t]).length() : 0;
const h = N / i / g;
if (P = h, a[t * 4 + 0] = 0, a[t * 4 + 1] = h, a[t * 4 + 2] = 1, a[t * 4 + 3] = h, e.skipPositions) {
const V = new n.Vector3().lerpVectors(v, f, 0.5);
if (e.skipPositions.some((F) => F.distanceTo(V) < 0.26))
continue;
}
t !== o.length - 1 && (r[t * 6 + 0] = t * 2 + 0, r[t * 6 + 1] = t * 2 + 2, r[t * 6 + 2] = t * 2 + 1, r[t * 6 + 3] = t * 2 + 2, r[t * 6 + 4] = t * 2 + 3, r[t * 6 + 5] = t * 2 + 1);
}
const m = new n.BufferGeometry();
return m.setAttribute("position", new n.BufferAttribute(s, 3)), m.setAttribute("uv", new n.BufferAttribute(a, 2)), m.setIndex(new n.BufferAttribute(r, 1)), { geometry: m, maxU: P, curvePath: u, curvePoints: o };
}
function I(e) {
if (O(e)) {
const i = [e.closed, e.curve_type, e.tension], p = e.points.map((c) => new n.Vector3().fromArray(c));
return new n.CatmullRomCurve3(p, ...i);
}
}
function O(e) {
return e.type === "CatmullRomCurve3";
}
export {
Q as createLineGeometry
};