three-base 2.0.25 → 2.0.26

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.
@@ -1,15 +1,16 @@
1
- import { Group, InstancedMesh, Matrix4, Mesh, Vector3, Color } from 'three';
1
+ import { Color, Group, InstancedMesh, Matrix4, Mesh } from 'three';
2
2
  export default class InstancedGroupMesh extends Mesh {
3
- url: string;
3
+ children: InstancedMesh[];
4
4
  isInstancedGroupMesh: boolean;
5
5
  count: number;
6
- size: Vector3;
7
6
  meshCollect: Record<string, Mesh[]>;
8
7
  instanceCollect: Record<string, InstancedMesh>;
9
- children: InstancedMesh[];
10
8
  static matrix: Matrix4;
11
9
  constructor(group: Group, count: number);
12
10
  computeBoundingSphere: import("lodash").DebouncedFunc<() => void>;
13
11
  setMatrixAt(index: number, matrix: Matrix4): void;
14
12
  setColorAt(index: number, color: (instancedMesh: InstancedMesh) => Color | undefined): void;
13
+ updateMatrixWorld(force?: boolean): void;
14
+ addEventListener(name: string, callback: (e: any) => void): void;
15
+ removeEventListener(name: string, callback: (e: any) => void): void;
15
16
  }
@@ -2,29 +2,26 @@
2
2
  * @Author: xiaosihan
3
3
  * @Date: 2023-02-21 09:55:52
4
4
  * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2024-01-20 21:58:20
5
+ * @Last Modified time: 2024-01-27 17:41:26
6
6
  */
7
7
  import { throttle } from 'lodash';
8
- import { InstancedMesh, Matrix4, Mesh, Vector3 } from 'three';
9
- import threeUtils from './threeUtils';
8
+ import { InstancedMesh, Matrix4, Mesh } from 'three';
10
9
  // 模型组转成 实例对象
11
10
  export default class InstancedGroupMesh extends Mesh {
12
- url = "";
13
11
  isInstancedGroupMesh = true;
14
12
  count = 0;
15
- size = new Vector3(1, 1, 1);
16
13
  meshCollect = {};
17
14
  instanceCollect = {};
18
15
  static matrix = new Matrix4();
19
16
  constructor(group, count) {
20
17
  super();
21
- this.size.copy(threeUtils.getSize(group));
22
18
  this.count = count;
23
19
  const { meshCollect, instanceCollect } = this;
24
20
  group.updateMatrixWorld(true);
25
21
  // collect all mesh counts, in case there are more than one copy of the same mesh in this group
26
22
  group.traverse((obj) => {
27
23
  const mesh = obj;
24
+ obj.userData = this.userData;
28
25
  if (mesh.isMesh) {
29
26
  mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
30
27
  mesh.rotation.setFromQuaternion(mesh.quaternion);
@@ -49,8 +46,6 @@ export default class InstancedGroupMesh extends Mesh {
49
46
  const meshes = meshCollect[uuid];
50
47
  const mesh = meshes[0];
51
48
  const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * count);
52
- instancedMesh.name = mesh.name;
53
- instancedMesh.frustumCulled = false;
54
49
  instanceCollect[uuid] = instancedMesh;
55
50
  this.add(instancedMesh);
56
51
  });
@@ -88,4 +83,24 @@ export default class InstancedGroupMesh extends Mesh {
88
83
  });
89
84
  });
90
85
  }
86
+ // 同步属性
87
+ updateMatrixWorld(force) {
88
+ super.updateMatrixWorld(force);
89
+ const { cursor, enableEvent } = this.userData;
90
+ this.traverse(obj => {
91
+ Object.assign(obj.userData, { cursor, enableEvent });
92
+ });
93
+ }
94
+ // 事件监听
95
+ addEventListener(name, callback) {
96
+ this.children.map(mesh => {
97
+ mesh.addEventListener(name, callback);
98
+ });
99
+ }
100
+ // 取消事件的监听
101
+ removeEventListener(name, callback) {
102
+ this.children.map(mesh => {
103
+ mesh.removeEventListener(name, callback);
104
+ });
105
+ }
91
106
  }
@@ -2,37 +2,29 @@
2
2
  * @Author: xiaosihan
3
3
  * @Date: 2023-02-21 09:55:52
4
4
  * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2024-01-20 21:58:20
5
+ * @Last Modified time: 2024-01-27 17:41:26
6
6
  */
7
7
 
8
8
  import { throttle } from 'lodash';
9
- import { Group, InstancedMesh, Material, Matrix4, Mesh, Vector3, Color } from 'three'
10
- import threeUtils from './threeUtils';
9
+ import { Color, Group, InstancedMesh, Material, Matrix4, Mesh } from 'three';
11
10
 
12
11
  // 模型组转成 实例对象
13
12
  export default class InstancedGroupMesh extends Mesh {
14
13
 
15
- url = "";
14
+ declare children: InstancedMesh[];
16
15
 
17
16
  isInstancedGroupMesh = true
18
17
 
19
- count: number = 0
20
-
21
- size = new Vector3(1, 1, 1);
18
+ count: number = 0;
22
19
 
23
20
  meshCollect: Record<string, Mesh[]> = {}
24
21
 
25
22
  instanceCollect: Record<string, InstancedMesh> = {}
26
23
 
27
- declare children: InstancedMesh[];
28
-
29
24
  static matrix = new Matrix4()
30
25
 
31
26
  constructor(group: Group, count: number) {
32
27
  super()
33
-
34
- this.size.copy(threeUtils.getSize(group));
35
-
36
28
  this.count = count
37
29
 
38
30
  const { meshCollect, instanceCollect } = this
@@ -41,7 +33,10 @@ export default class InstancedGroupMesh extends Mesh {
41
33
 
42
34
  // collect all mesh counts, in case there are more than one copy of the same mesh in this group
43
35
  group.traverse((obj) => {
44
- const mesh = obj as Mesh
36
+ const mesh = obj as Mesh;
37
+
38
+ obj.userData = this.userData;
39
+
45
40
  if (mesh.isMesh) {
46
41
 
47
42
  mesh.matrixWorld.decompose(mesh.position, mesh.quaternion, mesh.scale);
@@ -68,15 +63,13 @@ export default class InstancedGroupMesh extends Mesh {
68
63
  Object.keys(meshCollect).forEach(uuid => {
69
64
  const meshes = meshCollect[uuid]
70
65
  const mesh = meshes[0]
66
+
71
67
  const instancedMesh = new InstancedMesh(mesh.geometry, mesh.material, meshes.length * count)
72
- instancedMesh.name = mesh.name;
73
- instancedMesh.frustumCulled = false;
74
- instanceCollect[uuid] = instancedMesh
68
+ instanceCollect[uuid] = instancedMesh;
75
69
  this.add(instancedMesh)
76
70
  })
77
71
  }
78
72
 
79
-
80
73
  // 更新包围盒
81
74
  computeBoundingSphere = throttle(() => {
82
75
  this.traverse(m => {
@@ -91,12 +84,10 @@ export default class InstancedGroupMesh extends Mesh {
91
84
  collect.forEach((mesh: Mesh, i: number) => {
92
85
  InstancedGroupMesh.matrix.copy(mesh.matrix)
93
86
  InstancedGroupMesh.matrix.premultiply(matrix)
94
-
95
87
  instancedMesh.setMatrixAt(collect.length * index + i, InstancedGroupMesh.matrix);
96
88
  instancedMesh.instanceMatrix.needsUpdate = true;
97
89
  })
98
90
  })
99
-
100
91
  this.computeBoundingSphere();
101
92
  }
102
93
 
@@ -115,4 +106,28 @@ export default class InstancedGroupMesh extends Mesh {
115
106
  })
116
107
  }
117
108
 
118
- }
109
+ // 同步属性
110
+ updateMatrixWorld(force?: boolean): void {
111
+ super.updateMatrixWorld(force);
112
+ const { cursor, enableEvent } = this.userData;
113
+ this.traverse(obj => {
114
+ Object.assign(obj.userData, { cursor, enableEvent });
115
+ });
116
+ }
117
+
118
+ // 事件监听
119
+ addEventListener(name: string, callback: (e: any) => void) {
120
+ this.children.map(mesh => {
121
+ mesh.addEventListener(name, callback);
122
+ });
123
+ }
124
+
125
+ // 取消事件的监听
126
+ removeEventListener(name: string, callback: (e: any) => void) {
127
+ this.children.map(mesh => {
128
+ mesh.removeEventListener(name, callback);
129
+ });
130
+ }
131
+
132
+ }
133
+
package/ThreeMouse.d.ts CHANGED
@@ -11,7 +11,7 @@ export interface ThreeMouseEvent extends Intersection {
11
11
  y: number;
12
12
  }
13
13
  /**
14
- * harp.gl 专用的鼠标拾取模型对象
14
+ * three.js 专用的鼠标拾取模型对象
15
15
  */
16
16
  export default class ThreeMouse {
17
17
  constructor(renderer: THREE.WebGLRenderer, camera: THREE.Camera, scene: THREE.Scene);
package/ThreeMouse.js CHANGED
@@ -8,7 +8,7 @@ import * as THREE from "three";
8
8
  import { Raycaster } from "three";
9
9
  import { debounce } from "lodash";
10
10
  /**
11
- * harp.gl 专用的鼠标拾取模型对象
11
+ * three.js 专用的鼠标拾取模型对象
12
12
  */
13
13
  export default class ThreeMouse {
14
14
  constructor(renderer, camera, scene) {
@@ -108,7 +108,7 @@ export default class ThreeMouse {
108
108
  let intersection = this._getSelected(meshs);
109
109
  if (intersection) {
110
110
  const { face, object } = intersection;
111
- face && face.normal.applyMatrix4(object.matrixWorld);
111
+ // face && face.normal.applyMatrix4(object.matrixWorld);
112
112
  const MouseEvent = Object.assign(intersection, {
113
113
  renderer: this._renderer,
114
114
  camera: this._camera,
package/ThreeMouse.ts CHANGED
@@ -21,7 +21,7 @@ export interface ThreeMouseEvent extends Intersection {
21
21
 
22
22
 
23
23
  /**
24
- * harp.gl 专用的鼠标拾取模型对象
24
+ * three.js 专用的鼠标拾取模型对象
25
25
  */
26
26
  export default class ThreeMouse {
27
27
 
@@ -152,7 +152,7 @@ export default class ThreeMouse {
152
152
 
153
153
  if (intersection) {
154
154
  const { face, object } = intersection;
155
- face && face.normal.applyMatrix4(object.matrixWorld);
155
+ // face && face.normal.applyMatrix4(object.matrixWorld);
156
156
  const MouseEvent: ThreeMouseEvent = Object.assign(intersection, { // 将来可能用到的对象
157
157
  renderer: this._renderer,
158
158
  camera: this._camera,
package/Transition.js CHANGED
@@ -47,7 +47,9 @@ export default class Transition {
47
47
  _delay = 0;
48
48
  // 设置过渡时间
49
49
  setDuration(duration = 500) {
50
- this._duration = duration;
50
+ setTimeout(() => {
51
+ this._duration = duration;
52
+ }, 0);
51
53
  return this;
52
54
  }
53
55
  // 设置延时
package/Transition.ts CHANGED
@@ -69,7 +69,9 @@ export default class Transition {
69
69
 
70
70
  // 设置过渡时间
71
71
  setDuration(duration = 500): this {
72
- this._duration = duration;
72
+ setTimeout(() => {
73
+ this._duration = duration;
74
+ }, 0);
73
75
  return this;
74
76
  }
75
77
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-base",
3
- "version": "2.0.25",
3
+ "version": "2.0.26",
4
4
  "description": "three 3d three-base",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"
package/threeLoader.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { EventDispatcher, Texture, TextureLoader } from "three";
1
+ import { EventDispatcher, Object3D, Texture, TextureLoader } from "three";
2
2
  import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
3
3
  import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
4
4
  import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader";
@@ -16,6 +16,8 @@ declare class ThreeLoader extends EventDispatcher {
16
16
  stlLoader: STLLoader;
17
17
  mtlLoader: MTLLoader;
18
18
  gltfLoader: GLTFLoader;
19
+ /** 加载模型支持glb,fbx,stl,obj */
20
+ loadModal(url: string): Promise<Object3D<import("three").Object3DEventMap>>;
19
21
  onload: boolean;
20
22
  }
21
23
  declare const threeLoader: ThreeLoader;
package/threeLoader.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * @Last Modified by: xiaosihan
5
5
  * @Last Modified time: 2023-02-07 11:11:48
6
6
  */
7
- import { CanvasTexture, DefaultLoadingManager, EventDispatcher, RepeatWrapping, TextureLoader, VideoTexture } from "three";
7
+ import { CanvasTexture, DefaultLoadingManager, EventDispatcher, Mesh, MeshLambertMaterial, Object3D, RepeatWrapping, TextureLoader, VideoTexture } from "three";
8
8
  import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
9
9
  import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
10
10
  import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader";
@@ -114,6 +114,37 @@ class ThreeLoader extends EventDispatcher {
114
114
  loader.setDRACOLoader(dracoLoader);
115
115
  return loader;
116
116
  })();
117
+ /** 加载模型支持glb,fbx,stl,obj */
118
+ async loadModal(url) {
119
+ let Object3d = new Object3D();
120
+ if (/\.fbx$/i.test(url)) {
121
+ await threeLoader.fbxLoader.loadAsync(url).then(res => {
122
+ Object3d = res;
123
+ }).catch(() => {
124
+ console.error(`${url},加载失败`);
125
+ });
126
+ }
127
+ else if (/\.(gltf|glb)$/i.test(url)) {
128
+ await threeLoader.gltfLoader.loadAsync(url).then(res => {
129
+ Object3d = res.scene;
130
+ Object3d.animations = res.animations;
131
+ }).catch(() => {
132
+ console.error(`${url},加载失败`);
133
+ });
134
+ }
135
+ else if (/\.stl$/i.test(url)) {
136
+ await threeLoader.stlLoader.loadAsync(url).then(geometry => {
137
+ const material = new MeshLambertMaterial();
138
+ Object3d = new Mesh(geometry, material);
139
+ }).catch(() => {
140
+ console.error(`${url},加载失败`);
141
+ });
142
+ }
143
+ else {
144
+ throw new Error(`${url},不支持的格式`);
145
+ }
146
+ return Object3d;
147
+ }
117
148
  // 是否加载完成
118
149
  onload = true;
119
150
  }
package/threeLoader.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * @Last Modified time: 2023-02-07 11:11:48
6
6
  */
7
7
 
8
- import { CanvasTexture, DefaultLoadingManager, EventDispatcher, RepeatWrapping, SRGBColorSpace, Texture, TextureLoader, VideoTexture } from "three";
8
+ import { CanvasTexture, DefaultLoadingManager, EventDispatcher, Mesh, MeshLambertMaterial, Object3D, RepeatWrapping, SRGBColorSpace, Texture, TextureLoader, VideoTexture } from "three";
9
9
  import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
10
10
  import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
11
11
  import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader";
@@ -141,6 +141,38 @@ class ThreeLoader extends EventDispatcher {
141
141
  return loader;
142
142
  })()
143
143
 
144
+
145
+ /** 加载模型支持glb,fbx,stl,obj */
146
+ async loadModal(url: string) {
147
+ let Object3d = new Object3D();
148
+
149
+ if (/\.fbx$/i.test(url)) {
150
+ await threeLoader.fbxLoader.loadAsync(url).then(res => {
151
+ Object3d = res;
152
+ }).catch(() => {
153
+ console.error(`${url},加载失败`);
154
+ })
155
+ } else if (/\.(gltf|glb)$/i.test(url)) {
156
+ await threeLoader.gltfLoader.loadAsync(url).then(res => {
157
+ Object3d = res.scene;
158
+ Object3d.animations = res.animations;
159
+ }).catch(() => {
160
+ console.error(`${url},加载失败`);
161
+ })
162
+ } else if (/\.stl$/i.test(url)) {
163
+ await threeLoader.stlLoader.loadAsync(url).then(geometry => {
164
+ const material = new MeshLambertMaterial();
165
+ Object3d = new Mesh(geometry, material);
166
+ }).catch(() => {
167
+ console.error(`${url},加载失败`);
168
+ })
169
+ } else {
170
+ throw new Error(`${url},不支持的格式`);
171
+ }
172
+
173
+ return Object3d;
174
+ }
175
+
144
176
  // 是否加载完成
145
177
  onload = true;
146
178