Support a rotated camera
This commit is contained in:
parent
06b4b885fe
commit
2589c0bcc1
4 changed files with 31 additions and 21 deletions
|
@ -14,7 +14,7 @@
|
||||||
<a-sky color="#ECECEC"></a-sky>
|
<a-sky color="#ECECEC"></a-sky>
|
||||||
|
|
||||||
<a-entity position="0 0 3.8">
|
<a-entity position="0 0 3.8">
|
||||||
<a-camera look-controls-enabled="false"></a-camera>
|
<a-camera look-controls-enabled="false" keyboard-controls="fpsMode: true"></a-camera>
|
||||||
</a-entity>
|
</a-entity>
|
||||||
</a-scene>
|
</a-scene>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import aframe from 'aframe';
|
import aframe from 'aframe';
|
||||||
import extras from 'aframe-extras';
|
import extras from 'aframe-extras';
|
||||||
|
import keyboardControls from 'aframe-keyboard-controls';
|
||||||
import clickDragComponent from '../src/index';
|
import clickDragComponent from '../src/index';
|
||||||
|
|
||||||
extras.physics.registerAll(aframe);
|
extras.physics.registerAll(aframe);
|
||||||
|
aframe.registerComponent('keyboard-controls', keyboardControls);
|
||||||
clickDragComponent(aframe);
|
clickDragComponent(aframe);
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,7 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aframe": "^0.3.0",
|
"aframe": "^0.3.0",
|
||||||
"aframe-extras": "^2.5.3",
|
"aframe-extras": "^2.5.3",
|
||||||
|
"aframe-keyboard-controls": "github:jesstelford/aframe-keyboard-controls#fps-mode",
|
||||||
"babel-cli": "^6.14.0",
|
"babel-cli": "^6.14.0",
|
||||||
"babel-plugin-transform-object-rest-spread": "^6.8.0",
|
"babel-plugin-transform-object-rest-spread": "^6.8.0",
|
||||||
"babel-preset-es2015": "^6.9.0",
|
"babel-preset-es2015": "^6.9.0",
|
||||||
|
|
47
src/index.js
47
src/index.js
|
@ -9,6 +9,16 @@ const DRAG_END_EVENT = 'dragend';
|
||||||
|
|
||||||
const TIME_TO_KEEP_LOG = 300;
|
const TIME_TO_KEEP_LOG = 300;
|
||||||
|
|
||||||
|
function forceWorldUpdate(threeElement) {
|
||||||
|
|
||||||
|
let element = threeElement;
|
||||||
|
while (element.parent) {
|
||||||
|
element = element.parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
element.updateMatrixWorld(true);
|
||||||
|
}
|
||||||
|
|
||||||
function cameraPositionToVec3(camera, vec3) {
|
function cameraPositionToVec3(camera, vec3) {
|
||||||
|
|
||||||
let element = camera;
|
let element = camera;
|
||||||
|
@ -35,19 +45,18 @@ function cameraPositionToVec3(camera, vec3) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function localToWorld(THREE, threeCamera, vector) {
|
||||||
|
forceWorldUpdate(threeCamera);
|
||||||
|
return threeCamera.localToWorld(vector);
|
||||||
|
}
|
||||||
|
|
||||||
const {unproject} = (function unprojectFunction() {
|
const {unproject} = (function unprojectFunction() {
|
||||||
|
|
||||||
let initialized = false;
|
let initialized = false;
|
||||||
|
|
||||||
let cameraPosition;
|
|
||||||
|
|
||||||
let cameraWorld;
|
|
||||||
let matrix;
|
let matrix;
|
||||||
|
|
||||||
function initialize(THREE) {
|
function initialize(THREE) {
|
||||||
cameraPosition = new THREE.Vector3();
|
|
||||||
|
|
||||||
cameraWorld = new THREE.Matrix4();
|
|
||||||
matrix = new THREE.Matrix4();
|
matrix = new THREE.Matrix4();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -57,19 +66,13 @@ const {unproject} = (function unprojectFunction() {
|
||||||
|
|
||||||
unproject(THREE, vector, camera) {
|
unproject(THREE, vector, camera) {
|
||||||
|
|
||||||
|
const threeCamera = camera.components.camera.camera;
|
||||||
|
|
||||||
initialized = initialized || initialize(THREE);
|
initialized = initialized || initialize(THREE);
|
||||||
|
|
||||||
cameraPositionToVec3(camera, cameraPosition);
|
vector.applyProjection(matrix.getInverse(threeCamera.projectionMatrix));
|
||||||
|
|
||||||
cameraWorld.identity();
|
return localToWorld(THREE, threeCamera, vector);
|
||||||
cameraWorld.setPosition(cameraPosition);
|
|
||||||
|
|
||||||
matrix.multiplyMatrices(
|
|
||||||
cameraWorld,
|
|
||||||
matrix.getInverse(camera.components.camera.camera.projectionMatrix)
|
|
||||||
);
|
|
||||||
|
|
||||||
return vector.applyProjection(matrix);
|
|
||||||
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -125,16 +128,20 @@ const {screenCoordsToDirection} = (function screenCoordsToDirectionFunction() {
|
||||||
// apply camera transformation from near-plane of mouse x/y into 3d space
|
// 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
|
// NOTE: This should be replaced with THREE code directly once the aframe bug
|
||||||
// is fixed:
|
// is fixed:
|
||||||
// const projectedVector = new THREE
|
/*
|
||||||
// .Vector3(mouseX, mouseY, -1)
|
cameraPositionToVec3(aframeCamera, cameraPosAsVec3);
|
||||||
// .unproject(threeCamera);
|
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
|
||||||
const {x, y, z} = projectedVector.sub(cameraPosAsVec3).normalize();
|
const {x, y, z} = projectedVector.sub(cameraPosAsVec3).normalize();
|
||||||
|
|
||||||
return {x, y, z};
|
return {x, y, z};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue