var w = window.innerWidth,
h = window.innerHeight;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(60, w / h, 1, 1000);
camera.position.setScalar(5);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(w, h);
document.body.appendChild(renderer.domElement);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
scene.add(new THREE.GridHelper(10, 10));
var prismGeom = new THREE.ConeBufferGeometry(1, 2, 3);
prismGeom.translate(0, 1, 0);
var prismMat = new THREE.MeshBasicMaterial({
color: "red",
wireframe: true
});
var prism = new THREE.Mesh(prismGeom, prismMat);
prism.position.set(-1, 0, -2);
scene.add(prism);
var centralMarker = new THREE.Mesh(new THREE.SphereBufferGeometry(0.125, 4, 2), new THREE.MeshBasicMaterial({
color: "aqua"
}));
scene.add(centralMarker);
var pointMarker = new THREE.Mesh(centralMarker.geometry, new THREE.MeshBasicMaterial({
color: "magenta"
}));
pointMarker.visible = false;
scene.add(pointMarker);
var oldTarget = scene.position;
var targeted = false;
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var intersects = [];
window.addEventListener("mousedown", onMouseDown, false);
window.addEventListener("mouseup", onMouseUp, false);
function onMouseDown(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
intersects = raycaster.intersectObject(prism);
if (intersects.length > 0) {
targeted = true;
controls.target.copy(intersects[0].point);
pointMarker.position.copy(intersects[0].point);
pointMarker.visible = true;
controls.update();
}
}
function onMouseUp(event) {
if (!targeted) return;
let shift = new THREE.Vector3().copy(oldTarget).sub(controls.target);
camera.position.add(shift);
controls.target.copy(oldTarget);
controls.update();
targeted = false;
pointMarker.visible = false;
}
renderer.setAnimationLoop(() => {
renderer.render(scene, camera)
})
body {
overflow: hidden;
margin: 0;
}
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>