Merge pull request #22 from patrickocoffeyo/fix/canvas-coordinates
Incorporate canvas position in mouse position calculation
This commit is contained in:
commit
c74d79e997
1 changed files with 6 additions and 32 deletions
38
src/index.js
38
src/index.js
|
@ -89,19 +89,6 @@ const {unproject} = (function unprojectFunction() {
|
||||||
};
|
};
|
||||||
}());
|
}());
|
||||||
|
|
||||||
function clientCoordsTo3DCanvasCoords(
|
|
||||||
clientX,
|
|
||||||
clientY,
|
|
||||||
offsetX,
|
|
||||||
offsetY,
|
|
||||||
clientWidth,
|
|
||||||
clientHeight
|
|
||||||
) {
|
|
||||||
return {
|
|
||||||
x: (((clientX - offsetX) / clientWidth) * 2) - 1,
|
|
||||||
y: (-((clientY - offsetY) / clientHeight) * 2) + 1,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const {screenCoordsToDirection} = (function screenCoordsToDirectionFunction() {
|
const {screenCoordsToDirection} = (function screenCoordsToDirectionFunction() {
|
||||||
|
|
||||||
|
@ -127,28 +114,15 @@ const {screenCoordsToDirection} = (function screenCoordsToDirectionFunction() {
|
||||||
initialized = initialized || initialize(THREE);
|
initialized = initialized || initialize(THREE);
|
||||||
|
|
||||||
// scale mouse coordinates down to -1 <-> +1
|
// scale mouse coordinates down to -1 <-> +1
|
||||||
const {x: mouseX, y: mouseY} = clientCoordsTo3DCanvasCoords(
|
const scene = document.querySelector('a-scene');
|
||||||
clientX, clientY,
|
const bounds = scene.canvas.getBoundingClientRect();
|
||||||
0, 0, // TODO: Replace with canvas position
|
const left = clientX - bounds.left;
|
||||||
window.innerWidth,
|
const top = clientY - bounds.top;
|
||||||
window.innerHeight
|
const mouseX = ((left / bounds.width) * 2) - 1;
|
||||||
);
|
const mouseY = -((top / bounds.height) * 2) + 1;
|
||||||
|
|
||||||
mousePosAsVec3.set(mouseX, mouseY, -1);
|
mousePosAsVec3.set(mouseX, mouseY, -1);
|
||||||
|
|
||||||
// apply camera transformation from near-plane of mouse x/y into 3d space
|
|
||||||
// NOTE: This should be replaced with THREE code directly once the aframe bug
|
|
||||||
// is fixed:
|
|
||||||
/*
|
|
||||||
cameraPositionToVec3(aframeCamera, cameraPosAsVec3);
|
|
||||||
const {x, y, z} = new THREE
|
|
||||||
.Vector3(mouseX, mouseY, -1)
|
|
||||||
.unproject(aframeCamera.components.camera.camera)
|
|
||||||
.sub(cameraPosAsVec3)
|
|
||||||
.normalize();
|
|
||||||
*/
|
|
||||||
const projectedVector = unproject(THREE, mousePosAsVec3, aframeCamera);
|
const projectedVector = unproject(THREE, mousePosAsVec3, aframeCamera);
|
||||||
|
|
||||||
cameraPositionToVec3(aframeCamera, cameraPosAsVec3);
|
cameraPositionToVec3(aframeCamera, cameraPosAsVec3);
|
||||||
|
|
||||||
// Get the unit length direction vector from the camera's position
|
// Get the unit length direction vector from the camera's position
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue