-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
189 lines (161 loc) · 5.87 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import * as THREE from './jsm/three.module.js';
import { GLTFExporter } from './jsm/GLTFExporter.js';
let camera, scene, renderer, container;
let conLeft, conRight, xrConLeft, xrConRight;
let light, cubeInterval, cubeColor;
let markers = new THREE.Group();
let outputEl, totalPointsEl, statusEl;
init();
requestSession();
window.addEventListener("unload", closeSession);
function init() {
outputEl = document.getElementById('output');
totalPointsEl = document.getElementById('totalPoints');
statusEl = document.getElementById('status');
container = document.createElement('div');
document.body.appendChild(container);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
light = new THREE.HemisphereLight(0xffffbb, 0x080820, 1);
scene.add(light);
conLeft = new THREE.Mesh(new THREE.BoxGeometry(0.1, 0.1, 0.1), new THREE.MeshLambertMaterial({
color: 0xff0000
}));
conRight = new THREE.Mesh(new THREE.BoxGeometry(0.1, 0.1, 0.1), new THREE.MeshLambertMaterial({
color: 0x0000ff
}));
conLeft.visible = false;
conRight.visible = false;
scene.add(conLeft, conRight);
scene.add(markers);
renderer = new THREE.WebGLRenderer({
alpha: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
renderer.xr.enabled = true;
document.addEventListener('click', function (e) {
if (e.target.id === 'outputPoints') outputPoints();
if (e.target.id === 'downloadPoints') downloadPoints();
else if (e.target.id === 'importPoints') importPoints();
else if (e.target.id === 'downloadScene') downloadScene();
}, false);
}
function requestSession() {
totalPointsEl.parentElement.style.display = 'none';
statusEl.parentElement.style.display = 'block';
statusEl.innerHTML = 'Requesting WebXR session';
navigator.xr.isSessionSupported('immersive-vr').then(function () {
let options = { optionalFeatures: ['local-floor', 'bounded-floor'] };
navigator.xr.requestSession('immersive-vr', options).then(onSessionStarted).catch(function (err) {
totalPointsEl.parentElement.style.display = 'none';
statusEl.parentElement.style.display = 'block';
statusEl.innerHTML = err;
});
});
}
function onSessionStarted(session) {
renderer.xr.setSession(session);
xrConLeft = renderer.xr.getController(0);
xrConRight = renderer.xr.getController(1);
renderer.xr.getSession().addEventListener('selectstart', onSelectStart);
renderer.xr.getSession().addEventListener('selectend', onSelectEnd);
renderer.xr.getSession().addEventListener('select', onSelect);
animate();
totalPointsEl.parentElement.style.display = 'block';
statusEl.parentElement.style.display = 'none';
}
async function closeSession() {
totalPointsEl.parentElement.style.display = 'none';
statusEl.parentElement.style.display = 'block';
statusEl.innerHTML = 'WebXR session ended';
await renderer.xr.getSession().end();
}
function onSelectStart() {
cubeColor = Math.random() * 0xffffff;
addMarker(xrConRight.position);
cubeInterval = setInterval(function () {
addMarker(xrConRight.position);
}, 250)
}
function onSelectEnd() {
clearInterval(cubeInterval);
}
function onSelect() {
clearInterval(cubeInterval);
}
function animate() {
renderer.setAnimationLoop(render);
}
function render() {
conLeft.position.x = xrConLeft.position.x;
conLeft.position.y = xrConLeft.position.y;
conLeft.position.z = xrConLeft.position.z;
conLeft.rotation.x = xrConLeft.rotation.x;
conLeft.rotation.y = xrConLeft.rotation.y;
conLeft.rotation.z = xrConLeft.rotation.z;
conRight.position.x = xrConRight.position.x;
conRight.position.y = xrConRight.position.y;
conRight.position.z = xrConRight.position.z;
conRight.rotation.x = xrConRight.rotation.x;
conRight.rotation.y = xrConRight.rotation.y;
conRight.rotation.z = xrConRight.rotation.z;
renderer.render(scene, camera);
}
function addMarker(position) {
let marker = new THREE.Mesh(new THREE.BoxGeometry(0.05, 0.05, 0.05), new THREE.MeshLambertMaterial({ color: cubeColor }));
marker.position.x = position.x;
marker.position.y = position.y;
marker.position.z = position.z;
markers.add(marker);
totalPointsEl.innerHTML = markers.children.length;
}
function outputPoints() {
let points = [];
for (let i = 0; i < markers.children.length; i++) {
points.push({
x: markers.children[i].position.x,
y: markers.children[i].position.y,
z: markers.children[i].position.z,
});
}
outputEl.value = JSON.stringify(points);
}
function importPoints() {
let points = JSON.parse(outputEl.value);
if (typeof points === 'string') points = JSON.parse(points); // Output from text file is wrapped in quotes
cubeColor = Math.random() * 0xffffff;
for (let i = 0; i < points.length; i++) {
addMarker(points[i]);
}
outputEl.value = '';
}
function downloadScene() {
var exporter = new GLTFExporter();
exporter.parse(scene, function (gltf) {
download('gltf', JSON.stringify(gltf));
}, {
truncateDrawRange: false
});
}
function downloadPoints() {
outputPoints();
download('txt', JSON.stringify(outputEl.value));
}
function download(extension, data) {
var downloadLink = document.createElement('a');
downloadLink.setAttribute('href', `data:text/plain;charset=utf-8,${encodeURIComponent(data)}`);
downloadLink.setAttribute('download', `environment_${dateTimeString()}.${extension}`);
downloadLink.style.display = 'none';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
function dateTimeString() {
let today = new Date();
let ampm = (today.getHours() >= 12) ? "PM" : "AM";
let date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
let time = today.getHours() + "." + today.getMinutes() + "." + today.getSeconds();
return `${date}_${time}${ampm}`;
}