three-slicer-viewer 0.3.1 → 0.3.3

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,89 @@
1
+ import { U as f } from "./viewer_defaults-B7UUZ582.js";
2
+ const N = {
3
+ 0: [0.35, 0.38, 0.44],
4
+ // travel
5
+ 1: [0.98, 0.45, 0.16],
6
+ // wall
7
+ 2: [0.36, 0.72, 0.94],
8
+ // sparse infill
9
+ 3: [0.2, 0.55, 0.86],
10
+ // solid infill
11
+ 4: [0.62, 0.66, 0.72],
12
+ // skirt / brim
13
+ 5: [0.5, 0.54, 0.6],
14
+ // support
15
+ 6: [0.42, 0.46, 0.52],
16
+ // raft
17
+ 7: [0.96, 0.76, 0.22],
18
+ // gap fill
19
+ 8: [0.88, 0.55, 0.3],
20
+ // thin wall
21
+ 9: [0.3, 0.8, 0.62],
22
+ // bridge
23
+ 10: [0.8, 0.42, 0.72],
24
+ // ironing
25
+ 11: [0.58, 0.5, 0.88]
26
+ // prime tower
27
+ }, T = {
28
+ 0: "Travel",
29
+ 1: "Wall",
30
+ 2: "Sparse",
31
+ 3: "Solid",
32
+ 4: "Skirt",
33
+ 5: "Support",
34
+ 6: "Raft",
35
+ 7: "Gap",
36
+ 8: "Thin",
37
+ 9: "Bridge",
38
+ 10: "Ironing",
39
+ 11: "Prime"
40
+ }, b = [
41
+ [0.9, 0.26, 0.3],
42
+ [0.27, 0.6, 0.93],
43
+ [0.36, 0.78, 0.42],
44
+ [0.98, 0.72, 0.2],
45
+ [0.72, 0.42, 0.9],
46
+ [0.2, 0.78, 0.8],
47
+ [0.95, 0.5, 0.72],
48
+ [0.6, 0.6, 0.64]
49
+ ];
50
+ function m(r) {
51
+ const n = /^#?([0-9a-f]{6})$/i.exec(String(r ?? "").trim());
52
+ if (!n) return m(f);
53
+ const t = parseInt(n[1], 16);
54
+ return [(t >> 16 & 255) / 255, (t >> 8 & 255) / 255, (t & 255) / 255];
55
+ }
56
+ function R(r) {
57
+ const n = (t) => Math.max(0, Math.min(255, Math.round((Number(t) || 0) * 255)));
58
+ return n(r?.[0]) * 65536 + n(r?.[1]) * 256 + n(r?.[2]);
59
+ }
60
+ const O = [
61
+ [0.043, 0.173, 0.478],
62
+ [0.055, 0.325, 0.639],
63
+ [0.067, 0.478, 0.8],
64
+ [0.078, 0.631, 0.961],
65
+ [0.118, 0.749, 0.804],
66
+ [0.157, 0.867, 0.647],
67
+ [0.376, 0.902, 0.4],
68
+ [0.667, 0.898, 0.157],
69
+ [0.988, 0.976, 0.012],
70
+ [0.988, 0.663, 0.012],
71
+ [0.58, 0.149, 0.086]
72
+ ];
73
+ function S(r, n, t, i = O) {
74
+ const o = i.length - 1;
75
+ if (o < 0) return [0, 0, 0];
76
+ if (!Number.isFinite(r) || !Number.isFinite(n) || !Number.isFinite(t) || t <= n || r <= n) return i[0];
77
+ if (r >= t) return i[o];
78
+ const c = (r - n) / (t - n) * o, a = Math.min(o - 1, Math.floor(c)), u = c - a, s = i[a], e = i[a + 1];
79
+ return [s[0] + (e[0] - s[0]) * u, s[1] + (e[1] - s[1]) * u, s[2] + (e[2] - s[2]) * u];
80
+ }
81
+ export {
82
+ O as D,
83
+ b as T,
84
+ N as a,
85
+ T as b,
86
+ m as h,
87
+ R as p,
88
+ S as r
89
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-slicer-viewer",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "The three-slicer viewer without the slicer — model loading, G-code parsing, GPU toolpath rendering and the settings transforms, under MIT. Plug a slicer in through the `slicer` prop.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -35,6 +35,10 @@
35
35
  "types": "./types/toggle.d.ts",
36
36
  "default": "./src/settings/toggle_eval.js"
37
37
  },
38
+ "./paint": {
39
+ "types": "./types/paint.d.ts",
40
+ "default": "./src/core/paint_tools.js"
41
+ },
38
42
  "./components": {
39
43
  "types": "./types/components.d.ts",
40
44
  "default": "./dist/components.js"
@@ -49,6 +53,7 @@
49
53
  "VIEWER.md",
50
54
  "data",
51
55
  "dist",
56
+ "src/core/paint_tools.js",
52
57
  "src/data.js",
53
58
  "src/settings",
54
59
  "types",
@@ -0,0 +1,8 @@
1
+ // The painting tools that act on one click instead of a drag (upstream's Smart fill, Bucket fill and the single-facet
2
+ // POINTER). Read by the brush input, both brush panels and the slicer worker (`three-slicer-viewer/paint`), so a
3
+ // tool added here is one the whole paint path treats as a fill.
4
+ export const FILL_TOOLS = new Set(['smart', 'bucket', 'triangle'])
5
+
6
+ // The fills that spread across neighbouring facets and so take a fill angle. Triangle is bucket fill with the
7
+ // propagation off (the worker passes -1 as its angle), so it has no angle to set.
8
+ export const ANGLE_FILL_TOOLS = new Set(['smart', 'bucket'])
@@ -1,14 +1,19 @@
1
1
  // AUTO-GENERATED by types/gen_kernel_params.mjs — DO NOT EDIT.
2
2
  // The schema keys deriveKernelParams consumes (the distinct "From setting" column of engine/PARAMS.md).
3
3
  export const KERNEL_SETTING_KEYS = [
4
+ "alternate_extra_wall",
4
5
  "bottom_shell_layers",
6
+ "bottom_solid_infill_flow_ratio",
5
7
  "bottom_surface_filament_id",
8
+ "bridge_flow",
6
9
  "bridge_no_support",
7
10
  "bridge_speed",
11
+ "brim_flow_ratio",
8
12
  "brim_object_gap",
9
13
  "brim_width",
10
14
  "close_fan_the_first_x_layers",
11
15
  "default_acceleration",
16
+ "detect_thin_wall",
12
17
  "enable_arc_fitting",
13
18
  "enable_pressure_advance",
14
19
  "enable_prime_tower",
@@ -17,27 +22,36 @@ export const KERNEL_SETTING_KEYS = [
17
22
  "filament_cost",
18
23
  "filament_density",
19
24
  "filament_diameter",
25
+ "filament_end_gcode",
20
26
  "filament_flow_ratio",
21
27
  "filament_map",
28
+ "filament_max_volumetric_speed",
22
29
  "filament_retraction_length",
23
30
  "filament_retraction_minimum_travel",
24
31
  "filament_retraction_speed",
25
32
  "filament_settings_id",
26
33
  "filament_type",
27
34
  "filament_z_hop",
35
+ "filter_out_gap_fill",
36
+ "first_layer_flow_ratio",
28
37
  "flush_into_infill",
29
38
  "flush_multiplier",
30
39
  "flush_volumes_matrix",
31
40
  "full_fan_speed_layer",
41
+ "gap_fill_flow_ratio",
42
+ "gap_infill_speed",
32
43
  "hot_plate_temp",
33
44
  "independent_support_layer_height",
34
45
  "infill_direction",
46
+ "infill_wall_overlap",
35
47
  "initial_layer_line_width",
36
48
  "initial_layer_print_height",
37
49
  "initial_layer_speed",
38
50
  "inner_wall_filament_id",
51
+ "inner_wall_flow_ratio",
39
52
  "inner_wall_line_width",
40
53
  "internal_solid_filament_id",
54
+ "internal_solid_infill_flow_ratio",
41
55
  "internal_solid_infill_line_width",
42
56
  "ironing_flow",
43
57
  "ironing_spacing",
@@ -60,12 +74,16 @@ export const KERNEL_SETTING_KEYS = [
60
74
  "max_volumetric_extrusion_rate_slope",
61
75
  "nozzle_diameter",
62
76
  "nozzle_temperature",
77
+ "only_one_wall_first_layer",
63
78
  "outer_wall_filament_id",
79
+ "outer_wall_flow_ratio",
64
80
  "outer_wall_line_width",
65
81
  "outer_wall_speed",
82
+ "precise_outer_wall",
66
83
  "pressure_advance",
67
84
  "prime_tower_width",
68
85
  "prime_volume",
86
+ "print_flow_ratio",
69
87
  "printable_area",
70
88
  "printable_height",
71
89
  "raft_contact_distance",
@@ -76,14 +94,17 @@ export const KERNEL_SETTING_KEYS = [
76
94
  "retraction_length",
77
95
  "retraction_minimum_travel",
78
96
  "retraction_speed",
97
+ "scarf_joint_flow_ratio",
79
98
  "seam_position",
80
99
  "seam_slope_type",
100
+ "set_other_flow_ratios",
81
101
  "skirt_distance",
82
102
  "skirt_height",
83
103
  "skirt_loops",
84
104
  "slow_down_layer_time",
85
105
  "sparse_infill_density",
86
106
  "sparse_infill_filament_id",
107
+ "sparse_infill_flow_ratio",
87
108
  "sparse_infill_line_width",
88
109
  "sparse_infill_pattern",
89
110
  "spiral_mode",
@@ -93,8 +114,10 @@ export const KERNEL_SETTING_KEYS = [
93
114
  "support_bottom_z_distance",
94
115
  "support_expansion",
95
116
  "support_filament",
117
+ "support_flow_ratio",
96
118
  "support_interface_bottom_layers",
97
119
  "support_interface_filament",
120
+ "support_interface_flow_ratio",
98
121
  "support_interface_pattern",
99
122
  "support_interface_spacing",
100
123
  "support_interface_top_layers",
@@ -108,7 +131,10 @@ export const KERNEL_SETTING_KEYS = [
108
131
  "support_threshold_overlap",
109
132
  "support_top_z_distance",
110
133
  "support_type",
134
+ "thick_bridges",
135
+ "top_bottom_infill_wall_overlap",
111
136
  "top_shell_layers",
137
+ "top_solid_infill_flow_ratio",
112
138
  "top_surface_filament_id",
113
139
  "top_surface_line_width",
114
140
  "travel_acceleration",
@@ -124,8 +150,10 @@ export const KERNEL_SETTING_KEYS = [
124
150
  "tree_support_tip_diameter",
125
151
  "tree_support_top_rate",
126
152
  "tree_support_wall_count",
153
+ "wall_direction",
127
154
  "wall_generator",
128
155
  "wall_loops",
156
+ "wall_sequence",
129
157
  "wipe_tower_x",
130
158
  "wipe_tower_y",
131
159
  "z_hop",
@@ -59,12 +59,29 @@ const PASSTHROUGH_NUM = [
59
59
  // rather than silently dropping them.
60
60
  'outer_wall_filament_id', 'inner_wall_filament_id', 'sparse_infill_filament_id',
61
61
  'top_surface_filament_id', 'bottom_surface_filament_id', 'internal_solid_filament_id',
62
+ // Read by the kernel's classic wall generator (wall_generator = classic).
63
+ 'filter_out_gap_fill',
64
+ // Upstream's flow multipliers (GCode.cpp:7343-7390): the print-wide one and each role's own (emit.cpp role_flow_ratio).
65
+ 'print_flow_ratio', 'top_solid_infill_flow_ratio', 'bottom_solid_infill_flow_ratio', 'bridge_flow', 'brim_flow_ratio',
66
+ 'scarf_joint_flow_ratio', 'outer_wall_flow_ratio', 'inner_wall_flow_ratio', 'sparse_infill_flow_ratio',
67
+ 'internal_solid_infill_flow_ratio', 'gap_fill_flow_ratio', 'support_flow_ratio', 'support_interface_flow_ratio',
68
+ 'first_layer_flow_ratio',
62
69
  ]
63
- const PASSTHROUGH_BOOL = ['independent_support_layer_height']
70
+ const PASSTHROUGH_BOOL = ['independent_support_layer_height',
71
+ 'detect_thin_wall', 'precise_outer_wall', 'only_one_wall_first_layer', 'alternate_extra_wall',
72
+ // thick_bridges picks upstream's round bridge thread; set_other_flow_ratios turns the per-role ratios on.
73
+ 'thick_bridges', 'set_other_flow_ratios']
74
+ // coPercent options. A 3mf import delivers "15%" while the schema default is the bare 15, so the sign is stripped and
75
+ // the kernel gets the number of percent either way.
76
+ const PASSTHROUGH_PERCENT = ['infill_wall_overlap', 'top_bottom_infill_wall_overlap']
77
+ // coEnum options the kernel reads by their upstream spelling.
78
+ const PASSTHROUGH_STR = ['wall_sequence', 'wall_direction']
64
79
  // Per-filament vectors, one entry per filament, passed positionally like every other per-extruder array.
65
80
  // filament_map is which physical extruder each filament sits in; density and cost turn extruded millimetres into
66
- // the grams and currency the G-code footer reports.
67
- const PASSTHROUGH_NUM_VECTOR = ['filament_map', 'filament_density', 'filament_cost']
81
+ // the grams and currency the G-code footer reports. gap_infill_speed is per extruder rather than per filament; the
82
+ // kernel reads its first entry, the wall filament's on the single-material path. filament_max_volumetric_speed caps
83
+ // every extrusion's speed as upstream does (GCode.cpp:7492).
84
+ const PASSTHROUGH_NUM_VECTOR = ['filament_map', 'filament_density', 'filament_cost', 'gap_infill_speed', 'filament_max_volumetric_speed']
68
85
  // Material identity. The kernel needs the type for the two decisions upstream makes by material name (PETG's
69
86
  // extra unretract, TPU on the first layer) and the settings id for the footer.
70
87
  const PASSTHROUGH_STR_VECTOR = ['filament_type', 'filament_settings_id']
@@ -192,6 +209,22 @@ export function serializeProjectSettings(settings) {
192
209
  }
193
210
 
194
211
 
212
+ // Issue 63: the custom G-code the kernel expands with upstream's PlaceholderParser (custom_gcode.cpp). The kernel
213
+ // gets the flattened settings it expands them against only when one of these holds text, which keeps every caller
214
+ // without a template on the raw path, byte-identical to before. The settings go in the form a 3mf's
215
+ // project_settings.config stores (serializeProjectSettings), as one JSON string: the kernel's flat key search
216
+ // never looks inside a string value, so no key in it can be taken for a kernel parameter the map omits.
217
+ // The kernel names the same templates in custom_gcode.cpp; test_custom_gcode.mjs checks each key here is expanded there.
218
+ export const CUSTOM_GCODE_KEYS = ['machine_start_gcode', 'machine_end_gcode', 'filament_end_gcode']
219
+
220
+ function placeholderConfig(settings, plate) {
221
+ const hasText = key => [settings?.[key]].flat().some(value => String(value ?? '').trim() !== '')
222
+ if (!CUSTOM_GCODE_KEYS.some(hasText)) return {}
223
+ // upstream's Print::get_plate_number_formatted: the 1-based plate, zero-padded to two digits
224
+ const plateNumber = String(plate + 1).padStart(2, '0')
225
+ return { placeholder_config: JSON.stringify({ ...serializeProjectSettings(settings), $plate_number: plateNumber }) }
226
+ }
227
+
195
228
  // Right-panel settings values -> kernel parameters (derived from schema keys)
196
229
  // opts.plate: which plate's entry to take from per-plate array options (wipe_tower_x/y — upstream coFloats,
197
230
  // one entry per plate). Defaults to 0, so every existing caller reads exactly what it always did.
@@ -228,8 +261,13 @@ export function deriveKernelParams(settings, opts) {
228
261
  // the default style silently sliced grid. An explicit non-tree style keeps winning over the type.
229
262
  const styleRaw = str('support_style')
230
263
  const typeRaw = str('support_type')
231
- const support_style =
232
- /tree|organic/i.test(styleRaw) || (/tree/i.test(typeRaw) && styleRaw === 'default') ? 'tree' : 'grid'
264
+ let support_style = 'grid'
265
+ if (/tree|organic/i.test(styleRaw) || (/tree/i.test(typeRaw) && styleRaw === 'default')) support_style = 'tree'
266
+ // Which tree. Upstream's slim / strong / hybrid run TreeSupport and organic runs TreeSupport3D
267
+ // (SupportParameters.hpp:190-204), and a tree type left on 'default' resolves to organic there. The kernel defaults to
268
+ // organic too, so only the three others are sent: an organic slice keeps exactly the parameters it had. Before this the
269
+ // style never reached the kernel and every tree style sliced organic.
270
+ const tree_style = { tree_slim: 'slim', tree_strong: 'strong', tree_hybrid: 'hybrid' }[styleRaw]
233
271
 
234
272
  // Bed: bounding box of the first rectangle in printable_area
235
273
  const pa = settingRaw(settings, 'printable_area')
@@ -309,6 +347,12 @@ export function deriveKernelParams(settings, opts) {
309
347
  if (Number.isFinite(value)) passthrough[key] = value
310
348
  }
311
349
  for (const key of PASSTHROUGH_BOOL) if (present(key)) passthrough[key] = bool(key, false)
350
+ for (const key of PASSTHROUGH_PERCENT) {
351
+ if (!present(key)) continue
352
+ const value = Number(String(S(key)).replace('%', ''))
353
+ if (Number.isFinite(value)) passthrough[key] = value
354
+ }
355
+ for (const key of PASSTHROUGH_STR) if (present(key)) passthrough[key] = String(S(key))
312
356
  for (const key of PASSTHROUGH_NUM_VECTOR) {
313
357
  if (!present(key)) continue
314
358
  const raw = settings[key]
@@ -330,6 +374,10 @@ export function deriveKernelParams(settings, opts) {
330
374
  }
331
375
  }
332
376
 
377
+ // Sent only for the three non-organic styles (see tree_style above), so an empty map still derives the same keys.
378
+ const treeStyle = {}
379
+ if (tree_style) treeStyle.tree_style = tree_style
380
+
333
381
  const perExtruder = {}
334
382
  for (const [param, key, scalar] of [
335
383
  ['extruder_nozzle_temp', 'nozzle_temperature', num('nozzle_temperature')],
@@ -352,6 +400,7 @@ export function deriveKernelParams(settings, opts) {
352
400
  ...widths,
353
401
  ...machine,
354
402
  ...passthrough, // present only for keys the settings map actually holds (see PASSTHROUGH_* above)
403
+ ...treeStyle,
355
404
  layer_height: num('layer_height'),
356
405
  first_layer_height: num('initial_layer_print_height'),
357
406
  line_width,
@@ -392,6 +441,7 @@ export function deriveKernelParams(settings, opts) {
392
441
  // to it would rewrite the emitted G-code for every caller. The kernel keeps its own preamble when these are empty.
393
442
  machine_start_gcode: String(settings?.machine_start_gcode ?? ''),
394
443
  machine_end_gcode: String(settings?.machine_end_gcode ?? ''),
444
+ ...placeholderConfig(settings, plate),
395
445
  enable_support: bool('enable_support'),
396
446
  support_threshold_angle: num('support_threshold_angle'),
397
447
  support_top_z_distance: num('support_top_z_distance'),
package/types/gcode.d.ts CHANGED
@@ -11,9 +11,35 @@ export interface GcodeLayer {
11
11
 
12
12
  export interface ParseGcodeResult {
13
13
  layers: GcodeLayer[]
14
- stats: { layers: number; path_segments: number; travel_segments: number; tools: number[] }
14
+ stats: {
15
+ layers: number; path_segments: number; travel_segments: number; filament_mm: number
16
+ /** Tool ids that extruded or were selected. Firmware opcodes above T254 (Bambu's T1000, T255, …) are not tools. */
17
+ tools: number[]
18
+ /** Filament length (mm of E) per tool id — the kernel's own stat name. */
19
+ filament_mm_by_tool: number[]
20
+ /** The file's own palette per tool (`; filament_colour` / `; extruder_colour`), null where it names none. */
21
+ colors?: (string | null)[]
22
+ }
15
23
  }
16
24
 
25
+ /**
26
+ * A result's colour list, one entry per tool: its own palette (`stats.colors`), then the session palette, then the
27
+ * categorical stand-in for a tool neither names. What the toolpath is drawn in and what an export states.
28
+ */
29
+ export function resultToolColors(
30
+ stats: { colors?: (string | null)[]; filament_mm_by_tool?: number[] } | null | undefined,
31
+ sessionColors?: string[] | null,
32
+ ): string[]
33
+
34
+ /** A result's G-code as it is saved: `withFilamentColours` over `resultToolColors`. */
35
+ export function exportedGcode(
36
+ result: { gcode: string; stats?: { colors?: (string | null)[]; filament_mm_by_tool?: number[] } },
37
+ sessionColors?: string[] | null,
38
+ ): string
39
+
40
+ /** Appends `; filament_colour = …` unless the text already states a palette. */
41
+ export function withFilamentColours(gcode: string, colors: (string | null | undefined)[]): string
42
+
17
43
  export interface ParseGcodeOptions {
18
44
  /** Used to derive bead width from E when the file has no ;WIDTH: comments. Default 1.75. */
19
45
  filamentDiameter?: number
@@ -0,0 +1,7 @@
1
+ // three-slicer-viewer/paint — the painting tool names the viewer and the slicer worker share.
2
+
3
+ /** The tools that fill on one click rather than paint along a drag: 'smart', 'bucket', 'triangle'. */
4
+ export const FILL_TOOLS: ReadonlySet<string>
5
+
6
+ /** The fills that spread across neighbouring facets and take a fill angle: 'smart', 'bucket'. */
7
+ export const ANGLE_FILL_TOOLS: ReadonlySet<string>
package/types/viewer.d.ts CHANGED
@@ -81,8 +81,10 @@ export interface ViewportProps {
81
81
  /**
82
82
  * G-code text to render instead of a slice result. Parsed into the same layer stream the kernel produces and
83
83
  * shown on the selected plate; the kernel is never started. While it is set, auto re-slice leaves that plate alone.
84
+ * A `{ plateIndex: text }` map puts each text on its own (0-based) plate, adding plates as needed — the shape an
85
+ * opened `.gcode.3mf` takes. This prop wins over a `.gcode.3mf` the user opens while it is set.
84
86
  */
85
- gcode?: string | null
87
+ gcode?: string | Record<number, string> | null
86
88
  /**
87
89
  * An `.sl1` archive rendered as a raster preview — the SLA half of {@link ViewportProps.gcode}'s contract: it
88
90
  * lands on the selected plate and the kernel is never started. Set both and the G-code wins; one plate holds
@@ -167,7 +169,7 @@ export interface ViewportProps {
167
169
  * way: OrbitControls preventDefaults `contextmenu` itself.
168
170
  * - `logs` — console diagnostics, from the component and from the slice worker.
169
171
  */
170
- export type ViewportFeature = 'shortcuts' | 'warmup' | 'drop' | 'filePicker' | 'contextMenu' | 'logs'
172
+ export type ViewportFeature = 'shortcuts' | 'warmup' | 'drop' | 'filePicker' | 'contextMenu' | 'logs' | 'versionBadge'
171
173
 
172
174
  /**
173
175
  * The panels that accept `'readonly'` as well as `true`/`false` — the right column and the cards in it. Everything
@@ -1,188 +0,0 @@
1
- import * as x from "three";
2
- import { OBJLoader as S } from "three/examples/jsm/loaders/OBJLoader.js";
3
- import { PLYLoader as v } from "three/examples/jsm/loaders/PLYLoader.js";
4
- import { AMFLoader as P } from "three/examples/jsm/loaders/AMFLoader.js";
5
- import { p as T } from "./parse_3mf-jEUAw8JM.js";
6
- import { makeParse3mfWorker as B } from "./make_worker.js";
7
- const W = 80, M = 84, _ = 50, $ = (t) => M + t * _;
8
- let g = null, E = !1, b = 0;
9
- const h = /* @__PURE__ */ new Map();
10
- function D() {
11
- if (E || typeof Worker > "u") return null;
12
- if (g) return g;
13
- try {
14
- g = B(), g.onmessage = (t) => {
15
- const { id: r, objects: s, project: o, error: n } = t.data || {}, i = h.get(r);
16
- i && (h.delete(r), n ? i.reject(new Error(n)) : i.resolve({ objects: s, project: o }));
17
- }, g.onerror = () => {
18
- E = !0;
19
- for (const [, t] of h) t.reject(new Error("3MF parse worker failed"));
20
- h.clear(), g = null;
21
- };
22
- } catch {
23
- E = !0, g = null;
24
- }
25
- return g;
26
- }
27
- async function I(t, r) {
28
- const s = D();
29
- if (!s) return T(t, r);
30
- try {
31
- const o = ++b, n = new Promise((i, c) => h.set(o, { resolve: i, reject: c }));
32
- return s.postMessage({ id: o, buffer: t, baseName: r }), await n;
33
- } catch {
34
- return T(t, r);
35
- }
36
- }
37
- const L = ["stl", "obj", "3mf", "amf", "ply"], j = /* @__PURE__ */ new Map();
38
- function H(t, r) {
39
- for (const s of (Array.isArray(t) ? t : String(t).split(",")).map((o) => o.trim().toLowerCase()))
40
- j.set(s, r), L.includes(s) || L.push(s);
41
- }
42
- function O(t) {
43
- const r = t.lastIndexOf(".");
44
- return r < 0 ? "" : t.slice(r + 1).toLowerCase();
45
- }
46
- function R(t) {
47
- const r = new DataView(t);
48
- if (t.byteLength >= M) {
49
- const c = r.getUint32(W, !0);
50
- if (t.byteLength === $(c)) {
51
- const l = new Float32Array(c * 9);
52
- let f = M, u = 0;
53
- for (let y = 0; y < c; y++) {
54
- f += 12;
55
- for (let e = 0; e < 3; e++)
56
- l[u++] = r.getFloat32(f, !0), l[u++] = r.getFloat32(f + 4, !0), l[u++] = r.getFloat32(f + 8, !0), f += 12;
57
- f += 2;
58
- }
59
- return l;
60
- }
61
- }
62
- const s = new TextDecoder().decode(t), o = /vertex\s+([-\d.eE+]+)\s+([-\d.eE+]+)\s+([-\d.eE+]+)/g, n = [];
63
- let i;
64
- for (; i = o.exec(s); ) n.push(+i[1], +i[2], +i[3]);
65
- return n.length < 9 ? null : new Float32Array(n);
66
- }
67
- function k(t, r) {
68
- const s = t?.attributes?.position;
69
- if (!s || s.count < 3) return null;
70
- const o = t.index, n = new x.Vector3(), i = Math.floor((o ? o.count : s.count) / 3), c = new Float32Array(i * 9);
71
- let l = 0;
72
- const f = (u) => {
73
- n.fromBufferAttribute(s, u), r && n.applyMatrix4(r), c[l++] = n.x, c[l++] = n.y, c[l++] = n.z;
74
- };
75
- if (o)
76
- for (let u = 0; u < i * 3; u++) f(o.getX(u));
77
- else
78
- for (let u = 0; u < i * 3; u++) f(u);
79
- return c;
80
- }
81
- function A(t) {
82
- t.updateMatrixWorld(!0);
83
- const r = [];
84
- return t.traverse((s) => {
85
- s.isMesh && s.geometry && r.push({ geometry: s.geometry, matrix: s.matrixWorld });
86
- }), r;
87
- }
88
- function F(t, r, s) {
89
- const o = r.map((l) => k(l.geometry, l.matrix)).filter(Boolean);
90
- if (!o.length) return [];
91
- if (s && o.length > 1)
92
- return o.map((l, f) => ({ name: `${t}#${f + 1}`, modelPos: l }));
93
- if (o.length === 1) return [{ name: t, modelPos: o[0] }];
94
- let n = 0;
95
- for (const l of o) n += l.length;
96
- const i = new Float32Array(n);
97
- let c = 0;
98
- for (const l of o)
99
- i.set(l, c), c += l.length;
100
- return [{ name: t, modelPos: i }];
101
- }
102
- async function V(t, r) {
103
- const s = O(t);
104
- switch (s) {
105
- case "stl": {
106
- const o = R(r);
107
- if (!o) throw new Error("Failed to parse STL");
108
- return [{ name: t, modelPos: o }];
109
- }
110
- case "obj": {
111
- const o = new S().parse(new TextDecoder().decode(r));
112
- return F(t, A(o), !1);
113
- }
114
- case "ply": {
115
- const o = new v().parse(r), n = k(o, null);
116
- if (!n) throw new Error("No PLY geometry");
117
- return [{ name: t, modelPos: n }];
118
- }
119
- case "3mf": {
120
- const { objects: o, project: n } = await I(r, t), i = o.map((c) => ({ name: c.name, modelPos: c.tris, objectid: c.objectid, paint: c.paint, bbox: c.bbox, project: n }));
121
- if (!i.length) throw new Error("No mesh in 3MF");
122
- return i;
123
- }
124
- case "amf": {
125
- const o = new P().parse(r), n = F(t, A(o), !0);
126
- if (!n.length) throw new Error("No mesh in AMF");
127
- return n;
128
- }
129
- default: {
130
- const o = j.get(s);
131
- if (!o) throw new Error(`Unsupported format: .${s}`);
132
- const n = await o(r, t);
133
- if (!n?.length) throw new Error(`No mesh in .${s}`);
134
- return n;
135
- }
136
- }
137
- }
138
- function X(t) {
139
- const r = Math.floor(t.length / 9);
140
- if (r < 2) return null;
141
- const s = 1e4, o = /* @__PURE__ */ new Map(), n = new Int32Array(r * 3);
142
- for (let e = 0; e < r; e++)
143
- for (let a = 0; a < 3; a++) {
144
- const p = e * 9 + a * 3, m = `${Math.round(t[p] * s)},${Math.round(t[p + 1] * s)},${Math.round(t[p + 2] * s)}`;
145
- let d = o.get(m);
146
- d === void 0 && (d = o.size, o.set(m, d)), n[e * 3 + a] = d;
147
- }
148
- const i = new Int32Array(r);
149
- for (let e = 0; e < r; e++) i[e] = e;
150
- const c = (e) => {
151
- for (; i[e] !== e; )
152
- i[e] = i[i[e]], e = i[e];
153
- return e;
154
- }, l = (e, a) => {
155
- e = c(e), a = c(a), e !== a && (i[a] = e);
156
- }, f = /* @__PURE__ */ new Map();
157
- for (let e = 0; e < r; e++)
158
- for (let a = 0; a < 3; a++) {
159
- const p = n[e * 3 + a], m = n[e * 3 + (a + 1) % 3], d = p < m ? p * 4294967296 + m : m * 4294967296 + p, w = f.get(d);
160
- w === void 0 ? f.set(d, e) : l(e, w);
161
- }
162
- const u = /* @__PURE__ */ new Map();
163
- for (let e = 0; e < r; e++) {
164
- const a = c(e);
165
- let p = u.get(a);
166
- p || (p = [], u.set(a, p)), p.push(e);
167
- }
168
- return u.size < 2 ? null : [...u.values()].sort((e, a) => a.length - e.length).map((e) => {
169
- const a = new Float32Array(e.length * 9);
170
- let p = 0;
171
- for (const m of e) {
172
- const d = m * 9;
173
- for (let w = 0; w < 9; w++) a[p++] = t[d + w];
174
- }
175
- return a;
176
- });
177
- }
178
- export {
179
- W as S,
180
- M as a,
181
- _ as b,
182
- L as c,
183
- X as d,
184
- O as f,
185
- V as l,
186
- H as r,
187
- $ as s
188
- };