Repository navigation
Expand file tree
/
Copy pathAR1.html
More file actions
83 lines (69 loc) · 3.05 KB
/
Copy pathAR1.html
File metadata and controls
83 lines (69 loc) · 3.05 KB
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
<head>
<script src="https://aframe.io/releases/0.7.1/aframe.min.js"></script>
<script src="https://rawgit.com/chenzlabs/three.ar.js/0f645a5/dist/three.ar.min.js"></script>
<script src="https://rawgit.com/chenzlabs/aframe-ar/0cf8d5a/dist/aframe-ar.min.js"></script>
<script src="https://cdn.rawgit.com/donmccurdy/aframe-extras/cfe5f316/dist/aframe-extras.js"></script>
<script type="text/javascript">
AFRAME.registerComponent('shadow-material', {
init() {
this.material = new THREE.ShadowMaterial();
this.el.getOrCreateObject3D('mesh').material = this.material;
this.material.opacity = 0.3;
}
});
</script>
<!-- Prevent touch causing flicker on iOS. -->
<style> * { -webkit-tap-highlight-color: rgba(0,0,0,0); } </style>
</head>
<body>
<div style="height: 100px; position: fixed; top: 0; left: 0;background-color: white;z-index: 9999;">
<span>Distance: <p id="distance" style="color: black;"></p></span>
</div>
<a-scene ar>
<a-camera ar-raycaster raycaster cursor="fuse:false"> </a-camera>
<a-ring id="cursor" radius-inner="0.02" radius-outer="0.03" color="teal" position="0 -0.8 -5" rotation="-90 0 0"></a-ring>
<a-ring id="pointA" color="red" radius-inner="0.02" radius-outer="0.03" position="0 0 -0.75" rotation="-90 0 0"></a-ring>
<a-plane id="plane" width="0.5" height="0.5" position="0 0 0" rotation="-90 0 0"
color="white" shadow="receive: true" shadow-material></a-plane>
</a-scene>
<script type="text/javascript">
function onSceneLoaded() {
const raycaster = document.querySelector('[ar-raycaster]');
const cursor = document.querySelector('#cursor');
const pointA = document.querySelector('#pointA');
const distanceDiv = document.querySelector('#distance');
let calc = false;
let lastEvent = null;
raycaster.addEventListener('raycaster-intersection', (event) => {
const target = event.detail.intersections[0].point;
cursor.setAttribute('position', target);
if(calc) {
const FirstPosition = pointA.object3D.position;
const dist = FirstPosition.distanceTo(target);
distanceDiv.innerHTML = dist.toFixed(3) + " m";
}
});
raycaster.addEventListener('click', () => {
const currentEvent = new Date().getTime();
if (lastEvent && (currentEvent - lastEvent < 1000)) {
return;
}
lastEvent = currentEvent;
const target = raycaster.components.cursor.intersection.point;
if(!calc) {
pointA.object3D.visible = true;
pointA.setAttribute('position', target);
const plane = document.querySelector('#plane');
plane.object3D.visible = false;
calc = true;
}
else {
calc = false
pointA.object3D.visible = false;
}
});
}
const scene = document.querySelector('a-scene');
scene.addEventListener('loaded', onSceneLoaded);
</script>
</body>