three-base 1.0.8 → 1.0.10
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/index.ts +6 -0
- package/lib/KeyFrameAnimation.ts +115 -0
- package/lib/ThreeBase.ts +4 -4
- package/lib/threeUtils.ts +3 -2
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -8,6 +8,12 @@ export { default as EventMesh } from "./lib/EventMesh";
|
|
|
8
8
|
export { default as ExtrudeMesh } from "./lib/ExtrudeMesh";
|
|
9
9
|
export type { ExtrudePath } from "./lib/ExtrudeMesh";
|
|
10
10
|
|
|
11
|
+
|
|
12
|
+
// 关键帧动画对象
|
|
13
|
+
export { default as KeyFrameAnimation } from "./lib/KeyFrameAnimation";
|
|
14
|
+
export type { KeyFrameAnimationProps } from "./lib/KeyFrameAnimation";
|
|
15
|
+
export type { KeyFrame } from "./lib/KeyFrameAnimation";
|
|
16
|
+
|
|
11
17
|
//点光源
|
|
12
18
|
export { default as PointLight } from "./lib/PointLight";
|
|
13
19
|
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: xiaosihan
|
|
3
|
+
* @Date: 2023-02-16 00:22:31
|
|
4
|
+
* @Last Modified by: xiaosihan
|
|
5
|
+
* @Last Modified time: 2023-02-16 19:08:55
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import _ from "lodash";
|
|
9
|
+
import { BufferAttribute, Euler, Line, LineBasicMaterial, Object3D, Vector3 } from "three";
|
|
10
|
+
import threeUtils from "./threeUtils";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
// 关键帧类型
|
|
14
|
+
export type Keyframe = {
|
|
15
|
+
position: Vector3;
|
|
16
|
+
scale: Vector3;
|
|
17
|
+
rotation: Euler;
|
|
18
|
+
timeTemp: number; //时间节点 单位毫秒.
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
export type KeyframeAnimationProps = {
|
|
23
|
+
object3d: Object3D; // 需要移动的对象
|
|
24
|
+
keyframes: Array<Keyframe>,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 关键帧动画对象
|
|
28
|
+
export default class KeyframeAnimation extends Object3D {
|
|
29
|
+
|
|
30
|
+
constructor(props: KeyframeAnimationProps) {
|
|
31
|
+
super();
|
|
32
|
+
|
|
33
|
+
// 设置关键帧对象数组
|
|
34
|
+
this.setKeyframes(props.keyframes);
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
// 设置对象
|
|
38
|
+
this.setObject3D(props.object3d);
|
|
39
|
+
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 需要需要移动的对象
|
|
43
|
+
object3d: Object3D;
|
|
44
|
+
|
|
45
|
+
//路径辅助线
|
|
46
|
+
halperLine = new Line(undefined, new LineBasicMaterial({ color: "#f00" }));
|
|
47
|
+
|
|
48
|
+
// 关键帧数组
|
|
49
|
+
keyframes: Array<Keyframe> = [];
|
|
50
|
+
|
|
51
|
+
// 设置需要移动的对象
|
|
52
|
+
setObject3D(object3d: Object3D) {
|
|
53
|
+
this.object3d = object3d;
|
|
54
|
+
if (threeUtils.isDev) {
|
|
55
|
+
this.children = [object3d, this.halperLine];
|
|
56
|
+
} else {
|
|
57
|
+
this.children = [object3d];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 获取单个关键帧
|
|
62
|
+
getKeyframe(i: number) {
|
|
63
|
+
return this.keyframes[i];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 设置单个关键帧
|
|
67
|
+
setKeyframe(i: number, keyframe: Keyframe) {
|
|
68
|
+
this.keyframes[i] = keyframe;
|
|
69
|
+
this.setKeyframes(this.keyframes);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
//设置全部关键帧
|
|
73
|
+
setKeyframes(keyframes: Array<Keyframe>) {
|
|
74
|
+
this.keyframes = keyframes;
|
|
75
|
+
const position = [];
|
|
76
|
+
keyframes.map(k => {
|
|
77
|
+
position.push(k.position.x);
|
|
78
|
+
position.push(k.position.y);
|
|
79
|
+
position.push(k.position.z);
|
|
80
|
+
});
|
|
81
|
+
this.halperLine.geometry.setAttribute("position", new BufferAttribute(new Float32Array(position), 3, false));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 更具关键帧的信息更新 对象的位置
|
|
85
|
+
update() {
|
|
86
|
+
const { timeTemp } = threeUtils;
|
|
87
|
+
const maxtimetemp = _.last(this.keyframes).timeTemp;
|
|
88
|
+
const currentTime = timeTemp % maxtimetemp;
|
|
89
|
+
|
|
90
|
+
const currentKeyframeIndex = this.keyframes.findIndex(k => currentTime < k.timeTemp);
|
|
91
|
+
|
|
92
|
+
const startKeyframe = this.keyframes[currentKeyframeIndex - 1];
|
|
93
|
+
const endKeyframe = this.keyframes[currentKeyframeIndex];
|
|
94
|
+
const progress = (currentTime - startKeyframe.timeTemp) / (endKeyframe.timeTemp - startKeyframe.timeTemp);
|
|
95
|
+
|
|
96
|
+
this.object3d.position.copy(endKeyframe.position).sub(startKeyframe.position).multiplyScalar(progress).add(startKeyframe.position);
|
|
97
|
+
this.object3d.scale.copy(endKeyframe.scale).sub(startKeyframe.scale).multiplyScalar(progress).add(startKeyframe.scale);
|
|
98
|
+
|
|
99
|
+
this.object3d.rotation.set(
|
|
100
|
+
(endKeyframe.rotation.x - startKeyframe.rotation.x) * progress + startKeyframe.rotation.x,
|
|
101
|
+
(endKeyframe.rotation.y - startKeyframe.rotation.y) * progress + startKeyframe.rotation.y,
|
|
102
|
+
(endKeyframe.rotation.z - startKeyframe.rotation.z) * progress + startKeyframe.rotation.z,
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
this.dispatchEvent({ type: "update" });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
//更新关键帧的实例矩阵
|
|
109
|
+
updateMatrix(): void {
|
|
110
|
+
this.update();
|
|
111
|
+
super.updateMatrix();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
}
|
package/lib/ThreeBase.ts
CHANGED
|
@@ -86,10 +86,12 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
86
86
|
// 设置 dom 容器
|
|
87
87
|
async setContainer(div: HTMLDivElement | null) {
|
|
88
88
|
if (div) {
|
|
89
|
+
//删除这个容器里面的所有canvas
|
|
89
90
|
const canvas = div.getElementsByTagName("canvas");
|
|
90
91
|
for (let c of canvas) c.remove();
|
|
91
92
|
div.append(this.renderer.domElement);
|
|
92
93
|
|
|
94
|
+
// 监听容器的尺寸的改变
|
|
93
95
|
elementResizeEvent(div, () => {
|
|
94
96
|
let rect = window.getComputedStyle(div);
|
|
95
97
|
let width = parseInt(rect.width);
|
|
@@ -97,15 +99,13 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
97
99
|
this.resize(width, height);
|
|
98
100
|
});
|
|
99
101
|
|
|
100
|
-
|
|
101
102
|
// 如果上下文被销毁了, 就恢复上下文
|
|
102
103
|
if (this.isRendererContextLost) {
|
|
103
104
|
this.renderer.forceContextRestore();
|
|
105
|
+
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
106
|
+
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
104
107
|
}
|
|
105
108
|
|
|
106
|
-
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
107
|
-
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
108
|
-
|
|
109
109
|
let rect = window.getComputedStyle(div);
|
|
110
110
|
let width = parseInt(rect.width);
|
|
111
111
|
let height = parseInt(rect.height);
|
package/lib/threeUtils.ts
CHANGED
|
@@ -14,9 +14,10 @@ class ThreeUtils extends EventDispatcher {
|
|
|
14
14
|
|
|
15
15
|
this.requestAnimationFrame();
|
|
16
16
|
}
|
|
17
|
-
|
|
17
|
+
//@ts-ignore
|
|
18
18
|
isProd = process.env.NODE_ENV === "production";
|
|
19
|
-
|
|
19
|
+
|
|
20
|
+
//@ts-ignore
|
|
20
21
|
isDev = process.env.NODE_ENV === "development";
|
|
21
22
|
|
|
22
23
|
timeTemp = performance.now();
|