finalize absolute orientation rotation

This commit is contained in:
Bit Borealis 2023-10-05 15:05:23 +00:00
parent 74f8a59366
commit f4ca9ff1f7
Signed by: theotheroracle
GPG Key ID: 2D816A2DCA6E5649
1 changed files with 7 additions and 94 deletions

View File

@ -151,9 +151,7 @@ function setupThreeJsScene() {
function handleSensorReading(
sensor: any,
scene: THREE.Scene,
calibratedDirection: THREE.Vector3 | null,
currentDirection: THREE.Vector3 | null,
relativeDirection: THREE.Vector3 | null,
calibratedRotation: THREE.Quaternion | null,
render: (direction: THREE.Quaternion | null) => void,
checkbox: HTMLInputElement | null
) {
@ -163,14 +161,10 @@ function handleSensorReading(
// Apply the sensor's quaternion to the vector
const orientation = new THREE.Quaternion(...sensor.quaternion).normalize();
// Set the initial orientation if it's not set already
currentDirection = scene.up.clone().applyQuaternion(orientation);
if (!calibratedDirection) {
calibratedDirection = currentDirection;
console.log("cal", calibratedDirection);
if (!calibratedRotation) {
calibratedRotation = orientation.invert();
}
console.log("debug")
relativeDirection = calibratedDirection.clone().sub(currentDirection).normalize();
const relativeOrientation = new THREE.Quaternion().setFromUnitVectors(calibratedDirection, currentDirection);
const relativeOrientation = orientation.clone().multiply(calibratedRotation.clone());
render(relativeOrientation);
});
@ -188,7 +182,7 @@ function handleSensorReading(
// Otherwise, start the sensor
sensor.start();
console.log("started");
calibratedDirection = null;
calibratedRotation = null;
fps = setFPS;
}
});
@ -224,14 +218,14 @@ function sensorStart(sensorName: sensorType, box: HTMLElement, checkboxId: strin
}
// Initialize default orientation
let calibratedDirection: THREE.Vector3 | null = null;
let calibratedRotation: THREE.Quaternion | null = null;
let currentDirection: THREE.Vector3 | null = null;
let relativeDirection: THREE.Vector3 | null = null;
var checkbox = document.getElementById(checkboxId) as HTMLInputElement;
// Handle sensor readings
handleSensorReading(sensor, scene, calibratedDirection, currentDirection, relativeDirection, render, checkbox);
handleSensorReading(sensor, scene, calibratedRotation, render, checkbox);
render(null);
// Render the first frame
@ -240,85 +234,4 @@ function sensorStart(sensorName: sensorType, box: HTMLElement, checkboxId: strin
}
}
function gyroStart() {
// Create an AbsoluteOrientationSensor object
const sensor = new window.AbsoluteOrientationSensor({ frequency: 60, referenceFrame: 'device' });
// Create a renderer
const sceneSize = new THREE.Vector3(100, 100, 100);
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(sceneSize.x, sceneSize.y);
renderer.setPixelRatio(window.devicePixelRatio / 2.5);
const scene = new THREE.Scene();
const axes = new THREE.AxesHelper(5);
scene.add(axes);
const camera = new THREE.PerspectiveCamera(75, sceneSize.x / sceneSize.y, 0.1, 1000);
camera.translateOnAxis(scene.up.clone(), 2);
camera.lookAt(axes.position);
// Get the checkbox element
const gyroBox = document.getElementById("gyroBox") as HTMLElement;
const checkbox = document.getElementById("gyro") as HTMLInputElement;
(gyroBox || document.body).appendChild(renderer.domElement);
show(gyroBox);
// Create an arrow
// Profiling
let sampleCount = 0; // Number of frames since the last second
let sampleSec = 0; // Time last second was recorded for a frame
// Animation loop
let setFPS = 60; // Desired framerate
let fps = 0; // Startup fps
// Initialize default orientation
let calibratedDirection: THREE.Vector3 | null;
let currentDirection: THREE.Vector3 | null;
let relativeDirection: THREE.Vector3 | null;
const render = (direction: THREE.Quaternion | null) => {
if (!direction) {
direction = new THREE.Quaternion().identity();
}
axes.setRotationFromQuaternion(direction);
renderer.render(scene, camera);
}
// Add an event listener for sensor readings
sensor.addEventListener('reading', () => {
// Apply the sensor's quaternion to the vector
const orientation = new THREE.Quaternion(...sensor.quaternion).normalize();
// Set the initial orientation if it's not set already
currentDirection = scene.up.clone().applyQuaternion(orientation);
if (!calibratedDirection) {
calibratedDirection = currentDirection;
console.log("cal", calibratedDirection);
}
relativeDirection = calibratedDirection.clone().sub(currentDirection).normalize();
const relativeOrientation = new THREE.Quaternion().setFromUnitVectors(calibratedDirection, currentDirection);
render(relativeOrientation);
// Profiling
let currTime = new Date().getTime();
if (sampleSec < currTime - 1000) {
console.log("Samples:", sampleCount)
sampleCount = 0;
sampleSec = currTime;
}
else { sampleCount++; }
});
// Render the first frame
render(null);
}
checkPerms();