@ridp/threejs 1.4.6 → 1.4.7

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
@@ -61,6 +61,24 @@ yarn add @ridp/threejs dexie@^4 vue@^3 three@^0.178
61
61
  pnpm add @ridp/threejs dexie@^4 vue@^3 three@^0.178
62
62
  ```
63
63
 
64
+ ### vite.config.js 配置
65
+ 因为涉及 worker,需要添加以下配置项:
66
+ ```javascript
67
+ import { defineConfig } from 'vite';
68
+
69
+ export default defineConfig(() => {
70
+ return {
71
+ // plugins,
72
+ // build: { ... }
73
+ optimizeDeps: {
74
+ // ...
75
+ exclude: ['@ridp/threejs']
76
+ },
77
+ }
78
+ })
79
+
80
+ ```
81
+
64
82
  ## 快速开始
65
83
 
66
84
  ### 基础场景初始化
@@ -244,8 +262,8 @@ threeJsIns.setView(model, ViewType.ISO, {
244
262
  ```
245
263
 
246
264
  **迁移指南:**
247
- | 旧用法 (frameArea) | 新用法 (setView) |
248
- |-------------------|------------------|
265
+ | 旧用法 (frameArea) | 新用法 (setView) |
266
+ | ----------------------- | ---------------------------------------------- |
249
267
  | `frameArea(model, 1.0)` | `setView(model, ViewType.ISO, { scale: 1.0 })` |
250
268
  | `frameArea(model, 0.5)` | `setView(model, ViewType.ISO, { scale: 0.5 })` |
251
269
  | `frameArea(model, 2.0)` | `setView(model, ViewType.ISO, { scale: 2.0 })` |
@@ -338,15 +356,15 @@ threeJsIns.setView(model, ViewType.ISO, { scale: 1.0, animate: true });
338
356
 
339
357
  **迁移对照表:**
340
358
 
341
- | 功能 | useThreeJs (已弃用) | ThreeIns 类 (推荐) |
342
- |------|-------------------|-------------------|
343
- | 初始化场景 | `useThreeJs('#el', opts)` | `new ThreeIns('#el', opts)` |
344
- | 访问 scene | `const { scene } = useThreeJs(...)` | `threeJsIns.scene` |
345
- | 访问 camera | `const { camera } = useThreeJs(...)` | `threeJsIns.camera` |
346
- | 添加动画 | `addAnimate(fn)` | `threeJsIns.addAnimate(fn)` |
347
- | 视角切换 | ❌ 不支持 | ✅ `setView(model, ViewType.ISO)` |
348
- | 资源清理 | `dispose()` | ✅ `threeJsIns.dispose()` |
349
- | 适用范围 | 仅 Vue 3 | 所有 JS/TS 项目 |
359
+ | 功能 | useThreeJs (已弃用) | ThreeIns 类 (推荐) |
360
+ | ----------- | ------------------------------------ | -------------------------------- |
361
+ | 初始化场景 | `useThreeJs('#el', opts)` | `new ThreeIns('#el', opts)` |
362
+ | 访问 scene | `const { scene } = useThreeJs(...)` | `threeJsIns.scene` |
363
+ | 访问 camera | `const { camera } = useThreeJs(...)` | `threeJsIns.camera` |
364
+ | 添加动画 | `addAnimate(fn)` | `threeJsIns.addAnimate(fn)` |
365
+ | 视角切换 | ❌ 不支持 | ✅ `setView(model, ViewType.ISO)` |
366
+ | 资源清理 | `dispose()` | ✅ `threeJsIns.dispose()` |
367
+ | 适用范围 | 仅 Vue 3 | 所有 JS/TS 项目 |
350
368
 
351
369
  ---
352
370
 
@@ -725,8 +743,7 @@ import {
725
743
  createRaycaster, // 创建射线投射器
726
744
  createAxesHelper, // 创建坐标轴辅助器
727
745
  createArrowHelper, // 创建箭头辅助器
728
- createStats, // 创建性能监控器
729
- createTransformControls // 创建变换控制器
746
+ createStats // 创建性能监控器
730
747
  } from '@ridp/threejs';
731
748
 
732
749
  // 示例
@@ -737,6 +754,20 @@ const stats = createStats();
737
754
  document.body.appendChild(stats.dom);
738
755
  ```
739
756
 
757
+ **功能说明:**
758
+ - `createCameraHelper(camera)` - 创建相机辅助线,显示相机视锥体
759
+ - `createGridHelper(size, divisions, color1, color2)` - 创建网格辅助线
760
+ - `size`: 网格大小,默认 150
761
+ - `divisions`: 分段数
762
+ - `color1/color2`: 中心线和其他线的颜色
763
+ - `createBox3Helper(model)` - 创建包围盒辅助线
764
+ - `createOrbitControl(camera, dom)` - 创建轨道控制器
765
+ - `createMapControls(camera, dom)` - 创建地图控制器
766
+ - `createRaycaster()` - 创建射线投射器,用于鼠标拾取
767
+ - `createAxesHelper(size)` - 创建坐标轴辅助线,默认大小 10
768
+ - `createArrowHelper(dir, origin, length, color)` - 创建箭头辅助线
769
+ - `createStats()` - 创建性能监控器(基于 stats.js)
770
+
740
771
  ### initEnvImage()
741
772
 
742
773
  初始化场景环境贴图。
@@ -754,6 +785,30 @@ await initEnvImage(scene, '/hdr/outdoor.hdr');
754
785
  await initEnvImage(scene, '/textures/env.jpg');
755
786
  ```
756
787
 
788
+ ### getCommonParent()
789
+
790
+ 查找多个网格对象的公共父节点。
791
+
792
+ ```javascript
793
+ import { getCommonParent } from '@ridp/threejs';
794
+
795
+ // 查找多个选中对象的公共父节点
796
+ const meshes = [mesh1, mesh2, mesh3];
797
+ const commonParent = getCommonParent(meshes, scene);
798
+
799
+ console.log('公共父节点:', commonParent);
800
+ ```
801
+
802
+ **功能说明:**
803
+ - 遍历每个网格的所有父节点,统计出现频率最高的父节点
804
+ - 返回的公共父节点可作为分组、操作的目标对象
805
+ - 常用于批量操作、场景组织等场景
806
+
807
+ **使用场景:**
808
+ - 批量选择对象时查找共同的容器
809
+ - 场景层级分析
810
+ - 分组操作前的父节点确定
811
+
757
812
  ### modelOptimizer
758
813
 
759
814
  模型优化工具,用于减少多边形数量和优化材质。
@@ -779,26 +834,28 @@ const optimized = modelOptimizer.simplifyModel(model, {
779
834
  modelOptimizer.optimizeMaterials(model);
780
835
  ```
781
836
 
782
- ### disposeObject()
837
+ ### disposeThreeObject()
783
838
 
784
839
  深度释放 3D 对象及其子对象的资源。
785
840
 
786
841
  ```javascript
787
- import { disposeObject } from '@ridp/threejs';
842
+ import { disposeThreeObject } from '@ridp/threejs';
788
843
 
789
844
  // 释放模型资源
790
- disposeObject(model);
845
+ disposeThreeObject(model);
791
846
 
792
847
  // 释放整个场景
793
- disposeObject(scene);
848
+ disposeThreeObject(scene);
794
849
  ```
795
850
 
796
851
  **清理内容:**
797
- - 几何体 (geometry)
798
- - 材质 (material)
799
- - 纹理 (textures)
852
+ - 几何体 (geometry.dispose())
853
+ - 材质 (material.dispose())
854
+ - 材质中的所有纹理 (texture.dispose())
800
855
  - 子对象递归清理
801
856
 
857
+ **注意:** 此函数会递归遍历对象的所有子对象,确保所有资源都被正确释放,避免内存泄漏。
858
+
802
859
  ### sceneRebuilder
803
860
 
804
861
  渐进式场景重建工具,用于大型模型的分批渲染。
@@ -817,45 +874,93 @@ const builder = new ProgressiveSceneBuilder(scene, camera, {
817
874
  await builder.build(model);
818
875
  ```
819
876
 
820
- ### objectQuery
877
+ ### 模型序列化工具
821
878
 
822
- 3D 对象查询工具,支持按名称、UUID、类型等条件查询。
879
+ 用于 Three.js 对象的序列化和反序列化,支持将 3D 对象转换为可存储的数据格式。
823
880
 
824
881
  ```javascript
825
- import { objectQuery } from '@ridp/threejs';
882
+ import {
883
+ object3DToData,
884
+ object3DToDataSync,
885
+ dataToObject3D,
886
+ dataToObject3DSync
887
+ } from '@ridp/threejs';
826
888
 
827
- // 按名称查询
828
- const wheels = objectQuery.queryByName(scene, 'wheel_*');
889
+ // 异步序列化(推荐,避免阻塞主线程)
890
+ const data = await object3DToData(model, 50);
829
891
 
830
- // 按 UUID 查询
831
- const mesh = objectQuery.queryByUuid(scene, 'uuid-123');
892
+ // 同步序列化
893
+ const data = object3DToDataSync(model);
832
894
 
833
- // 按类型查询
834
- const allMeshes = objectQuery.queryByType(scene, 'Mesh');
895
+ // 异步反序列化(推荐,避免阻塞主线程)
896
+ const restoredModel = await dataToObject3D(data, 50);
835
897
 
836
- // 按条件查询
837
- const largeObjects = objectQuery.query(scene, (obj) => {
838
- return obj.scale.x > 10;
839
- });
898
+ // 同步反序列化
899
+ const restoredModel = dataToObject3DSync(data);
900
+ ```
901
+
902
+ **功能说明:**
903
+ - `object3DToData(object, chunkSize)` - 异步序列化对象,使用分块处理避免阻塞
904
+ - `chunkSize`: 每帧处理的对象数量,默认 50
905
+ - `object3DToDataSync(object)` - 同步序列化对象
906
+ - `dataToObject3D(data, chunkSize)` - 异步反序列化数据为 3D 对象
907
+ - `dataToObject3DSync(data)` - 同步反序列化数据为 3D 对象
908
+
909
+ **序列化内容:**
910
+ - 对象类型、名称、位置、旋转、缩放
911
+ - 几何体数据(顶点、索引、UV 等)
912
+ - 材质数据(颜色、纹理、属性等)
913
+ - userData 自定义数据
914
+ - 子对象层级关系
915
+
916
+ **使用场景:**
917
+ - 场景状态保存和恢复
918
+ - 模型数据的本地存储
919
+ - 跨页面/跨会话的模型传输
920
+
921
+ ### 对象查询工具
922
+
923
+ 基于 userData 的对象查找工具。
924
+
925
+ ```javascript
926
+ import { getObjectByUserData, getRootObj } from '@ridp/threejs';
927
+
928
+ // 向下遍历查找首个匹配 userData 的对象
929
+ const target = getObjectByUserData(mesh, 'type', 'wheel');
930
+
931
+ // 向上查找根节点(根据 userData 判定)
932
+ const root = getRootObj(mesh, 'isRoot', true);
840
933
  ```
841
934
 
935
+ **功能说明:**
936
+ - `getObjectByUserData(obj, key, value)` - 从指定对象开始向下遍历,查找首个 userData[key]=value 的对象(包括自身)
937
+ - `getRootObj(obj, rootKey, value)` - 从指定对象开始向上查找父级,直到找到 userData[rootKey]=value 的根对象
938
+
842
939
  ### CSS3D 辅助工具
843
940
 
844
941
  ```javascript
845
- import { createCSS3DObject, createCSS3DSprite } from '@ridp/threejs';
942
+ import { createInfoPlane, createTagPlane } from '@ridp/threejs';
846
943
 
847
- // 创建 CSS3D 对象
848
- const css3dObject = createCSS3DObject(domElement);
849
- css3dObject.position.set(0, 10, 0);
850
- scene.add(css3dObject);
944
+ // 从 DOM ID 创建 CSS3D 信息面板
945
+ const infoPlane = createInfoPlane('my-info-panel', [0.3, 0.3, 0.3]);
946
+ infoPlane.position.set(0, 10, 0);
947
+ scene.add(infoPlane);
851
948
 
852
- // 创建 CSS3D 精灵(始终面向相机)
853
- const sprite = createCSS3DSprite(domElement, {
854
- scale: 0.01
855
- });
856
- scene.add(sprite);
949
+ // 创建 CSS3D 标签面板
950
+ const tagPlane = createTagPlane('设备名称', 0.01);
951
+ tagPlane.position.set(5, 5, 5);
952
+ scene.add(tagPlane);
857
953
  ```
858
954
 
955
+ **功能说明:**
956
+ - `createInfoPlane(id, scale)` - 从页面中已存在的 DOM 元素创建 CSS3D 精灵
957
+ - `id`: DOM 元素的 ID
958
+ - `scale`: 缩放比例数组 `[x, y, z]`,默认 `[0.3, 0.3, 0.3]`
959
+ - ⚠️ 注意: HTML 元素不能设置为绝对定位
960
+ - `createTagPlane(text, scale)` - 创建带文本的 CSS3D 标签
961
+ - `text`: 标签文本内容
962
+ - `scale`: 统一缩放比例(三个维度相同)
963
+
859
964
  ### CacheMonitor
860
965
 
861
966
  缓存性能监控工具。