-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperDragControls.js
216 lines (162 loc) · 7.16 KB
/
superDragControls.js
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//类的声明方式 class xxx extends xxx
//然后用constructor 设置函数变量;
import { Vector2, Raycaster, EventDispatcher, Group} from 'three';
import { DragControls } from './DragControls.js';
import { OrbitControls } from './OrbitControls.js';
import { SelectionBox } from '../interactive/SelectionBox.js';
import { SelectionHelper } from '../interactive/SelectionHelper.js';
let _shiftSelection = false;
let _boxSelectable = false;
let _group;
const mouse = new Vector2(), raycaster = new Raycaster();
class superDragControls extends EventDispatcher {
constructor( _objects, _camera, _domElement, _scene, _renderer, _damping = true ){
//orbitControls
super();
this.objects = _objects;
this.camera = _camera;
this.domElement = _domElement;
this.damping = _damping;
this.renderer = _renderer;
this.scene = _scene;
this.color = 0x0088ff;
this.dampingFactor = 0.1;
_group = new Group();
this.scene.add( _group );
const scope = this;
this.orbitControls = new OrbitControls( _camera, _domElement);
this.orbitControls.update();
this.orbitControls.dampingFactor = this.dampingFactor;
this.orbitControls.enableDamping = _damping;
//dragControls
this.dragControls = new DragControls( this.objects, this.camera, this.domElement );
scope.addEventListener('dragstart', function(event){
scope.orbitControls.enabled = false;
});
scope.addEventListener('dragend', function(event){
scope.orbitControls.enabled = true;
});
//SelectionBox
const _selectionBox = new SelectionBox( this.camera, this.scene );
const _helper = new SelectionHelper( this.renderer, 'selectBox' );
_helper.enabled = false;
_domElement.addEventListener( 'pointerdown', function ( event ) {
if(!_raytest(event)) return;
scope.dispatchEvent( { type: 'dragstart' } );
if( !_boxSelectable ){
for ( const _item of _selectionBox.collection ) {
_item.material.emissive.set( 0x000000 );
}
return;
}
var _groupMember = _group.children;
console.log(_groupMember);
for( i = 0; i < fullobjects.length; i++ ){
scope.scene.attach( _groupMember[i]);
}
_selectionBox.startPoint.set(
( event.clientX / window.innerWidth ) * 2 - 1,
- ( event.clientY / window.innerHeight ) * 2 + 1,
0.5 );
});
scope.domElement.addEventListener( 'pointermove', function ( event ) {
/* if(!_boxSelectable){
return
} */
//scope.orbitControls.enabled = false;
if ( _helper.isDown && _boxSelectable ) {
for ( let i = 0; i < _selectionBox.collection.length; i ++ ) {
_selectionBox.collection[ i ].material.emissive.set( 0x000000 );
}
_selectionBox.endPoint.set(
( event.clientX / window.innerWidth ) * 2 - 1,
- ( event.clientY / window.innerHeight ) * 2 + 1,
0.5 );
const _allSelected = _selectionBox.select();
for ( let i = 0; i < _allSelected.length; i ++ ) {
_allSelected[ i ].material.emissive.set( scope.color );
_group.attach(_allSelected[i]);
}
}
});
this.domElement.addEventListener( 'pointerup', function ( event ) {
scope.dispatchEvent( { type: 'dragend' } );
if(!_boxSelectable)
return;
_selectionBox.endPoint.set(
( event.clientX / window.innerWidth ) * 2 - 1,
- ( event.clientY / window.innerHeight ) * 2 + 1,
0.5 );
const _allSelected = _selectionBox.select();
for ( let i = 0; i < _allSelected.length; i ++ ) {
_allSelected[ i ].material.emissive.set( scope.color );
_group.attach(_allSelected[i]);
}
scope.orbitControls.enabled = true;
});
scope.domElement.addEventListener( 'click', _onClick );
window.addEventListener( 'keydown', _onKeyDown );
window.addEventListener( 'keyup', _onKeyUp );
this.dispose = function(){
this.dragControls.deactivate();
}
this.getObjects = function(){
return this.objects;
}
this.update = function(){
this.orbitControls.update();
}
function _onKeyDown( event ) {
scope.orbitControls.enabled = false;
if(event.keyCode === 17){
_boxSelectable = true;
_helper.enabled = true;
}else if(event.keyCode === 16){
_shiftSelection = true;
}
}
function _onKeyUp() {
_shiftSelection = false;
_boxSelectable = false;
//this.orbitControls.enabled = true;
_helper.enabled = false;
//scope.dispatchEvent( { type: 'onkeyup' } );
scope.orbitControls.enabled = true;
}
function _raytest(event){
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, _camera );
var result = raycaster.intersectObjects( scope.objects, true ).length > 0 ? 1 : null;
return result;
}
function _onClick( event ) {
event.preventDefault();
if ( _shiftSelection === true ) {
const draggableObjects = scope.getObjects();
draggableObjects.length = 0;
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, _camera );
const intersections = raycaster.intersectObjects( scope.scene.children, true );
if ( intersections.length > 0) {
const _obj = intersections[ 0 ].object;
if ( _group.children.includes( _obj ) === true ) {
_obj.material.emissive.set( 0x000000 );
scope.scene.attach( _obj );
} else {
_obj.material.emissive.set( scope.color );
_group.attach( _obj );
}
scope.dragControls.transformGroup = true;
draggableObjects.push( _group );
}
if ( _group.children.length === 0 ) {
scope.dragControls.transformGroup = false;
draggableObjects.push( ...scope.scene.children );
}
}
}
}
}
export { superDragControls };