three-cad-viewer 4.3.4 → 4.3.6

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.
Files changed (59) hide show
  1. package/dist/scene/clipping.d.ts +6 -0
  2. package/dist/three-cad-viewer.esm.js +20 -5
  3. package/dist/three-cad-viewer.esm.js.map +1 -1
  4. package/dist/three-cad-viewer.esm.min.js +1 -1
  5. package/dist/three-cad-viewer.js +20 -5
  6. package/dist/three-cad-viewer.min.js +1 -1
  7. package/package.json +2 -3
  8. package/src/_version.ts +0 -1
  9. package/src/camera/camera.ts +0 -445
  10. package/src/camera/controls/CADOrbitControls.ts +0 -241
  11. package/src/camera/controls/CADTrackballControls.ts +0 -598
  12. package/src/camera/controls.ts +0 -380
  13. package/src/core/patches.ts +0 -16
  14. package/src/core/studio-manager.ts +0 -652
  15. package/src/core/types.ts +0 -892
  16. package/src/core/viewer-state.ts +0 -784
  17. package/src/core/viewer.ts +0 -4821
  18. package/src/index.ts +0 -151
  19. package/src/rendering/environment.ts +0 -840
  20. package/src/rendering/light-detection.ts +0 -327
  21. package/src/rendering/material-factory.ts +0 -735
  22. package/src/rendering/material-presets.ts +0 -289
  23. package/src/rendering/raycast.ts +0 -291
  24. package/src/rendering/room-environment.ts +0 -192
  25. package/src/rendering/studio-composer.ts +0 -577
  26. package/src/rendering/studio-floor.ts +0 -108
  27. package/src/rendering/texture-cache.ts +0 -324
  28. package/src/rendering/tree-model.ts +0 -542
  29. package/src/rendering/triplanar.ts +0 -329
  30. package/src/scene/animation.ts +0 -343
  31. package/src/scene/axes.ts +0 -108
  32. package/src/scene/bbox.ts +0 -223
  33. package/src/scene/clipping.ts +0 -640
  34. package/src/scene/grid.ts +0 -864
  35. package/src/scene/nestedgroup.ts +0 -1444
  36. package/src/scene/objectgroup.ts +0 -866
  37. package/src/scene/orientation.ts +0 -259
  38. package/src/scene/render-shape.ts +0 -634
  39. package/src/tools/cad_tools/measure.ts +0 -811
  40. package/src/tools/cad_tools/select.ts +0 -100
  41. package/src/tools/cad_tools/tools.ts +0 -231
  42. package/src/tools/cad_tools/ui.ts +0 -454
  43. package/src/tools/cad_tools/zebra.ts +0 -369
  44. package/src/types/html.d.ts +0 -5
  45. package/src/types/n8ao.d.ts +0 -28
  46. package/src/types/three-augmentation.d.ts +0 -60
  47. package/src/ui/display.ts +0 -3295
  48. package/src/ui/index.html +0 -505
  49. package/src/ui/info.ts +0 -177
  50. package/src/ui/slider.ts +0 -206
  51. package/src/ui/toolbar.ts +0 -347
  52. package/src/ui/treeview.ts +0 -945
  53. package/src/utils/decode-instances.ts +0 -233
  54. package/src/utils/font.ts +0 -60
  55. package/src/utils/gpu-tracker.ts +0 -265
  56. package/src/utils/logger.ts +0 -92
  57. package/src/utils/sizeof.ts +0 -116
  58. package/src/utils/timer.ts +0 -69
  59. package/src/utils/utils.ts +0 -446
@@ -1,324 +0,0 @@
1
- import * as THREE from "three";
2
- import { gpuTracker } from "../utils/gpu-tracker.js";
3
- import { logger } from "../utils/logger.js";
4
-
5
- // =============================================================================
6
- // Constants
7
- // =============================================================================
8
-
9
- /**
10
- * Texture fields that carry sRGB color data.
11
- *
12
- * When a texture is used for one of these roles, its `colorSpace` must be set
13
- * to `SRGBColorSpace` so Three.js applies the sRGB-to-linear decode on
14
- * sampling. All other texture roles (normal, metallic-roughness, occlusion,
15
- * thickness, transmission, roughness maps) remain `LinearSRGBColorSpace`.
16
- */
17
- const SRGB_TEXTURE_ROLES = new Set([
18
- "baseColorTexture",
19
- "emissiveTexture",
20
- "sheenColorTexture",
21
- "specularColorTexture",
22
- ]);
23
-
24
- /**
25
- * Three.js MeshPhysicalMaterial map property names that carry sRGB color data.
26
- *
27
- * Used by threejs-materials integration where texture params use Three.js property
28
- * names directly (e.g., "map", "emissiveMap") instead of MaterialAppearance
29
- * role names (e.g., "baseColorTexture", "emissiveTexture").
30
- */
31
- const THREEJS_SRGB_MAPS = new Set([
32
- "map",
33
- "emissiveMap",
34
- "sheenColorMap",
35
- "specularColorMap",
36
- ]);
37
-
38
- // =============================================================================
39
- // TextureCache
40
- // =============================================================================
41
-
42
- /**
43
- * Manages loading, caching, and lifecycle of all Studio mode textures.
44
- *
45
- * The TextureCache is the **sole owner** of all THREE.Texture objects used by
46
- * Studio mode. Materials reference textures but never dispose them directly.
47
- * Only TextureCache.dispose() / disposeFull() disposes GPU texture resources.
48
- *
49
- * Resolution order for texture reference strings:
50
- * 1. `data:` prefix -- treat as data URI, load directly
51
- * 2. Otherwise -- treat as URL, resolve relative to HTML page
52
- *
53
- * Features:
54
- * - In-flight promise deduplication (no duplicate loads for the same key)
55
- * - Correct colorSpace assignment per texture semantic role
56
- * - GPU resource tracking via gpuTracker
57
- */
58
- class TextureCache {
59
- /** Textures cache (disposed on clear/dispose, rebuilt per shape data) */
60
- private _cache: Map<string, THREE.Texture> = new Map();
61
-
62
- /** In-flight load promises keyed by cache key */
63
- private _inflight: Map<string, Promise<THREE.Texture>> = new Map();
64
-
65
- /** THREE.TextureLoader instance (created lazily) */
66
- private _textureLoader: THREE.TextureLoader | null = null;
67
-
68
- /** Whether this cache has been fully disposed */
69
- private _disposed = false;
70
-
71
- /** Max anisotropic filtering level.
72
- * Default 16 covers most GPUs; clamped by the driver if unsupported. */
73
- maxAnisotropy = 16;
74
-
75
- // ---------------------------------------------------------------------------
76
- // Public API
77
- // ---------------------------------------------------------------------------
78
-
79
- /**
80
- * Resolve a texture reference string and return a cached or newly loaded
81
- * THREE.Texture with the correct colorSpace set.
82
- *
83
- * @param ref - Texture reference string (table key, data URI, or URL)
84
- * @param textureRole - The texture role name (MaterialAppearance field name or proxy role)
85
- * (e.g. "baseColorTexture", "normalTexture"). Used to determine colorSpace.
86
- * @returns The resolved THREE.Texture, or null if the reference is invalid
87
- * or loading fails
88
- */
89
- async get(
90
- ref: string,
91
- textureRole: string,
92
- ): Promise<THREE.Texture | null> {
93
- if (this._disposed) {
94
- logger.warn("TextureCache.get() called after dispose");
95
- return null;
96
- }
97
-
98
- if (!ref) {
99
- return null;
100
- }
101
-
102
- // Determine the target color space based on the texture role
103
- const colorSpace = SRGB_TEXTURE_ROLES.has(textureRole)
104
- ? THREE.SRGBColorSpace
105
- : THREE.LinearSRGBColorSpace;
106
-
107
- // 1. data: prefix (data URI)
108
- if (ref.startsWith("data:")) {
109
- return this._getFromDataUri(ref, colorSpace);
110
- }
111
-
112
- // 2. URL (relative to HTML page)
113
- return this._getFromUrl(ref, colorSpace);
114
- }
115
-
116
- /**
117
- * Dispose textures (called on viewer.clear() when shape data is replaced).
118
- *
119
- * Disposes all textures in the cache and clears in-flight promises.
120
- */
121
- dispose(): void {
122
- for (const [key, texture] of this._cache) {
123
- gpuTracker.untrack("texture", texture);
124
- texture.dispose();
125
- logger.debug(`Disposed texture: ${key}`);
126
- }
127
- this._cache.clear();
128
-
129
- // Clear in-flight promises (they may resolve but won't be used)
130
- this._inflight.clear();
131
- }
132
-
133
- /**
134
- * Dispose all textures.
135
- *
136
- * Called on viewer.dispose() when the viewer is fully torn down.
137
- * After this call, the TextureCache cannot be used again.
138
- */
139
- disposeFull(): void {
140
- this._disposed = true;
141
- this.dispose();
142
- this._textureLoader = null;
143
- logger.debug("TextureCache fully disposed");
144
- }
145
-
146
- // ---------------------------------------------------------------------------
147
- // Private: Data URI loading
148
- // ---------------------------------------------------------------------------
149
-
150
- /**
151
- * Load a texture from a data URI string.
152
- */
153
- private async _getFromDataUri(
154
- dataUri: string,
155
- colorSpace: THREE.ColorSpace,
156
- ): Promise<THREE.Texture | null> {
157
- // Use the data URI itself as the cache key
158
- const cacheKey = dataUri;
159
-
160
- const cached = this._cache.get(cacheKey);
161
- if (cached) {
162
- cached.colorSpace = colorSpace;
163
- return cached;
164
- }
165
-
166
- const inflight = this._inflight.get(cacheKey);
167
- if (inflight) {
168
- const texture = await inflight;
169
- texture.colorSpace = colorSpace;
170
- return texture;
171
- }
172
-
173
- return this._loadAndCache(cacheKey, dataUri, colorSpace);
174
- }
175
-
176
- // ---------------------------------------------------------------------------
177
- // Private: URL loading
178
- // ---------------------------------------------------------------------------
179
-
180
- /**
181
- * Load a texture from a URL (resolved relative to the HTML page).
182
- */
183
- private async _getFromUrl(
184
- url: string,
185
- colorSpace: THREE.ColorSpace,
186
- ): Promise<THREE.Texture | null> {
187
- const cached = this._cache.get(url);
188
- if (cached) {
189
- cached.colorSpace = colorSpace;
190
- return cached;
191
- }
192
-
193
- const inflight = this._inflight.get(url);
194
- if (inflight) {
195
- const texture = await inflight;
196
- texture.colorSpace = colorSpace;
197
- return texture;
198
- }
199
-
200
- return this._loadAndCache(url, url, colorSpace);
201
- }
202
-
203
- // ---------------------------------------------------------------------------
204
- // Private: Core loading
205
- // ---------------------------------------------------------------------------
206
-
207
- /**
208
- * Load a texture from a source (URL or data URI), cache it, and return it.
209
- *
210
- * Deduplicates in-flight loads for the same cache key.
211
- *
212
- * @param cacheKey - Key for the user cache
213
- * @param source - URL or data URI to load from
214
- * @param colorSpace - Color space to assign to the loaded texture
215
- * @returns The loaded texture, or null on failure
216
- */
217
- private async _loadAndCache(
218
- cacheKey: string,
219
- source: string,
220
- colorSpace: THREE.ColorSpace,
221
- ): Promise<THREE.Texture | null> {
222
- const promise = this._doLoad(source, colorSpace);
223
- this._inflight.set(cacheKey, promise);
224
-
225
- try {
226
- const texture = await promise;
227
-
228
- // Check if disposed while loading:
229
- // - disposeFull() sets _disposed
230
- // - dispose() clears _inflight (so our entry is gone)
231
- if (this._disposed || !this._inflight.has(cacheKey)) {
232
- texture.dispose();
233
- return null;
234
- }
235
-
236
- // Cache and track
237
- this._cache.set(cacheKey, texture);
238
- const label = cacheKey.startsWith("data:")
239
- ? `Texture (data URI, ${cacheKey.length} chars)`
240
- : `Texture: ${cacheKey}`;
241
- gpuTracker.trackTexture(texture, label);
242
- logger.debug(`Loaded and cached texture: ${label}`);
243
-
244
- return texture;
245
- } catch (error) {
246
- if (this._disposed) return null;
247
-
248
- const displayKey = cacheKey.startsWith("data:")
249
- ? `data URI (${cacheKey.length} chars)`
250
- : cacheKey;
251
- logger.warn(
252
- `Failed to load texture "${displayKey}":`,
253
- error instanceof Error ? error.message : error,
254
- );
255
- return null;
256
- } finally {
257
- this._inflight.delete(cacheKey);
258
- }
259
- }
260
-
261
- /**
262
- * Perform the actual texture load via THREE.TextureLoader.
263
- *
264
- * THREE.TextureLoader handles both URLs and data URIs.
265
- */
266
- private _doLoad(
267
- source: string,
268
- colorSpace: THREE.ColorSpace,
269
- ): Promise<THREE.Texture> {
270
- const loader = this._ensureTextureLoader();
271
-
272
- return new Promise<THREE.Texture>((resolve, reject) => {
273
- loader.load(
274
- source,
275
- (texture) => {
276
- texture.colorSpace = colorSpace;
277
- texture.wrapS = THREE.RepeatWrapping;
278
- texture.wrapT = THREE.RepeatWrapping;
279
- texture.anisotropy = this.maxAnisotropy;
280
- resolve(texture);
281
- },
282
- undefined, // onProgress (not used)
283
- (error) => {
284
- reject(
285
- error instanceof Error
286
- ? error
287
- : new Error(`Texture load failed: ${source.substring(0, 100)}`),
288
- );
289
- },
290
- );
291
- });
292
- }
293
-
294
- // ---------------------------------------------------------------------------
295
- // Private: Utilities
296
- // ---------------------------------------------------------------------------
297
-
298
- /**
299
- * Get or create the THREE.TextureLoader (lazy initialization).
300
- */
301
- private _ensureTextureLoader(): THREE.TextureLoader {
302
- if (!this._textureLoader) {
303
- this._textureLoader = new THREE.TextureLoader();
304
- logger.debug("Created TextureLoader");
305
- }
306
- return this._textureLoader;
307
- }
308
-
309
- }
310
-
311
- /**
312
- * Get the correct color space for a Three.js material map property name.
313
- *
314
- * sRGB maps (color data): map, emissiveMap, sheenColorMap, specularColorMap
315
- * Linear maps (non-color data): everything else (normalMap, roughnessMap, etc.)
316
- *
317
- * @param mapName - Three.js material property name (e.g., "map", "normalMap")
318
- * @returns THREE.SRGBColorSpace or THREE.LinearSRGBColorSpace
319
- */
320
- function getColorSpaceForMap(mapName: string): THREE.ColorSpace {
321
- return THREEJS_SRGB_MAPS.has(mapName) ? THREE.SRGBColorSpace : THREE.LinearSRGBColorSpace;
322
- }
323
-
324
- export { TextureCache, SRGB_TEXTURE_ROLES, getColorSpaceForMap };