This repository has been archived by the owner on Aug 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
box_demo.html
233 lines (183 loc) · 6.36 KB
/
box_demo.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<html>
<head>
<title>Box demo with Three.js</title>
<meta meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<style>
html,body {
margin: 0;
padding: 0;
width: 100%;
text-align: center;
overflow-x: hidden;
}
.portrait canvas {
transform-origin: 0 0;
transform: rotate(-90deg) translateX(-100%);
}
.desktop canvas {
transform: scale(-1, 1);
}
</style>
</head>
<body>
<h1>Box demo with Three.js</h1>
<p>On Chrome on Android, tap the screen to start playing video stream.</p>
<p>Show <a href="https://github.com/artoolkit/artoolkit5/blob/master/doc/patterns/Matrix%20code%203x3%20(72dpi)/20.png">3x3 marker id 20</a> to camera to display a box on top of it. Tap the box to open it.
<p>← <a href="index.html">Back to examples</a></p>
<script async src="js/artoolkit.min.js"></script>
<script async src="js/third_party/three.js/three.min.js"></script>
<script async src="js/artoolkit.three.js"></script>
<script>
var findObjectUnderEvent = function(ev, camera, objects) {
var style = getComputedStyle(ev.target);
var elementTransform = style.getPropertyValue('transform');
var elementTransformOrigin = style.getPropertyValue('transform-origin');
var xyz = elementTransformOrigin.replace(/px/g, '').split(" ");
xyz[0] = parseFloat(xyz[0]);
xyz[1] = parseFloat(xyz[1]);
xyz[2] = parseFloat(xyz[2] || 0);
var mat = new THREE.Matrix4();
mat.identity();
if (/^matrix\(/.test(elementTransform)) {
var elems = elementTransform.replace(/^matrix\(|\)$/g, '').split(' ');
mat.elements[0] = parseFloat(elems[0]);
mat.elements[1] = parseFloat(elems[1]);
mat.elements[4] = parseFloat(elems[2]);
mat.elements[5] = parseFloat(elems[3]);
mat.elements[12] = parseFloat(elems[4]);
mat.elements[13] = parseFloat(elems[5]);
} else if (/^matrix3d\(/i.test(elementTransform)) {
var elems = elementTransform.replace(/^matrix3d\(|\)$/ig, '').split(' ');
for (var i=0; i<16; i++) {
mat.elements[i] = parseFloat(elems[i]);
}
}
var mat2 = new THREE.Matrix4();
mat2.makeTranslation(xyz[0], xyz[1], xyz[2]);
mat2.multiply(mat);
mat.makeTranslation(-xyz[0], -xyz[1], -xyz[2]);
mat2.multiply(mat);
var vec = new THREE.Vector3(ev.layerX, ev.layerY, 0);
vec.applyMatrix4(mat2);
var width = parseFloat(style.getPropertyValue('width'));
var height = parseFloat(style.getPropertyValue('height'));
var mouse3D = new THREE.Vector3(
( vec.x / width ) * 2 - 1,
-( vec.y / height ) * 2 + 1,
0.5
);
mouse3D.unproject( camera );
mouse3D.sub( camera.position );
mouse3D.normalize();
var raycaster = new THREE.Raycaster( camera.position, mouse3D );
var intersects = raycaster.intersectObjects( objects );
if ( intersects.length > 0 ) {
var obj = intersects[ 0 ].object
return obj;
}
};
var createBox = function() {
// The AR scene.
//
// The box object is going to be placed on top of the marker in the video.
// I'm adding it to the markerRoot object and when the markerRoot moves,
// the box and its children move with it.
//
var box = new THREE.Object3D();
var boxWall = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 0.1, 1, 1, 1),
new THREE.MeshLambertMaterial({color: 0xffffff})
);
boxWall.position.z = -0.5;
box.add(boxWall);
boxWall = boxWall.clone();
boxWall.position.z = +0.5;
box.add(boxWall);
boxWall = boxWall.clone();
boxWall.position.z = 0;
boxWall.position.x = -0.5;
boxWall.rotation.y = Math.PI/2;
box.add(boxWall);
boxWall = boxWall.clone();
boxWall.position.x = +0.5;
box.add(boxWall);
boxWall = boxWall.clone();
boxWall.position.x = 0;
boxWall.position.y = -0.5;
boxWall.rotation.y = 0;
boxWall.rotation.x = Math.PI/2;
box.add(boxWall);
// Keep track of the box walls to test if the mouse clicks happen on top of them.
var walls = box.children.slice();
// Create a pivot for the lid of the box to make it rotate around its "hinge".
var pivot = new THREE.Object3D();
pivot.position.y = 0.5;
pivot.position.x = 0.5;
// The lid of the box is attached to the pivot and the pivot is attached to the box.
boxWall = boxWall.clone();
boxWall.position.y = 0;
boxWall.position.x = -0.5;
pivot.add(boxWall);
box.add(pivot);
walls.push(boxWall);
box.position.z = 0.5;
box.rotation.x = Math.PI/2;
box.open = false;
box.tick = function() {
// Animate the box lid to open rotation or closed rotation, depending on the value of the open variable.
pivot.rotation.z += ((box.open ? -Math.PI/1.5 : 0) - pivot.rotation.z) * 0.1;
};
return {box: box, walls: walls};
};
window.ARThreeOnLoad = function() {
ARController.getUserMediaThreeScene({maxARVideoSize: 320, cameraParam: 'Data/camera_para-iPhone 5 rear 640x480 1.0m.dat',
onSuccess: function(arScene, arController, arCamera) {
arController.setPatternDetectionMode(artoolkit.AR_MATRIX_CODE_DETECTION);
document.body.className = arController.orientation;
var renderer = new THREE.WebGLRenderer({antialias: true});
if (arController.orientation === 'portrait') {
var w = (window.innerWidth / arController.videoHeight) * arController.videoWidth;
var h = window.innerWidth;
renderer.setSize(w, h);
renderer.domElement.style.paddingBottom = (w-h) + 'px';
} else {
if (/Android|mobile|iPad|iPhone/i.test(navigator.userAgent)) {
renderer.setSize(window.innerWidth, (window.innerWidth / arController.videoWidth) * arController.videoHeight);
} else {
renderer.setSize(arController.videoWidth, arController.videoHeight);
document.body.className += ' desktop';
}
}
document.body.insertBefore(renderer.domElement, document.body.firstChild);
// Create a couple of lights for our AR scene.
var light = new THREE.PointLight(0xffffff);
light.position.set(40, 40, 40);
arScene.scene.add(light);
var light = new THREE.PointLight(0xff8800);
light.position.set(-40, -20, -30);
arScene.scene.add(light);
var box = createBox();
renderer.domElement.addEventListener('click', function(ev) {
if (findObjectUnderEvent(ev, arScene.camera, box.walls)) {
box.box.open = !box.box.open;
}
}, false);
var markerRoot = arController.createThreeBarcodeMarker(20);
markerRoot.add(box.box);
arScene.scene.add(markerRoot);
var tick = function() {
arScene.process();
box.box.tick();
arScene.renderOn(renderer);
requestAnimationFrame(tick);
};
tick();
}});
delete window.ARThreeOnLoad;
};
if (window.ARController && ARController.getUserMediaThreeScene) {
ARThreeOnLoad();
}
</script>
</body>
</html>