-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ordering System (HW6).html
473 lines (429 loc) · 14.3 KB
/
Ordering System (HW6).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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<!DOCTYPE html>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" charset="utf-8">
<title>請問您今天要來點兔子嗎?</title>
<link rel="shortcut icon" href="https://i.imgur.com/jnjsKvZ.png">
<!-- three.js library -->
<script src="https://cdn.jsdelivr.net/npm/three@0.113.0/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.113.0/examples/js/loaders/MTLLoader.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.113.0/examples/js/loaders/OBJLoader.js"></script>
<script src="js/stats.min.js"></script>
<!-- ar.js -->
<script src="js/ar.min.js"></script>
<style>
body {
margin: 0px;
overflow: hidden;
outline : none; /* 避免點選會有藍線或虛線 */
-webkit-user-select: none; /* 禁止選取 for Chrome */
-moz-user-select: none; /* 禁止選取 for Firefox */
}
</style>
<body>
<script>
// global variables
var renderer, scene, camera;
var sceneHUD, cameraHUD;
var stats;
var markerRoot;
var markerHiro;
var arToolKitSource, arToolKitContext;
var texture_muffin, texture_donut, texture_toast, texture_waffle;
var raycaster, pickables;
var muffin = new THREE.Object3D();
var donut = new THREE.Object3D();
var toast = new THREE.Object3D();
var waffle = new THREE.Object3D();
var angle = 0;
init();
animate();
function init() {
let _iOSDevice = !!navigator.platform.match(/iPhone|iPod|iPad/);
console.log ('iOS: ' + _iOSDevice)
// init renderer
renderer = new THREE.WebGLRenderer({
antialias : true,
alpha: true
});
renderer.setClearColor(new THREE.Color('lightgrey'), 0)
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 );
// for multi-pass rendering
renderer.autoClear = false;
//renderer shadow
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
raycaster = new THREE.Raycaster();
pickables = [];
// init scene and camera
scene = new THREE.Scene();
camera = new THREE.Camera();
scene.add(camera);
sceneHUD = new THREE.Scene();
cameraHUD = new THREE.OrthographicCamera (-10,10,10,-10, -10,10);
////////////////////////////////////
var loader = new THREE.TextureLoader();
loader.setCrossOrigin ('');
///////////// 選取按鈕 /////////////
var geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3(-1, 1, 0),//左上
new THREE.Vector3(-1, -1, 0),//左下
new THREE.Vector3(1, -1, 0),//右下
new THREE.Vector3(1, 1, 0)//右上
);
var face = new THREE.Face3(0, 1, 2);
face.materialIndex = 0;
geometry.faces.push(face);
face = new THREE.Face3(0, 2, 3);
face.materialIndex = 0;
geometry.faces.push(face);
geometry.faceVertexUvs[0].push([new THREE.Vector2(0, 1), new THREE.Vector2(0, 0), new THREE.Vector2(1, 0)]);
geometry.faceVertexUvs[0].push([new THREE.Vector2(0, 1), new THREE.Vector2(1, 0), new THREE.Vector2(1, 1)]);
geometry.computeBoundingSphere();
geometry.computeFaceNormals();
geometry.computeVertexNormals();
texture_muffin = new THREE.Mesh (geometry, new THREE.MeshBasicMaterial({map: loader.load('texture/HW6/muffin.png'), transparent: true}));
texture_donut = new THREE.Mesh (geometry, new THREE.MeshBasicMaterial({map: loader.load('texture/HW6/donut.png'), transparent: true}));
texture_toast = new THREE.Mesh (geometry, new THREE.MeshBasicMaterial({map: loader.load('texture/HW6/toast.png'), transparent: true}));
texture_waffle = new THREE.Mesh (geometry, new THREE.MeshBasicMaterial({map: loader.load('texture/HW6/waffle.png'), transparent: true}));
texture_muffin.position.set(9, 4.5, 0);
texture_donut.position.set(9, 1.5, 0);
texture_toast.position.set(9, -1.5, 0);
texture_waffle.position.set(9, -4.5, 0);
sceneHUD.add(texture_muffin);
sceneHUD.add(texture_donut);
sceneHUD.add(texture_toast);
sceneHUD.add(texture_waffle);
////////////////////////////////////////////////////////////////////////////////
// handle arToolkitSource
////////////////////////////////////////////////////////////////////////////////
arToolkitSource = new THREEx.ArToolkitSource({
sourceType : 'webcam'
})
arToolkitSource.init(function onReady(){
onResize()
})
// handle resize
window.addEventListener('resize', function(){
onResize()
})
function onResize(){
arToolkitSource.onResizeElement();
arToolkitSource.copyElementSizeTo(renderer.domElement);
if( arToolkitContext.arController !== null ){
arToolkitSource.copyElementSizeTo(arToolkitContext.arController.canvas)
}
}
////////////////////////////////////////////////////////////////////////////////
// initialize arToolkitContext
////////////////////////////////////////////////////////////////////////////////
arToolkitContext = new THREEx.ArToolkitContext({
cameraParametersUrl: 'deputyProgram/camera_para.dat',
detectionMode: 'mono',
maxDetectionRate: 30,
canvasWidth: 80*3,
canvasHeight: 60*3,
})
// initialize it
arToolkitContext.init(function onCompleted(){
// copy projection matrix to camera
camera.projectionMatrix.copy( arToolkitContext.getProjectionMatrix() );
})
markerRoot = new THREE.Group()
scene.add(markerRoot)
var artoolkitMarker = new THREEx.ArMarkerControls(arToolkitContext, markerRoot, {
type: 'pattern',
patternUrl: 'deputyProgram/patt.hiro'
})
markerRoot.add( new THREE.AmbientLight( 0xffffff ) );
var light = new THREE.DirectionalLight( 'white', 1 );
light.position.set( 25, 100, -25 );
markerRoot.add( light );
light.castShadow = true;
light.shadow.mapSize.set(1024,1024);
light.shadow.camera.near = 50;
light.shadow.camera.far = 500;
var gridXZ = new THREE.GridHelper(100, 10, 'red', 'white');
//markerRoot.add(unitize (gridXZ, 1));
var axes = new THREE.AxisHelper(100);
//markerRoot.add(unitize (axes, 1));
//Marker hidden
var marker_hidden = markerHide();
markerRoot.add(marker_hidden);
// model muffin
var onProgress = function(xhr) {
if (xhr.lengthComputable) {
var percentComplete = xhr.loaded / xhr.total * 100;
console.log(Math.round(percentComplete, 2) + '% downloaded');
}
};
var onError = function(xhr) {};
var mtlLoader = new THREE.MTLLoader();
mtlLoader.setPath('model/HW6/');
mtlLoader.load('Blueberry_Muffin.mtl', function(materials) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials(materials);
objLoader.setPath('model/HW6/');
objLoader.load('Blueberry_Muffin.obj', function(object) {
muffin.add(unitize (object, .5));
muffin.visible = false;
muffin.traverse (
function (mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.castShadow = true;
mesh.receiveShadow = true;
}
}
);
markerRoot.add(muffin);
object.traverse (
function(mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.material.side = THREE.DoubleSide;
}
});
}, onProgress, onError);
});
//model donut
mtlLoader.setPath('model/HW6/');
mtlLoader.load('donut.mtl', function(materials) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials(materials);
objLoader.setPath('model/HW6/');
objLoader.load('donut.obj', function(object) {
donut.add(unitize (object, .5));
donut.position.y = .1;
donut.visible = false;
donut.traverse (
function (mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.castShadow = true;
mesh.receiveShadow = true;
}
}
);
markerRoot.add(donut);
object.traverse (
function(mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.material.side = THREE.DoubleSide;
}
});
}, onProgress, onError);
});
//model toast
mtlLoader.setPath('model/HW6/');
mtlLoader.load('Toast.mtl', function(materials) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials(materials);
objLoader.setPath('model/HW6/');
objLoader.load('Toast.obj', function(object) {
toast.add(unitize (object, .5));
toast.visible = false;
toast.traverse (
function (mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.castShadow = true;
mesh.receiveShadow = true;
}
}
);
markerRoot.add(toast);
object.traverse (
function(mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.material.side = THREE.DoubleSide;
}
});
}, onProgress, onError);
});
//model waffle
mtlLoader.setPath('model/HW6/');
mtlLoader.load('waffle.mtl', function(materials) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials(materials);
objLoader.setPath('model/HW6/');
objLoader.load('waffle.obj', function(object) {
waffle.add(unitize (object, .6));
waffle.position.y = .05;
waffle.visible = false;
waffle.traverse (
function (mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.castShadow = true;
mesh.receiveShadow = true;
}
}
);
markerRoot.add(waffle);
object.traverse (
function(mesh) {
if (mesh instanceof THREE.Mesh) {
mesh.material.side = THREE.DoubleSide;
}
});
}, onProgress, onError);
});
//FPS
stats = new Stats();
document.body.appendChild( stats.dom );
//iOS touch介面?
if (_iOSDevice)
window.addEventListener('touchstart', onTouchStart, false)
else
window.addEventListener ('mousedown', onMouseDown, false);
}
function animate() {
requestAnimationFrame (animate);
if( arToolkitSource.ready === false ) return
arToolkitContext.update( arToolkitSource.domElement )
///////////////////////////////////////////
angle += 0.01;
muffin.rotation.y = -angle;
donut.rotation.y = -angle;
toast.rotation.y = -angle;
waffle.rotation.y = -angle;
texture_muffin.material.visible = markerRoot.visible;
texture_donut.material.visible = markerRoot.visible;
texture_toast.material.visible = markerRoot.visible;
texture_waffle.material.visible = markerRoot.visible;
renderer.clear();
renderer.render (scene, camera);
renderer.render (sceneHUD, cameraHUD);
stats.update();
}
function onTouchStart (event) {
console.log ('in touch start');
if(event.touches.length == 1) {
event.preventDefault();
let touchX = (event.touches[0].pageX / window.innerWidth) * 2 - 1;
let touchY = -(event.touches[0].pageY / window.innerHeight) * 2 + 1;
if(markerRoot.visible === true){
if(touchX < 1 && touchX > 0.80 && touchY < 0.74 && touchY > 0.54){
muffin.visible = true;
donut.visible = false;
toast.visible = false;
waffle.visible = false;
texture_muffin.material.opacity = .5;
texture_donut.material.opacity = 1;
texture_toast.material.opacity = 1;
texture_waffle.material.opacity = 1;
}
if(touchX < 1 && touchX > 0.80 && touchY < 0.34 && touchY > 0.06){
muffin.visible = false;
donut.visible = true;
toast.visible = false;
waffle.visible = false;
texture_muffin.material.opacity = 1;
texture_donut.material.opacity = .5;
texture_toast.material.opacity = 1;
texture_waffle.material.opacity = 1;
}
if(touchX < 1 && touchX > 0.80 && touchY < -0.06 && touchY > -0.34){
muffin.visible = false;
donut.visible = false;
toast.visible = true;
waffle.visible = false;
texture_muffin.material.opacity = 1;
texture_donut.material.opacity = 1;
texture_toast.material.opacity = .5;
texture_waffle.material.opacity = 1;
}
if(touchX < 1 && touchX > 0.80 && touchY < -0.54 && touchY > -0.74){
muffin.visible = false;
donut.visible = false;
toast.visible = false;
waffle.visible = true;
texture_muffin.material.opacity = 1;
texture_donut.material.opacity = 1;
texture_toast.material.opacity = 1;
texture_waffle.material.opacity = .5;
}
}
}
}
function onMouseDown (event) {
event.preventDefault();
let mouseX = (event.clientX / window.innerWidth) * 2 - 1;
let mouseY = -(event.clientY / window.innerHeight) * 2 + 1;
if(markerRoot.visible === true){
if(mouseX < 1 && mouseX > 0.80 && mouseY < 0.84 && mouseY > 0.54){
muffin.visible = true;
donut.visible = false;
toast.visible = false;
waffle.visible = false;
texture_muffin.material.opacity = .5;
texture_donut.material.opacity = 1;
texture_toast.material.opacity = 1;
texture_waffle.material.opacity = 1;
}
if(mouseX < 1 && mouseX > 0.80 && mouseY < 0.38 && mouseY > 0.08){
muffin.visible = false;
donut.visible = true;
toast.visible = false;
waffle.visible = false;
texture_muffin.material.opacity = 1;
texture_donut.material.opacity = .5;
texture_toast.material.opacity = 1;
texture_waffle.material.opacity = 1;
}
if(mouseX < 1 && mouseX > 0.80 && mouseY < -0.08 && mouseY > -0.38){
muffin.visible = false;
donut.visible = false;
toast.visible = true;
waffle.visible = false;
texture_muffin.material.opacity = 1;
texture_donut.material.opacity = 1;
texture_toast.material.opacity = .5;
texture_waffle.material.opacity = 1;
}
if(mouseX < 1 && mouseX > 0.80 && mouseY < -0.54 && mouseY > -0.84){
muffin.visible = false;
donut.visible = false;
toast.visible = false;
waffle.visible = true;
texture_muffin.material.opacity = 1;
texture_donut.material.opacity = 1;
texture_toast.material.opacity = 1;
texture_waffle.material.opacity = .5;
}
}
}
function markerHide(){
var plane = new THREE.Object3D();
var material = new THREE.MeshStandardMaterial({color: 0xdddddd});
var geometry = new THREE.PlaneGeometry(1.1, 1.1); //marker顯示誤差會沒辦法隱藏全部,因此設大一點比較保險。
var mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = -Math.PI/2;
mesh.receiveShadow = true;
plane.add (mesh);
return plane;
}
function unitize (object, targetSize) {
// find bounding box of 'object'
var box3 = new THREE.Box3();
box3.setFromObject (object);
var size = new THREE.Vector3();
size.subVectors (box3.max, box3.min);
var center = new THREE.Vector3();
center.addVectors(box3.max, box3.min).multiplyScalar (0.5);
console.log ('center: ' + center.x + ', '+center.y + ', '+center.z );
console.log ('size: ' + size.x + ', ' + size.y + ', '+size.z );
// uniform scaling according to objSize
var objSize = Math.max (size.x, size.y, size.z);
var scaleSet = targetSize/objSize;
var theObject = new THREE.Object3D();
theObject.add (object);
object.scale.set (scaleSet, scaleSet, scaleSet);
//object.position.set (-center.x*scaleSet, -center.y*scaleSet + size.y/2*scaleSet, -center.z*scaleSet);
return theObject;
}
</script>
</body>