Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calibration JSON Schema #2

Merged
merged 6 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ Keep in mind that this is still just a prototype and that I'm not a frontend dev
## TO-DOs

- Create a calibration page to allow users to calibrate the position and orientation of their camera, and to configure their chroma key (green screen).

- Allow the Mixed Reality Capture session to be initialized with a JSON file with a saved calibration.

## How to test the example

Expand Down Expand Up @@ -87,3 +85,40 @@ renderer.render( scene, camera );
mixedRealityCapture.render( renderer.xr, scene );

```

Alternatively, you can instantiate the calibration with a JSON provided by the user:

```javascript

// ...

const json = `
{
"schemaVersion": 1,
"camera": {
"width": 1280,
"height": 720,
"fov": 38,
"position": [0, 1.5, 0],
"orientation": [0, 0, 0, 1]
},
"chromaKey": {
"color": [0, 1, 0],
"similarity": 0.25,
"smoothness": 0
},
"delay": 4
}
`;

const calibrationData = JSON.parse( json );

const calibration = MRC.Calibration.fromData( calibrationData );

// ...

mixedRealityCapture = new MRC.MixedRealityCapture( calibration );

// ...

```
134 changes: 134 additions & 0 deletions examples/calibration/calibration-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { Calibration } from 'reality-mixer';

const exampleCalibrationJSON = `
{
"schemaVersion": 1,
"camera": {
"width": 1920,
"height": 1080,
"fov": 38,
"position": [0, 1.5, 0],
"orientation": [0, 0, 0, 1]
},
"chromaKey": {
"color": [0, 1, 0],
"similarity": 0.25,
"smoothness": 0
},
"delay": 4
}
`

function CalibrationInput(
onCompleted,
initialCalibrationJSON = null
) {
let initialJSON;

if (initialCalibrationJSON == null) {
let persistedJSON = localStorage.getItem("calibration-v1");

if (persistedJSON == null) {
initialJSON = exampleCalibrationJSON;
} else {
initialJSON = persistedJSON;
}
} else {
initialJSON = initialCalibrationJSON;
}

let popupDiv = document.createElement("div");

popupDiv.style = `
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #303841;
border-radius: 4px;
font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
color: #EFEFEF;
overflow: hidden;
`

let titleDiv = document.createElement("div");

titleDiv.style = "background-color: #50565E; padding: 8px";
titleDiv.innerText = "Paste or drag and drop your Mixed Reality calibration below:"

let resultDiv = document.createElement("div");

resultDiv.style = "padding: 8px; white-space: pre-wrap; background-color: green;";

let editor = document.createElement("textarea");

editor.style = `
font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
border: none;
box-shadow: none;
outline: none;
background-color: #303841;
color: #EFEFEF;
resize: none;
margin: 0px;
`

editor.rows = 20;
editor.cols = 80;
editor.spellcheck = false;
editor.value = initialJSON;

function validateCalibration() {
const maybeJson = editor.value;

try {
const json = JSON.parse(maybeJson);
const calibration = Calibration.fromData(json);

resultDiv.style.backgroundColor = "green";
resultDiv.innerHTML = "";

let link = document.createElement("a");
link.innerText = "Click here to continue...";
link.href = "#";
link.style.color = "#EFEFEF";

link.onclick = function() {
localStorage.setItem("calibration-v1", maybeJson);
onCompleted(popupDiv, calibration);
return false;
}

resultDiv.appendChild(link);
} catch (error) {
resultDiv.innerText = error;
resultDiv.style.backgroundColor = "red";
}
}

editor.oninput = validateCalibration;
validateCalibration();

popupDiv.appendChild(titleDiv);
popupDiv.appendChild(editor);
popupDiv.appendChild(resultDiv);

popupDiv.ondrop = function(e) {
e.preventDefault();

let file = e.dataTransfer.files[0],
reader = new FileReader();

reader.onload = function(event) {
editor.value = event.target.result;
validateCalibration();
};

reader.readAsText(file);
return false;
}

return popupDiv;
}

export { CalibrationInput };
38 changes: 38 additions & 0 deletions examples/calibration_input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Calibration input</title>
<style>
body { margin: 0; }
</style>
</head>
<body style="background-color:black;">

<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>

<script type="importmap">
{
"imports": {
"three": "../node_modules/three/build/three.module.js",
"reality-mixer": "../src/mrc.js"
}
}
</script>

<script type="module">
import { CalibrationInput } from './calibration/calibration-input.js';

function onCalibrationInput(editor, calibration) {
editor.parentNode.removeChild(editor);
console.log(calibration);
// We can then initialize the Mixed Reality Capture with this calibration....
}

const calibrationInput = CalibrationInput(onCalibrationInput);

document.body.appendChild(calibrationInput);
</script>

</body>
</html>
25 changes: 17 additions & 8 deletions examples/webxr_vr_ballshooter.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import * as THREE from 'three';
import * as MRC from 'reality-mixer';
import { CalibrationInput } from './calibration/calibration-input.js';

import { BoxLineGeometry } from '../node_modules/three/examples/jsm/geometries/BoxLineGeometry.js';
import { VRButton } from '../node_modules/three/examples/jsm/webxr/VRButton.js';
Expand All @@ -44,10 +45,23 @@

const clock = new THREE.Clock();

init();
animate();
// Mixed Reality Calibration Input

function init() {
function onCalibrationInput(editor, calibration) {
editor.parentNode.removeChild(editor);
console.log(calibration);

init(calibration);
animate();
}

const calibrationInput = CalibrationInput(onCalibrationInput);

document.body.appendChild(calibrationInput);

//

function init(calibration) {

scene = new THREE.Scene();
scene.background = new THREE.Color( 0x505050 );
Expand Down Expand Up @@ -89,11 +103,6 @@

// Mixed Reality Capture

const cameraCalibration = new MRC.CameraCalibration();
const chromaKey = new MRC.ChromaKey();
const delayInFrames = 5;
const calibration = new MRC.Calibration( cameraCalibration, chromaKey, delayInFrames );

mixedRealityCapture = new MRC.MixedRealityCapture( calibration );

//
Expand Down
Loading