@ridp/threejs 1.4.7 → 1.5.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 CHANGED
@@ -180,31 +180,46 @@ new ThreeIns(selector: string, options: ThreeInsOptions)
180
180
 
181
181
  ##### setView()
182
182
 
183
- 设置模型视角。
183
+ 设置模型视角 - 通过移动相机位置来实现视角切换。支持预设视角类型和自定义方向向量。
184
184
 
185
185
  ```javascript
186
186
  threeJsIns.setView(
187
187
  model: Object3D,
188
- viewType: ViewType,
188
+ viewType: ViewType | Vector3, // 视角类型:可以是预设枚举或自定义 Vector3 方向
189
189
  options?: {
190
- scale?: number, // 缩放比例,1=占满画布,0.5=50%,2=0%(默认: 1)
190
+ scale?: number, // 缩放比例,1=占满画布,0.5=50%,2.0=200%(默认: 0.8)
191
+ position?: string | Vector3, // 模型在画布中的位置
191
192
  showBox?: boolean, // 是否显示包围盒(默认: false)
192
- animate?: boolean, // 是否启用动画(默认: false)
193
+ animate?: boolean, // 是否启用动画(默认: true)
193
194
  duration?: number, // 动画时长(毫秒,默认: 1000)
194
- offset?: Vector3 // 中心点偏移
195
+ offset?: Vector3, // 额外的手动偏移量
196
+ boxColor?: number // 包围盒颜色(默认: 0xffff00)
195
197
  }
196
198
  ): void
197
199
  ```
198
200
 
199
- **视角类型 (ViewType):**
201
+ **视角类型:**
202
+
203
+ **方式 1: 使用 ViewType 枚举(预设视角)**
200
204
  - `ViewType.TOP` - 俯视(从上往下)
201
205
  - `ViewType.RIGHT` - 右视图(从右往左)
202
206
  - `ViewType.LEFT` - 左侧视(从左往右)
203
207
  - `ViewType.ISO` - 等轴测视角(对角线上方俯视)
204
208
 
209
+ **方式 2: 使用自定义 Vector3 方向向量**
210
+ - 任意方向向量,如 `Vector3(1, 1, 1)`、`Vector3(2, 0, 1)`
211
+ - 向量会被自动归一化,实现相对模型中心的任意角度查看
212
+ - 支持负值和零值,如 `Vector3(0, -1, 1)` (仰视)、`Vector3(0, 0, 1)` (平视)
213
+
214
+ **position 参数选项:**
215
+ - 字符串选项:`'center'`(居中)、`'top-left'`(左上)、`'top-right'`(右上)、`'bottom-left'`(左下)、`'bottom-right'`(右下)
216
+ - Vector3:自定义偏移向量
217
+
205
218
  **示例:**
219
+
220
+ **使用预设 ViewType:**
206
221
  ```javascript
207
- // 设置等轴测视角,模型占满画布
222
+ // 基础用法 - 设置等轴测视角,模型占满画布
208
223
  threeJsIns.setView(model, ViewType.ISO, {
209
224
  scale: 1.0,
210
225
  showBox: true,
@@ -217,8 +232,113 @@ threeJsIns.setView(model, ViewType.TOP, {
217
232
  scale: 0.5,
218
233
  animate: true
219
234
  });
235
+
236
+ // 使用位置控制 - 左上角显示
237
+ threeJsIns.setView(model, ViewType.ISO, {
238
+ position: 'top-left',
239
+ scale: 0.8
240
+ });
241
+
242
+ // 组合使用 position 和 offset
243
+ threeJsIns.setView(model, ViewType.TOP, {
244
+ position: 'top-left', // 先自动定位到左上
245
+ offset: new THREE.Vector3(50, 0, 0), // 再额外向右偏移 50
246
+ scale: 0.8
247
+ });
248
+ ```
249
+
250
+ **使用自定义 Vector3 方向:**
251
+ ```javascript
252
+ // 从对角线方向查看模型(类似等轴测)
253
+ threeJsIns.setView(model, new THREE.Vector3(1, 1, 1));
254
+
255
+ // 从右前方查看模型
256
+ threeJsIns.setView(model, new THREE.Vector3(2, 1, 1));
257
+
258
+ // 从左侧平视模型
259
+ threeJsIns.setView(model, new THREE.Vector3(0, 0, 1));
260
+
261
+ // 从下方仰视模型
262
+ threeJsIns.setView(model, new THREE.Vector3(0, -1, 1));
263
+
264
+ // 自定义方向 + 其他选项
265
+ threeJsIns.setView(model, new THREE.Vector3(1, 2, 1), {
266
+ scale: 0.6,
267
+ position: 'center',
268
+ showBox: true,
269
+ animate: true,
270
+ duration: 1500
271
+ });
272
+
273
+ // 从左前方查看(负值 X)
274
+ threeJsIns.setView(model, new THREE.Vector3(-2, 1, 1));
275
+
276
+ // 从反方向对角线查看
277
+ threeJsIns.setView(model, new THREE.Vector3(-1, 1, 1));
278
+ ```
279
+
280
+ **两种方式对比:**
281
+
282
+ | 特性 | ViewType 枚举 | Vector3 自定义方向 |
283
+ |------|--------------|-------------------|
284
+ | 易用性 | ⭐⭐⭐⭐⭐ 简单直观 | ⭐⭐⭐ 需要理解 3D 向量 |
285
+ | 灵活性 | ⭐⭐⭐ 固定 4 种视角 | ⭐⭐⭐⭐⭐ 无限可能 |
286
+ | 适用场景 | 常用标准视角 | 特殊角度查看 |
287
+ | 向量归一化 | 自动处理 | 自动归一化 |
288
+ | 组合使用 | ✅ 支持 | ✅ 支持 |
289
+
290
+ ##### setViewByRotation()
291
+
292
+ 通过旋转物体切换视角 - 保持相机固定,通过旋转物体来实现视角切换。
293
+
294
+ ```javascript
295
+ threeJsIns.setViewByRotation(
296
+ model: Object3D,
297
+ viewType: ViewType,
298
+ options?: {
299
+ animate?: boolean, // 是否启用动画(默认: true)
300
+ duration?: number, // 动画时长(毫秒,默认: 1000)
301
+ resetRotation?: boolean // 旋转前是否重置物体旋转(默认: true)
302
+ }
303
+ ): { rotation: object, viewType: string, degrees: object }
304
+ ```
305
+
306
+ **旋转角度配置:**
307
+ - `ViewType.LEFT` - 物体绕 X 轴旋转 45°,Y 轴旋转 45°
308
+ - `ViewType.RIGHT` - 物体绕 Y 轴旋转 90°
309
+ - `ViewType.TOP` - 物体绕 X 轴旋转 90°
310
+ - `ViewType.ISO` - 物体绕 X 轴旋转 45°,Y 轴旋转 45°
311
+
312
+ **示例:**
313
+ ```javascript
314
+ // 左侧视:物体绕 X 轴 45°,Y 轴 45°
315
+ const result = threeJsIns.setViewByRotation(model, ViewType.LEFT);
316
+
317
+ // 俯视:物体绕 X 轴 90°,无动画
318
+ threeJsIns.setViewByRotation(model, ViewType.TOP, {
319
+ animate: false
320
+ });
321
+
322
+ // 自定义动画时间
323
+ threeJsIns.setViewByRotation(model, ViewType.ISO, {
324
+ duration: 1500
325
+ });
326
+
327
+ // 不重置旋转,在当前基础上累加
328
+ threeJsIns.setViewByRotation(model, ViewType.LEFT, {
329
+ resetRotation: false
330
+ });
220
331
  ```
221
332
 
333
+ **两种视角切换方式对比:**
334
+
335
+ | 特性 | setView() | setViewByRotation() |
336
+ |------|-----------|---------------------|
337
+ | 实现方式 | 移动相机位置 | 旋转物体 |
338
+ | 相机位置 | 改变 | 固定 |
339
+ | 物体姿态 | 保持不变 | 改变 |
340
+ | 适用场景 | 通用视角切换 | 特殊视觉效果 |
341
+
222
342
  ##### frameArea()
223
343
 
224
344
  > **⚠️ 已弃用** - 此方法仅为兼容旧版本保留,请使用 `setView()` 方法代替。
@@ -401,6 +521,10 @@ const {
401
521
  getMemoryCacheInfo, // 获取内存缓存统计
402
522
  deleteMemoryCache, // 删除指定模型的内存缓存
403
523
 
524
+ // 缓存迁移和验证 (v1.5.0+)
525
+ cleanLegacyCache, // 清理旧版本格式的缓存数据
526
+ validateCache, // 验证并修复缓存
527
+
404
528
  // 性能监控
405
529
  cacheMonitor // 缓存监控器实例
406
530
  } = loader;
@@ -631,6 +755,92 @@ const model3 = await loader.asyncFetch('/models/car.glb', '1.0.0');
631
755
  // [ asyncFetch ] ====> IndexedDB 缓存命中
632
756
  ```
633
757
 
758
+ ##### 缓存迁移和验证 API (v1.5.0+)
759
+
760
+ **cleanLegacyCache()**
761
+
762
+ 清理旧版本格式的缓存数据,手动触发清理不需要等待数据库版本升级。
763
+
764
+ ```javascript
765
+ const result = await loader.cleanLegacyCache();
766
+
767
+ // 返回值:
768
+ {
769
+ total: 10, // 总记录数
770
+ deleted: 3, // 删除的记录数
771
+ kept: 7, // 保留的记录数
772
+ deletedPaths: [ // 被删除的路径列表
773
+ '/models/old1.glb',
774
+ '/models/old2.glb',
775
+ '/models/old3.glb'
776
+ ]
777
+ }
778
+ ```
779
+
780
+ **validateCache()**
781
+
782
+ 验证并修复缓存完整性。
783
+
784
+ ```javascript
785
+ const result = await loader.validateCache();
786
+
787
+ // 返回值:
788
+ {
789
+ total: 10, // 总记录数
790
+ valid: 8, // 有效记录数
791
+ invalid: 2, // 无效记录数
792
+ repairs: 1 // 修复的操作数
793
+ }
794
+ ```
795
+
796
+ **使用场景:**
797
+
798
+ ```javascript
799
+ // 场景 1: 应用启动时验证缓存
800
+ async function initApp() {
801
+ const { validateCache, asyncFetch } = useGLTFLoader();
802
+
803
+ // 验证并修复缓存
804
+ const result = await validateCache();
805
+ if (result.invalid > 0) {
806
+ console.warn(`发现 ${result.invalid} 条无效缓存,已自动清理`);
807
+ }
808
+
809
+ // 继续加载模型
810
+ const model = await asyncFetch('/models/car.glb', '1.0.0');
811
+ }
812
+
813
+ // 场景 2: 手动清理旧版本格式
814
+ async function cleanupOldCache() {
815
+ const { cleanLegacyCache } = useGLTFLoader();
816
+
817
+ const result = await cleanLegacyCache();
818
+ console.log(`清理完成: 删除 ${result.deleted} 条,保留 ${result.kept} 条`);
819
+ }
820
+
821
+ // 场景 3: 用户遇到缓存问题时一键修复
822
+ async function repairCache() {
823
+ const { validateCache, cleanLegacyCache } = useGLTFLoader();
824
+
825
+ // 先验证
826
+ const validation = await validateCache();
827
+ console.log('验证结果:', validation);
828
+
829
+ // 如果仍有问题,强制清理旧版本
830
+ if (validation.invalid > 0) {
831
+ const cleaned = await cleanLegacyCache();
832
+ console.log('清理结果:', cleaned);
833
+ }
834
+ }
835
+ ```
836
+
837
+ **缓存版本说明:**
838
+
839
+ - **v1-v3 (旧版本)**: 使用序列化对象格式 (`object3DToData`)
840
+ - **v4+ (新版本)**: 直接存储 ArrayBuffer 原始数据
841
+ - **自动迁移**: 数据库会自动从旧版本升级,清理旧格式数据
842
+ - **手动清理**: 可使用 `cleanLegacyCache()` 手动清理
843
+
634
844
  ### useRaycaster()
635
845
 
636
846
  射线拾取工具,用于鼠标交互和物体选择。
@@ -1332,9 +1542,8 @@ otherObjects.castShadow = false;
1332
1542
  A: 使用 `asyncFetch` 的重试机制:
1333
1543
 
1334
1544
  ```javascript
1335
- const [err, model] = await asyncFetch(url, version, {
1336
- retryCount: 5,
1337
- timeout: 60000
1545
+ const [err, model] = await asyncFetch(url, version, null, {
1546
+ maxRetries: 5
1338
1547
  });
1339
1548
 
1340
1549
  if (err || !model) {
@@ -1343,7 +1552,50 @@ if (err || !model) {
1343
1552
  }
1344
1553
  ```
1345
1554
 
1346
- ### Q: 如何清理模型缓存?
1555
+ ### Q: 如何清理旧版本缓存数据?
1556
+
1557
+ A: 使用 `cleanLegacyCache` 方法手动清理旧版本格式:
1558
+
1559
+ ```javascript
1560
+ import { useGLTFLoader } from '@ridp/threejs';
1561
+
1562
+ const { cleanLegacyCache } = useGLTFLoader();
1563
+
1564
+ // 清理旧版本序列化格式
1565
+ const result = await cleanLegacyCache();
1566
+ console.log('清理结果:', result);
1567
+ // 输出: { total: 10, deleted: 3, kept: 7, deletedPaths: [...] }
1568
+ ```
1569
+
1570
+ **自动迁移**: 当用户首次使用新版本时,数据库会自动从 v4 升级到 v5,自动清理旧版本格式数据。
1571
+
1572
+ ### Q: 如何验证缓存完整性?
1573
+
1574
+ A: 使用 `validateCache` 方法:
1575
+
1576
+ ```javascript
1577
+ import { useGLTFLoader } from '@ridp/threejs';
1578
+
1579
+ const { validateCache } = useGLTFLoader();
1580
+
1581
+ // 验证并修复缓存
1582
+ const result = await validateCache();
1583
+ console.log('验证结果:', result);
1584
+ // 输出: { total: 10, valid: 8, invalid: 2, repairs: 1 }
1585
+
1586
+ if (result.invalid > 0) {
1587
+ console.warn(`发现 ${result.invalid} 条无效缓存,已自动清理`);
1588
+ }
1589
+ ```
1590
+
1591
+ ### Q: 升级后原有缓存会被删除吗?
1592
+
1593
+ A: 只有旧版本序列化格式的数据会被删除。ArrayBuffer 格式的数据会被保留。自动迁移过程会:
1594
+ - ✅ 保留所有 ArrayBuffer 格式的数据
1595
+ - ❌ 删除所有旧版本序列化格式的数据
1596
+ - 📊 在控制台输出迁移日志
1597
+
1598
+ ### Q: 如何清空所有缓存?
1347
1599
 
1348
1600
  A: 使用 `clearCache` 方法:
1349
1601
 
@@ -1405,6 +1657,121 @@ function captureScreenshot() {
1405
1657
 
1406
1658
  ## 更新日志
1407
1659
 
1660
+ ### v1.5.0 (2026-01-12)
1661
+
1662
+ **🎉 重大更新:**
1663
+
1664
+ - ✨ **增强 setView() 方法 - 支持自定义 Vector3 方向**:
1665
+ - **新增自定义方向支持**: 除了预设的 ViewType 枚举,现在可以直接传入 Vector3 方向向量
1666
+ - **无限视角可能性**: 支持任意相机角度,如 `Vector3(1,1,1)`、`Vector3(2,0,1)` 等
1667
+ - **自动归一化**: 向量会被自动归一化,确保相机距离计算准确
1668
+ - **完整兼容性**: 向后兼容 ViewType 枚举,同时支持新的 Vector3 参数
1669
+ - **灵活组合**: 自定义方向可以与 scale、position、offset 等参数组合使用
1670
+ - 使用示例:
1671
+ ```javascript
1672
+ // 从对角线方向查看
1673
+ threeJsIns.setView(model, new THREE.Vector3(1, 1, 1));
1674
+
1675
+ // 从右前方查看
1676
+ threeJsIns.setView(model, new THREE.Vector3(2, 1, 1));
1677
+
1678
+ // 从左侧平视
1679
+ threeJsIns.setView(model, new THREE.Vector3(0, 0, 1));
1680
+
1681
+ // 从下方仰视
1682
+ threeJsIns.setView(model, new THREE.Vector3(0, -1, 1));
1683
+ ```
1684
+
1685
+ - ✨ **新增 setViewByRotation() 方法**: 通过旋转物体切换视角
1686
+ - 提供另一种视角切换方式(与 setView 互补)
1687
+ - 左视图:物体绕 X 轴 45°,Y 轴旋转 45°
1688
+ - 支持动画过渡和旋转重置选项
1689
+ - 适用于特殊视觉效果需求
1690
+
1691
+ - ✨ **新增 setView() position 参数**: 控制模型在画布中的位置
1692
+ - 支持 5 种预设位置:`center`、`top-left`、`top-right`、`bottom-left`、`bottom-right`
1693
+ - 支持自定义 Vector3 偏移
1694
+ - 可与 offset 参数组合使用
1695
+
1696
+ - ✨ **新增缓存迁移和验证工具**:
1697
+ - 数据库版本升级:v4 → v5
1698
+ - 自动清理旧版本序列化格式数据
1699
+ - 新增 `cleanLegacyCache()` - 手动清理旧版本缓存
1700
+ - 新增 `validateCache()` - 验证并修复缓存完整性
1701
+ - 新增 `cleanLegacyFormats()` - IDBCache 实例方法
1702
+ - 新增 `validateAndRepair()` - IDBCache 实例方法
1703
+
1704
+ - 🔧 **增强 setView() 方法**:
1705
+ - **重大增强**: 支持 Vector3 自定义方向向量
1706
+ - 新增 `position` 参数控制模型在画布中的位置
1707
+ - 新增 `boxColor` 参数自定义包围盒颜色
1708
+ - 优化视角偏移计算逻辑
1709
+ - 完善文档和 JSDoc 注释
1710
+
1711
+ - 🐛 **修复缓存版本兼容性问题**:
1712
+ - 旧版本(v1-v3)使用序列化对象格式
1713
+ - 新版本(v4+)使用 ArrayBuffer 格式
1714
+ - 自动识别并删除旧格式数据
1715
+ - 添加完整的格式验证逻辑
1716
+
1717
+ **API 变更:**
1718
+
1719
+ ```javascript
1720
+ // ============ 新功能:自定义 Vector3 方向 ============
1721
+ // 从对角线方向查看模型
1722
+ threeJsIns.setView(model, new THREE.Vector3(1, 1, 1));
1723
+
1724
+ // 从右前方查看
1725
+ threeJsIns.setView(model, new THREE.Vector3(2, 1, 1), {
1726
+ scale: 0.8,
1727
+ position: 'center',
1728
+ animate: true
1729
+ });
1730
+
1731
+ // 从左侧平视
1732
+ threeJsIns.setView(model, new THREE.Vector3(0, 0, 1));
1733
+
1734
+ // 从下方仰视
1735
+ threeJsIns.setView(model, new THREE.Vector3(0, -1, 1));
1736
+
1737
+ // ============ 其他新功能 ============
1738
+ // 新增 setViewByRotation
1739
+ threeJsIns.setViewByRotation(model, ViewType.LEFT, {
1740
+ animate: true,
1741
+ duration: 1000,
1742
+ resetRotation: true
1743
+ });
1744
+
1745
+ // setView position 参数
1746
+ threeJsIns.setView(model, ViewType.ISO, {
1747
+ position: 'top-left', // 位置控制
1748
+ scale: 0.8,
1749
+ offset: new THREE.Vector3(50, 0, 0)
1750
+ });
1751
+
1752
+ // 缓存迁移和验证
1753
+ const { cleanLegacyCache, validateCache } = useGLTFLoader();
1754
+ await cleanLegacyCache(); // 清理旧版本缓存
1755
+ await validateCache(); // 验证和修复缓存
1756
+ ```
1757
+
1758
+ **Breaking Changes:**
1759
+
1760
+ 无破坏性变更,所有新功能都是向后兼容的增强。
1761
+
1762
+ **升级指南:**
1763
+
1764
+ - 如果使用预设视角(ViewType),无需修改任何代码
1765
+ - 如果需要自定义视角,可以直接传入 Vector3 对象替代 ViewType
1766
+ - 建议查看新示例代码了解更多用法
1767
+
1768
+ **迁移说明:**
1769
+
1770
+ - 数据库会自动从 v4 升级到 v5
1771
+ - 旧版本序列化格式数据会被自动删除
1772
+ - ArrayBuffer 格式数据会被保留
1773
+ - 提供手动清理工具供用户使用
1774
+
1408
1775
  ### v1.4.2 (2026-01-07)
1409
1776
 
1410
1777
  **🎉 重大更新:**