@mark1russell7/visuals 0.3.0 → 0.3.2

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.
@@ -0,0 +1,687 @@
1
+ import { i as fieldValue, l as levelShape, r as fieldRowStep, s as SHAPE_OUTLINES } from "./mark-AZ5AuAIJ.js";
2
+ import "./view-BB_EQx3I.js";
3
+ import { BufferAttribute, BufferGeometry, Color, DataTexture, DoubleSide, InstancedBufferAttribute, InstancedBufferGeometry, Mesh, MeshBasicMaterial, NearestFilter, Plane, RGBAFormat, SRGBColorSpace, ShaderMaterial, UnsignedByteType, Vector3 } from "three";
4
+ // src/markers/content.ts
5
+ /** The radius of the sign of a point with no size, in CSS pixels. The sign of a disc has this radius. */
6
+ const SIGN_RADIUS = 7;
7
+ /** The width of the line around a sign, as a part of the radius of the sign. */
8
+ const HALO = .36;
9
+ /** The width of the line around a run, in CSS pixels, for each highlight. */
10
+ const CASING = [
11
+ 1.5,
12
+ 2.5,
13
+ 3
14
+ ];
15
+ /** The size of a sign for each highlight, as a multiple of its size. */
16
+ const HIGHLIGHT_SIZE = [
17
+ 1,
18
+ 1.15,
19
+ 1.3
20
+ ];
21
+ /** The half of the width of the line around a field with a highlight, in CSS pixels, for each highlight. */
22
+ const BORDER = [
23
+ 0,
24
+ 1,
25
+ 1.5
26
+ ];
27
+ /** The half of the smallest width of a run in the picture, in CSS pixels. Thus a thin run far from the camera stays in the picture. */
28
+ const LEAST_RUN = 1;
29
+ /** The part of a mark that is not clear while a solid hides it. */
30
+ const HIDDEN_OPACITY = .45;
31
+ /** The distance around a mark where the pointer finds it, in CSS pixels. */
32
+ const PICK_MARGIN = 4;
33
+ /** An outline with more sides than this value is a circle in the picture. The shaders have the same value. */
34
+ const ROUND_SIDES = 12;
35
+ /** This function gives the radius of the circle around an outline and its line, for a disc with the radius 1. */
36
+ function reachOf(outline) {
37
+ if (outline.sides > ROUND_SIDES) return outline.radius + HALO;
38
+ const inner = Math.cos(Math.PI / outline.sides);
39
+ return (outline.radius * inner + HALO) / inner;
40
+ }
41
+ /**
42
+ * This function gives the radius of the sign of a level with the line around it, in CSS pixels.
43
+ * `radius` is the radius of the sign of a disc. Give the result to the field `clear` of a label, so the text stays off the sign.
44
+ */
45
+ function signReach(level, radius = 7) {
46
+ return radius * reachOf(SHAPE_OUTLINES[levelShape(level)]);
47
+ }
48
+ const SIGN_VERTEX = `
49
+ uniform float pixelAngle;
50
+ uniform float halo;
51
+ uniform float behind;
52
+ attribute vec3 center;
53
+ attribute vec3 lifted;
54
+ attribute vec4 outline;
55
+ attribute vec3 fill;
56
+ attribute vec3 ring;
57
+ attribute vec4 look;
58
+ varying vec2 vPlace;
59
+ varying vec4 vOutline;
60
+ varying vec3 vFill;
61
+ varying vec3 vRing;
62
+ varying float vOpacity;
63
+ void main() {
64
+ vOpacity = mix(look.z, look.w, behind);
65
+ vOutline = outline;
66
+ vFill = fill;
67
+ vRing = ring;
68
+ vec4 inView = modelViewMatrix * vec4(center, 1.0);
69
+ float pixel = max(-inView.z, 0.0001) * pixelAngle;
70
+ float radius = look.y > 0.0 ? look.y : look.x * pixel;
71
+ float inner = outline.y > ${ROUND_SIDES.toFixed(1)} ? 1.0 : cos(3.141592653589793 / outline.y);
72
+ // The square has the outline, the line around it and one pixel for the soft edge.
73
+ float reach = (outline.x * inner + halo) / inner + pixel / max(radius, 0.000001);
74
+ vPlace = position.xy * reach;
75
+ vec3 above = (modelViewMatrix * vec4(lifted, 1.0)).xyz;
76
+ // The direction out of the surface, in the space of the camera. Its length is the lift.
77
+ vec3 normal = above - inView.xyz;
78
+ inView.xy += position.xy * reach * radius;
79
+ gl_Position = projectionMatrix * inView;
80
+ // Each corner has the depth of the point of the lifted surface that is behind it, as a picture on the surface has.
81
+ // Thus the surface of the sign hides no part of the sign, and a solid in front of the surface hides the sign.
82
+ float slope = dot(normal, inView.xyz);
83
+ float along = abs(slope) > 0.000000001 ? clamp(dot(normal, above) / slope, 0.5, 2.0) : 1.0;
84
+ vec4 depthAt = projectionMatrix * vec4(inView.xyz * along, 1.0);
85
+ gl_Position.z = depthAt.z / depthAt.w * gl_Position.w;
86
+ if (vOpacity <= 0.0) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
87
+ }`;
88
+ const SIGN_FRAGMENT = `
89
+ uniform float halo;
90
+ varying vec2 vPlace;
91
+ varying vec4 vOutline;
92
+ varying vec3 vFill;
93
+ varying vec3 vRing;
94
+ varying float vOpacity;
95
+ void main() {
96
+ float radius = length(vPlace);
97
+ // The distance from the edge of the outline: below 0 in the outline.
98
+ float edge = radius - vOutline.x;
99
+ if (vOutline.y <= ${ROUND_SIDES.toFixed(1)}) {
100
+ float sector = 6.283185307179586 / vOutline.y;
101
+ float turn = mod(atan(vPlace.y, vPlace.x) - vOutline.z, sector) - 0.5 * sector;
102
+ edge = radius * cos(turn) - vOutline.x * cos(0.5 * sector);
103
+ }
104
+ float soft = max(fwidth(edge), 0.000001);
105
+ float front = 1.0 - smoothstep(-soft, 0.0, edge);
106
+ if (vOutline.w > 0.0) front *= smoothstep(vOutline.w - soft, vOutline.w, radius);
107
+ float whole = 1.0 - smoothstep(halo - soft, halo, edge);
108
+ gl_FragColor = vec4(mix(vRing, vFill, front), whole * vOpacity);
109
+ if (gl_FragColor.a <= 0.0) discard;
110
+ #include <colorspace_fragment>
111
+ }`;
112
+ const SEGMENT_VERTEX = `
113
+ uniform float pixelAngle;
114
+ uniform float behind;
115
+ attribute vec3 start;
116
+ attribute vec3 finish;
117
+ attribute vec3 fill;
118
+ attribute vec3 wide;
119
+ attribute vec2 shade;
120
+ attribute vec2 dash;
121
+ varying float vAcross;
122
+ varying float vAlong;
123
+ varying float vLength;
124
+ varying float vBody;
125
+ varying float vScale;
126
+ varying vec2 vDash;
127
+ varying vec3 vFill;
128
+ varying float vOpacity;
129
+ void main() {
130
+ vOpacity = mix(shade.x, shade.y, behind);
131
+ vFill = fill;
132
+ vDash = dash;
133
+ vec3 from = (modelViewMatrix * vec4(start, 1.0)).xyz;
134
+ vec3 to = (modelViewMatrix * vec4(finish, 1.0)).xyz;
135
+ vec3 here = mix(from, to, position.y);
136
+ vec3 line = to - from;
137
+ vLength = length(line);
138
+ vec3 along = vLength > 0.0 ? line / vLength : vec3(1.0, 0.0, 0.0);
139
+ float pixel = max(-here.z, 0.0001) * pixelAngle;
140
+ // The half of the width: the width in the space, not less than a width in pixels, plus a width in pixels.
141
+ vBody = max(wide.y, wide.z * pixel) + wide.x * pixel;
142
+ float reach = vBody + pixel;
143
+ // The square looks at the camera: its side is at a right angle to the line and to the sight line.
144
+ vec3 side = cross(along, here);
145
+ float sideLength = length(side);
146
+ side = sideLength > 0.000001 ? side / sideLength : vec3(0.0, 1.0, 0.0);
147
+ float cap = position.y * 2.0 - 1.0;
148
+ vAcross = position.x * reach;
149
+ vAlong = position.y * vLength + cap * reach;
150
+ // A dash has its length in pixels at the middle of the line.
151
+ vScale = 1.0 / (max(-0.5 * (from.z + to.z), 0.0001) * pixelAngle);
152
+ gl_Position = projectionMatrix * vec4(here + side * vAcross + along * (cap * reach), 1.0);
153
+ // Each corner has the depth of the side of a tube that is nearest to the camera.
154
+ // Thus the surface below a run does not hide the half of the run that is nearest to it.
155
+ vec4 depthAt = projectionMatrix * vec4(here * max(1.0 - reach / max(length(here), 0.0001), 0.5), 1.0);
156
+ gl_Position.z = depthAt.z / depthAt.w * gl_Position.w;
157
+ if (vOpacity <= 0.0) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
158
+ }`;
159
+ const SEGMENT_FRAGMENT = `
160
+ varying float vAcross;
161
+ varying float vAlong;
162
+ varying float vLength;
163
+ varying float vBody;
164
+ varying float vScale;
165
+ varying vec2 vDash;
166
+ varying vec3 vFill;
167
+ varying float vOpacity;
168
+ void main() {
169
+ // The distance from the line between the two ends. Thus each end is round.
170
+ float past = max(max(-vAlong, vAlong - vLength), 0.0);
171
+ float away = length(vec2(vAcross, past));
172
+ float soft = max(fwidth(away), 0.000001);
173
+ float alpha = (1.0 - smoothstep(vBody - soft, vBody, away)) * vOpacity;
174
+ if (vDash.x > 0.0 && mod(vAlong * vScale, vDash.x + vDash.y) > vDash.x) alpha = 0.0;
175
+ gl_FragColor = vec4(vFill, alpha);
176
+ if (gl_FragColor.a <= 0.0) discard;
177
+ #include <colorspace_fragment>
178
+ }`;
179
+ const vector = (point) => new Vector3(point.x, point.y, point.z);
180
+ /** This function tells if a field has a grid that a texture can hold. */
181
+ function hasGrid(field) {
182
+ return Number.isInteger(field.columns) && Number.isInteger(field.rows) && field.columns >= 1 && field.rows >= 1 && field.cell > 0;
183
+ }
184
+ /** This function makes the square of two triangles that each instance of a mesh uses. `low` is the y value of its first side. */
185
+ function squareOf(low) {
186
+ const geometry = new InstancedBufferGeometry();
187
+ geometry.setAttribute("position", new BufferAttribute(new Float32Array([
188
+ -1,
189
+ low,
190
+ 0,
191
+ 1,
192
+ low,
193
+ 0,
194
+ 1,
195
+ 1,
196
+ 0,
197
+ -1,
198
+ 1,
199
+ 0
200
+ ]), 3));
201
+ geometry.setIndex([
202
+ 0,
203
+ 1,
204
+ 2,
205
+ 0,
206
+ 2,
207
+ 3
208
+ ]);
209
+ return geometry;
210
+ }
211
+ /**
212
+ * This function makes the content of a stage that draws marks. Examine the marks with `checkMarks` first.
213
+ *
214
+ * - A point is a sign with the shape and the color of its level. A sign looks at the camera.
215
+ * A sign with no size keeps its size in the picture.
216
+ * - A run is a round line with its width in the space. Its center is half of its width above its surface.
217
+ * - A link is a dashed line. The picture shows it while its record, or the record of one of its ends, has a highlight.
218
+ * - A field is a grid of colors on its plane. A cell with no value is clear.
219
+ * - A meter has no place, so the content ignores it.
220
+ *
221
+ * A mark that a solid of an earlier content hides is less clear, and a mark with a highlight stays solid.
222
+ * A sign gets a pick before each other record, and a run gets a pick before a field.
223
+ * A run and a field give the distance of their pick.
224
+ * Put the content after the content of the solids in `stackContents`.
225
+ */
226
+ function createMarkers(marks, options = {}) {
227
+ const signRadius = options.signRadius ?? 7;
228
+ const linkHalf = (options.linkWidth ?? 2) / 2;
229
+ const linkDash = options.linkDash ?? [6, 4];
230
+ const points = marks.filter((mark) => mark.type === "point");
231
+ const paths = marks.filter((mark) => mark.type === "run");
232
+ const lines = marks.filter((mark) => mark.type === "link");
233
+ const grids = marks.filter((mark) => mark.type === "field").filter(hasGrid);
234
+ const records = new Set([
235
+ ...points,
236
+ ...paths,
237
+ ...lines,
238
+ ...grids
239
+ ].map((mark) => mark.record));
240
+ const corners = [];
241
+ for (const mark of points) corners.push(vector(mark.frame.at));
242
+ for (const mark of paths) for (const frame of mark.path) corners.push(vector(frame.at));
243
+ for (const mark of lines) corners.push(vector(mark.from.at), vector(mark.to.at));
244
+ for (const mark of grids) {
245
+ const origin = vector(mark.frame.at);
246
+ const across = vector(mark.across).multiplyScalar(mark.columns * mark.cell);
247
+ const down = vector(fieldRowStep(mark)).multiplyScalar(mark.rows * mark.cell);
248
+ corners.push(origin, origin.clone().add(across), origin.clone().add(down), origin.clone().add(across).add(down));
249
+ }
250
+ const low = new Vector3(1e400, 1e400, 1e400);
251
+ const high = new Vector3(-1e400, -1e400, -1e400);
252
+ for (const corner of corners) {
253
+ low.min(corner);
254
+ high.max(corner);
255
+ }
256
+ const some = corners.length > 0;
257
+ const size = some ? high.distanceTo(low) : 0;
258
+ const lift = options.lift ?? (size > 0 ? size / 400 : .01);
259
+ const center = some ? low.clone().add(high).multiplyScalar(.5) : new Vector3();
260
+ const box = some ? [
261
+ 0,
262
+ 1,
263
+ 2,
264
+ 3,
265
+ 4,
266
+ 5,
267
+ 6,
268
+ 7
269
+ ].map((at) => ({
270
+ x: at & 1 ? high.x : low.x,
271
+ y: at & 2 ? high.y : low.y,
272
+ z: at & 4 ? high.z : low.z
273
+ })) : [];
274
+ /** The length of one CSS pixel at the distance 1 from the camera. The first step of the stage gives the correct value. */
275
+ const pixelAngle = { value: 2 * Math.tan(32 * Math.PI / 360) / 720 };
276
+ /** The direction of the camera at the last step. */
277
+ let sight;
278
+ const color = new Color();
279
+ const put = (to, at, from) => {
280
+ to[at * 3] = from.r;
281
+ to[at * 3 + 1] = from.g;
282
+ to[at * 3 + 2] = from.b;
283
+ };
284
+ const signs = points.map((mark) => ({
285
+ mark,
286
+ at: vector(mark.frame.at),
287
+ world: mark.size === void 0 ? 0 : Math.max(mark.size, 0) / 2,
288
+ outline: SHAPE_OUTLINES[levelShape(mark.level)],
289
+ shown: true,
290
+ highlight: 0
291
+ }));
292
+ const signGeometry = squareOf(-1);
293
+ const signCenters = new Float32Array(signs.length * 3);
294
+ const signLifted = new Float32Array(signs.length * 3);
295
+ const signOutlines = new Float32Array(signs.length * 4);
296
+ signs.forEach((part, index) => {
297
+ const above = vector(part.mark.frame.out).multiplyScalar(lift).add(part.at);
298
+ signCenters.set([
299
+ part.at.x,
300
+ part.at.y,
301
+ part.at.z
302
+ ], index * 3);
303
+ signLifted.set([
304
+ above.x,
305
+ above.y,
306
+ above.z
307
+ ], index * 3);
308
+ signOutlines.set([
309
+ part.outline.radius,
310
+ part.outline.sides,
311
+ part.outline.start,
312
+ part.outline.hole
313
+ ], index * 4);
314
+ });
315
+ const signFills = new InstancedBufferAttribute(new Float32Array(signs.length * 3), 3);
316
+ const signRings = new InstancedBufferAttribute(new Float32Array(signs.length * 3), 3);
317
+ const signLooks = new InstancedBufferAttribute(new Float32Array(signs.length * 4), 4);
318
+ signGeometry.setAttribute("center", new InstancedBufferAttribute(signCenters, 3));
319
+ signGeometry.setAttribute("lifted", new InstancedBufferAttribute(signLifted, 3));
320
+ signGeometry.setAttribute("outline", new InstancedBufferAttribute(signOutlines, 4));
321
+ signGeometry.setAttribute("fill", signFills);
322
+ signGeometry.setAttribute("ring", signRings);
323
+ signGeometry.setAttribute("look", signLooks);
324
+ signGeometry.instanceCount = signs.length;
325
+ const runSegments = paths.reduce((sum, mark) => sum + Math.max(mark.path.length - 1, 0), 0);
326
+ const segmentCount = runSegments * 2 + grids.length * 4 + lines.length;
327
+ const segmentGeometry = squareOf(0);
328
+ const starts = new Float32Array(segmentCount * 3);
329
+ const finishes = new Float32Array(segmentCount * 3);
330
+ const dashes = new Float32Array(segmentCount * 2);
331
+ const setSegment = (index, from, to) => {
332
+ starts.set([
333
+ from.x,
334
+ from.y,
335
+ from.z
336
+ ], index * 3);
337
+ finishes.set([
338
+ to.x,
339
+ to.y,
340
+ to.z
341
+ ], index * 3);
342
+ };
343
+ let nextRun = 0;
344
+ const runs = paths.map((mark) => {
345
+ const line = mark.path.map((frame) => vector(frame.out).multiplyScalar(Math.max(mark.width, 0) / 2).add(vector(frame.at)));
346
+ const part = {
347
+ mark,
348
+ points: line,
349
+ casing: nextRun,
350
+ body: runSegments + nextRun,
351
+ shown: true,
352
+ highlight: 0
353
+ };
354
+ for (let at = 0; at + 1 < line.length; at += 1) {
355
+ const from = line[at];
356
+ const to = line[at + 1];
357
+ if (from === void 0 || to === void 0) continue;
358
+ setSegment(part.casing + at, from, to);
359
+ setSegment(part.body + at, from, to);
360
+ }
361
+ nextRun += Math.max(line.length - 1, 0);
362
+ return part;
363
+ });
364
+ const fields = grids.map((mark, index) => {
365
+ const out = vector(mark.frame.out);
366
+ const origin = vector(mark.frame.at);
367
+ const across = vector(mark.across);
368
+ const down = vector(fieldRowStep(mark));
369
+ const width = across.clone().multiplyScalar(mark.columns * mark.cell);
370
+ const height = down.clone().multiplyScalar(mark.rows * mark.cell);
371
+ const first = out.clone().multiplyScalar(lift).add(origin);
372
+ const square = [
373
+ first,
374
+ first.clone().add(width),
375
+ first.clone().add(width).add(height),
376
+ first.clone().add(height)
377
+ ];
378
+ const geometry = new BufferGeometry();
379
+ geometry.setAttribute("position", new BufferAttribute(new Float32Array(square.flatMap((corner) => [
380
+ corner.x,
381
+ corner.y,
382
+ corner.z
383
+ ])), 3));
384
+ geometry.setAttribute("uv", new BufferAttribute(new Float32Array([
385
+ 0,
386
+ 0,
387
+ 1,
388
+ 0,
389
+ 1,
390
+ 1,
391
+ 0,
392
+ 1
393
+ ]), 2));
394
+ geometry.setIndex([
395
+ 0,
396
+ 1,
397
+ 2,
398
+ 0,
399
+ 2,
400
+ 3
401
+ ]);
402
+ const texture = new DataTexture(new Uint8Array(mark.columns * mark.rows * 4), mark.columns, mark.rows, RGBAFormat, UnsignedByteType);
403
+ texture.colorSpace = SRGBColorSpace;
404
+ texture.magFilter = NearestFilter;
405
+ texture.minFilter = NearestFilter;
406
+ texture.generateMipmaps = false;
407
+ const mesh = new Mesh(geometry, new MeshBasicMaterial({
408
+ map: texture,
409
+ transparent: true,
410
+ depthWrite: false,
411
+ side: DoubleSide
412
+ }));
413
+ mesh.renderOrder = 7;
414
+ const border = runSegments * 2 + index * 4;
415
+ square.forEach((corner, at) => {
416
+ setSegment(border + at, corner, square[(at + 1) % 4] ?? corner);
417
+ });
418
+ return {
419
+ mark,
420
+ mesh,
421
+ texture,
422
+ plane: new Plane().setFromNormalAndCoplanarPoint(out, first),
423
+ origin,
424
+ across,
425
+ down,
426
+ border,
427
+ shown: true,
428
+ highlight: 0
429
+ };
430
+ });
431
+ const links = lines.map((mark, index) => {
432
+ const segment = runSegments * 2 + grids.length * 4 + index;
433
+ setSegment(segment, vector(mark.from.at), vector(mark.to.at));
434
+ dashes.set([Math.max(linkDash[0], 0), Math.max(linkDash[1], 0)], segment * 2);
435
+ return {
436
+ mark,
437
+ segment,
438
+ shown: true
439
+ };
440
+ });
441
+ const segmentFills = new InstancedBufferAttribute(new Float32Array(segmentCount * 3), 3);
442
+ const segmentWides = new InstancedBufferAttribute(new Float32Array(segmentCount * 3), 3);
443
+ const segmentShades = new InstancedBufferAttribute(new Float32Array(segmentCount * 2), 2);
444
+ segmentGeometry.setAttribute("start", new InstancedBufferAttribute(starts, 3));
445
+ segmentGeometry.setAttribute("finish", new InstancedBufferAttribute(finishes, 3));
446
+ segmentGeometry.setAttribute("dash", new InstancedBufferAttribute(dashes, 2));
447
+ segmentGeometry.setAttribute("fill", segmentFills);
448
+ segmentGeometry.setAttribute("wide", segmentWides);
449
+ segmentGeometry.setAttribute("shade", segmentShades);
450
+ segmentGeometry.instanceCount = segmentCount;
451
+ const passes = (geometry, vertexShader, fragmentShader, order, count) => [1, 0].map((behind, at) => {
452
+ const material = new ShaderMaterial({
453
+ uniforms: {
454
+ pixelAngle,
455
+ halo: { value: HALO },
456
+ behind: { value: behind }
457
+ },
458
+ vertexShader,
459
+ fragmentShader,
460
+ transparent: true,
461
+ depthTest: behind === 0,
462
+ depthWrite: false,
463
+ side: DoubleSide
464
+ });
465
+ const mesh = new Mesh(geometry, material);
466
+ mesh.renderOrder = order + at;
467
+ mesh.frustumCulled = false;
468
+ mesh.visible = count > 0;
469
+ return mesh;
470
+ });
471
+ const segmentMeshes = passes(segmentGeometry, SEGMENT_VERTEX, SEGMENT_FRAGMENT, 8, segmentCount);
472
+ const signMeshes = passes(signGeometry, SIGN_VERTEX, SIGN_FRAGMENT, 10, signs.length);
473
+ let painted;
474
+ /** This function gives each cell of each field its color. */
475
+ const paintFields = (theme) => {
476
+ const stops = theme.field.map((hex) => color.set(hex).getRGB({
477
+ r: 0,
478
+ g: 0,
479
+ b: 0
480
+ }, SRGBColorSpace));
481
+ for (const part of fields) {
482
+ const { min, max } = part.mark.scale;
483
+ const own = color.set(theme.levels[part.mark.level]).getRGB({
484
+ r: 0,
485
+ g: 0,
486
+ b: 0
487
+ }, SRGBColorSpace);
488
+ const data = part.texture.image.data;
489
+ for (let cell = 0; cell < part.mark.columns * part.mark.rows; cell += 1) {
490
+ const value = part.mark.values[cell];
491
+ if (value === null || value === void 0 || !Number.isFinite(value)) {
492
+ data.fill(0, cell * 4, cell * 4 + 4);
493
+ continue;
494
+ }
495
+ const place = (max > min ? Math.min(Math.max((value - min) / (max - min), 0), 1) : 0) * Math.max(stops.length - 1, 0);
496
+ const before = stops[Math.min(Math.floor(place), Math.max(stops.length - 2, 0))] ?? own;
497
+ const after = stops[Math.min(Math.floor(place), Math.max(stops.length - 2, 0)) + 1] ?? before;
498
+ const mix = place - Math.min(Math.floor(place), Math.max(stops.length - 2, 0));
499
+ data[cell * 4] = Math.round((before.r + (after.r - before.r) * mix) * 255);
500
+ data[cell * 4 + 1] = Math.round((before.g + (after.g - before.g) * mix) * 255);
501
+ data[cell * 4 + 2] = Math.round((before.b + (after.b - before.b) * mix) * 255);
502
+ data[cell * 4 + 3] = 255;
503
+ }
504
+ part.texture.needsUpdate = true;
505
+ part.mesh.material.opacity = Math.min(Math.max(theme.fieldOpacity ?? .8, 0), 1);
506
+ }
507
+ };
508
+ /** This function writes the colors, the sizes and the shades of each mark from its highlight and from the filter. */
509
+ const sync = (theme) => {
510
+ signs.forEach((part, index) => {
511
+ const lit = part.highlight !== 0;
512
+ const grow = HIGHLIGHT_SIZE[part.highlight];
513
+ put(signFills.array, index, color.set(theme.levels[part.mark.level]));
514
+ put(signRings.array, index, color.set(lit ? theme.accent : theme.halo));
515
+ signLooks.array.set([
516
+ part.world > 0 ? 0 : signRadius * grow,
517
+ part.world * grow,
518
+ part.shown ? 1 : 0,
519
+ part.shown ? lit ? 1 : HIDDEN_OPACITY : 0
520
+ ], index * 4);
521
+ });
522
+ const fills = segmentFills.array;
523
+ const setLook = (segment, pixels, world, least, inSight, hidden) => {
524
+ segmentWides.array.set([
525
+ pixels,
526
+ world,
527
+ least
528
+ ], segment * 3);
529
+ segmentShades.array.set([inSight, hidden], segment * 2);
530
+ };
531
+ for (const part of runs) {
532
+ const lit = part.highlight !== 0;
533
+ const half = Math.max(part.mark.width, 0) / 2;
534
+ const inSight = part.shown ? 1 : 0;
535
+ const hidden = part.shown ? lit ? 1 : HIDDEN_OPACITY : 0;
536
+ for (let at = 0; at + 1 < part.points.length; at += 1) {
537
+ put(fills, part.casing + at, color.set(lit ? theme.accent : theme.halo));
538
+ setLook(part.casing + at, CASING[part.highlight], half, LEAST_RUN, inSight, hidden);
539
+ put(fills, part.body + at, color.set(theme.levels[part.mark.level]));
540
+ setLook(part.body + at, 0, half, LEAST_RUN, inSight, hidden);
541
+ }
542
+ }
543
+ for (const part of fields) {
544
+ const lit = part.shown && part.highlight !== 0;
545
+ part.mesh.visible = part.shown;
546
+ for (let at = 0; at < 4; at += 1) {
547
+ put(fills, part.border + at, color.set(theme.accent));
548
+ setLook(part.border + at, BORDER[part.highlight], 0, 0, lit ? 1 : 0, lit ? HIDDEN_OPACITY : 0);
549
+ }
550
+ }
551
+ for (const part of links) {
552
+ put(fills, part.segment, color.set(theme.levels[part.mark.level]));
553
+ setLook(part.segment, linkHalf, 0, 0, part.shown ? 1 : 0, part.shown ? 1 : 0);
554
+ }
555
+ signFills.needsUpdate = true;
556
+ signRings.needsUpdate = true;
557
+ signLooks.needsUpdate = true;
558
+ segmentFills.needsUpdate = true;
559
+ segmentWides.needsUpdate = true;
560
+ segmentShades.needsUpdate = true;
561
+ };
562
+ /** The marks that the filter accepts. A link also needs a highlight, so it has its own state. */
563
+ let accepted = () => true;
564
+ let lit = () => 0;
565
+ const setShown = () => {
566
+ for (const part of signs) part.shown = accepted(part.mark);
567
+ for (const part of runs) part.shown = accepted(part.mark);
568
+ for (const part of fields) part.shown = accepted(part.mark);
569
+ for (const part of links) part.shown = accepted(part.mark) && (lit(part.mark.record) !== 0 || lit(part.mark.from.record) !== 0 || lit(part.mark.to.record) !== 0);
570
+ };
571
+ const toMark = new Vector3();
572
+ const onRay = new Vector3();
573
+ const onLine = new Vector3();
574
+ const hit = new Vector3();
575
+ return {
576
+ objects: [
577
+ ...fields.map((part) => part.mesh),
578
+ ...segmentMeshes,
579
+ ...signMeshes
580
+ ],
581
+ extent: {
582
+ center: {
583
+ x: center.x,
584
+ y: center.y,
585
+ z: center.z
586
+ },
587
+ corners: box
588
+ },
589
+ has: (record) => records.has(record),
590
+ pick(ray) {
591
+ const { origin, direction } = ray.ray;
592
+ const forward = sight ?? direction;
593
+ const facing = direction.dot(forward);
594
+ if (facing <= 1e-9) return void 0;
595
+ let sign;
596
+ let nearest = 1e400;
597
+ for (const part of signs) {
598
+ if (!part.shown) continue;
599
+ toMark.subVectors(part.at, origin);
600
+ const depth = toMark.dot(forward);
601
+ if (depth <= 0) continue;
602
+ const pixel = depth * pixelAngle.value;
603
+ const radius = (part.world > 0 ? part.world : signRadius * pixel) * HIGHLIGHT_SIZE[part.highlight] * part.outline.radius + PICK_MARGIN * pixel;
604
+ const away = hit.copy(direction).multiplyScalar(depth / facing).sub(toMark).length();
605
+ if (away > radius || away / pixel >= nearest) continue;
606
+ sign = part;
607
+ nearest = away / pixel;
608
+ }
609
+ if (sign !== void 0) return {
610
+ record: sign.mark.record,
611
+ value: sign.mark
612
+ };
613
+ let found;
614
+ let distance = 1e400;
615
+ for (const part of runs) {
616
+ if (!part.shown) continue;
617
+ for (let at = 0; at + 1 < part.points.length; at += 1) {
618
+ const from = part.points[at];
619
+ const to = part.points[at + 1];
620
+ if (from === void 0 || to === void 0) continue;
621
+ const squared = ray.ray.distanceSqToSegment(from, to, onRay, onLine);
622
+ const along = onRay.distanceTo(origin);
623
+ const pixel = along * facing * pixelAngle.value;
624
+ const reach = Math.max(part.mark.width / 2, LEAST_RUN * pixel) + (CASING[0] + PICK_MARGIN) * pixel;
625
+ const side = Math.max(along - reach, 0);
626
+ if (squared > reach * reach || side >= distance) continue;
627
+ found = part.mark;
628
+ distance = side;
629
+ }
630
+ }
631
+ if (found !== void 0) return {
632
+ record: found.record,
633
+ value: found,
634
+ distance
635
+ };
636
+ for (const part of fields) {
637
+ if (!part.shown || ray.ray.intersectPlane(part.plane, hit) === null) continue;
638
+ const along = hit.distanceTo(origin);
639
+ if (along >= distance) continue;
640
+ toMark.subVectors(hit, part.origin);
641
+ const column = Math.floor(toMark.dot(part.across) / part.mark.cell);
642
+ const row = Math.floor(toMark.dot(part.down) / part.mark.cell);
643
+ if (fieldValue(part.mark, column, row) === null) continue;
644
+ found = part.mark;
645
+ distance = along;
646
+ }
647
+ return found === void 0 ? void 0 : {
648
+ record: found.record,
649
+ value: found,
650
+ distance
651
+ };
652
+ },
653
+ paint(theme, highlight) {
654
+ if (theme !== painted) paintFields(theme);
655
+ painted = theme;
656
+ lit = highlight;
657
+ for (const part of signs) part.highlight = highlight(part.mark.record);
658
+ for (const part of runs) part.highlight = highlight(part.mark.record);
659
+ for (const part of fields) part.highlight = highlight(part.mark.record);
660
+ setShown();
661
+ sync(theme);
662
+ },
663
+ step(frame) {
664
+ pixelAngle.value = 2 * Math.tan(frame.fieldOfView * Math.PI / 360) / Math.max(frame.height, 1);
665
+ sight = frame.camera.getWorldDirection(sight ?? new Vector3());
666
+ return false;
667
+ },
668
+ show(test) {
669
+ accepted = test;
670
+ setShown();
671
+ if (painted !== void 0) sync(painted);
672
+ },
673
+ dispose() {
674
+ signGeometry.dispose();
675
+ segmentGeometry.dispose();
676
+ for (const mesh of [...segmentMeshes, ...signMeshes]) mesh.material.dispose();
677
+ for (const part of fields) {
678
+ part.mesh.geometry.dispose();
679
+ part.mesh.material.dispose();
680
+ part.texture.dispose();
681
+ }
682
+ }
683
+ };
684
+ }
685
+ export { SIGN_RADIUS, createMarkers, signReach };
686
+
687
+ //# sourceMappingURL=markers.js.map