generated from mi-classroom/mi-master-wt-beiboot-2023
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeo-object.html
192 lines (152 loc) · 5.86 KB
/
geo-object.html
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
190
191
192
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script>
; (function () {
var src = '//cdn.jsdelivr.net/npm/eruda';
if (!/eruda=true/.test(window.location) && localStorage.getItem('active-eruda') != 'true') return;
document.write('<scr' + 'ipt src="' + src + '"></scr' + 'ipt>');
document.write('<scr' + 'ipt>eruda.init();</scr' + 'ipt>');
})();
</script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
}
}
</script>
<link rel="stylesheet" href="./assets/base.css">
<title>Geo Object</title>
</head>
<body>
<script type="module">
import * as THREE from 'three';
import { ARButton } from 'three/addons/webxr/ARButton.js';
// @see https://www.movable-type.co.uk/scripts/latlong.html
function distanceBetweenToPoints(lat1, lon1, lat2, lon2) {
const R = 6371e3; // metres
const φ1 = lat1 * Math.PI / 180; // φ, λ in radians
const φ2 = lat2 * Math.PI / 180;
const Δφ = (lat2 - lat1) * Math.PI / 180;
const Δλ = (lon2 - lon1) * Math.PI / 180;
const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const d = R * c; // in metres
return d;
}
// Geo location
const startLocation = {
latitude: null,
longitude: null,
altitude: null,
};
const target = {
latitude: 51.0313889,
longitude: 7.0300000,
};
let geoId;
let hasReachedTarget = false;
function success(pos) {
const crd = pos.coords;
// Set start location
if (!startLocation.latitude && !startLocation.longitude) {
startLocation.latitude = crd.latitude;
startLocation.longitude = crd.longitude;
startLocation.altitude = crd.altitude;
}
const distance = distanceBetweenToPoints(target.latitude, target.longitude, crd.latitude, crd.longitude);
console.log('Your current position is:', distance, 'meters away from the start location');
if (distance < 15) {
console.log("Congratulations, you reached the target");
//navigator.geolocation.clearWatch(geoId);
hasReachedTarget = true;
}
}
geoId = navigator.geolocation.watchPosition(
success,
(err) => {
console.error(`ERROR(${err.code}): ${err.message}`);
}, {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 0,
}
);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
// Lighting
const light = new THREE.HemisphereLight(0xffffff, 0xbbbbff, 1);
light.position.set(0.5, 1, 0.25);
scene.add(light);
// Renderer
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.xr.enabled = true;
document.body.appendChild(renderer.domElement);
document.body.appendChild(ARButton.createButton(renderer, { requiredFeatures: ['hit-test'] }));
const controller = renderer.xr.getController(0);
controller.addEventListener('select', onSelect);
scene.add(controller);
// Reticle
const reticle = new THREE.Mesh(
new THREE.RingGeometry(0.15, 0.2, 32).rotateX(-Math.PI / 2),
new THREE.MeshBasicMaterial()
);
reticle.matrixAutoUpdate = false;
reticle.visible = false;
scene.add(reticle);
const cube = new THREE.CylinderGeometry(0.1, 0.1, 0.2, 32).translate(0, 0.1, 0);
const material = new THREE.MeshPhongMaterial({ color: 0xdd1166 });
const mesh = new THREE.Mesh(cube, material);
mesh.visible = false;
scene.add(mesh);
function onSelect() {
if (reticle.visible) {
mesh.visible = true;
reticle.matrix.decompose(mesh.position, mesh.quaternion, mesh.scale);
}
}
// Animation
let hitTestSource = null;
let hitTestSourceRequested = false;
renderer.setAnimationLoop((timestamp, frame) => {
if (frame) {
const referenceSpace = renderer.xr.getReferenceSpace();
const session = renderer.xr.getSession();
if (hitTestSourceRequested === false) {
session.requestReferenceSpace('viewer').then(function (referenceSpace) {
session.requestHitTestSource({ space: referenceSpace }).then(function (source) {
hitTestSource = source;
});
});
session.addEventListener('end', function () {
hitTestSourceRequested = false;
hitTestSource = null;
});
hitTestSourceRequested = true;
}
if (hitTestSource && hasReachedTarget) {
const hitTestResults = frame.getHitTestResults(hitTestSource);
if (hitTestResults.length) {
const hit = hitTestResults[0];
reticle.visible = true;
reticle.matrix.fromArray(hit.getPose(referenceSpace).transform.matrix);
} else {
reticle.visible = false;
}
}
}
renderer.render(scene, camera);
});
</script>
</body>
</html>