-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
339 lines (302 loc) · 10.1 KB
/
index.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!DOCTYPE html>
<head>
<meta
name="viewport"
content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"
/>
<title>Shapes - AR</title>
<link rel="stylesheet" href="css/main.css" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
</head>
<script src="js/lib/three.min.js"></script>
<script src="js/lib/stats.min.js"></script>
<script src="js/lib/ar-threex.js"></script>
<script src="js/gestureDetector.js"></script>
<script src="js/gestureHandler.js"></script>
<body>
<div id="loading-screen">
<img id="loading-logo" src="data/logo.png" />
<h1>
Shapes <sup>AR</sup> <br />
<br />
is <br />
<br />
loading...
</h1>
<img class="animated-gestures" src="data/camera.gif" />
<div id="camera">Allow camera permissions</div>
<br />
<img id="scan" class="animated-gestures" src="data/scan.gif" /> Scan marker
using phone
<br />
<img
id="gesture-move"
class="animated-gestures"
src="data/moveGesture.gif"
/>
Rotate shapes using fingers
<br />
<img
id="gesture-pinch"
class="animated-gestures"
src="data/pinchGesture.gif"
/>
Make them bigger by pinching<br />
</div>
<div id="header">
<button id="btn-about" onclick="showAbout()">?</button>
<img id="img-header" src="data/logo.png" />
<h1>Shapes <sup>AR</sup></h1>
</div>
<div id="UI">
<button onclick="changeShape()">New Shape!</button>
<button onclick="changeColor()">Change Color!</button>
<br />
<br />
<a href="data/hiro.png" target="_blank" id="markerLink">Get marker</a>
</div>
<script>
//////////////////////////////////////////////////////////////////////////////////
// Init
//////////////////////////////////////////////////////////////////////////////////
// init renderer
var renderer = new THREE.WebGLRenderer({
// antialias : true, // Mobile optimization
alpha: true,
});
renderer.setClearColor(new THREE.Color("lightgrey"), 0);
// renderer.setPixelRatio( 1/2 );
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.domElement.style.position = "absolute";
renderer.domElement.style.top = "0px";
renderer.domElement.style.left = "0px";
document.body.appendChild(renderer.domElement);
// array of functions for the rendering loop
var onRenderFcts = [];
var arToolkitContext, arMarkerControls;
var markerRoot;
// init scene and camera
var scene = new THREE.Scene();
//////////////////////////////////////////////////////////////////////////////////
// Initialize a basic camera
//////////////////////////////////////////////////////////////////////////////////
// Create a camera
var camera = new THREE.Camera();
scene.add(camera);
markerRoot = new THREE.Group();
scene.add(markerRoot);
////////////////////////////////////////////////////////////////////////////////
// handle arToolkitSource
////////////////////////////////////////////////////////////////////////////////
var arToolkitSource = new THREEx.ArToolkitSource({
sourceType: "webcam",
});
arToolkitSource.init(function onReady() {
initARContext();
setTimeout(() => {
document.getElementById("loading-screen").remove();
}, 5000);
setTimeout(() => {
onResize();
}, 2000);
});
// handle resize
window.addEventListener("resize", function () {
onResize();
});
function onResize() {
arToolkitSource.onResizeElement();
arToolkitSource.copyElementSizeTo(renderer.domElement);
if (window.arToolkitContext.arController !== null) {
arToolkitSource.copyElementSizeTo(
window.arToolkitContext.arController.canvas
);
}
}
////////////////////////////////////////////////////////////////////////////////
// initialize arToolkitContext
////////////////////////////////////////////////////////////////////////////////
function initARContext() {
// create atToolkitContext
arToolkitContext = new THREEx.ArToolkitContext({
cameraParametersUrl: "data/camera_para.dat",
detectionMode: "mono",
maxDetectionRate: 30,
canvasWidth: 80 * 3,
canvasHeight: 60 * 3,
});
// initialize it
arToolkitContext.init(() => {
// copy projection matrix to camera
camera.projectionMatrix.copy(arToolkitContext.getProjectionMatrix());
arToolkitContext.arController.orientation = getSourceOrientation();
arToolkitContext.arController.options.orientation =
getSourceOrientation();
console.log("arToolkitContext", arToolkitContext);
window.arToolkitContext = arToolkitContext;
});
arMarkerControls = new THREEx.ArMarkerControls(
arToolkitContext,
markerRoot,
{
type: "pattern",
patternUrl: "data/patt.hiro",
}
);
}
function getSourceOrientation() {
if (!arToolkitSource) {
return null;
}
console.log(
"actual source dimensions",
arToolkitSource.domElement.videoWidth,
arToolkitSource.domElement.videoHeight
);
if (
arToolkitSource.domElement.videoWidth >
arToolkitSource.domElement.videoHeight
) {
console.log("source orientation", "landscape");
return "landscape";
} else {
console.log("source orientation", "portrait");
return "portrait";
}
}
// update artoolkit on every frame
onRenderFcts.push(function () {
if (!arToolkitContext || !arToolkitSource || !arToolkitSource.ready) {
return;
}
arToolkitContext.update(arToolkitSource.domElement);
});
////////////////////////////////////////////////////////////////////////////////
// Create a ArMarkerControls
////////////////////////////////////////////////////////////////////////////////
var markerRoot = new THREE.Group();
scene.add(markerRoot);
// build a smoothedControls
var smoothedRoot = new THREE.Group();
scene.add(smoothedRoot);
var smoothedControls = new THREEx.ArSmoothedControls(smoothedRoot, {
lerpPosition: 0.4,
lerpQuaternion: 0.3,
lerpScale: 1,
});
onRenderFcts.push(function (delta) {
smoothedControls.update(markerRoot);
});
//////////////////////////////////////////////////////////////////////////////////
// add an object in the scene
//////////////////////////////////////////////////////////////////////////////////
var arWorldRoot = smoothedRoot;
const light1 = new THREE.AmbientLight(0x505050, 1);
const light2 = new THREE.PointLight(0xffffff, 2, 45);
light2.position.set(6, 25, 3);
arWorldRoot.add(light1);
arWorldRoot.add(light2);
function getRandomMaterial() {
randomColor = parseInt(
Math.floor(Math.random() * 16777215).toString(16),
16
);
return new THREE.MeshPhongMaterial({
color: new THREE.Color(randomColor),
});
}
var shapeArray = [
() => {
return new THREE.BoxGeometry(1, 1, 1);
},
() => {
return new THREE.ConeGeometry(0.5, 1, 20);
},
() => {
return new THREE.TetrahedronGeometry(0.7, 0);
},
() => {
return new THREE.CylinderGeometry(0.5, 0.5, 1, 20);
},
() => {
return new THREE.TorusKnotGeometry(0.4, 0.12, 64, 16);
},
() => {
return new THREE.OctahedronGeometry(0.7, 0);
},
() => {
return new THREE.SphereGeometry(0.5, 32, 16);
},
() => {
return new THREE.TorusGeometry(0.5, 0.2, 16, 100);
},
];
var currentShapeIndex = 4;
function getRandomShapeIndex() {
return Math.floor(Math.random() * shapeArray.length);
}
function setNewShape() {
var newShapeIndex = getRandomShapeIndex();
// Make sure we do not get the same shape with a different color
while (currentShapeIndex == newShapeIndex) {
newShapeIndex = getRandomShapeIndex();
}
currentShapeIndex = newShapeIndex;
var geometry = shapeArray[currentShapeIndex]();
var material = getRandomMaterial();
var shape = new THREE.Mesh(geometry, material);
shape.position.y = 0.7;
arWorldRoot.add(shape);
return shape;
}
var shape = setNewShape();
// Setup gestures
const gDetector = new gestureDetector();
const gHandler = new gestureHandler(shape);
onRenderFcts.push(function (delta) {
gHandler.update(shape);
});
//////////////////////////////////////////////////////////////////////////////////
// render the whole thing on the page
//////////////////////////////////////////////////////////////////////////////////
var stats = new Stats();
// document.body.appendChild(stats.dom);
// render the scene
onRenderFcts.push(function () {
renderer.render(scene, camera);
stats.update();
});
// run the rendering loop
var lastTimeMsec = null;
requestAnimationFrame(function animate(nowMsec) {
// keep looping
requestAnimationFrame(animate);
// measure time
lastTimeMsec = lastTimeMsec || nowMsec - 1000 / 60;
var deltaMsec = Math.min(200, nowMsec - lastTimeMsec);
lastTimeMsec = nowMsec;
// call each update function
onRenderFcts.forEach(function (onRenderFct) {
onRenderFct(deltaMsec / 1000, nowMsec / 1000);
});
});
//////////////////////////////////////////////////////////////////////////////////
// My functions
//////////////////////////////////////////////////////////////////////////////////
function changeShape() {
shape.geometry.dispose();
shape.material.dispose();
arWorldRoot.remove(shape);
shape = setNewShape();
}
function changeColor() {
shape.material = getRandomMaterial();
}
function showAbout() {
alert(
"Shapes-AR by Naushikha Jayawickrama\nDesigned for Computer Graphics II Course @ UCSC."
);
}
</script>
</body>