easy-threesdk 1.0.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/README.md +166 -0
- package/index.js +898 -0
- package/package.json +15 -0
- package/plugins/LabelControl.js +202 -0
- package/plugins/ModelControl.js +281 -0
package/README.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# ThreeSDK 使用文档
|
|
2
|
+
|
|
3
|
+
## 1. 简介
|
|
4
|
+
ThreeSDK 是一个基于 Three.js 的封装库,旨在简化 3D 场景的搭建、模型加载、交互控制以及标签系统的开发。通过封装常用的 Three.js 功能,开发者可以更快速地构建具有漫游、交互和标注功能的 3D 应用。
|
|
5
|
+
|
|
6
|
+
## 2. 核心功能
|
|
7
|
+
|
|
8
|
+
ThreeSDK 封装了以下核心能力:
|
|
9
|
+
|
|
10
|
+
### 2.1 场景基础建设
|
|
11
|
+
- **自动化初始化**:一键初始化 Scene、Camera、Renderer、OrbitControls 和 Lights。
|
|
12
|
+
- **响应式适配**:自动处理窗口大小调整 (Resize)。
|
|
13
|
+
- **光照系统**:内置环境光、方向光和边缘光 (Rim Light) 配置。
|
|
14
|
+
|
|
15
|
+
### 2.2 摄像机与视角控制
|
|
16
|
+
- **轨道控制器 (OrbitControls)**:默认支持旋转、缩放、平移。
|
|
17
|
+
- **第一人称模式 (First Person Mode)**:
|
|
18
|
+
- 固定相机位置,仅允许围绕自身旋转查看。
|
|
19
|
+
- 适用于全景查看。
|
|
20
|
+
- **漫游模式 (Roam Mode)**:
|
|
21
|
+
- 类似游戏的操作方式 (WASD / 方向键移动)。
|
|
22
|
+
- 鼠标拖拽改变视角朝向。
|
|
23
|
+
- 地面点击跳转 (Teleport)。
|
|
24
|
+
- **视角动画 (flyTo)**:平滑过渡到指定位置和视角的动画功能。
|
|
25
|
+
|
|
26
|
+
### 2.3 插件系统
|
|
27
|
+
采用插件化架构,通过 `plugins()` 方法注册功能模块。目前内置插件包括:
|
|
28
|
+
- **LabelControl**:基于 CSS3DRenderer 的 3D HTML 标签系统。
|
|
29
|
+
- **ModelControl**:GLTF 模型加载、模型点击/悬停交互管理。
|
|
30
|
+
|
|
31
|
+
### 2.4 其他工具
|
|
32
|
+
- **全景球体 (Panorama Sphere)**:快速创建全景背景。
|
|
33
|
+
- **场景组管理 (ThreeGroup)**:方便管理场景中的对象分组。
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 3. 快速上手 Demo
|
|
38
|
+
|
|
39
|
+
以下代码展示了如何使用 ThreeSDK 初始化场景、加载模型、添加标签以及启用交互。
|
|
40
|
+
|
|
41
|
+
```javascript
|
|
42
|
+
import * as THREE from "three";
|
|
43
|
+
import ThreeSdk from "./ThreeSdk/index.js";
|
|
44
|
+
import LabelControl from "./ThreeSdk/plugins/LabelControl.js";
|
|
45
|
+
import ModelControl from "./ThreeSdk/plugins/ModelControl.js";
|
|
46
|
+
|
|
47
|
+
// 1. 初始化 SDK
|
|
48
|
+
const container = document.getElementById("canvas-container");
|
|
49
|
+
const threeSdk = new ThreeSdk(container);
|
|
50
|
+
|
|
51
|
+
// 2. 配置并启动场景
|
|
52
|
+
threeSdk.init(
|
|
53
|
+
{
|
|
54
|
+
// 相机初始位置
|
|
55
|
+
position: new THREE.Vector3(0, 15, 20),
|
|
56
|
+
lookAt: new THREE.Vector3(0, 0, 0),
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
// 控制器配置
|
|
60
|
+
minDistance: 10,
|
|
61
|
+
maxDistance: 500,
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
// 灯光配置 (可选)
|
|
65
|
+
ambientLightIntensity: 0.8,
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// 3. 注册插件
|
|
70
|
+
threeSdk.plugins([LabelControl, ModelControl]);
|
|
71
|
+
|
|
72
|
+
// 4. 创建全景背景 (可选)
|
|
73
|
+
const textureLoader = new THREE.TextureLoader();
|
|
74
|
+
textureLoader.load("/path/to/panorama.jpg", (texture) => {
|
|
75
|
+
threeSdk.createPanoramaSphere(texture);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// 5. 使用 ModelControl 插件加载 GLTF 模型
|
|
79
|
+
const modelControl = threeSdk._PluginsManager.get("ModelControl");
|
|
80
|
+
|
|
81
|
+
modelControl.loadGLTFBuilding(
|
|
82
|
+
{
|
|
83
|
+
ModelID: "MyCar",
|
|
84
|
+
modelPath: "./resource/car/scene.gltf",
|
|
85
|
+
position: [0, 0], // x, z 坐标
|
|
86
|
+
scale: 0.1,
|
|
87
|
+
clickable: true,
|
|
88
|
+
},
|
|
89
|
+
(percent) => {
|
|
90
|
+
console.log(`模型加载进度: ${Math.round(percent)}%`);
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// 监听模型点击事件
|
|
95
|
+
modelControl.onModelClickListener((object) => {
|
|
96
|
+
if (object) {
|
|
97
|
+
console.log("点击了模型:", object.userData.name);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// 6. 使用 LabelControl 插件添加 3D 标签
|
|
102
|
+
const labelControl = threeSdk._PluginsManager.get("LabelControl");
|
|
103
|
+
|
|
104
|
+
labelControl.create3DLabel(
|
|
105
|
+
"CarLabel", // 标签 ID
|
|
106
|
+
`
|
|
107
|
+
<div style="color: white; background: rgba(0,0,0,0.6); padding: 10px; border-radius: 5px;">
|
|
108
|
+
<h3>My Car</h3>
|
|
109
|
+
<p>Click to view details</p>
|
|
110
|
+
</div>
|
|
111
|
+
`,
|
|
112
|
+
new THREE.Vector3(0, 10, 0), // 标签位置
|
|
113
|
+
{
|
|
114
|
+
size: 0.02, // 缩放比例
|
|
115
|
+
className: "my-label-class",
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
// 标签事件
|
|
119
|
+
click: (e) => {
|
|
120
|
+
console.log("点击了标签", e);
|
|
121
|
+
// 视角飞向标签位置
|
|
122
|
+
threeSdk.flyTo({
|
|
123
|
+
position: new THREE.Vector3(10, 10, 10),
|
|
124
|
+
target: new THREE.Vector3(0, 0, 0)
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
// 7. 切换视角模式示例
|
|
131
|
+
// 开启漫游模式
|
|
132
|
+
// threeSdk.enterRoamMode();
|
|
133
|
+
// 开启第一人称模式
|
|
134
|
+
// threeSdk.enterFirstPersonMode();
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## 4. API 概览
|
|
138
|
+
|
|
139
|
+
### ThreeSdk 类
|
|
140
|
+
|
|
141
|
+
| 方法 | 描述 |
|
|
142
|
+
|Data Type| Description|
|
|
143
|
+
|---|---|
|
|
144
|
+
| `init(cameraConfig, controlConfig, lightConfig)` | 初始化场景、相机、灯光和控制器。 |
|
|
145
|
+
| `plugins(pluginsList)` | 注册插件数组。 |
|
|
146
|
+
| `createPanoramaSphere(texture)` | 创建全景球体背景。 |
|
|
147
|
+
| `enterRoamMode()` | 进入漫游模式 (WASD移动)。 |
|
|
148
|
+
| `exitRoamMode()` | 退出漫游模式。 |
|
|
149
|
+
| `enterFirstPersonMode()` | 进入第一人称模式 (定点旋转)。 |
|
|
150
|
+
| `exitFirstPersonMode()` | 退出第一人称模式。 |
|
|
151
|
+
| `flyTo(viewInfo, options)` | 相机平滑飞行到指定视角。 |
|
|
152
|
+
| `createThreeGroup(name)` | 创建并获取一个 Three.Group。 |
|
|
153
|
+
|
|
154
|
+
### Plugin: ModelControl
|
|
155
|
+
|
|
156
|
+
| 方法 | 描述 |
|
|
157
|
+
|---|---|
|
|
158
|
+
| `loadGLTFBuilding(options, onLoadProcess)` | 加载 GLTF 模型,支持阴影和点击配置。 |
|
|
159
|
+
| `onModelClickListener(callback)` | 监听模型点击事件。 |
|
|
160
|
+
| `onModelMouseMoveListener(callback)` | 监听模型悬停事件。 |
|
|
161
|
+
|
|
162
|
+
### Plugin: LabelControl
|
|
163
|
+
|
|
164
|
+
| 方法 | 描述 |
|
|
165
|
+
|---|---|
|
|
166
|
+
| `create3DLabel(id, html, position, options, events)` | 创建 CSS3D 标签。 |
|