three-base 2.0.75 → 2.0.76
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/ApngTexture.d.ts +13 -13
- package/ApngTexture.js +61 -61
- package/ApngTextureManage.d.ts +18 -18
- package/ApngTextureManage.js +87 -87
- package/EventMesh.d.ts +18 -18
- package/EventMesh.js +59 -59
- package/ExtrudeMesh.d.ts +111 -111
- package/ExtrudeMesh.js +363 -363
- package/FPScontrol.d.ts +45 -45
- package/FPScontrol.js +171 -171
- package/Fire/Fire.d.ts +28 -28
- package/Fire/Fire.js +79 -79
- package/Fire/FireGromtry.d.ts +9 -9
- package/Fire/FireGromtry.js +40 -40
- package/Fire/FireMaterial.d.ts +28 -28
- package/Fire/FireMaterial.js +32 -32
- package/InstancedMeshBase.d.ts +32 -32
- package/InstancedMeshBase.js +234 -236
- package/InstancedMeshBase.ts +1 -4
- package/KeyFrameAnimation.d.ts +25 -25
- package/KeyFrameAnimation.js +78 -78
- package/MeshBase.d.ts +34 -34
- package/MeshBase.js +187 -187
- package/Mirror.d.ts +25 -25
- package/Mirror.js +139 -139
- package/PlaneModal1/PlaneModal1.d.ts +31 -31
- package/PlaneModal1/PlaneModal1.js +105 -105
- package/PlaneModal1/index.d.ts +1 -1
- package/PlaneModal1/index.js +1 -1
- package/PlaneText.d.ts +44 -44
- package/PlaneText.js +30 -30
- package/PlaneWithHtml.d.ts +11 -11
- package/PlaneWithHtml.js +68 -68
- package/PointLight/PointLight.d.ts +13 -13
- package/PointLight/PointLight.js +64 -64
- package/PointLight/index.d.ts +1 -1
- package/PointLight/index.js +1 -1
- package/Sky/Sky.d.ts +13 -13
- package/Sky/Sky.js +28 -28
- package/Sky/index.d.ts +1 -1
- package/Sky/index.js +1 -1
- package/SpriteModal1/SpriteModal1.d.ts +30 -30
- package/SpriteModal1/SpriteModal1.js +110 -110
- package/SpriteModal1/index.d.ts +1 -1
- package/SpriteModal1/index.js +1 -1
- package/SpriteText.d.ts +45 -45
- package/SpriteText.js +163 -163
- package/SpriteText2.d.ts +47 -47
- package/SpriteText2.js +198 -198
- package/SpriteWithHtml.d.ts +7 -7
- package/SpriteWithHtml.js +49 -49
- package/Text3D/Text3D.d.ts +20 -20
- package/Text3D/Text3D.js +62 -62
- package/Text3D/fontController.d.ts +59 -59
- package/Text3D/fontController.js +200 -200
- package/Text3D/index.d.ts +1 -1
- package/Text3D/index.js +1 -1
- package/ThreeBase.d.ts +92 -92
- package/ThreeBase.js +619 -619
- package/ThreeMouse.d.ts +39 -39
- package/ThreeMouse.js +359 -359
- package/Transition.d.ts +40 -40
- package/Transition.js +207 -207
- package/index.d.ts +1 -1
- package/index.js +2 -2
- package/keyContorls.d.ts +25 -25
- package/keyContorls.js +98 -98
- package/package.json +1 -1
- package/pass/FullScreenQuad.d.ts +7 -7
- package/pass/FullScreenQuad.js +25 -25
- package/pass/glow_pass.d.ts +2 -2
- package/pass/glow_pass.js +236 -236
- package/pass/stroke_pass.d.ts +2 -2
- package/pass/stroke_pass.js +202 -202
- package/shaders/FXAAShaderMaterial.d.ts +3 -3
- package/shaders/FXAAShaderMaterial.js +21 -21
- package/shaders.d.ts +17 -17
- package/shaders.js +99 -99
- package/threeLoader.d.ts +26 -26
- package/threeLoader.js +189 -189
- package/threeUtils.d.ts +47 -47
- package/threeUtils.js +266 -266
package/KeyFrameAnimation.js
CHANGED
|
@@ -1,78 +1,78 @@
|
|
|
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
|
-
import { last } from "lodash";
|
|
8
|
-
import { BufferAttribute, Line, LineBasicMaterial, Object3D } from "three";
|
|
9
|
-
import threeUtils from "./threeUtils";
|
|
10
|
-
// 关键帧动画对象
|
|
11
|
-
export default class KeyframeAnimation extends Object3D {
|
|
12
|
-
constructor(props) {
|
|
13
|
-
super();
|
|
14
|
-
// 速度调整
|
|
15
|
-
this.speedScale = props.speedScale || 1;
|
|
16
|
-
// 设置关键帧对象数组
|
|
17
|
-
this.setKeyframes(props.keyframes);
|
|
18
|
-
// 设置对象
|
|
19
|
-
this.setObject3D(props.object3d);
|
|
20
|
-
}
|
|
21
|
-
// 速度调整
|
|
22
|
-
speedScale = 1;
|
|
23
|
-
// 需要需要移动的对象
|
|
24
|
-
object3d;
|
|
25
|
-
//路径辅助线
|
|
26
|
-
halperLine = new Line(undefined, new LineBasicMaterial({ color: "#f00" }));
|
|
27
|
-
// 关键帧数组
|
|
28
|
-
keyframes = [];
|
|
29
|
-
// 设置需要移动的对象
|
|
30
|
-
setObject3D(object3d) {
|
|
31
|
-
this.object3d = object3d;
|
|
32
|
-
if (threeUtils.isDev) {
|
|
33
|
-
this.children = [object3d, this.halperLine];
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
this.children = [object3d];
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
// 获取单个关键帧
|
|
40
|
-
getKeyframe(i) {
|
|
41
|
-
return this.keyframes[i];
|
|
42
|
-
}
|
|
43
|
-
// 设置单个关键帧
|
|
44
|
-
setKeyframe(i, keyframe) {
|
|
45
|
-
this.keyframes[i] = keyframe;
|
|
46
|
-
this.setKeyframes(this.keyframes);
|
|
47
|
-
}
|
|
48
|
-
//设置全部关键帧
|
|
49
|
-
setKeyframes(keyframes) {
|
|
50
|
-
this.keyframes = keyframes;
|
|
51
|
-
const position = [];
|
|
52
|
-
keyframes.map(k => {
|
|
53
|
-
position.push(k.position.x);
|
|
54
|
-
position.push(k.position.y);
|
|
55
|
-
position.push(k.position.z);
|
|
56
|
-
});
|
|
57
|
-
this.halperLine.geometry.setAttribute("position", new BufferAttribute(new Float32Array(position), 3, false));
|
|
58
|
-
}
|
|
59
|
-
// 更具关键帧的信息更新 对象的位置
|
|
60
|
-
update() {
|
|
61
|
-
const { _timeTemp } = threeUtils;
|
|
62
|
-
const maxtimetemp = last(this.keyframes).timeTemp;
|
|
63
|
-
const currentTime = _timeTemp % maxtimetemp;
|
|
64
|
-
const currentKeyframeIndex = this.keyframes.findIndex(k => currentTime < k.timeTemp);
|
|
65
|
-
const startKeyframe = this.keyframes[currentKeyframeIndex - 1];
|
|
66
|
-
const endKeyframe = this.keyframes[currentKeyframeIndex];
|
|
67
|
-
const progress = (currentTime - startKeyframe.timeTemp) / (endKeyframe.timeTemp - startKeyframe.timeTemp);
|
|
68
|
-
this.object3d.position.copy(endKeyframe.position).sub(startKeyframe.position).multiplyScalar(progress).add(startKeyframe.position);
|
|
69
|
-
this.object3d.scale.copy(endKeyframe.scale).sub(startKeyframe.scale).multiplyScalar(progress).add(startKeyframe.scale);
|
|
70
|
-
this.object3d.rotation.set((endKeyframe.rotation.x - startKeyframe.rotation.x) * progress + startKeyframe.rotation.x, (endKeyframe.rotation.y - startKeyframe.rotation.y) * progress + startKeyframe.rotation.y, (endKeyframe.rotation.z - startKeyframe.rotation.z) * progress + startKeyframe.rotation.z);
|
|
71
|
-
this.dispatchEvent({ type: "update" });
|
|
72
|
-
}
|
|
73
|
-
//更新关键帧的实例矩阵
|
|
74
|
-
updateMatrix() {
|
|
75
|
-
this.update();
|
|
76
|
-
super.updateMatrix();
|
|
77
|
-
}
|
|
78
|
-
}
|
|
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
|
+
import { last } from "lodash";
|
|
8
|
+
import { BufferAttribute, Line, LineBasicMaterial, Object3D } from "three";
|
|
9
|
+
import threeUtils from "./threeUtils";
|
|
10
|
+
// 关键帧动画对象
|
|
11
|
+
export default class KeyframeAnimation extends Object3D {
|
|
12
|
+
constructor(props) {
|
|
13
|
+
super();
|
|
14
|
+
// 速度调整
|
|
15
|
+
this.speedScale = props.speedScale || 1;
|
|
16
|
+
// 设置关键帧对象数组
|
|
17
|
+
this.setKeyframes(props.keyframes);
|
|
18
|
+
// 设置对象
|
|
19
|
+
this.setObject3D(props.object3d);
|
|
20
|
+
}
|
|
21
|
+
// 速度调整
|
|
22
|
+
speedScale = 1;
|
|
23
|
+
// 需要需要移动的对象
|
|
24
|
+
object3d;
|
|
25
|
+
//路径辅助线
|
|
26
|
+
halperLine = new Line(undefined, new LineBasicMaterial({ color: "#f00" }));
|
|
27
|
+
// 关键帧数组
|
|
28
|
+
keyframes = [];
|
|
29
|
+
// 设置需要移动的对象
|
|
30
|
+
setObject3D(object3d) {
|
|
31
|
+
this.object3d = object3d;
|
|
32
|
+
if (threeUtils.isDev) {
|
|
33
|
+
this.children = [object3d, this.halperLine];
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
this.children = [object3d];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// 获取单个关键帧
|
|
40
|
+
getKeyframe(i) {
|
|
41
|
+
return this.keyframes[i];
|
|
42
|
+
}
|
|
43
|
+
// 设置单个关键帧
|
|
44
|
+
setKeyframe(i, keyframe) {
|
|
45
|
+
this.keyframes[i] = keyframe;
|
|
46
|
+
this.setKeyframes(this.keyframes);
|
|
47
|
+
}
|
|
48
|
+
//设置全部关键帧
|
|
49
|
+
setKeyframes(keyframes) {
|
|
50
|
+
this.keyframes = keyframes;
|
|
51
|
+
const position = [];
|
|
52
|
+
keyframes.map(k => {
|
|
53
|
+
position.push(k.position.x);
|
|
54
|
+
position.push(k.position.y);
|
|
55
|
+
position.push(k.position.z);
|
|
56
|
+
});
|
|
57
|
+
this.halperLine.geometry.setAttribute("position", new BufferAttribute(new Float32Array(position), 3, false));
|
|
58
|
+
}
|
|
59
|
+
// 更具关键帧的信息更新 对象的位置
|
|
60
|
+
update() {
|
|
61
|
+
const { _timeTemp } = threeUtils;
|
|
62
|
+
const maxtimetemp = last(this.keyframes).timeTemp;
|
|
63
|
+
const currentTime = _timeTemp % maxtimetemp;
|
|
64
|
+
const currentKeyframeIndex = this.keyframes.findIndex(k => currentTime < k.timeTemp);
|
|
65
|
+
const startKeyframe = this.keyframes[currentKeyframeIndex - 1];
|
|
66
|
+
const endKeyframe = this.keyframes[currentKeyframeIndex];
|
|
67
|
+
const progress = (currentTime - startKeyframe.timeTemp) / (endKeyframe.timeTemp - startKeyframe.timeTemp);
|
|
68
|
+
this.object3d.position.copy(endKeyframe.position).sub(startKeyframe.position).multiplyScalar(progress).add(startKeyframe.position);
|
|
69
|
+
this.object3d.scale.copy(endKeyframe.scale).sub(startKeyframe.scale).multiplyScalar(progress).add(startKeyframe.scale);
|
|
70
|
+
this.object3d.rotation.set((endKeyframe.rotation.x - startKeyframe.rotation.x) * progress + startKeyframe.rotation.x, (endKeyframe.rotation.y - startKeyframe.rotation.y) * progress + startKeyframe.rotation.y, (endKeyframe.rotation.z - startKeyframe.rotation.z) * progress + startKeyframe.rotation.z);
|
|
71
|
+
this.dispatchEvent({ type: "update" });
|
|
72
|
+
}
|
|
73
|
+
//更新关键帧的实例矩阵
|
|
74
|
+
updateMatrix() {
|
|
75
|
+
this.update();
|
|
76
|
+
super.updateMatrix();
|
|
77
|
+
}
|
|
78
|
+
}
|
package/MeshBase.d.ts
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import { AnimationAction, AnimationMixer, Clock, Group, Mesh, Object3D, Vector3 } from "three";
|
|
2
|
-
export default class MeshBase extends Group {
|
|
3
|
-
constructor(url?: string);
|
|
4
|
-
url: string;
|
|
5
|
-
static models: Map<string, Object3D>;
|
|
6
|
-
static loadModeling: Map<string, boolean>;
|
|
7
|
-
loaded: boolean;
|
|
8
|
-
hasAnimation: boolean;
|
|
9
|
-
mixer?: AnimationMixer;
|
|
10
|
-
animationActions: {
|
|
11
|
-
[key: string]: AnimationAction;
|
|
12
|
-
};
|
|
13
|
-
getSuffix(filename: string): string;
|
|
14
|
-
loadModel(url: string, filename?: string): Promise<void>;
|
|
15
|
-
frustumCulled: boolean;
|
|
16
|
-
castShadow: boolean;
|
|
17
|
-
receiveShadow: boolean;
|
|
18
|
-
resoles: any[];
|
|
19
|
-
awaitLoaded(): Promise<void>;
|
|
20
|
-
traverseMesh(callback: (mesh: Mesh) => void): Promise<void>;
|
|
21
|
-
clock: Clock;
|
|
22
|
-
setClipActionWeight(weights?: {
|
|
23
|
-
[key: string]: number;
|
|
24
|
-
}): Promise<void>;
|
|
25
|
-
getAnimas(): string[];
|
|
26
|
-
setAnimas(anima?: string): void;
|
|
27
|
-
stop(): void;
|
|
28
|
-
updateMatrix(): void;
|
|
29
|
-
getSize(): Promise<Vector3>;
|
|
30
|
-
center(): Promise<void>;
|
|
31
|
-
alignBottom(y?: number): Promise<void>;
|
|
32
|
-
scaleTo(size?: number, key?: Array<"x" | "y" | "z">): Promise<void>;
|
|
33
|
-
expandBynormal(length: number): void;
|
|
34
|
-
}
|
|
1
|
+
import { AnimationAction, AnimationMixer, Clock, Group, Mesh, Object3D, Vector3 } from "three";
|
|
2
|
+
export default class MeshBase extends Group {
|
|
3
|
+
constructor(url?: string);
|
|
4
|
+
url: string;
|
|
5
|
+
static models: Map<string, Object3D>;
|
|
6
|
+
static loadModeling: Map<string, boolean>;
|
|
7
|
+
loaded: boolean;
|
|
8
|
+
hasAnimation: boolean;
|
|
9
|
+
mixer?: AnimationMixer;
|
|
10
|
+
animationActions: {
|
|
11
|
+
[key: string]: AnimationAction;
|
|
12
|
+
};
|
|
13
|
+
getSuffix(filename: string): string;
|
|
14
|
+
loadModel(url: string, filename?: string): Promise<void>;
|
|
15
|
+
frustumCulled: boolean;
|
|
16
|
+
castShadow: boolean;
|
|
17
|
+
receiveShadow: boolean;
|
|
18
|
+
resoles: any[];
|
|
19
|
+
awaitLoaded(): Promise<void>;
|
|
20
|
+
traverseMesh(callback: (mesh: Mesh) => void): Promise<void>;
|
|
21
|
+
clock: Clock;
|
|
22
|
+
setClipActionWeight(weights?: {
|
|
23
|
+
[key: string]: number;
|
|
24
|
+
}): Promise<void>;
|
|
25
|
+
getAnimas(): string[];
|
|
26
|
+
setAnimas(anima?: string): void;
|
|
27
|
+
stop(): void;
|
|
28
|
+
updateMatrix(): void;
|
|
29
|
+
getSize(): Promise<Vector3>;
|
|
30
|
+
center(): Promise<void>;
|
|
31
|
+
alignBottom(y?: number): Promise<void>;
|
|
32
|
+
scaleTo(size?: number, key?: Array<"x" | "y" | "z">): Promise<void>;
|
|
33
|
+
expandBynormal(length: number): void;
|
|
34
|
+
}
|
package/MeshBase.js
CHANGED
|
@@ -1,187 +1,187 @@
|
|
|
1
|
-
import { AnimationMixer, Clock, Group, Vector3 } from "three";
|
|
2
|
-
import threeUtils from "./threeUtils";
|
|
3
|
-
import threeLoader from "./threeLoader";
|
|
4
|
-
import * as SkeletonUtils from 'three/examples/jsm/utils/SkeletonUtils';
|
|
5
|
-
// 模型基类
|
|
6
|
-
export default class MeshBase extends Group {
|
|
7
|
-
constructor(url) {
|
|
8
|
-
super();
|
|
9
|
-
url && this.loadModel(url);
|
|
10
|
-
}
|
|
11
|
-
url = "";
|
|
12
|
-
// 模型对象
|
|
13
|
-
static models = new Map();
|
|
14
|
-
// 模型是否在加载中
|
|
15
|
-
static loadModeling = new Map();
|
|
16
|
-
// 加载完成
|
|
17
|
-
loaded = false;
|
|
18
|
-
// 是否有动画
|
|
19
|
-
hasAnimation = false;
|
|
20
|
-
//动画混合器
|
|
21
|
-
mixer;
|
|
22
|
-
//动画剪辑对象数组
|
|
23
|
-
animationActions = {};
|
|
24
|
-
// 获取文件名的后缀
|
|
25
|
-
getSuffix(filename) {
|
|
26
|
-
const match = filename.match(/\.([^\.]+)$/);
|
|
27
|
-
return match ? match[1].toLowerCase() : '';
|
|
28
|
-
}
|
|
29
|
-
// 如果url 是blob地址的话,就用suffix字段来制定模型的类型
|
|
30
|
-
async loadModel(url, filename = "") {
|
|
31
|
-
this.url = url;
|
|
32
|
-
if (!url) {
|
|
33
|
-
this.clear();
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
this.loaded = false;
|
|
37
|
-
// 没有模型并且没有加载任务时就去加载模型
|
|
38
|
-
if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
|
|
39
|
-
MeshBase.loadModeling.set(url, true);
|
|
40
|
-
// 万能加载器
|
|
41
|
-
let modal = await threeLoader.loadModel(url, filename);
|
|
42
|
-
MeshBase.models.set(url, modal);
|
|
43
|
-
MeshBase.loadModeling.set(url, false);
|
|
44
|
-
}
|
|
45
|
-
// 如果模型还在加载中, 就等待模型加载完成
|
|
46
|
-
do {
|
|
47
|
-
await new Promise(resolve => setTimeout(resolve, 50));
|
|
48
|
-
} while (MeshBase.loadModeling.get(url));
|
|
49
|
-
if (MeshBase.models.get(url)) {
|
|
50
|
-
this.clear();
|
|
51
|
-
const mesh = MeshBase.models.get(url);
|
|
52
|
-
// 判断是否有动画
|
|
53
|
-
this.hasAnimation = mesh.animations.length > 0;
|
|
54
|
-
if (this.hasAnimation) {
|
|
55
|
-
const model = SkeletonUtils.clone(mesh);
|
|
56
|
-
this.mixer = new AnimationMixer(model);
|
|
57
|
-
this.animationActions = {};
|
|
58
|
-
mesh.animations.map(animation => {
|
|
59
|
-
const animationAction = this.mixer.clipAction(animation).play();
|
|
60
|
-
animationAction.weight = 0;
|
|
61
|
-
this.animationActions[animation.name] = animationAction;
|
|
62
|
-
});
|
|
63
|
-
this.add(model);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
this.add(mesh.clone(true));
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
70
|
-
this.loaded = true;
|
|
71
|
-
// 加载完成事件
|
|
72
|
-
this.dispatchEvent({ type: "loaded" });
|
|
73
|
-
}
|
|
74
|
-
// 超出绘制区域时不绘制
|
|
75
|
-
frustumCulled = false;
|
|
76
|
-
// 创建阴影
|
|
77
|
-
castShadow = false;
|
|
78
|
-
// 接受阴影
|
|
79
|
-
receiveShadow = false;
|
|
80
|
-
// promise.resole 队列
|
|
81
|
-
resoles = [];
|
|
82
|
-
// 等待加载加载完成
|
|
83
|
-
async awaitLoaded() {
|
|
84
|
-
if (!this.loaded) {
|
|
85
|
-
await new Promise((resolve) => {
|
|
86
|
-
if (this.resoles.length === 0) {
|
|
87
|
-
(async () => {
|
|
88
|
-
while (!this.loaded) {
|
|
89
|
-
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
90
|
-
}
|
|
91
|
-
while (this.resoles.length > 0) {
|
|
92
|
-
this.resoles.pop()();
|
|
93
|
-
}
|
|
94
|
-
})();
|
|
95
|
-
}
|
|
96
|
-
this.resoles.unshift(resolve);
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
//遍历模型
|
|
101
|
-
async traverseMesh(callback) {
|
|
102
|
-
// 等待加载完成
|
|
103
|
-
await this.awaitLoaded();
|
|
104
|
-
this.traverse(obj => {
|
|
105
|
-
const mesh = obj;
|
|
106
|
-
if (mesh.isMesh) {
|
|
107
|
-
callback(mesh);
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
clock = new Clock();
|
|
112
|
-
//设置动画的权重
|
|
113
|
-
async setClipActionWeight(weights = {}) {
|
|
114
|
-
// 等待加载完成
|
|
115
|
-
await this.awaitLoaded();
|
|
116
|
-
for (let key in this.animationActions) {
|
|
117
|
-
this.animationActions[key].weight = weights[key] || 0;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
//获取动画列表
|
|
121
|
-
getAnimas() {
|
|
122
|
-
return Object.keys(this.animationActions);
|
|
123
|
-
}
|
|
124
|
-
//播放指定动画
|
|
125
|
-
setAnimas(anima = "") {
|
|
126
|
-
this.setClipActionWeight({ [anima]: 1 });
|
|
127
|
-
}
|
|
128
|
-
//停止所有动画
|
|
129
|
-
stop() {
|
|
130
|
-
this.setClipActionWeight();
|
|
131
|
-
}
|
|
132
|
-
updateMatrix() {
|
|
133
|
-
//播放动画
|
|
134
|
-
if (this.mixer) {
|
|
135
|
-
this.mixer.update(this.clock.getDelta());
|
|
136
|
-
}
|
|
137
|
-
// 深度应用属性
|
|
138
|
-
this.traverse(obj => {
|
|
139
|
-
const mesh = obj;
|
|
140
|
-
if (mesh.isMesh) {
|
|
141
|
-
mesh.frustumCulled = this.frustumCulled;
|
|
142
|
-
mesh.castShadow = this.castShadow;
|
|
143
|
-
mesh.receiveShadow = this.receiveShadow;
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
super.updateMatrix();
|
|
147
|
-
}
|
|
148
|
-
// 获取模型的尺寸
|
|
149
|
-
async getSize() {
|
|
150
|
-
await this.awaitLoaded();
|
|
151
|
-
return threeUtils.getSize(this);
|
|
152
|
-
}
|
|
153
|
-
// 居中
|
|
154
|
-
async center() {
|
|
155
|
-
await this.awaitLoaded();
|
|
156
|
-
threeUtils.center(this);
|
|
157
|
-
}
|
|
158
|
-
//底对齐
|
|
159
|
-
async alignBottom(y = 0) {
|
|
160
|
-
await this.awaitLoaded();
|
|
161
|
-
threeUtils.alignBottom(this, y);
|
|
162
|
-
}
|
|
163
|
-
// 放大到指定尺寸
|
|
164
|
-
async scaleTo(size = 1, key = ["x", "y", "z"]) {
|
|
165
|
-
await this.awaitLoaded();
|
|
166
|
-
threeUtils.scaleTo(this, size, key);
|
|
167
|
-
}
|
|
168
|
-
//按照法线拓展
|
|
169
|
-
expandBynormal(length) {
|
|
170
|
-
this.traverseMesh(mesh => {
|
|
171
|
-
// 没有法线就计算法线
|
|
172
|
-
mesh.geometry = mesh.geometry.clone();
|
|
173
|
-
if (!mesh.geometry.getAttribute("normal")) {
|
|
174
|
-
mesh.geometry.computeVertexNormals();
|
|
175
|
-
}
|
|
176
|
-
const normal = mesh.geometry.getAttribute("normal");
|
|
177
|
-
const position = mesh.geometry.getAttribute("position");
|
|
178
|
-
const v3 = new Vector3(0, 0, 0);
|
|
179
|
-
for (let i = 0; i < position.count; i++) {
|
|
180
|
-
v3.set(normal.getX(i), normal.getY(i), normal.getZ(i));
|
|
181
|
-
v3.setLength(length);
|
|
182
|
-
position.setXYZ(i, position.getX(i) + v3.x, position.getY(i) + v3.y, position.getZ(i) + v3.z);
|
|
183
|
-
}
|
|
184
|
-
position.needsUpdate = true;
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
}
|
|
1
|
+
import { AnimationMixer, Clock, Group, Vector3 } from "three";
|
|
2
|
+
import threeUtils from "./threeUtils";
|
|
3
|
+
import threeLoader from "./threeLoader";
|
|
4
|
+
import * as SkeletonUtils from 'three/examples/jsm/utils/SkeletonUtils';
|
|
5
|
+
// 模型基类
|
|
6
|
+
export default class MeshBase extends Group {
|
|
7
|
+
constructor(url) {
|
|
8
|
+
super();
|
|
9
|
+
url && this.loadModel(url);
|
|
10
|
+
}
|
|
11
|
+
url = "";
|
|
12
|
+
// 模型对象
|
|
13
|
+
static models = new Map();
|
|
14
|
+
// 模型是否在加载中
|
|
15
|
+
static loadModeling = new Map();
|
|
16
|
+
// 加载完成
|
|
17
|
+
loaded = false;
|
|
18
|
+
// 是否有动画
|
|
19
|
+
hasAnimation = false;
|
|
20
|
+
//动画混合器
|
|
21
|
+
mixer;
|
|
22
|
+
//动画剪辑对象数组
|
|
23
|
+
animationActions = {};
|
|
24
|
+
// 获取文件名的后缀
|
|
25
|
+
getSuffix(filename) {
|
|
26
|
+
const match = filename.match(/\.([^\.]+)$/);
|
|
27
|
+
return match ? match[1].toLowerCase() : '';
|
|
28
|
+
}
|
|
29
|
+
// 如果url 是blob地址的话,就用suffix字段来制定模型的类型
|
|
30
|
+
async loadModel(url, filename = "") {
|
|
31
|
+
this.url = url;
|
|
32
|
+
if (!url) {
|
|
33
|
+
this.clear();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
this.loaded = false;
|
|
37
|
+
// 没有模型并且没有加载任务时就去加载模型
|
|
38
|
+
if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
|
|
39
|
+
MeshBase.loadModeling.set(url, true);
|
|
40
|
+
// 万能加载器
|
|
41
|
+
let modal = await threeLoader.loadModel(url, filename);
|
|
42
|
+
MeshBase.models.set(url, modal);
|
|
43
|
+
MeshBase.loadModeling.set(url, false);
|
|
44
|
+
}
|
|
45
|
+
// 如果模型还在加载中, 就等待模型加载完成
|
|
46
|
+
do {
|
|
47
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
48
|
+
} while (MeshBase.loadModeling.get(url));
|
|
49
|
+
if (MeshBase.models.get(url)) {
|
|
50
|
+
this.clear();
|
|
51
|
+
const mesh = MeshBase.models.get(url);
|
|
52
|
+
// 判断是否有动画
|
|
53
|
+
this.hasAnimation = mesh.animations.length > 0;
|
|
54
|
+
if (this.hasAnimation) {
|
|
55
|
+
const model = SkeletonUtils.clone(mesh);
|
|
56
|
+
this.mixer = new AnimationMixer(model);
|
|
57
|
+
this.animationActions = {};
|
|
58
|
+
mesh.animations.map(animation => {
|
|
59
|
+
const animationAction = this.mixer.clipAction(animation).play();
|
|
60
|
+
animationAction.weight = 0;
|
|
61
|
+
this.animationActions[animation.name] = animationAction;
|
|
62
|
+
});
|
|
63
|
+
this.add(model);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
this.add(mesh.clone(true));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
70
|
+
this.loaded = true;
|
|
71
|
+
// 加载完成事件
|
|
72
|
+
this.dispatchEvent({ type: "loaded" });
|
|
73
|
+
}
|
|
74
|
+
// 超出绘制区域时不绘制
|
|
75
|
+
frustumCulled = false;
|
|
76
|
+
// 创建阴影
|
|
77
|
+
castShadow = false;
|
|
78
|
+
// 接受阴影
|
|
79
|
+
receiveShadow = false;
|
|
80
|
+
// promise.resole 队列
|
|
81
|
+
resoles = [];
|
|
82
|
+
// 等待加载加载完成
|
|
83
|
+
async awaitLoaded() {
|
|
84
|
+
if (!this.loaded) {
|
|
85
|
+
await new Promise((resolve) => {
|
|
86
|
+
if (this.resoles.length === 0) {
|
|
87
|
+
(async () => {
|
|
88
|
+
while (!this.loaded) {
|
|
89
|
+
await new Promise(resolve => requestAnimationFrame(resolve));
|
|
90
|
+
}
|
|
91
|
+
while (this.resoles.length > 0) {
|
|
92
|
+
this.resoles.pop()();
|
|
93
|
+
}
|
|
94
|
+
})();
|
|
95
|
+
}
|
|
96
|
+
this.resoles.unshift(resolve);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//遍历模型
|
|
101
|
+
async traverseMesh(callback) {
|
|
102
|
+
// 等待加载完成
|
|
103
|
+
await this.awaitLoaded();
|
|
104
|
+
this.traverse(obj => {
|
|
105
|
+
const mesh = obj;
|
|
106
|
+
if (mesh.isMesh) {
|
|
107
|
+
callback(mesh);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
clock = new Clock();
|
|
112
|
+
//设置动画的权重
|
|
113
|
+
async setClipActionWeight(weights = {}) {
|
|
114
|
+
// 等待加载完成
|
|
115
|
+
await this.awaitLoaded();
|
|
116
|
+
for (let key in this.animationActions) {
|
|
117
|
+
this.animationActions[key].weight = weights[key] || 0;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
//获取动画列表
|
|
121
|
+
getAnimas() {
|
|
122
|
+
return Object.keys(this.animationActions);
|
|
123
|
+
}
|
|
124
|
+
//播放指定动画
|
|
125
|
+
setAnimas(anima = "") {
|
|
126
|
+
this.setClipActionWeight({ [anima]: 1 });
|
|
127
|
+
}
|
|
128
|
+
//停止所有动画
|
|
129
|
+
stop() {
|
|
130
|
+
this.setClipActionWeight();
|
|
131
|
+
}
|
|
132
|
+
updateMatrix() {
|
|
133
|
+
//播放动画
|
|
134
|
+
if (this.mixer) {
|
|
135
|
+
this.mixer.update(this.clock.getDelta());
|
|
136
|
+
}
|
|
137
|
+
// 深度应用属性
|
|
138
|
+
this.traverse(obj => {
|
|
139
|
+
const mesh = obj;
|
|
140
|
+
if (mesh.isMesh) {
|
|
141
|
+
mesh.frustumCulled = this.frustumCulled;
|
|
142
|
+
mesh.castShadow = this.castShadow;
|
|
143
|
+
mesh.receiveShadow = this.receiveShadow;
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
super.updateMatrix();
|
|
147
|
+
}
|
|
148
|
+
// 获取模型的尺寸
|
|
149
|
+
async getSize() {
|
|
150
|
+
await this.awaitLoaded();
|
|
151
|
+
return threeUtils.getSize(this);
|
|
152
|
+
}
|
|
153
|
+
// 居中
|
|
154
|
+
async center() {
|
|
155
|
+
await this.awaitLoaded();
|
|
156
|
+
threeUtils.center(this);
|
|
157
|
+
}
|
|
158
|
+
//底对齐
|
|
159
|
+
async alignBottom(y = 0) {
|
|
160
|
+
await this.awaitLoaded();
|
|
161
|
+
threeUtils.alignBottom(this, y);
|
|
162
|
+
}
|
|
163
|
+
// 放大到指定尺寸
|
|
164
|
+
async scaleTo(size = 1, key = ["x", "y", "z"]) {
|
|
165
|
+
await this.awaitLoaded();
|
|
166
|
+
threeUtils.scaleTo(this, size, key);
|
|
167
|
+
}
|
|
168
|
+
//按照法线拓展
|
|
169
|
+
expandBynormal(length) {
|
|
170
|
+
this.traverseMesh(mesh => {
|
|
171
|
+
// 没有法线就计算法线
|
|
172
|
+
mesh.geometry = mesh.geometry.clone();
|
|
173
|
+
if (!mesh.geometry.getAttribute("normal")) {
|
|
174
|
+
mesh.geometry.computeVertexNormals();
|
|
175
|
+
}
|
|
176
|
+
const normal = mesh.geometry.getAttribute("normal");
|
|
177
|
+
const position = mesh.geometry.getAttribute("position");
|
|
178
|
+
const v3 = new Vector3(0, 0, 0);
|
|
179
|
+
for (let i = 0; i < position.count; i++) {
|
|
180
|
+
v3.set(normal.getX(i), normal.getY(i), normal.getZ(i));
|
|
181
|
+
v3.setLength(length);
|
|
182
|
+
position.setXYZ(i, position.getX(i) + v3.x, position.getY(i) + v3.y, position.getZ(i) + v3.z);
|
|
183
|
+
}
|
|
184
|
+
position.needsUpdate = true;
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
package/Mirror.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { BufferGeometry, Camera, Group, Material, Matrix4, Mesh, PerspectiveCamera, Plane, PlaneGeometry, Scene, ShaderMaterial, Vector3, Vector4, WebGLRenderTarget, WebGLRenderer } from "three";
|
|
2
|
-
export default class Mirror extends Mesh {
|
|
3
|
-
constructor(geometry?: BufferGeometry);
|
|
4
|
-
static geometry: PlaneGeometry;
|
|
5
|
-
isReflector: boolean;
|
|
6
|
-
type: string;
|
|
7
|
-
camera: PerspectiveCamera;
|
|
8
|
-
reflectorPlane: Plane;
|
|
9
|
-
normal: Vector3;
|
|
10
|
-
reflectorWorldPosition: Vector3;
|
|
11
|
-
cameraWorldPosition: Vector3;
|
|
12
|
-
rotationMatrix: Matrix4;
|
|
13
|
-
lookAtPosition: Vector3;
|
|
14
|
-
clipPlane: Vector4;
|
|
15
|
-
view: Vector3;
|
|
16
|
-
target: Vector3;
|
|
17
|
-
q: Vector4;
|
|
18
|
-
textureMatrix: Matrix4;
|
|
19
|
-
virtualCamera: PerspectiveCamera;
|
|
20
|
-
renderTarget: WebGLRenderTarget<import("three").Texture>;
|
|
21
|
-
material: ShaderMaterial;
|
|
22
|
-
onBeforeRender: (renderer: WebGLRenderer, scene: Scene, camera: Camera, geometry: BufferGeometry, material: Material, group: Group) => void;
|
|
23
|
-
getRenderTarget(): WebGLRenderTarget<import("three").Texture>;
|
|
24
|
-
dispose(): void;
|
|
25
|
-
}
|
|
1
|
+
import { BufferGeometry, Camera, Group, Material, Matrix4, Mesh, PerspectiveCamera, Plane, PlaneGeometry, Scene, ShaderMaterial, Vector3, Vector4, WebGLRenderTarget, WebGLRenderer } from "three";
|
|
2
|
+
export default class Mirror extends Mesh {
|
|
3
|
+
constructor(geometry?: BufferGeometry);
|
|
4
|
+
static geometry: PlaneGeometry;
|
|
5
|
+
isReflector: boolean;
|
|
6
|
+
type: string;
|
|
7
|
+
camera: PerspectiveCamera;
|
|
8
|
+
reflectorPlane: Plane;
|
|
9
|
+
normal: Vector3;
|
|
10
|
+
reflectorWorldPosition: Vector3;
|
|
11
|
+
cameraWorldPosition: Vector3;
|
|
12
|
+
rotationMatrix: Matrix4;
|
|
13
|
+
lookAtPosition: Vector3;
|
|
14
|
+
clipPlane: Vector4;
|
|
15
|
+
view: Vector3;
|
|
16
|
+
target: Vector3;
|
|
17
|
+
q: Vector4;
|
|
18
|
+
textureMatrix: Matrix4;
|
|
19
|
+
virtualCamera: PerspectiveCamera;
|
|
20
|
+
renderTarget: WebGLRenderTarget<import("three").Texture>;
|
|
21
|
+
material: ShaderMaterial;
|
|
22
|
+
onBeforeRender: (renderer: WebGLRenderer, scene: Scene, camera: Camera, geometry: BufferGeometry, material: Material, group: Group) => void;
|
|
23
|
+
getRenderTarget(): WebGLRenderTarget<import("three").Texture>;
|
|
24
|
+
dispose(): void;
|
|
25
|
+
}
|