threedviewer 2.0.0 → 2.0.1
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/README.md +97 -36
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,6 +18,9 @@ ThreeDViewer is a React component library for easily integrating Three.js-based
|
|
|
18
18
|
- Path tracing for high-quality rendering with customizable parameters
|
|
19
19
|
- Environment map support for realistic lighting and reflections
|
|
20
20
|
- Screenshot capture when rendering is complete (optional)
|
|
21
|
+
- **New in v2.0**: Event-driven architecture with typed events
|
|
22
|
+
- **New in v2.0**: Comprehensive error handling with Result pattern
|
|
23
|
+
- **New in v2.0**: Enhanced TypeScript support with modular option interfaces
|
|
21
24
|
|
|
22
25
|
## Installation
|
|
23
26
|
|
|
@@ -33,6 +36,8 @@ or if you're using yarn:
|
|
|
33
36
|
yarn add threedviewer
|
|
34
37
|
```
|
|
35
38
|
|
|
39
|
+
> **Note:** Version 2.0.0 introduces breaking changes. If you're upgrading from v1.x, please see the [Migration Guide](./MIGRATION_GUIDE.md).
|
|
40
|
+
|
|
36
41
|
## Usage
|
|
37
42
|
|
|
38
43
|
Here's a super simple example of how to use the `SimpleViewer` component in your React application:
|
|
@@ -118,8 +123,8 @@ function App() {
|
|
|
118
123
|
fov: 60,
|
|
119
124
|
autoFitToObject: false,
|
|
120
125
|
},
|
|
121
|
-
|
|
122
|
-
...defaultOptions.
|
|
126
|
+
lighting: {
|
|
127
|
+
...defaultOptions.lighting,
|
|
123
128
|
ambient: { intensity: 0.5 },
|
|
124
129
|
directional: { position: [10, 10, 5] },
|
|
125
130
|
},
|
|
@@ -157,9 +162,60 @@ In this example, we demonstrate how to use external scene references, handle cam
|
|
|
157
162
|
The main component for displaying 3D objects.
|
|
158
163
|
|
|
159
164
|
Props:
|
|
160
|
-
- `object` (required): A Three.js `Object3D` to be displayed in the viewer.
|
|
165
|
+
- `object` (required): A Three.js `Object3D` to be displayed in the viewer, or a URL string to a 3D model file.
|
|
161
166
|
- `options` (optional): An object containing viewer options (see below).
|
|
162
167
|
|
|
168
|
+
### Event System (New in v2.0)
|
|
169
|
+
|
|
170
|
+
The SimpleViewer now provides an event-driven API through the `events` property on the viewer handle:
|
|
171
|
+
|
|
172
|
+
```javascript
|
|
173
|
+
import React, { useRef, useEffect } from 'react';
|
|
174
|
+
import { SimpleViewer, SimpleViewerHandle } from 'threedviewer';
|
|
175
|
+
|
|
176
|
+
function App() {
|
|
177
|
+
const viewerRef = useRef<SimpleViewerHandle>(null);
|
|
178
|
+
|
|
179
|
+
useEffect(() => {
|
|
180
|
+
if (!viewerRef.current) return;
|
|
181
|
+
|
|
182
|
+
const { events } = viewerRef.current;
|
|
183
|
+
|
|
184
|
+
// Subscribe to events
|
|
185
|
+
events.on('model:loaded', ({ model, loadTime }) => {
|
|
186
|
+
console.log('Model loaded in', loadTime, 'ms');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
events.on('render:complete', ({ frame, renderTime }) => {
|
|
190
|
+
console.log('Frame', frame, 'rendered in', renderTime, 'ms');
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
events.on('error', ({ error }) => {
|
|
194
|
+
console.error('Viewer error:', error);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// Cleanup
|
|
198
|
+
return () => {
|
|
199
|
+
events.removeAllListeners();
|
|
200
|
+
};
|
|
201
|
+
}, []);
|
|
202
|
+
|
|
203
|
+
return (
|
|
204
|
+
<SimpleViewer
|
|
205
|
+
ref={viewerRef}
|
|
206
|
+
object="https://modelviewer.dev/shared-assets/models/RobotExpressive.glb"
|
|
207
|
+
/>
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Available events:
|
|
213
|
+
- `model:loaded` - Fired when a model is successfully loaded
|
|
214
|
+
- `model:error` - Fired when model loading fails
|
|
215
|
+
- `render:complete` - Fired after each render frame
|
|
216
|
+
- `controls:change` - Fired when camera controls are updated
|
|
217
|
+
- `error` - General error event
|
|
218
|
+
|
|
163
219
|
## Configuration Options
|
|
164
220
|
|
|
165
221
|
`SimpleViewer` accepts an `options` prop for customization. Here's an overview of available options:
|
|
@@ -167,29 +223,29 @@ Props:
|
|
|
167
223
|
```javascript
|
|
168
224
|
const defaultOptions: SimpleViewerOptions = {
|
|
169
225
|
staticScene: true, // It stops animation loop if there is no interactions
|
|
170
|
-
backgroundColor: '#f0f0f7',
|
|
226
|
+
backgroundColor: '#f0f0f7',
|
|
171
227
|
camera: {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
228
|
+
position: [2, 6, 2],
|
|
229
|
+
target: [0, 0, 0], // Center of the scene
|
|
230
|
+
fov: 75,
|
|
231
|
+
near: 0.1,
|
|
232
|
+
far: 100000,
|
|
177
233
|
autoFitToObject: true,
|
|
178
234
|
},
|
|
179
|
-
|
|
180
|
-
|
|
235
|
+
lighting: {
|
|
236
|
+
ambient: {
|
|
181
237
|
color: '#404040',
|
|
182
238
|
intensity: Math.PI,
|
|
183
239
|
},
|
|
184
|
-
|
|
240
|
+
hemisphere: {
|
|
185
241
|
skyColor: '#ffffbb',
|
|
186
242
|
groundColor: '#080820',
|
|
187
243
|
intensity: 1,
|
|
188
244
|
},
|
|
189
|
-
|
|
245
|
+
directional: {
|
|
190
246
|
color: '#ffffff',
|
|
191
247
|
intensity: Math.PI,
|
|
192
|
-
position:
|
|
248
|
+
position: [6, 6, 6],
|
|
193
249
|
castShadow: true,
|
|
194
250
|
shadow: {
|
|
195
251
|
mapSize: {
|
|
@@ -209,10 +265,10 @@ const defaultOptions: SimpleViewerOptions = {
|
|
|
209
265
|
},
|
|
210
266
|
},
|
|
211
267
|
},
|
|
212
|
-
|
|
268
|
+
render: {
|
|
213
269
|
antialias: true,
|
|
214
270
|
alpha: false,
|
|
215
|
-
|
|
271
|
+
shadowMap: true,
|
|
216
272
|
pixelRatio: window.devicePixelRatio,
|
|
217
273
|
shadowMapType: THREE.VSMShadowMap,
|
|
218
274
|
toneMapping: THREE.ACESFilmicToneMapping,
|
|
@@ -220,17 +276,17 @@ const defaultOptions: SimpleViewerOptions = {
|
|
|
220
276
|
},
|
|
221
277
|
controls: {
|
|
222
278
|
type: ControlType.OrbitControls, // 'OrbitControls' or 'MapControls'
|
|
223
|
-
enabled: true,
|
|
224
|
-
enableDamping: true,
|
|
225
|
-
dampingFactor: 0.25,
|
|
226
|
-
enableZoom: true,
|
|
227
|
-
enableRotate: true,
|
|
228
|
-
enablePan: true,
|
|
279
|
+
enabled: true,
|
|
280
|
+
enableDamping: true,
|
|
281
|
+
dampingFactor: 0.25,
|
|
282
|
+
enableZoom: true,
|
|
283
|
+
enableRotate: true,
|
|
284
|
+
enablePan: true,
|
|
229
285
|
},
|
|
230
286
|
helpers: {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
287
|
+
grid: true,
|
|
288
|
+
gridColor: '#333333',
|
|
289
|
+
axes: false,
|
|
234
290
|
object3DHelper: false,
|
|
235
291
|
addGizmo: false, // new Gizmo control is disabled by default
|
|
236
292
|
},
|
|
@@ -243,18 +299,20 @@ const defaultOptions: SimpleViewerOptions = {
|
|
|
243
299
|
},
|
|
244
300
|
animationLoop: null,
|
|
245
301
|
usePathTracing: true, // Enables path tracing for high-quality rendering
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
302
|
+
pathTracing: {
|
|
303
|
+
enabled: true,
|
|
304
|
+
samples: 300, // Limits the number of samples for path tracing
|
|
249
305
|
bounces: 8,
|
|
250
306
|
transmissiveBounces: 4,
|
|
251
307
|
lowResScale: 0.7,
|
|
252
308
|
renderScale: 1.0,
|
|
253
|
-
enablePathTracing: true,
|
|
254
309
|
dynamicLowRes: true,
|
|
255
310
|
},
|
|
311
|
+
environment: {
|
|
312
|
+
url: 'https://cdn.polyhaven.com/asset_img/primary/belfast_sunset_puresky.png', // Environment map URL for lighting and reflections
|
|
313
|
+
studio: true, // Enables a studio-like lighting environment
|
|
314
|
+
},
|
|
256
315
|
replaceWithScreenshotOnComplete: false, // Option to replace viewer with a screenshot after path tracing is complete
|
|
257
|
-
studioEnvironment: true, // Enables a studio-like lighting environment
|
|
258
316
|
};
|
|
259
317
|
```
|
|
260
318
|
|
|
@@ -263,22 +321,25 @@ const defaultOptions: SimpleViewerOptions = {
|
|
|
263
321
|
ThreeDViewer now supports path tracing for high-quality rendering with customizable settings:
|
|
264
322
|
|
|
265
323
|
- `usePathTracing`: Enables or disables path tracing.
|
|
266
|
-
- `
|
|
267
|
-
- `
|
|
324
|
+
- `pathTracing`: Customizes path tracing settings, including:
|
|
325
|
+
- `enabled`: Enables the path tracing mode.
|
|
326
|
+
- `samples`: Limits the number of path tracing samples to prevent infinite rendering.
|
|
268
327
|
- `bounces`: Number of light bounces.
|
|
269
328
|
- `transmissiveBounces`: Number of transmissive bounces.
|
|
270
329
|
- `lowResScale`: Low-resolution scale factor for performance optimization.
|
|
271
330
|
- `renderScale`: Controls the overall rendering scale.
|
|
272
|
-
- `enablePathTracing`: Enables the path tracing mode.
|
|
273
331
|
- `dynamicLowRes`: Adjusts resolution dynamically based on performance.
|
|
274
332
|
|
|
275
333
|
### Environment Map
|
|
276
334
|
|
|
277
335
|
To improve lighting and reflections, ThreeDViewer supports environment maps:
|
|
278
336
|
|
|
279
|
-
- `
|
|
280
|
-
```
|
|
281
|
-
|
|
337
|
+
- `environment.url`: You can provide a URL to an environment map. For example:
|
|
338
|
+
```javascript
|
|
339
|
+
environment: {
|
|
340
|
+
url: 'https://cdn.polyhaven.com/asset_img/primary/sunset_in_the_chalk_quarry.png',
|
|
341
|
+
studio: true // Enable studio-like lighting
|
|
342
|
+
}
|
|
282
343
|
```
|
|
283
344
|
|
|
284
345
|
This will automatically load and apply the environment map to the scene.
|