选中事件。

This commit is contained in:
liteng 2018-06-27 12:00:21 +08:00
parent f74fe5a3ae
commit 021bdaa895

View File

@ -69,135 +69,6 @@ function Viewport(app) {
sceneHelpers.add(transformControls);
// object picking
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
// events
function getIntersects(point, objects) {
mouse.set((point.x * 2) - 1, -(point.y * 2) + 1);
raycaster.setFromCamera(mouse, camera);
return raycaster.intersectObjects(objects);
}
var onDownPosition = new THREE.Vector2();
var onUpPosition = new THREE.Vector2();
var onDoubleClickPosition = new THREE.Vector2();
function getMousePosition(dom, x, y) {
var rect = dom.getBoundingClientRect();
return [(x - rect.left) / rect.width, (y - rect.top) / rect.height];
}
function handleClick() {
if (onDownPosition.distanceTo(onUpPosition) === 0) {
var intersects = getIntersects(onUpPosition, objects);
if (intersects.length > 0) {
var object = intersects[0].object;
if (object.userData.object !== undefined) {
// helper
editor.select(object.userData.object);
} else {
editor.select(object);
}
} else {
editor.select(null);
}
_this.app.call('render');
}
}
function onMouseDown(event) {
event.preventDefault();
var array = getMousePosition(container.dom, event.clientX, event.clientY);
onDownPosition.fromArray(array);
document.addEventListener('mouseup', onMouseUp, false);
}
function onMouseUp(event) {
var array = getMousePosition(container.dom, event.clientX, event.clientY);
onUpPosition.fromArray(array);
handleClick();
document.removeEventListener('mouseup', onMouseUp, false);
}
function onTouchStart(event) {
var touch = event.changedTouches[0];
var array = getMousePosition(container.dom, touch.clientX, touch.clientY);
onDownPosition.fromArray(array);
document.addEventListener('touchend', onTouchEnd, false);
}
function onTouchEnd(event) {
var touch = event.changedTouches[0];
var array = getMousePosition(container.dom, touch.clientX, touch.clientY);
onUpPosition.fromArray(array);
handleClick();
document.removeEventListener('touchend', onTouchEnd, false);
}
function onDoubleClick(event) {
var array = getMousePosition(container.dom, event.clientX, event.clientY);
onDoubleClickPosition.fromArray(array);
var intersects = getIntersects(onDoubleClickPosition, objects);
if (intersects.length > 0) {
var intersect = intersects[0];
_this.app.call('objectFocused', _this, intersect.object);
}
}
container.dom.addEventListener('mousedown', onMouseDown, false);
container.dom.addEventListener('touchstart', onTouchStart, false);
container.dom.addEventListener('dblclick', onDoubleClick, false);
// 编辑器控件
// controls need to be added *after* main logic,