@vctrl/hooks 0.16.2 → 0.17.0
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.
- package/CHANGELOG.md +14 -0
- package/GLTFLoader-CygFefRr.cjs +1 -0
- package/GLTFLoader-NiJ7qzh1.js +1381 -0
- package/README.md +0 -20
- package/__vite-browser-external-Bn_csQuM.js +7 -0
- package/__vite-browser-external-CVmj8TrW.cjs +1 -0
- package/chunk-foFoljWS.js +23 -0
- package/chunk-gGpyby2o.cjs +1 -0
- package/index.cjs.js +1 -0
- package/index.d.ts +5 -0
- package/index.es.js +4 -0
- package/lib-DKn2MJ9X.js +4111 -0
- package/lib-DZ-MihdJ.cjs +2 -0
- package/model-exporter-SMSAY90N.cjs +1 -0
- package/model-exporter-cvguDGnn.js +6512 -0
- package/package.json +1 -1
- package/server-communication-Bj6ZxFw2.js +4659 -0
- package/server-communication-yXZR8eso.cjs +18 -0
- package/use-export-model/use-export-model.d.ts +14 -0
- package/use-export-model-CaijtW1c.cjs +1 -0
- package/use-export-model-DzVKdqnY.js +49 -0
- package/use-export-model.cjs.js +1 -0
- package/use-export-model.es.js +2 -0
- package/use-load-model/event-system.d.ts +7 -0
- package/use-load-model/index.d.ts +4 -0
- package/use-load-model/state.d.ts +19 -0
- package/use-load-model/types.d.ts +231 -0
- package/use-load-model/use-load-model.d.ts +30 -0
- package/use-load-model/utils/calculate-referenced-bytes.d.ts +8 -0
- package/use-load-model/utils/index.d.ts +4 -0
- package/use-load-model/utils/read-directory.d.ts +8 -0
- package/use-load-model/utils/reconstruct-files.d.ts +29 -0
- package/use-load-model/utils/resolve-scene-payload.d.ts +6 -0
- package/use-load-model-B6UXiFWh.js +517 -0
- package/use-load-model-CH-WPQ_s.cjs +1 -0
- package/use-load-model.cjs.js +1 -0
- package/use-load-model.es.js +18 -0
- package/{src/use-optimize-model/index.ts → use-optimize-model/index.d.ts} +2 -2
- package/use-optimize-model/state.d.ts +13 -0
- package/use-optimize-model/types.d.ts +39 -0
- package/use-optimize-model/use-calc-optimization-info.d.ts +6 -0
- package/use-optimize-model/use-optimize-model.d.ts +119 -0
- package/use-optimize-model/utils/index.d.ts +2 -0
- package/use-optimize-model/utils/texture-optimization.d.ts +17 -0
- package/use-optimize-model/utils/validation.d.ts +8 -0
- package/use-optimize-model-C-fzzKh6.cjs +1 -0
- package/use-optimize-model-Cb3WMvDZ.js +299 -0
- package/use-optimize-model.cjs.js +1 -0
- package/use-optimize-model.es.js +2 -0
- package/utils/server-communication.d.ts +122 -0
- package/project.json +0 -55
- package/src/index.ts +0 -38
- package/src/use-export-model/use-export-model.ts +0 -113
- package/src/use-load-model/event-system.ts +0 -46
- package/src/use-load-model/index.ts +0 -15
- package/src/use-load-model/model-context.tsx +0 -174
- package/src/use-load-model/state.ts +0 -75
- package/src/use-load-model/types.ts +0 -284
- package/src/use-load-model/use-load-model.ts +0 -682
- package/src/use-load-model/utils/calculate-referenced-bytes.ts +0 -123
- package/src/use-load-model/utils/index.ts +0 -7
- package/src/use-load-model/utils/read-directory.ts +0 -35
- package/src/use-load-model/utils/reconstruct-files.ts +0 -85
- package/src/use-load-model/utils/resolve-scene-payload.ts +0 -151
- package/src/use-optimize-model/state.ts +0 -40
- package/src/use-optimize-model/types.ts +0 -37
- package/src/use-optimize-model/use-calc-optimization-info.ts +0 -101
- package/src/use-optimize-model/use-optimize-model.ts +0 -467
- package/src/use-optimize-model/utils/index.ts +0 -29
- package/src/use-optimize-model/utils/texture-optimization.ts +0 -292
- package/src/use-optimize-model/utils/validation.ts +0 -62
- package/src/utils/server-communication.ts +0 -351
- package/tsconfig.json +0 -21
- package/tsconfig.lib.json +0 -16
- package/vite.config.ts +0 -77
- /package/{src/use-export-model/index.ts → use-export-model/index.d.ts} +0 -0
package/src/index.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
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
|
-
// Export hooks
|
|
18
|
-
export { default as useLoadModel } from './use-load-model/use-load-model'
|
|
19
|
-
export { default as useExportModel } from './use-export-model/use-export-model'
|
|
20
|
-
export { default as useOptimizeModel } from './use-optimize-model/use-optimize-model'
|
|
21
|
-
|
|
22
|
-
// Export utilities
|
|
23
|
-
export { reconstructGltfFiles } from './use-load-model/utils/reconstruct-files'
|
|
24
|
-
export { ServerCommunicationService } from './utils/server-communication'
|
|
25
|
-
export type { ServerRequestConfig } from './utils/server-communication'
|
|
26
|
-
|
|
27
|
-
// Re-export types for convenience
|
|
28
|
-
export type {
|
|
29
|
-
EventTypes,
|
|
30
|
-
EventHandler,
|
|
31
|
-
ModelFile,
|
|
32
|
-
LoadData,
|
|
33
|
-
UseLoadModelReturn,
|
|
34
|
-
SceneDataLoadOptions,
|
|
35
|
-
SceneLoadOptions,
|
|
36
|
-
ServerSceneData,
|
|
37
|
-
SceneLoadResult
|
|
38
|
-
} from './use-load-model/types'
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { Document } from '@gltf-transform/core'
|
|
2
|
-
import { ModelExporter } from '@vctrl/core/model-exporter'
|
|
3
|
-
import fileSaver from 'file-saver'
|
|
4
|
-
import { useCallback, useRef } from 'react'
|
|
5
|
-
|
|
6
|
-
import { ModelFile } from '../use-load-model'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* A React hook for exporting a 3D model using the core ModelExporter.
|
|
10
|
-
*
|
|
11
|
-
* @param {Function} [onSaved] Called when the export is complete.
|
|
12
|
-
* @param {Function} [onError] Called when an error occurs during exporting.
|
|
13
|
-
* @returns An object with export functions.
|
|
14
|
-
*/
|
|
15
|
-
const useExportModel = (
|
|
16
|
-
onSaved?: () => void,
|
|
17
|
-
onError?: (error: Error) => void
|
|
18
|
-
) => {
|
|
19
|
-
const exporterRef = useRef<ModelExporter>(new ModelExporter())
|
|
20
|
-
|
|
21
|
-
const handleThreeGltfExport = useCallback(
|
|
22
|
-
async (file: ModelFile | null, binary: boolean): Promise<void> => {
|
|
23
|
-
const scene = file?.model
|
|
24
|
-
if (!scene) {
|
|
25
|
-
console.error('Scene not initialized')
|
|
26
|
-
return
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
try {
|
|
30
|
-
const exporter = exporterRef.current
|
|
31
|
-
const baseFileName = file.name.replace(/\.[^/.]+$/, '') // Remove extension
|
|
32
|
-
|
|
33
|
-
if (binary) {
|
|
34
|
-
// Export as GLB
|
|
35
|
-
const result = await exporter.exportThreeJSGLB(scene, {})
|
|
36
|
-
if (!(result.data instanceof Uint8Array)) {
|
|
37
|
-
throw new Error('Expected GLB export to return Uint8Array')
|
|
38
|
-
}
|
|
39
|
-
fileSaver.saveAs(
|
|
40
|
-
new Blob([new Uint8Array(result.data)]),
|
|
41
|
-
`${baseFileName}.glb`
|
|
42
|
-
)
|
|
43
|
-
} else {
|
|
44
|
-
// Export as GLTF with ZIP
|
|
45
|
-
const result = await exporter.exportThreeJSGLTF(scene)
|
|
46
|
-
const zipBuffer = await exporter.createZIPArchive(
|
|
47
|
-
result,
|
|
48
|
-
baseFileName
|
|
49
|
-
)
|
|
50
|
-
fileSaver.saveAs(
|
|
51
|
-
new Blob([new Uint8Array(zipBuffer)]),
|
|
52
|
-
`${baseFileName}.zip`
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (onSaved) onSaved()
|
|
57
|
-
} catch (error) {
|
|
58
|
-
console.error('Export failed:', error)
|
|
59
|
-
if (onError) onError(error as Error)
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
[onSaved, onError]
|
|
63
|
-
)
|
|
64
|
-
|
|
65
|
-
const handleDocumentGltfExport = useCallback(
|
|
66
|
-
async function (
|
|
67
|
-
jsonDocument: Document,
|
|
68
|
-
file: ModelFile | null,
|
|
69
|
-
binary = false,
|
|
70
|
-
download = true
|
|
71
|
-
) {
|
|
72
|
-
try {
|
|
73
|
-
const exporter = exporterRef.current
|
|
74
|
-
const baseFileName = file?.name.replace(/\.[^/.]+$/, '') || 'model'
|
|
75
|
-
|
|
76
|
-
if (binary) {
|
|
77
|
-
// Export as GLB
|
|
78
|
-
const result = await exporter.exportDocumentGLB(jsonDocument)
|
|
79
|
-
fileSaver.saveAs(
|
|
80
|
-
new Blob([new Uint8Array(result.data)]),
|
|
81
|
-
`${baseFileName}.glb`
|
|
82
|
-
)
|
|
83
|
-
} else {
|
|
84
|
-
const result = await exporter.exportDocumentGLTF(jsonDocument)
|
|
85
|
-
|
|
86
|
-
if (!download) return result
|
|
87
|
-
|
|
88
|
-
// Export as GLTF with ZIP
|
|
89
|
-
const zipBuffer = await exporter.createZIPArchive(
|
|
90
|
-
result,
|
|
91
|
-
baseFileName
|
|
92
|
-
)
|
|
93
|
-
fileSaver.saveAs(
|
|
94
|
-
new Blob([new Uint8Array(zipBuffer)]),
|
|
95
|
-
`${baseFileName}.zip`
|
|
96
|
-
)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (onSaved) onSaved()
|
|
100
|
-
} catch (error) {
|
|
101
|
-
console.error('Export failed:', error)
|
|
102
|
-
if (onError) onError(error as Error)
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
[onError, onSaved]
|
|
106
|
-
)
|
|
107
|
-
return {
|
|
108
|
-
handleThreeGltfExport,
|
|
109
|
-
handleDocumentGltfExport
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export default useExportModel
|
|
@@ -1,46 +0,0 @@
|
|
|
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 { EventData, EventHandler, EventTypes } from './types'
|
|
18
|
-
|
|
19
|
-
const listeners = {} as Record<EventTypes, EventHandler<EventTypes>[]>
|
|
20
|
-
const eventSystem = {
|
|
21
|
-
emit<T extends EventTypes>(event: T, data?: EventData[T]): void {
|
|
22
|
-
const handlers = listeners[event]
|
|
23
|
-
console.debug(`Event emitted: ${event}`, data)
|
|
24
|
-
if (handlers) {
|
|
25
|
-
handlers.forEach((handler) => handler(data))
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
on<T extends EventTypes>(event: T, handler?: EventHandler<T>): void {
|
|
30
|
-
if (!listeners[event]) {
|
|
31
|
-
listeners[event] = []
|
|
32
|
-
}
|
|
33
|
-
listeners[event].push(handler as EventHandler<EventTypes>)
|
|
34
|
-
},
|
|
35
|
-
|
|
36
|
-
off<T extends EventTypes>(event: T, handler?: EventHandler<T>): void {
|
|
37
|
-
const handlers = listeners[event]
|
|
38
|
-
if (handlers) {
|
|
39
|
-
listeners[event] = handlers.filter(
|
|
40
|
-
(h) => h !== handler
|
|
41
|
-
) as EventHandler<EventTypes>[]
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export default eventSystem
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export { default as useLoadModel } from './use-load-model'
|
|
2
|
-
export {
|
|
3
|
-
type EventHandler,
|
|
4
|
-
type EventTypes,
|
|
5
|
-
type InputFileOrDirectory,
|
|
6
|
-
type LoadData,
|
|
7
|
-
type ModelFile,
|
|
8
|
-
type SceneDataLoadOptions,
|
|
9
|
-
type SceneLoadOptions,
|
|
10
|
-
type SceneLoadResult,
|
|
11
|
-
type UseLoadModelReturn,
|
|
12
|
-
type ServerSceneData
|
|
13
|
-
} from './types'
|
|
14
|
-
export { reconstructGltfFiles } from './utils/reconstruct-files'
|
|
15
|
-
export * from './model-context'
|
|
@@ -1,174 +0,0 @@
|
|
|
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 React, { createContext, useContext } from 'react'
|
|
18
|
-
|
|
19
|
-
import { useOptimizeModel } from '../use-optimize-model'
|
|
20
|
-
import { UseLoadModelReturn } from './types'
|
|
21
|
-
import useLoadModel from './use-load-model'
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Props for ModelProvider when an optimizer is provided.
|
|
25
|
-
* This ensures the optimizer property is required and properly typed.
|
|
26
|
-
*/
|
|
27
|
-
interface ModelProviderPropsWithOptimizer extends React.PropsWithChildren {
|
|
28
|
-
/** The optimizer instance from useOptimizeModel hook */
|
|
29
|
-
optimizer: ReturnType<typeof useOptimizeModel>
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Props for ModelProvider when no optimizer is used.
|
|
34
|
-
* The optimizer property must not be provided.
|
|
35
|
-
*/
|
|
36
|
-
interface ModelProviderPropsWithoutOptimizer extends React.PropsWithChildren {
|
|
37
|
-
/** Explicitly prevents optimizer from being passed */
|
|
38
|
-
optimizer?: never
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Discriminated union of ModelProvider props.
|
|
43
|
-
* Allows the provider to be used with or without an optimizer,
|
|
44
|
-
* ensuring type safety for both scenarios.
|
|
45
|
-
*/
|
|
46
|
-
type ModelProviderProps =
|
|
47
|
-
| ModelProviderPropsWithOptimizer
|
|
48
|
-
| ModelProviderPropsWithoutOptimizer
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Context for model loading with optimizer integration.
|
|
52
|
-
* When this context has a value, the optimizer property will be fully typed
|
|
53
|
-
* with all optimization methods and state.
|
|
54
|
-
*/
|
|
55
|
-
const ModelContextWithOptimizer = createContext<
|
|
56
|
-
UseLoadModelReturn<true> | undefined
|
|
57
|
-
>(undefined)
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Context for model loading without optimizer integration.
|
|
61
|
-
* When this context has a value, the optimizer property will be typed as null.
|
|
62
|
-
*/
|
|
63
|
-
const ModelContextWithoutOptimizer = createContext<
|
|
64
|
-
UseLoadModelReturn<false> | undefined
|
|
65
|
-
>(undefined)
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Provider component for model loading context.
|
|
69
|
-
*
|
|
70
|
-
* This component uses a dual-context pattern to provide type-safe access to model loading
|
|
71
|
-
* functionality with or without optimizer integration.
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* // With optimizer
|
|
75
|
-
* const optimizer = useOptimizeModel()
|
|
76
|
-
* <ModelProvider optimizer={optimizer}>
|
|
77
|
-
* <App />
|
|
78
|
-
* </ModelProvider>
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* // Without optimizer
|
|
82
|
-
* <ModelProvider>
|
|
83
|
-
* <App />
|
|
84
|
-
* </ModelProvider>
|
|
85
|
-
*
|
|
86
|
-
* @param props - Component props
|
|
87
|
-
* @param props.children - Child components that will have access to the model context
|
|
88
|
-
* @param props.optimizer - Optional optimizer instance from useOptimizeModel hook
|
|
89
|
-
*/
|
|
90
|
-
const ModelProvider = ({ children, optimizer }: ModelProviderProps) => {
|
|
91
|
-
// Call the hook once to get the model loading functionality
|
|
92
|
-
// The generic type will be inferred based on whether optimizer is defined
|
|
93
|
-
const value = useLoadModel(optimizer)
|
|
94
|
-
|
|
95
|
-
// Provide the value to the appropriate context based on whether optimizer is present
|
|
96
|
-
// This enables type-safe consumption via useModelContext
|
|
97
|
-
if (optimizer) {
|
|
98
|
-
// Optimizer is present: use ModelContextWithOptimizer
|
|
99
|
-
// The type assertion is safe because we know optimizer is defined
|
|
100
|
-
return (
|
|
101
|
-
<ModelContextWithOptimizer.Provider
|
|
102
|
-
value={value as unknown as UseLoadModelReturn<true>}
|
|
103
|
-
>
|
|
104
|
-
{children}
|
|
105
|
-
</ModelContextWithOptimizer.Provider>
|
|
106
|
-
)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
// No optimizer: use ModelContextWithoutOptimizer
|
|
110
|
-
// The type assertion is safe because we know optimizer is undefined
|
|
111
|
-
return (
|
|
112
|
-
<ModelContextWithoutOptimizer.Provider
|
|
113
|
-
value={value as unknown as UseLoadModelReturn<false>}
|
|
114
|
-
>
|
|
115
|
-
{children}
|
|
116
|
-
</ModelContextWithoutOptimizer.Provider>
|
|
117
|
-
)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Hook to access the model loading context.
|
|
122
|
-
*
|
|
123
|
-
* This hook provides type-safe access to model loading functionality.
|
|
124
|
-
* The return type is automatically inferred based on how the ModelProvider was configured.
|
|
125
|
-
*
|
|
126
|
-
* By default (no argument), it assumes an optimizer is present and returns the full
|
|
127
|
-
* optimizer integration. You can explicitly specify the expected configuration using
|
|
128
|
-
* the requireOptimizer parameter.
|
|
129
|
-
*
|
|
130
|
-
* @example
|
|
131
|
-
* // Default: assumes optimizer is present
|
|
132
|
-
* const model = useModelContext()
|
|
133
|
-
* model.optimizer.applyOptimization() // ✅ Fully typed
|
|
134
|
-
*
|
|
135
|
-
* @example
|
|
136
|
-
* // Explicitly require optimizer
|
|
137
|
-
* const model = useModelContext(true)
|
|
138
|
-
* model.optimizer.simplifyOptimization() // ✅ Fully typed
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* // Explicitly indicate no optimizer
|
|
142
|
-
* const model = useModelContext(false)
|
|
143
|
-
* model.optimizer // ✅ Typed as null
|
|
144
|
-
*
|
|
145
|
-
* @throws {Error} If used outside of a ModelProvider
|
|
146
|
-
* @returns The model loading context with type-safe optimizer property
|
|
147
|
-
*/
|
|
148
|
-
function useModelContext(): UseLoadModelReturn<true>
|
|
149
|
-
/**
|
|
150
|
-
* @param requireOptimizer - Set to true to explicitly require optimizer integration
|
|
151
|
-
*/
|
|
152
|
-
function useModelContext(requireOptimizer: true): UseLoadModelReturn<true>
|
|
153
|
-
/**
|
|
154
|
-
* @param requireOptimizer - Set to false to explicitly indicate no optimizer
|
|
155
|
-
*/
|
|
156
|
-
function useModelContext(requireOptimizer: false): UseLoadModelReturn<false>
|
|
157
|
-
|
|
158
|
-
function useModelContext(_?: boolean): UseLoadModelReturn<boolean> {
|
|
159
|
-
// Check both contexts - only one will have a value based on how ModelProvider was configured
|
|
160
|
-
const withOptimizer = useContext(ModelContextWithOptimizer)
|
|
161
|
-
const withoutOptimizer = useContext(ModelContextWithoutOptimizer)
|
|
162
|
-
|
|
163
|
-
// Use whichever context has a value (they're mutually exclusive)
|
|
164
|
-
const context = withOptimizer ?? withoutOptimizer
|
|
165
|
-
|
|
166
|
-
// Ensure the hook is used within a ModelProvider
|
|
167
|
-
if (!context) {
|
|
168
|
-
throw new Error('useModelContext must be used within a ModelProvider')
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
return context
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export { ModelProvider, useModelContext }
|
|
@@ -1,75 +0,0 @@
|
|
|
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
|
-
|
|
19
|
-
import { Action, LoadData } from './types'
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Initial state for the useReadModelFiles hook
|
|
23
|
-
*
|
|
24
|
-
* @property file - Currently loaded file
|
|
25
|
-
* @property isFileLoading - Flag indicating if a file is currently being loaded
|
|
26
|
-
* @property progress - Current loading progress (0-100)
|
|
27
|
-
* @property supportedFileTypes - List of supported file types
|
|
28
|
-
*/
|
|
29
|
-
export const initialState: LoadData = {
|
|
30
|
-
/**
|
|
31
|
-
* Currently loaded file
|
|
32
|
-
*/
|
|
33
|
-
file: null,
|
|
34
|
-
/**
|
|
35
|
-
* Flag indicating if a file is currently being loaded
|
|
36
|
-
*/
|
|
37
|
-
isFileLoading: false,
|
|
38
|
-
/**
|
|
39
|
-
* Current loading progress (0-100)
|
|
40
|
-
*/
|
|
41
|
-
progress: 0,
|
|
42
|
-
/**
|
|
43
|
-
* List of supported file types
|
|
44
|
-
* @type {ModelFileTypes[]}
|
|
45
|
-
*/
|
|
46
|
-
supportedFileTypes: [
|
|
47
|
-
ModelFileTypes.gltf,
|
|
48
|
-
ModelFileTypes.glb,
|
|
49
|
-
ModelFileTypes.usdz
|
|
50
|
-
]
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Reducer function for the useReadModelFiles hook
|
|
55
|
-
*
|
|
56
|
-
* @param state - The current state of the reducer
|
|
57
|
-
* @param action - The action to be performed
|
|
58
|
-
* @returns The updated state
|
|
59
|
-
*/
|
|
60
|
-
function reducer(state: LoadData, action: Action): LoadData {
|
|
61
|
-
switch (action.type) {
|
|
62
|
-
case 'set-file':
|
|
63
|
-
return { ...state, file: action.payload }
|
|
64
|
-
case 'set-file-loading':
|
|
65
|
-
return { ...state, isFileLoading: action.payload }
|
|
66
|
-
case 'set-progress':
|
|
67
|
-
return { ...state, progress: action.payload }
|
|
68
|
-
case 'reset-state':
|
|
69
|
-
return { ...initialState }
|
|
70
|
-
default:
|
|
71
|
-
return state
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export default reducer
|