@vctrl/hooks 0.0.7-8 → 0.0.8

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 (3) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +209 -0
  3. package/package.json +1 -1
package/LICENSE ADDED
File without changes
package/README.md ADDED
@@ -0,0 +1,209 @@
1
+ # vctrl/hooks
2
+
3
+ [![Version and release packages to NPM](https://img.shields.io/github/actions/workflow/status/vectreal/vectreal-core/version-release.yaml?logo=github&logoColor=%23fc6c18&label=Version%20and%20release%20packages%20to%20NPM&color=%23fc6c18)
4
+ ](https://github.com/Vectreal/vectreal-core/actions/workflows/version-release.yaml)
5
+ [![@vctrl/hooks | NPM Downloads](https://img.shields.io/npm/dm/%40vctrl%2Fhooks?logo=npm&logoColor=%23fc6c18&label=%40vctrl%2Fhooks%20%7C%20NPM%20Downloads&color=%23fc6c18)](https://www.npmjs.com/package/@vctrl/hooks)
6
+
7
+ ## Overview
8
+
9
+ vctrl/hooks is a React hooks package designed to simplify 3D model loading and management within React applications. It's part of the vectreal-core ecosystem and is primarily used in the vctrl/viewer React component and the official website application.
10
+
11
+ The package provides a powerful hook for loading various 3D model file formats, along with a React context for easy state management and an event system for handling different stages of the model loading process.
12
+
13
+ ## Table of contents
14
+
15
+ - [vctrl/hooks](#vctrlhooks)
16
+ - [Overview](#overview)
17
+ - [Table of contents](#table-of-contents)
18
+ - [Features](#features)
19
+ - [Installation](#installation)
20
+ - [Usage](#usage)
21
+ - [API Reference](#api-reference)
22
+ - [useLoadModel](#useloadmodel)
23
+ - [Returns](#returns)
24
+ - [ModelContext](#modelcontext)
25
+ - [Event System](#event-system)
26
+ - [Events](#events)
27
+ - [Supported File Types](#supported-file-types)
28
+ - [File Loading Process](#file-loading-process)
29
+ - [State Management](#state-management)
30
+ - [Integration with Three.js](#integration-with-threejs)
31
+ - [Development](#development)
32
+ - [License](#license)
33
+ - [Contributing](#contributing)
34
+ - [Support](#support)
35
+
36
+ ## Features
37
+
38
+ - Direct 3D model file loading (supports GLTF, GLB, and USDZ formats)
39
+ - React context for state management (ModelContext)
40
+ - Event system for handling loading progress and completion
41
+ - Three.js integration
42
+ - TypeScript support
43
+
44
+ ## Installation
45
+
46
+ To install the package, use npm or yarn:
47
+
48
+ ```bash
49
+ npm install @vctrl/hooks
50
+ # or
51
+ yarn add @vctrl/hooks
52
+ ```
53
+
54
+ ## Usage
55
+
56
+ The main hook exported by this package is `useLoadModel`. Here's a basic example of how to use it:
57
+
58
+ ```jsx
59
+ import React from 'react';
60
+ import { useLoadModel } from '@vctrl/hooks/use-load-model';
61
+
62
+ function ModelLoader() {
63
+ const { handleFileUpload, file, progress, isLoading } = useLoadModel();
64
+
65
+ const onFileChange = (event) => {
66
+ const files = Array.from(event.target.files);
67
+ handleFileUpload(files);
68
+ };
69
+
70
+ return (
71
+ <div>
72
+ <input type="file" onChange={onFileChange} />
73
+ {isLoading && <p>Loading: {progress}%</p>}
74
+ {file && <p>Model loaded: {file.name}</p>}
75
+ </div>
76
+ );
77
+ }
78
+ ```
79
+
80
+ > Multiple files can be handled, e.g. when uploading a `.gltf` model, its `.bin` file and the relavant image texture files as `.jpeg`/`.png` (Other texture file formats have not been tested).
81
+
82
+ ## API Reference
83
+
84
+ ### useLoadModel
85
+
86
+ The main hook for loading and managing 3D model files.
87
+
88
+ #### Returns
89
+
90
+ - `file`: The loaded file object of type `ModelFile | null`.
91
+ - `isLoading`: A boolean indicating whether a file is currently being loaded.
92
+ - `progress`: A number between 0 and 100 representing the loading progress.
93
+ - `handleFileUpload`: A function to handle file upload. It accepts an array of `File` objects or a mixed array of `File` objects and directory entries.
94
+ - `on`: A function to subscribe to events.
95
+ - `off`: A function to unsubscribe from events.
96
+
97
+ ### ModelContext
98
+
99
+ A React context that provides the state and functions from `useLoadModel` to child components. It's implemented in `model-context.tsx`.
100
+
101
+ ```jsx
102
+ import { ModelProvider, useModelContext } from '@vctrl/hooks/use-load-model';
103
+
104
+ function App() {
105
+ return (
106
+ <ModelProvider>
107
+ <ModelConsumer />
108
+ </ModelProvider>
109
+ );
110
+ }
111
+
112
+ function ModelConsumer() {
113
+ const { file, isLoading, progress, handleFileUpload } = useModelContext();
114
+ // Use the context values
115
+ }
116
+ ```
117
+
118
+ > When using the React Context, only load models with the `useModelContext` hook.
119
+
120
+ ### Event System
121
+
122
+ The package includes a custom event system for handling various stages of the model loading process. The event system is implemented in `event-system.ts` and provides three main methods:
123
+
124
+ - `emit<T extends EventTypes>(event: T, data: EventData[T]): void`
125
+ - `on<T extends EventTypes>(event: T, handler: EventHandler<T>): void`
126
+ - `off<T extends EventTypes>(event: T, handler: EventHandler<T>): void`
127
+
128
+ #### Events
129
+
130
+ You can subscribe to these events using the `on` method:
131
+
132
+ - `'UPLOAD_PROGRESS'`: Emitted with the current progress (0-100) during file upload.
133
+ - `'UPLOAD_COMPLETE'`: Emitted with the loaded file object when the upload is complete.
134
+ - `'MULTIPLE_3D_MODELS'`: Emitted if multiple supported 3D model files are detected in the upload.
135
+ - `'UNSUPPORTED_FILE_TYPE'`: Emitted if an unsupported file type is uploaded.
136
+
137
+ Example usage:
138
+
139
+ ```javascript
140
+ const { on, off } = useLoadModel();
141
+
142
+ useEffect(() => {
143
+ const handleProgress = (progress) => {
144
+ console.log(`Upload progress: ${progress}%`);
145
+ };
146
+
147
+ on('UPLOAD_PROGRESS', handleProgress);
148
+
149
+ return () => off('UPLOAD_PROGRESS', handleProgress);
150
+ }, [on, off]);
151
+ ```
152
+
153
+ ## Supported File Types
154
+
155
+ The package currently supports the following 3D model file formats:
156
+
157
+ - GLTF (.gltf)
158
+ - GLB (.glb)
159
+ - USDZ (.usdz)
160
+
161
+ These are defined in the `ModelFileTypes` enum in `types.ts`.
162
+
163
+ ## File Loading Process
164
+
165
+ The file loading process, particularly for GLTF files, is handled by the `useLoadGltf` hook in `use-load-gltf.ts`. This hook performs the following steps:
166
+
167
+ 1. Parses the GLTF file content.
168
+ 2. Embeds external resources (buffers and images) into the GLTF content.
169
+ 3. Uses Three.js GLTFLoader to parse the modified GLTF content.
170
+ 4. Dispatches actions to update the state with the loaded model.
171
+
172
+ The loading process includes progress updates, which are communicated through the event system.
173
+
174
+ ## State Management
175
+
176
+ The package uses a reducer pattern for state management, implemented in `state.ts`. The state includes:
177
+
178
+ - `file`: The currently loaded model file.
179
+ - `isFileLoading`: A boolean indicating if a file is being loaded.
180
+ - `progress`: The current loading progress.
181
+ - `supportedFileTypes`: An array of supported file types.
182
+
183
+ Actions for updating the state are defined in the `Action` type in `types.ts`.
184
+
185
+ ## Integration with Three.js
186
+
187
+ The package integrates with Three.js for handling 3D model rendering. The loaded models are compatible with Three.js scene rendering, with the model stored as a Three.js `Object3D` in the `ModelFile` interface.
188
+
189
+ ## Development
190
+
191
+ This package is part of a monorepo workspace managed with Nx. To contribute or modify the package:
192
+
193
+ 1. Clone the monorepo
194
+ 2. Install dependencies: `npm install` or `yarn install`
195
+ 3. Make your changes
196
+ 4. Build the package: `nx build vctrl/hooks`
197
+ 5. Test your changes: `nx test vctrl/hooks`
198
+
199
+ ## License
200
+
201
+ Please refer to the LICENSE file in the package root for licensing information.
202
+
203
+ ## Contributing
204
+
205
+ Contributions are welcome! Please read the contributing guidelines in the [vectreal-core](https://github.com/vectreal/vectreal-core) monorepo before submitting pull requests.
206
+
207
+ ## Support
208
+
209
+ For issues, feature requests, or questions, please file an issue in the GitHub repository.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.7-8",
2
+ "version": "0.0.8",
3
3
  "name": "@vctrl/hooks",
4
4
  "description": "vctrl/hooks is a React hooks package designed to simplify 3D model loading and management within React applications. It's part of the vectreal-core ecosystem and is primarily used in the vctrl/viewer React component and the official website application.",
5
5
  "bugs": {