Skip to content

Commit a110abf

Browse files
authored
Merge pull request #2 from fabio914/calibration-json
Calibration JSON Schema
2 parents c999c2c + 9b39446 commit a110abf

File tree

9 files changed

+768
-19
lines changed

9 files changed

+768
-19
lines changed

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ Keep in mind that this is still just a prototype and that I'm not a frontend dev
1111
## TO-DOs
1212

1313
- Create a calibration page to allow users to calibrate the position and orientation of their camera, and to configure their chroma key (green screen).
14-
15-
- Allow the Mixed Reality Capture session to be initialized with a JSON file with a saved calibration.
1614

1715
## How to test the example
1816

@@ -87,3 +85,40 @@ renderer.render( scene, camera );
8785
mixedRealityCapture.render( renderer.xr, scene );
8886

8987
```
88+
89+
Alternatively, you can instantiate the calibration with a JSON provided by the user:
90+
91+
```javascript
92+
93+
// ...
94+
95+
const json = `
96+
{
97+
"schemaVersion": 1,
98+
"camera": {
99+
"width": 1280,
100+
"height": 720,
101+
"fov": 38,
102+
"position": [0, 1.5, 0],
103+
"orientation": [0, 0, 0, 1]
104+
},
105+
"chromaKey": {
106+
"color": [0, 1, 0],
107+
"similarity": 0.25,
108+
"smoothness": 0
109+
},
110+
"delay": 4
111+
}
112+
`;
113+
114+
const calibrationData = JSON.parse( json );
115+
116+
const calibration = MRC.Calibration.fromData( calibrationData );
117+
118+
// ...
119+
120+
mixedRealityCapture = new MRC.MixedRealityCapture( calibration );
121+
122+
// ...
123+
124+
```
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { Calibration } from 'reality-mixer';
2+
3+
const exampleCalibrationJSON = `
4+
{
5+
"schemaVersion": 1,
6+
"camera": {
7+
"width": 1920,
8+
"height": 1080,
9+
"fov": 38,
10+
"position": [0, 1.5, 0],
11+
"orientation": [0, 0, 0, 1]
12+
},
13+
"chromaKey": {
14+
"color": [0, 1, 0],
15+
"similarity": 0.25,
16+
"smoothness": 0
17+
},
18+
"delay": 4
19+
}
20+
`
21+
22+
function CalibrationInput(
23+
onCompleted,
24+
initialCalibrationJSON = null
25+
) {
26+
let initialJSON;
27+
28+
if (initialCalibrationJSON == null) {
29+
let persistedJSON = localStorage.getItem("calibration-v1");
30+
31+
if (persistedJSON == null) {
32+
initialJSON = exampleCalibrationJSON;
33+
} else {
34+
initialJSON = persistedJSON;
35+
}
36+
} else {
37+
initialJSON = initialCalibrationJSON;
38+
}
39+
40+
let popupDiv = document.createElement("div");
41+
42+
popupDiv.style = `
43+
position: fixed;
44+
top: 50%;
45+
left: 50%;
46+
transform: translate(-50%, -50%);
47+
background-color: #303841;
48+
border-radius: 4px;
49+
font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
50+
color: #EFEFEF;
51+
overflow: hidden;
52+
`
53+
54+
let titleDiv = document.createElement("div");
55+
56+
titleDiv.style = "background-color: #50565E; padding: 8px";
57+
titleDiv.innerText = "Paste or drag and drop your Mixed Reality calibration below:"
58+
59+
let resultDiv = document.createElement("div");
60+
61+
resultDiv.style = "padding: 8px; white-space: pre-wrap; background-color: green;";
62+
63+
let editor = document.createElement("textarea");
64+
65+
editor.style = `
66+
font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
67+
border: none;
68+
box-shadow: none;
69+
outline: none;
70+
background-color: #303841;
71+
color: #EFEFEF;
72+
resize: none;
73+
margin: 0px;
74+
`
75+
76+
editor.rows = 20;
77+
editor.cols = 80;
78+
editor.spellcheck = false;
79+
editor.value = initialJSON;
80+
81+
function validateCalibration() {
82+
const maybeJson = editor.value;
83+
84+
try {
85+
const json = JSON.parse(maybeJson);
86+
const calibration = Calibration.fromData(json);
87+
88+
resultDiv.style.backgroundColor = "green";
89+
resultDiv.innerHTML = "";
90+
91+
let link = document.createElement("a");
92+
link.innerText = "Click here to continue...";
93+
link.href = "#";
94+
link.style.color = "#EFEFEF";
95+
96+
link.onclick = function() {
97+
localStorage.setItem("calibration-v1", maybeJson);
98+
onCompleted(popupDiv, calibration);
99+
return false;
100+
}
101+
102+
resultDiv.appendChild(link);
103+
} catch (error) {
104+
resultDiv.innerText = error;
105+
resultDiv.style.backgroundColor = "red";
106+
}
107+
}
108+
109+
editor.oninput = validateCalibration;
110+
validateCalibration();
111+
112+
popupDiv.appendChild(titleDiv);
113+
popupDiv.appendChild(editor);
114+
popupDiv.appendChild(resultDiv);
115+
116+
popupDiv.ondrop = function(e) {
117+
e.preventDefault();
118+
119+
let file = e.dataTransfer.files[0],
120+
reader = new FileReader();
121+
122+
reader.onload = function(event) {
123+
editor.value = event.target.result;
124+
validateCalibration();
125+
};
126+
127+
reader.readAsText(file);
128+
return false;
129+
}
130+
131+
return popupDiv;
132+
}
133+
134+
export { CalibrationInput };

examples/calibration_input.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Calibration input</title>
6+
<style>
7+
body { margin: 0; }
8+
</style>
9+
</head>
10+
<body style="background-color:black;">
11+
12+
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
13+
14+
<script type="importmap">
15+
{
16+
"imports": {
17+
"three": "../node_modules/three/build/three.module.js",
18+
"reality-mixer": "../src/mrc.js"
19+
}
20+
}
21+
</script>
22+
23+
<script type="module">
24+
import { CalibrationInput } from './calibration/calibration-input.js';
25+
26+
function onCalibrationInput(editor, calibration) {
27+
editor.parentNode.removeChild(editor);
28+
console.log(calibration);
29+
// We can then initialize the Mixed Reality Capture with this calibration....
30+
}
31+
32+
const calibrationInput = CalibrationInput(onCalibrationInput);
33+
34+
document.body.appendChild(calibrationInput);
35+
</script>
36+
37+
</body>
38+
</html>

examples/webxr_vr_ballshooter.html

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

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

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

4546
const clock = new THREE.Clock();
4647

47-
init();
48-
animate();
48+
// Mixed Reality Calibration Input
4949

50-
function init() {
50+
function onCalibrationInput(editor, calibration) {
51+
editor.parentNode.removeChild(editor);
52+
console.log(calibration);
53+
54+
init(calibration);
55+
animate();
56+
}
57+
58+
const calibrationInput = CalibrationInput(onCalibrationInput);
59+
60+
document.body.appendChild(calibrationInput);
61+
62+
//
63+
64+
function init(calibration) {
5165

5266
scene = new THREE.Scene();
5367
scene.background = new THREE.Color( 0x505050 );
@@ -89,11 +103,6 @@
89103

90104
// Mixed Reality Capture
91105

92-
const cameraCalibration = new MRC.CameraCalibration();
93-
const chromaKey = new MRC.ChromaKey();
94-
const delayInFrames = 5;
95-
const calibration = new MRC.Calibration( cameraCalibration, chromaKey, delayInFrames );
96-
97106
mixedRealityCapture = new MRC.MixedRealityCapture( calibration );
98107

99108
//

0 commit comments

Comments
 (0)