-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy path3dUI.js
executable file
·134 lines (116 loc) · 4.69 KB
/
3dUI.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
/**
* Created by amandaghassaei on 5/5/17.
*/
function init3DUI(globals) {
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var raycasterPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1));
var isDragging = false;
var draggingNode = null;
var draggingNodeFixed = false;
var mouseDown = false;
var highlightedObj;
var highlighter1 = new Node(new THREE.Vector3());
highlighter1.setTransparent();
globals.threeView.scene.add(highlighter1.getObject3D());
$(document).dblclick(function() {
});
document.addEventListener('mousedown', function(){
mouseDown = true;
}, false);
document.addEventListener('mouseup', function(e){
isDragging = false;
if (draggingNode){
draggingNode.setFixed(draggingNodeFixed);
draggingNode = null;
globals.fixedHasChanged = true;
globals.threeView.enableControls(true);
setHighlightedObj(null);
globals.shouldCenterGeo = true;
}
mouseDown = false;
}, false);
document.addEventListener( 'mousemove', mouseMove, false );
function mouseMove(e){
if (mouseDown) {
isDragging = true;
}
if (!globals.userInteractionEnabled) return;
// e.preventDefault();
mouse.x = (e.clientX/window.innerWidth)*2-1;
mouse.y = - (e.clientY/window.innerHeight)*2+1;
raycaster.setFromCamera(mouse, globals.threeView.camera);
var _highlightedObj = null;
if (!isDragging) {
_highlightedObj = checkForIntersections(e, globals.model.getMesh());
setHighlightedObj(_highlightedObj);
} else if (isDragging && highlightedObj){
if (!draggingNode) {
draggingNode = highlightedObj;
draggingNodeFixed = draggingNode.isFixed();
draggingNode.setFixed(true);
globals.fixedHasChanged = true;
globals.threeView.enableControls(false);
}
var intersection = getIntersectionWithObjectPlane(highlightedObj.getPosition().clone());
highlightedObj.moveManually(intersection);
globals.nodePositionHasChanged = true;
}
if (highlightedObj){
var position = highlightedObj.getPosition();
highlighter1.getObject3D().position.set(position.x, position.y, position.z);
}
}
function getIntersectionWithObjectPlane(position){
var cameraOrientation = globals.threeView.camera.getWorldDirection();
var dist = position.dot(cameraOrientation);
raycasterPlane.set(cameraOrientation, -dist);
var intersection = new THREE.Vector3();
raycaster.ray.intersectPlane(raycasterPlane, intersection);
return intersection;
}
function setHighlightedObj(object){
if (highlightedObj && (object != highlightedObj)) {
// highlightedObj.unhighlight();
highlighter1.getObject3D().visible = false;
}
highlightedObj = object;
if (highlightedObj) {
// highlightedObj.highlight();
highlighter1.getObject3D().visible = true;
}
}
function checkForIntersections(e, objects){
var _highlightedObj = null;
var intersections = raycaster.intersectObjects(objects, false);
if (intersections.length>0){
var face = intersections[0].face;
var position = intersections[0].point;
var positionsArray = globals.model.getPositionsArray();
var vertices = [];
vertices.push(new THREE.Vector3(positionsArray[3*face.a], positionsArray[3*face.a+1], positionsArray[3*face.a+2]));
vertices.push(new THREE.Vector3(positionsArray[3*face.b], positionsArray[3*face.b+1], positionsArray[3*face.b+2]));
vertices.push(new THREE.Vector3(positionsArray[3*face.c], positionsArray[3*face.c+1], positionsArray[3*face.c+2]));
var dist = vertices[0].clone().sub(position).lengthSq();
var nodeIndex = face.a;
for (var i=1;i<3;i++){
var _dist = (vertices[i].clone().sub(position)).lengthSq();
if (_dist<dist){
dist = _dist;
if (i==1) nodeIndex = face.b;
else nodeIndex = face.c;
}
}
var nodesArray = globals.model.getNodes();
_highlightedObj = nodesArray[nodeIndex];
}
return _highlightedObj;
}
function hideHighlighters(){
highlighter1.getObject3D().visible = false;
}
// globals.threeView.sceneAdd(raycasterPlane);
return {
hideHighlighters: hideHighlighters
}
}