three-base 2.0.87 → 2.0.89

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/ThreeMouse.ts DELETED
@@ -1,483 +0,0 @@
1
- //
2
- //
3
- //
4
- //
5
- //
6
- // 启动鼠标事件
7
- import * as THREE from "three";
8
- import { Object3D, Raycaster, Scene } from "three";
9
- import type { Intersection } from "three";
10
- import { debounce } from "lodash";
11
-
12
- export interface ThreeMouseEvent extends Intersection {
13
- e: MouseEvent;
14
- renderer: THREE.Renderer;
15
- camera: THREE.Camera;
16
- type: string;
17
- offsetX: number;
18
- offsetY: number
19
- x: number;
20
- y: number;
21
- }
22
-
23
-
24
- /**
25
- * three.js 专用的鼠标拾取模型对象
26
- */
27
- export default class ThreeMouse {
28
-
29
- constructor(renderer: THREE.WebGLRenderer, camera: THREE.Camera, scene: THREE.Scene) {
30
-
31
- this._renderer = renderer;
32
- this._camera = camera;
33
- this._scene = scene;
34
- this._frame = this._renderer.info.render.frame;
35
-
36
- this._renderer.domElement.addEventListener("mouseenter", () => { this._mouseInRenderer = true }, true); //鼠标进入开启拾取
37
- this._renderer.domElement.addEventListener("mouseleave", this._mouseleave, true); //鼠标离开停止拾取
38
-
39
- this._renderer.domElement.addEventListener("mousemove", this._mousemove, true); //鼠标移动监听事件
40
- this._renderer.domElement.addEventListener("pointerdown", this._mousedown, true); //鼠标按下监听事件
41
-
42
- this._renderer.domElement.addEventListener("pointerup", this._mouseup, true); //鼠标弹起监听事件
43
- }
44
-
45
- // 渲染器
46
- _renderer!: THREE.WebGLRenderer;
47
-
48
- _camera!: THREE.Camera;
49
-
50
- _scene!: Scene;
51
-
52
- // 鼠标在渲染器上面的位置
53
- _mosePosition = new THREE.Vector2(0, 0);
54
-
55
- // 鼠标按下的位置 如果和鼠标弹起的位置相同 才算一次有效的点击事件
56
- _mouseDownPosition = new THREE.Vector2(0, 0);
57
-
58
- //当前鼠标指针上的对象
59
- _mesh_on?: Object3D;
60
-
61
- //鼠标按下的对象
62
- _mesh_Down?: Object3D;
63
-
64
- //射线拾取器
65
- _raycaster = new Raycaster();
66
-
67
- // 当前渲染的帧数 ( 鼠标滑动事件 一帧只需要执行费一次即可 )
68
- _frame: number = 0;
69
-
70
- //鼠标是否在渲染器上
71
- _mouseInRenderer: boolean = false;
72
-
73
- // 鼠标滑动的事件
74
- _mousemoveEvent?: MouseEvent;
75
-
76
- // 鼠标事件
77
- _mouseEvent?: MouseEvent;
78
-
79
- // 需要被拾取的对象数组
80
- _object3ds: Object3D[] = [];
81
-
82
- // 点击次数
83
- _click_num = 0;
84
-
85
- // 获取拾取到的对象
86
- _getSelected(object3ds: Object3D[]): Intersection | undefined {
87
- let intersection: Intersection | undefined;
88
-
89
- if (object3ds.length) {
90
- intersection = this._raycaster.intersectObjects(object3ds, false)[0];
91
- }
92
-
93
- return intersection;
94
- };
95
-
96
- // 判断对象上面是否有事件
97
- hasEvent(object?: Object3D, types: Array<"leftclick" | "rightclick" | "click" | "dbclick" | "mouseenter" | "mouseleave" | "mousemove" | "mousedown" | "mouseup"> = []) {
98
-
99
- if (!object) {
100
- return false;
101
- }
102
-
103
- // 如果 启用事件为关闭的话就不执行事件
104
- if (object["userData"] && object["userData"]["enableEvent"] === false) {
105
- return false;
106
- }
107
-
108
- const hasevent = types.some(type => {
109
- //@ts-ignore
110
- if (object["_listeners"] && object["_listeners"][type] && object["_listeners"][type][0]) {
111
- return true
112
- }
113
- // if (object["userData"] && object["userData"]["cursor"]) {
114
- // return true;
115
- // }
116
- return false;
117
- });
118
-
119
- return hasevent;
120
- }
121
-
122
- // 判断对象上是否有鼠标样式
123
- hasCursor(object?: Object3D) {
124
- if (!object) {
125
- return false;
126
- }
127
- // 如果 启用事件为关闭的话就不执行事件
128
- if (object["userData"] && object["userData"]["enableEvent"] === false) {
129
- return false;
130
- }
131
- if (object["userData"] && object["userData"]["cursor"]) {
132
- return true;
133
- }
134
- }
135
-
136
- // 分发鼠标拾取事件
137
- dispatchEvent() {
138
- // 提取有事件的对象
139
- const meshs: Object3D[] = [];
140
-
141
- this._scene.traverseVisible(mesh => {
142
- // 判断对象是否有指定的事件
143
- if (this.hasCursor(mesh) || this.hasEvent(mesh, ["leftclick", "rightclick", "click", "dbclick", "mouseenter", "mouseleave", "mousemove", "mousedown", "mouseup"])) {
144
- meshs.push(mesh);
145
- }
146
- });
147
-
148
- this._object3ds = meshs;
149
-
150
- // 没有鼠标事件就不往下执行了
151
- if (!this._mousemoveEvent || !this._mouseInRenderer) {
152
- return;
153
- }
154
- // 拾取模型
155
- let intersection = this._getSelected(meshs);
156
-
157
- if (intersection) {
158
- const { face, object } = intersection;
159
- // face && face.normal.applyMatrix4(object.matrixWorld);
160
- const MouseEvent: ThreeMouseEvent = Object.assign(intersection, { // 将来可能用到的对象
161
- e: this._mousemoveEvent,
162
- renderer: this._renderer,
163
- camera: this._camera,
164
- object: intersection.object,
165
- type: this._mousemoveEvent.type,
166
- offsetX: this._mousemoveEvent.offsetX,
167
- offsetY: this._mousemoveEvent.offsetY,
168
- x: this._mousemoveEvent.x,
169
- y: this._mousemoveEvent.y
170
- });
171
-
172
- // 触发滑动事件
173
- if (this._mesh_on === MouseEvent.object) {
174
- if (this.hasEvent(MouseEvent.object, ["mousemove"])) {
175
- MouseEvent.type = "mousemove";
176
- MouseEvent.object.dispatchEvent(MouseEvent as never);
177
- }
178
-
179
- // 鼠标划入滑出事件
180
- } else if (this._mesh_on !== MouseEvent.object) {
181
-
182
- if (this.hasEvent(this._mesh_on, ["mouseleave"])) {
183
- MouseEvent.type = "mouseleave";
184
- this._mesh_on!.dispatchEvent(MouseEvent as never);
185
- }
186
-
187
- if (this.hasEvent(MouseEvent.object, ["mouseenter"])) {
188
- MouseEvent.type = "mouseenter";
189
- MouseEvent.object.dispatchEvent(MouseEvent as never);
190
- }
191
-
192
- }
193
-
194
- this._mesh_on = MouseEvent.object || undefined; // 设置为当前鼠标上的对象
195
-
196
- // 鼠标样式
197
- if (this._mesh_on && this._mesh_on.userData && this._mesh_on.userData.cursor) {
198
- this._renderer.domElement.style.cursor = this._mesh_on.userData.cursor;
199
- } else {
200
- this._renderer.domElement.style.cursor = "auto";
201
- }
202
-
203
- } else {
204
-
205
- if (this.hasEvent(this._mesh_on, ["mouseleave"])) {
206
- // 鼠标离开事件
207
- this._mesh_on!.dispatchEvent({
208
- e: this._mouseEvent,
209
- renderer: this._renderer,
210
- camera: this._camera,
211
- object: this._mesh_on as Object3D,
212
- type: "mouseleave",
213
- offsetX: this._mousemoveEvent.offsetX,
214
- offsetY: this._mousemoveEvent.offsetY,
215
- x: this._mousemoveEvent.x,
216
- y: this._mousemoveEvent.y
217
- } as never);
218
- }
219
-
220
-
221
- // 置空当前鼠标上面的对象
222
- this._mesh_on = undefined;
223
-
224
- this._renderer.domElement.style.cursor = "auto"; // 鼠标样式
225
- }
226
-
227
- }
228
-
229
- // 鼠标离开事件
230
- _mouseleave = (e: MouseEvent) => {
231
- this._mouseEvent = e;
232
- this._mouseInRenderer = false;
233
-
234
- // 鼠标离开事件
235
- if (this._mousemoveEvent && this.hasEvent(this._mesh_on, ["mouseleave"])) {
236
- this._mesh_on!.dispatchEvent({
237
- e: this._mouseEvent,
238
- renderer: this._renderer,
239
- camera: this._camera,
240
- object: this._mesh_on as Object3D,
241
- type: "mouseleave",
242
- offsetX: this._mousemoveEvent.offsetX,
243
- offsetY: this._mousemoveEvent.offsetY,
244
- x: this._mousemoveEvent.x,
245
- y: this._mousemoveEvent.y
246
- } as never);
247
- }
248
-
249
- // 置空当前鼠标上面的对象
250
- this._mesh_on = undefined;
251
-
252
- this._renderer.domElement.style.cursor = "auto"; // 鼠标样式
253
- }
254
-
255
- // 鼠标滑动 设置鼠标位置
256
- _mousemove = (e: MouseEvent) => {
257
-
258
- this._mousemoveEvent = e;
259
-
260
- this._frame = this._renderer.info.render.frame;
261
-
262
- if (document.pointerLockElement) { // 如果当前有元素 锁住了鼠标
263
-
264
- this._mosePosition.set(0, 0); // 鼠标就指向canvas 的正中心
265
- } else {
266
-
267
- this._mosePosition.x = (e.offsetX / this._renderer.domElement.offsetWidth) * 2 - 1;
268
- this._mosePosition.y = -(e.offsetY / this._renderer.domElement.offsetHeight) * 2 + 1;
269
- }
270
-
271
- this._raycaster.setFromCamera(this._mosePosition, this._camera); // 设置射线拾取的参数
272
-
273
- };
274
-
275
- // 鼠标按下事件
276
- _mousedown = (e: MouseEvent) => {
277
- this._mouseEvent = e;
278
- // 记录按下的位置
279
- this._mouseDownPosition.set(e.offsetX, e.offsetY);
280
-
281
- this._mousemove(e);
282
-
283
- let intersection = this._getSelected(this._object3ds);
284
-
285
- if (!intersection || !this._mousemoveEvent) {
286
- return
287
- }
288
-
289
- this._mesh_Down = this._mesh_on = intersection.object as Object3D;
290
-
291
- // 触发鼠标按下事件
292
- if (this.hasEvent(this._mesh_Down, ["mousedown"])) {
293
-
294
- this._mesh_Down.dispatchEvent({
295
- e: this._mouseEvent,
296
- ...intersection,
297
- renderer: this._renderer,
298
- camera: this._camera,
299
- object: intersection.object as Object3D,
300
- type: "mousedown",
301
- offsetX: this._mousemoveEvent.offsetX,
302
- offsetY: this._mousemoveEvent.offsetY,
303
- x: this._mousemoveEvent.x,
304
- y: this._mousemoveEvent.y
305
- } as never);
306
-
307
- }
308
-
309
- }
310
-
311
- // 鼠标弹起事件
312
- _mouseup = debounce(async (e: PointerEvent) => {
313
- this._mouseEvent = e;
314
- // 提取有事件的对象
315
- const meshs: Object3D[] = [];
316
-
317
- let intersection = this._getSelected(this._object3ds);
318
-
319
- if (!intersection || !this._mousemoveEvent) {
320
- return;
321
- }
322
-
323
- this._mesh_on = intersection.object as Object3D;
324
-
325
- if (this.hasEvent(this._mesh_on, ["mouseup"])) {
326
-
327
- this._mesh_on.dispatchEvent({
328
- e: this._mouseEvent,
329
- ...intersection,
330
- renderer: this._renderer,
331
- camera: this._camera,
332
- object: intersection.object as Object3D,
333
- type: "mouseup",
334
- offsetX: this._mousemoveEvent.offsetX,
335
- offsetY: this._mousemoveEvent.offsetY,
336
- x: this._mousemoveEvent.x,
337
- y: this._mousemoveEvent.y
338
- } as never);
339
- }
340
-
341
- // 触发鼠标弹起事件
342
- if (
343
- this._mesh_Down &&
344
- this._mesh_Down === this._mesh_on &&
345
- Math.abs(this._mouseDownPosition.x - e.offsetX) < 5 &&
346
- Math.abs(this._mouseDownPosition.y - e.offsetY) < 5
347
- ) {
348
-
349
- // 是否有双击事件
350
- if (this.hasEvent(this._mesh_Down, ["dbclick"])) {
351
-
352
- this._click_num++;
353
-
354
- await new Promise(resolve => setTimeout(resolve, 200));
355
-
356
- // 只点击了一次
357
- if (this._click_num === 1) {
358
-
359
- if (this.hasEvent(this._mesh_Down, ["click"])) {
360
- this._mesh_Down.dispatchEvent({
361
- e: this._mouseEvent,
362
- ...intersection,
363
- renderer: this._renderer,
364
- camera: this._camera,
365
- object: intersection.object as Object3D,
366
- type: "click",
367
- offsetX: this._mousemoveEvent.offsetX,
368
- offsetY: this._mousemoveEvent.offsetY,
369
- x: this._mousemoveEvent.x,
370
- y: this._mousemoveEvent.y
371
- } as never);
372
- }
373
-
374
- if (this.hasEvent(this._mesh_Down, ["leftclick"]) && (e.button === 0)) {
375
- this._mesh_Down.dispatchEvent({
376
- e: this._mouseEvent,
377
- ...intersection,
378
- renderer: this._renderer,
379
- camera: this._camera,
380
- object: intersection.object as Object3D,
381
- type: "leftclick",
382
- offsetX: this._mousemoveEvent.offsetX,
383
- offsetY: this._mousemoveEvent.offsetY,
384
- x: this._mousemoveEvent.x,
385
- y: this._mousemoveEvent.y
386
- } as never);
387
- }
388
-
389
- if (this.hasEvent(this._mesh_Down, ["rightclick"]) && (e.button === 2)) {
390
- this._mesh_Down.dispatchEvent({
391
- e: this._mouseEvent,
392
- ...intersection,
393
- renderer: this._renderer,
394
- camera: this._camera,
395
- object: intersection.object as Object3D,
396
- type: "rightclick",
397
- offsetX: this._mousemoveEvent.offsetX,
398
- offsetY: this._mousemoveEvent.offsetY,
399
- x: this._mousemoveEvent.x,
400
- y: this._mousemoveEvent.y
401
- } as never);
402
- }
403
-
404
- } else if (this._click_num > 1) {
405
-
406
- if (this.hasEvent(this._mesh_Down, ["dbclick"]) && (new Date().valueOf() - this._click_num > 1) && (e.button === 0)) {
407
-
408
- this._mesh_Down.dispatchEvent({
409
- e: this._mouseEvent,
410
- ...intersection,
411
- renderer: this._renderer,
412
- camera: this._camera,
413
- object: intersection.object as Object3D,
414
- type: "dbclick",
415
- offsetX: this._mousemoveEvent.offsetX,
416
- offsetY: this._mousemoveEvent.offsetY,
417
- x: this._mousemoveEvent.x,
418
- y: this._mousemoveEvent.y
419
- } as never);
420
-
421
- }
422
-
423
- }
424
-
425
- } else {
426
-
427
- if (this.hasEvent(this._mesh_Down, ["click"])) {
428
- this._mesh_Down.dispatchEvent({
429
- e: this._mouseEvent,
430
- ...intersection,
431
- renderer: this._renderer,
432
- camera: this._camera,
433
- object: intersection.object as Object3D,
434
- type: "click",
435
- offsetX: this._mousemoveEvent.offsetX,
436
- offsetY: this._mousemoveEvent.offsetY,
437
- x: this._mousemoveEvent.x,
438
- y: this._mousemoveEvent.y
439
- } as never);
440
- }
441
-
442
- if (this.hasEvent(this._mesh_Down, ["leftclick"]) && (e.button === 0)) {
443
- this._mesh_Down.dispatchEvent({
444
- e: this._mouseEvent,
445
- ...intersection,
446
- renderer: this._renderer,
447
- camera: this._camera,
448
- object: intersection.object as Object3D,
449
- type: "leftclick",
450
- offsetX: this._mousemoveEvent.offsetX,
451
- offsetY: this._mousemoveEvent.offsetY,
452
- x: this._mousemoveEvent.x,
453
- y: this._mousemoveEvent.y
454
- } as never);
455
- }
456
-
457
- if (this.hasEvent(this._mesh_Down, ["rightclick"]) && (e.button === 2)) {
458
- this._mesh_Down.dispatchEvent({
459
- e: this._mouseEvent,
460
- ...intersection,
461
- renderer: this._renderer,
462
- camera: this._camera,
463
- object: intersection.object as Object3D,
464
- type: "rightclick",
465
- offsetX: this._mousemoveEvent.offsetX,
466
- offsetY: this._mousemoveEvent.offsetY,
467
- x: this._mousemoveEvent.x,
468
- y: this._mousemoveEvent.y
469
- } as never);
470
- }
471
-
472
- }
473
-
474
- this._click_num = 0;
475
-
476
- }
477
-
478
- })
479
-
480
-
481
-
482
-
483
- }