three-base 1.0.16 → 1.0.18
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 +94 -1
- package/lib/ThreeBase.ts +11 -2
- package/lib/threeLoader.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1 +1,94 @@
|
|
|
1
|
-
|
|
1
|
+
1. 创建一个Threebase 实例
|
|
2
|
+
|
|
3
|
+
```tsx
|
|
4
|
+
class HomeRender extends ThreeBase {
|
|
5
|
+
|
|
6
|
+
constructor() {
|
|
7
|
+
super();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// 环境光
|
|
11
|
+
ambientLight = (() => {
|
|
12
|
+
const ambientLight = new AmbientLight("#fff", 0.4);
|
|
13
|
+
this.scene.add(ambientLight);
|
|
14
|
+
return ambientLight;
|
|
15
|
+
})();
|
|
16
|
+
|
|
17
|
+
//点光源
|
|
18
|
+
pointLight = (() => {
|
|
19
|
+
const pointLight = new PointLight("#fff", 0.4);
|
|
20
|
+
pointLight.position.set(50, 40, 50);
|
|
21
|
+
this.scene.add(pointLight);
|
|
22
|
+
return pointLight;
|
|
23
|
+
})();
|
|
24
|
+
|
|
25
|
+
init() {
|
|
26
|
+
super.init();
|
|
27
|
+
// 使用这个方法来初始化相机的位置和朝向
|
|
28
|
+
this.resetLookAt({
|
|
29
|
+
center: new Vector3(0, 0, 0),
|
|
30
|
+
position: new Vector3(10.954, 19.452, 39.811)
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
box = (() => {
|
|
36
|
+
const geometry = new BoxGeometry(10, 10, 10);
|
|
37
|
+
const matearil = new MeshLambertMaterial({ color: 0xfff });
|
|
38
|
+
const mesh = new Mesh(geometry, matearil);
|
|
39
|
+
|
|
40
|
+
//创建一个动画对象
|
|
41
|
+
const transition = new Transition({ rotationY: 0 });
|
|
42
|
+
|
|
43
|
+
// 动画对象播放回调
|
|
44
|
+
transition.onChange(({ rotationY }) => {
|
|
45
|
+
mesh.rotation.y = rotationY;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
mesh.position.set(0, 0, 0);
|
|
49
|
+
mesh.userData = { cursor: "pointer", enableEvent: true };
|
|
50
|
+
|
|
51
|
+
// 提供了常用的鼠标事件
|
|
52
|
+
mesh.addEventListener("click", e => { });
|
|
53
|
+
mesh.addEventListener("dbclick", e => { });
|
|
54
|
+
|
|
55
|
+
mesh.addEventListener("leftclick", e => {
|
|
56
|
+
transition.set({ rotationY: Math.random() * 5 });
|
|
57
|
+
});
|
|
58
|
+
mesh.addEventListener("rightclick", e => { });
|
|
59
|
+
mesh.addEventListener("mouseenter", e => { });
|
|
60
|
+
mesh.addEventListener("mouseleave", e => { });
|
|
61
|
+
mesh.addEventListener("mousemove", e => { });
|
|
62
|
+
mesh.addEventListener("mousedown", e => { });
|
|
63
|
+
mesh.addEventListener("mouseup", e => { });
|
|
64
|
+
|
|
65
|
+
// 使用键盘修改模型的位置
|
|
66
|
+
// keyContorls.addEventListener("change", e => {
|
|
67
|
+
// mesh.position.add(e.direction);
|
|
68
|
+
// console.log(mesh.position.toArray().join(" ,"));
|
|
69
|
+
// });
|
|
70
|
+
|
|
71
|
+
this.scene.add(mesh);
|
|
72
|
+
return mesh;
|
|
73
|
+
})();
|
|
74
|
+
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
const homeRenderer = new HomeRender();
|
|
79
|
+
|
|
80
|
+
// 导出这个单例对象
|
|
81
|
+
export default homeRenderer;
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
2.添加到div里面
|
|
86
|
+
|
|
87
|
+
import homeRenderer form "./homeRenderer.ts";
|
|
88
|
+
|
|
89
|
+
homeRenderer.setContainer( document.getElementById("div") );
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
```
|
package/lib/ThreeBase.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import elementResizeEvent from "element-resize-event";
|
|
9
|
+
import { round } from "lodash";
|
|
9
10
|
import { AxesHelper, EventDispatcher, GridHelper, LinearFilter, NoBlending, NormalBlending, Object3D, PerspectiveCamera, RGBAFormat, Scene, Texture, Vector2, Vector3, WebGLRenderer, WebGLRenderTarget } from "three";
|
|
10
11
|
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
11
12
|
import { generateUUID } from "three/src/math/MathUtils";
|
|
@@ -221,9 +222,17 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
221
222
|
|
|
222
223
|
// 获取控制的位置和朝向 保留3位小数
|
|
223
224
|
getLookAt() {
|
|
225
|
+
const position = this.camera.position.toArray().map(v => round(v, 3));
|
|
226
|
+
const center = this.controls.target.toArray().map(v => round(v, 3));
|
|
227
|
+
if (threeUtils.isDev) {
|
|
228
|
+
console.log(`
|
|
229
|
+
center: new Vector3(${center.join(",")}),
|
|
230
|
+
position: new Vector3(${position.join(",")})
|
|
231
|
+
`);
|
|
232
|
+
}
|
|
224
233
|
return {
|
|
225
|
-
position: new Vector3(
|
|
226
|
-
center: new Vector3()
|
|
234
|
+
position: new Vector3(...position),
|
|
235
|
+
center: new Vector3(...center)
|
|
227
236
|
}
|
|
228
237
|
}
|
|
229
238
|
|
package/lib/threeLoader.ts
CHANGED
|
@@ -102,7 +102,7 @@ class ThreeLoader extends EventDispatcher {
|
|
|
102
102
|
// gltf 加载器
|
|
103
103
|
gltfLoader = (() => {
|
|
104
104
|
const loader = new GLTFLoader();
|
|
105
|
-
const dracoLoader = new class
|
|
105
|
+
const dracoLoader = new class ThreeBaseDRACOLoader extends DRACOLoader {
|
|
106
106
|
|
|
107
107
|
_loadLibrary(url: string, responseType: string) {
|
|
108
108
|
//@ts-ignore
|