@vctrl/hooks 0.9.5 → 0.16.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.
Files changed (71) hide show
  1. package/CHANGELOG.md +380 -0
  2. package/README.md +202 -383
  3. package/package.json +66 -55
  4. package/project.json +55 -0
  5. package/src/index.ts +38 -0
  6. package/src/use-export-model/use-export-model.ts +113 -0
  7. package/src/use-load-model/event-system.ts +46 -0
  8. package/src/use-load-model/index.ts +15 -0
  9. package/src/use-load-model/model-context.tsx +174 -0
  10. package/src/use-load-model/state.ts +75 -0
  11. package/src/use-load-model/types.ts +284 -0
  12. package/src/use-load-model/use-load-model.ts +682 -0
  13. package/src/use-load-model/utils/calculate-referenced-bytes.ts +123 -0
  14. package/src/use-load-model/utils/index.ts +7 -0
  15. package/src/use-load-model/utils/read-directory.ts +35 -0
  16. package/src/use-load-model/utils/reconstruct-files.ts +85 -0
  17. package/src/use-load-model/utils/resolve-scene-payload.ts +151 -0
  18. package/{use-optimize-model/index.d.ts → src/use-optimize-model/index.ts} +2 -1
  19. package/src/use-optimize-model/state.ts +40 -0
  20. package/src/use-optimize-model/types.ts +37 -0
  21. package/src/use-optimize-model/use-calc-optimization-info.ts +101 -0
  22. package/src/use-optimize-model/use-optimize-model.ts +467 -0
  23. package/src/use-optimize-model/utils/index.ts +29 -0
  24. package/src/use-optimize-model/utils/texture-optimization.ts +292 -0
  25. package/src/use-optimize-model/utils/validation.ts +62 -0
  26. package/src/utils/server-communication.ts +351 -0
  27. package/tsconfig.json +21 -0
  28. package/tsconfig.lib.json +16 -0
  29. package/vite.config.ts +77 -0
  30. package/GLTFExporter-BmLF430n.cjs +0 -18
  31. package/GLTFExporter-C4hCeb4H.js +0 -1263
  32. package/index.cjs.js +0 -1
  33. package/index.d.ts +0 -3
  34. package/index.es.js +0 -12
  35. package/model-context-CQJta_vi.cjs +0 -9
  36. package/model-context-D1Q8WdpT.js +0 -3431
  37. package/use-export-model/types.d.ts +0 -19
  38. package/use-export-model/use-export-model.d.ts +0 -14
  39. package/use-export-model/utils/data-uri-to-blob.d.ts +0 -9
  40. package/use-export-model/utils/export-handlers.d.ts +0 -23
  41. package/use-export-model/utils/file-helpers.d.ts +0 -17
  42. package/use-export-model/utils/index.d.ts +0 -3
  43. package/use-export-model-AFharGg5.js +0 -77
  44. package/use-export-model-CWcKp4A3.cjs +0 -1
  45. package/use-export-model.cjs.js +0 -1
  46. package/use-export-model.es.js +0 -4
  47. package/use-load-model/event-system.d.ts +0 -7
  48. package/use-load-model/file-type-hooks/index.d.ts +0 -2
  49. package/use-load-model/file-type-hooks/use-load-binary.d.ts +0 -11
  50. package/use-load-model/file-type-hooks/use-load-gltf.d.ts +0 -5
  51. package/use-load-model/index.d.ts +0 -3
  52. package/use-load-model/loaders/create-gltf-loader.d.ts +0 -3
  53. package/use-load-model/loaders/create-usdz-loader.d.ts +0 -3
  54. package/use-load-model/loaders/index.d.ts +0 -2
  55. package/use-load-model/model-context.d.ts +0 -78
  56. package/use-load-model/state.d.ts +0 -19
  57. package/use-load-model/types.d.ts +0 -51
  58. package/use-load-model/use-load-model.d.ts +0 -74
  59. package/use-load-model/utils/array-buffer-to-base64.d.ts +0 -2
  60. package/use-load-model/utils/index.d.ts +0 -2
  61. package/use-load-model/utils/read-directory.d.ts +0 -8
  62. package/use-load-model.cjs.js +0 -1
  63. package/use-load-model.es.js +0 -8
  64. package/use-optimize-model/state.d.ts +0 -13
  65. package/use-optimize-model/types.d.ts +0 -37
  66. package/use-optimize-model/use-optimize-model.d.ts +0 -29
  67. package/use-optimize-model-Cqkp__Vi.cjs +0 -1
  68. package/use-optimize-model-DZWIb_40.js +0 -127
  69. package/use-optimize-model.cjs.js +0 -1
  70. package/use-optimize-model.es.js +0 -4
  71. /package/{use-export-model/index.d.ts → src/use-export-model/index.ts} +0 -0
@@ -0,0 +1,284 @@
1
+ /* vectreal-core | vctrl/hooks
2
+ Copyright (C) 2024 Moritz Becker
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Affero General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Affero General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Affero General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
+
17
+ import { ModelFileTypes } from '@vctrl/core/model-loader'
18
+ import { Object3D } from 'three'
19
+
20
+ import { useOptimizeModel } from '../use-optimize-model'
21
+ import eventSystem from './event-system'
22
+ import { initialState } from './state'
23
+
24
+ import type { ServerOptions, ServerSceneData } from '@vctrl/core'
25
+
26
+ export type {
27
+ ExtendedGLTFDocument,
28
+ SceneAssetDataEntry,
29
+ SerializedSceneAssetDataMap as SceneAssetDataMap,
30
+ ServerSceneData,
31
+ ServerScenePayload
32
+ } from '@vctrl/core'
33
+
34
+ /**
35
+ * Type representing the input for file/folder uploads.
36
+ * Can be either File objects or FileSystemDirectoryHandle for folder drag-and-drop.
37
+ */
38
+ export type InputFileOrDirectory = (File | FileSystemDirectoryHandle)[]
39
+
40
+ /**
41
+ * Represents a loaded 3D model file with its metadata.
42
+ */
43
+ export interface ModelFile {
44
+ /** The Three.js Object3D scene containing the loaded model */
45
+ model: Object3D
46
+ /** The file type/format of the model (GLTF, GLB, USDZ) */
47
+ type: ModelFileTypes
48
+ /** The original filename of the model */
49
+ name: string
50
+ /** Byte size of referenced source package (gltf + referenced buffers/images) */
51
+ sourcePackageBytes?: number
52
+ /** Byte size of referenced texture assets from source package */
53
+ sourceTextureBytes?: number
54
+ }
55
+
56
+ /**
57
+ * Configuration options for loading a scene from the server.
58
+ */
59
+ export interface SceneLoadOptions {
60
+ /** The unique identifier of the scene to load */
61
+ sceneId: string
62
+ /** Server configuration (endpoint, auth, headers) */
63
+ serverOptions?: ServerOptions
64
+ /** Whether to automatically apply scene settings (default: true) */
65
+ applySettings?: boolean
66
+ }
67
+
68
+ /**
69
+ * Configuration options for loading an already-resolved scene payload.
70
+ */
71
+ export interface SceneDataLoadOptions {
72
+ /** Optional scene identifier for resolved payloads. */
73
+ sceneId?: string
74
+ /** Resolved server scene payload */
75
+ sceneData: ServerSceneData
76
+ /** Whether to automatically apply scene settings (default: true) */
77
+ applySettings?: boolean
78
+ }
79
+
80
+ /**
81
+ * Result of a scene load operation based on server data.
82
+ * Combines the loaded model with its settings.
83
+ */
84
+ export interface SceneLoadResult extends ServerSceneData {
85
+ /** The loaded model file */
86
+ file: ModelFile
87
+ /** Scene ID that was loaded */
88
+ sceneId?: string
89
+ }
90
+
91
+ /**
92
+ * State interface for model loading data.
93
+ * Contains the current loaded file, loading status, and progress information.
94
+ */
95
+ export interface LoadData {
96
+ /** The currently loaded model file, or null if no model is loaded */
97
+ file: ModelFile | null
98
+ /** Whether a file is currently being loaded */
99
+ isFileLoading: boolean
100
+ /** Loading progress percentage (0-100) */
101
+ progress: number
102
+ /** List of supported model file types */
103
+ supportedFileTypes: ModelFileTypes[]
104
+ }
105
+
106
+ /**
107
+ * Action types for the model loading reducer.
108
+ * Defines all possible state mutations for model loading operations.
109
+ */
110
+ export type Action =
111
+ | { type: 'set-file'; payload: ModelFile }
112
+ | { type: 'set-file-loading'; payload: boolean }
113
+ | { type: 'set-progress'; payload: number }
114
+ | { type: 'reset-state' }
115
+
116
+ /**
117
+ * Available event types emitted by the model loading system.
118
+ * Used for subscribing to various stages of the loading process.
119
+ */
120
+ export type EventTypes =
121
+ | 'multiple-models' // Emitted when multiple model files are detected in upload
122
+ | 'not-loaded-files' // Emitted when no supported files are found
123
+ | 'load-start' // Emitted when loading begins
124
+ | 'load-progress' // Emitted during loading progress updates
125
+ | 'load-complete' // Emitted when loading successfully completes
126
+ | 'load-reset' // Emitted when the state is reset
127
+ | 'load-error' // Emitted when an error occurs during loading
128
+ | 'server-load-start' // Emitted when server-based scene loading begins
129
+ | 'server-load-complete' // Emitted when server-based scene loading completes
130
+ | 'server-load-error' // Emitted when server-based scene loading fails
131
+
132
+ /**
133
+ * Maps event types to their corresponding data payloads.
134
+ * Ensures type safety when handling events.
135
+ */
136
+ export type EventData = {
137
+ /** Array of model files when multiple are detected */
138
+ 'multiple-models': File[]
139
+ /** Array of unsupported files */
140
+ 'not-loaded-files': File[]
141
+ /** No data for load start event */
142
+ 'load-start': null
143
+ /** Progress value (0-100) */
144
+ 'load-progress': number
145
+ /** The loaded model file data */
146
+ 'load-complete': LoadData['file']
147
+ /** No data for reset event */
148
+ 'load-reset': null
149
+ /** Error object when loading fails */
150
+ 'load-error': Error | unknown
151
+ /** Scene ID being loaded from server */
152
+ 'server-load-start': string
153
+ /** Complete scene load result */
154
+ 'server-load-complete': SceneLoadResult
155
+ /** Error during server scene loading */
156
+ 'server-load-error': Error | unknown
157
+ }
158
+
159
+ /**
160
+ * Type-safe event handler function.
161
+ * @template T - The event type being handled
162
+ */
163
+ export type EventHandler<T extends EventTypes> = (data?: EventData[T]) => void
164
+
165
+ /**
166
+ * Return type for the useLoadModel hook.
167
+ * Conditionally includes optimizer integration based on whether an optimizer was provided.
168
+ *
169
+ * @template HasOptimizer - Boolean indicating if optimizer integration is included
170
+ */
171
+ export type UseLoadModelReturn<HasOptimizer extends boolean> =
172
+ typeof initialState & {
173
+ /**
174
+ * Subscribe to model loading events.
175
+ * @param event - The event type to listen for
176
+ * @param handler - Callback function to handle the event
177
+ */
178
+ on: typeof eventSystem.on
179
+ /**
180
+ * Unsubscribe from model loading events.
181
+ * @param event - The event type to stop listening for
182
+ * @param handler - The callback function to remove
183
+ */
184
+ off: typeof eventSystem.off
185
+ /**
186
+ * Load 3D model files from File objects or directory handles.
187
+ * Supports GLTF, GLB, and USDZ formats with associated assets.
188
+ */
189
+ load: (filesOrDirectories: InputFileOrDirectory) => Promise<void>
190
+ /**
191
+ * Load a scene from already-resolved scene data.
192
+ * Preserves original asset payload semantics and bypasses upload-style file processing.
193
+ */
194
+ loadFromData: (options: SceneDataLoadOptions) => Promise<SceneLoadResult>
195
+ /**
196
+ * Load a scene from the server by scene ID.
197
+ * Fetches both the model and scene settings, applies them automatically.
198
+ *
199
+ * @param options - Scene loading configuration
200
+ * @returns Promise resolving to the loaded scene data
201
+ *
202
+ * @example
203
+ * ```tsx
204
+ * const model = useLoadModel()
205
+ *
206
+ * // Load a scene from the server
207
+ * const scene = await model.loadFromServer({
208
+ * sceneId: 'abc-123',
209
+ * serverOptions: {
210
+ * endpoint: '/api/load-scene',
211
+ * apiKey: 'optional-auth-token'
212
+ * }
213
+ * })
214
+ * ```
215
+ */
216
+ loadFromServer: (options: SceneLoadOptions) => Promise<SceneLoadResult>
217
+ /**
218
+ * Reset the model loading state and clear any loaded models.
219
+ */
220
+ reset: () => void
221
+ /**
222
+ * Optimizer integration object.
223
+ * - When optimizer is provided: Contains full optimization methods and state
224
+ * - When no optimizer: null
225
+ */
226
+ optimizer: HasOptimizer extends true
227
+ ? OptimizerIntegrationReturn<true>
228
+ : null
229
+ }
230
+
231
+ /**
232
+ * Return type of the useOptimizeModel hook.
233
+ * Provides access to all optimization methods and state.
234
+ */
235
+ export type OptimizerReturnType = ReturnType<typeof useOptimizeModel>
236
+
237
+ /**
238
+ * Conditional return type for optimizer integration.
239
+ * Extends the optimizer with additional integration methods when present.
240
+ *
241
+ * @template HasOptimizer - Boolean indicating if optimizer is integrated
242
+ */
243
+ export type OptimizerIntegrationReturn<HasOptimizer extends boolean = false> =
244
+ HasOptimizer extends true
245
+ ? OptimizerReturnType & {
246
+ /**
247
+ * Whether the optimizer is still preparing the currently visible model.
248
+ */
249
+ isPreparing: boolean
250
+
251
+ /**
252
+ * Applies an optimization and updates the loaded model with the result.
253
+ *
254
+ * This method:
255
+ * 1. Runs the specified optimization function
256
+ * 2. Retrieves the optimized model
257
+ * 3. Loads it back into the scene
258
+ * 4. Updates the model state
259
+ *
260
+ * @template TOptions - Type of options for the optimization function
261
+ * @param optimizationFunction - The optimization to apply (e.g., simplifyOptimization)
262
+ * @param options - Configuration options for the optimization
263
+ * @returns Promise that resolves when optimization is complete and model is updated
264
+ *
265
+ * @example
266
+ * // Apply simplification optimization
267
+ * await optimizer.applyOptimization(
268
+ * optimizer.simplifyOptimization,
269
+ * { ratio: 0.5 }
270
+ * )
271
+ *
272
+ * @example
273
+ * // Apply multiple optimizations in sequence
274
+ * await optimizer.applyOptimization(optimizer.dedupOptimization)
275
+ * await optimizer.applyOptimization(optimizer.quantizeOptimization, { bits: 12 })
276
+ */
277
+ applyOptimization: <TOptions>(
278
+ optimizationFunction?:
279
+ | ((options?: TOptions) => Promise<void>)
280
+ | undefined,
281
+ options?: TOptions
282
+ ) => Promise<void>
283
+ }
284
+ : null