DragControl.js
5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as THREE from 'three';
const getMousePosition = (event, container) => {
const rect = container.getBoundingClientRect();
const mouse = { x: 0, y: 0 };
mouse.x = ((event.clientX - rect.left) / (rect.right - rect.left)) * 2 - 1;
mouse.y = -((event.clientY - rect.top) / (rect.bottom - rect.top)) * 2 + 1;
return mouse;
};
class DragControl extends THREE.EventDispatcher {
constructor(camera, scene, container, rootObject = []) {
super();
this._raycaster = new THREE.Raycaster();
this._container = container;
this._camera = camera;
this._scene = scene;
this._rootObjects = rootObject;
this._insecPlane = new THREE.Plane();
this._insecPoint = new THREE.Vector3();
this._offset = new THREE.Vector3();
this._worldPosition = new THREE.Vector3();
this._selectObject = null;
this._hovered = null;
this._hoversize = 1.4;
this._enabled = true;
this.moving = false;
this._addEvent();
}
get rootsObject() {
return this._rootObjects;
}
get enabled() {
return this._enabled;
}
set enabled(enabled) {
this._enabled = enabled;
}
dispose = () => {
this.removeEvent();
this._selectObject = null;
this._hovered = null;
this._container = null;
this._camera = null;
this._rootObjects = [];
};
setRootsObjects = (rootObject) => {
this._rootObjects = rootObject;
};
_addEvent = () => {
if (!this._container) return;
this._container.addEventListener('pointermove', this.onPointermove);
this._container.addEventListener('pointerdown', this.onPointerdown);
this._container.addEventListener('pointerup', this.onPointerup);
};
removeEvent = () => {
if (!this._container) return;
this._container.removeEventListener('pointermove', this.onPointermove);
this._container.removeEventListener('pointerdown', this.onPointerdown);
this._container.removeEventListener('pointerup', this.onPointerup);
};
onPointerdown = (event) => {
if (!this._enabled) return;
if (!this._camera) return;
if (!this._container) return;
if (this.moving) return;
if (event.button !== 0) return;
const vector2D = getMousePosition(event, this._container);
const mouse = new THREE.Vector2(vector2D.x, vector2D.y);
this._raycaster.firstHitOnly = false;
this._camera.near = 1; // raycaster only can work on camera near plane is positive
this._raycaster.setFromCamera(mouse, this._camera);
const objects = this._scene.children.filter(
(el) => el.name.includes('active:') && el.isMesh === true && el.visible
);
const detects = this._rootObjects.length === 0 ? objects : this._rootObjects;
const res = this._raycaster.intersectObjects(detects);
if (res.length > 0) {
this.moving = true;
this._selectObject = res[0].object;
if (this._raycaster.ray.intersectPlane(this._insecPlane, this._insecPoint)) {
if (this._selectObject.parent) {
this._offset
.copy(this._insecPoint)
.sub(this._worldPosition.setFromMatrixPosition(this._selectObject.matrix));
}
this.dispatchEvent({ type: 'dragMousedown', object: this._selectObject });
}
} else {
this._selectObject = null;
}
};
onPointermove = (event) => {
// event.preventDefault();
this.updatePlane();
if (!this._enabled) return;
if (!this._camera) return;
if (!this._container) return;
if (!this.moving) return;
const vector2D = getMousePosition(event, this._container);
const mouse = new THREE.Vector2(vector2D.x, vector2D.y);
this._camera.near = 1; // raycaster only can work on camera near plane is positive
this._raycaster.setFromCamera(mouse, this._camera);
if (this._selectObject) {
if (this._raycaster.ray.intersectPlane(this._insecPlane, this._insecPoint)) {
const position = new THREE.Vector3().copy(this._insecPoint.sub(this._offset));
this._selectObject.position.set(position.x, position.y, position.z);
this.dispatchEvent({
type: 'dragMousemove',
object: this._selectObject,
position,
event
});
}
}
this._camera.near = -10000;
};
updatePlane = () => {
const camera = this._camera;
let vector = new THREE.Vector3(0, 0, 1);
const x = Math.abs(camera.position.x) < 0.000001;
const y = Math.abs(camera.position.y) < 0.000001;
const z = Math.abs(camera.position.z) < 0.000001;
if (!x && !y && !z) {
vector = new THREE.Vector3(0, 0, 1);
} else if (!y && x && z) {
vector = new THREE.Vector3(0, 1, 0);
} else if (!x && y && z) {
vector = new THREE.Vector3(1, 0, 0);
}
this._insecPlane.setFromNormalAndCoplanarPoint(vector, new THREE.Vector3());
};
onPointerup = () => {
if (!this.moving) return;
if (!this._container) return;
if (this._selectObject) this._selectObject = null;
this.moving = false;
this.dispatchEvent({ type: 'dragMouseup' });
};
}
export default DragControl;