-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
373 lines (344 loc) · 10.7 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
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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PONG</title>
<script type="text/javascript" src="three.js/build/three.js"></script>
<script type="text/javascript" src="arucojs/src/svd.js"></script>
<script type="text/javascript" src="arucojs/src/posit1.js"></script>
<script type="text/javascript" src="arucojs/src/cv.js"></script>
<script type="text/javascript" src="arucojs/src/aruco.js"></script>
<script type="text/javascript" src="creation.js"></script>
<script type="text/javascript" src="ball.js"></script>
<script type="text/javascript" src="score.js"></script>
<script type="text/javascript" src="transpose.js"></script>
<script>
var video, canvas, context, imageData, detector, posit;
var renderer;
var markersLength;
var scene3, scene4, sceneTrash;
var camera3, camera4;
var rotation_inv, translation_inv;
var deep = 0;
//Paddles variables
var modelSize = 60.0; //millimeters
var raq1SizeX, raq2SizeX;
var offsetx = modelSize*2;
var offsety = modelSize*4;
var decalage_raquette = 5;
var raquette1, raquette2;
var idPlateau = 17;
var idRaquette1 = 69;
var raquette1DirY = 0, raquette2DirY = 0, paddleSpeed = 3;
var paddleWidth = modelSize, paddleHeight = modelSize/4, paddleDepth = 10, paddleQuality = 1;
var center1, center2, center3;
//field variables
var plateau, texture;
var fieldWidth = modelSize + offsetx;
var fieldHeight = modelSize + offsety;
//ball variables
var ball;
var ballDirX = 1, ballDirY = 1, ballSpeed = 2;
var goBall = false;
function onLoad(){
video = document.getElementById("video");
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
canvas.width = parseInt(canvas.style.width);
canvas.height = parseInt(canvas.style.height);
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
if (navigator.getUserMedia){
init();
}
};
function init(){
navigator.getUserMedia({video:true},
function (stream){
if (window.webkitURL) {
video.src = window.webkitURL.createObjectURL(stream);
}
else if (video.mozSrcObject !== undefined) {
video.mozSrcObject = stream;
}
else {
video.src = stream;
}
},
function(error){
}
);
detector = new AR.Detector();
posit = new POS.Posit(modelSize, canvas.width);
createRenderers();
createScenes();
requestAnimationFrame(tick);
};
function createRenderers(){
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0xffffff, 1);
renderer.setSize(canvas.width, canvas.height);
document.getElementById("container").appendChild(renderer.domElement);
scene3 = new THREE.Scene();
camera3 = new THREE.OrthographicCamera(-0.5, 0.5, 0.5, -0.5);
scene3.add(camera3);
scene4 = new THREE.Scene();
//camera4 = new THREE.OrthographicCamera(-canvas.width,canvas.width,canvas.height,-canvas.height);
camera4 = new THREE.PerspectiveCamera(50, canvas.width / canvas.height, 1, 10000);
scene4.add(camera4);
sceneTrash = new THREE.Scene();
};
function createScenes(){
//Create texture with video
texture = createTexture();
scene3.add(texture);
//Create game field
plateau = createPlateau();
//Create paddles
raquette1 = createRaquette1();
raq1SizeX = raquette1.scale.x;
raquette2 = createRaquette2();
raq2SizeX = raquette2.scale.x;
plateau.add(raquette1);
//plateau.add(raquette2);
scene4.add(plateau);
// Create a ball with sphere geometry
ball = createBall();
plateau.add(ball);
//Set ball position
ball.position.x = 0;
ball.position.y = 0;
// set ball above the table surface
ball.position.z = 5;
ball.receiveShadow = true;
ball.castShadow = true;
var light = createPointLight();
scene4.add(light);
var spotLight = createSpotLight();
scene4.add(spotLight);
// MAGIC SHADOW CREATOR DELUXE EDITION with Lights PackTM DLC
renderer.shadowMapEnabled = true;
};
function tick(){
requestAnimationFrame(tick);
if (video.readyState === video.HAVE_ENOUGH_DATA){
snapshot();
var markers = detector.detect(imageData);
drawCorners(markers);
updateScenes(markers);
document.onkeydown = function(event) {
if (event.KeyCode = 13) {
goBall = !goBall;
}
}
if (goBall) {
ballPhysics();
}
paddlePhysics();
render();
}
};
function snapshot(){
context.drawImage(video, 0, 0, canvas.width, canvas.height);
imageData = context.getImageData(0, 0, canvas.width, canvas.height);
};
function drawCorners(markers){
var corners, corner, i, j;
context.lineWidth = 3;
for (i = 0; i < markers.length; ++ i){
corners = markers[i].corners;
context.strokeStyle = "red";
context.beginPath();
for (j = 0; j < corners.length; ++ j){
corner = corners[j];
context.moveTo(corner.x, corner.y);
corner = corners[(j + 1) % corners.length];
context.lineTo(corner.x, corner.y);
}
context.stroke();
context.closePath();
context.strokeStyle = "green";
context.strokeRect(corners[0].x - 2, corners[0].y - 2, 4, 4);
}
};
function render(){
renderer.clear();
renderer.autoClear = false;
renderer.render(scene3, camera3);
renderer.render(scene4, camera4);
};
function updateScenes(markers){
var corners, corner, pose, i, j, k;
markersLength = markers.length;
if(markersLength > 0) {
var indexPlateau = -1;
var indexRaquette1 = -1;
var indexRaquette2 = -1;
//On récupère les indices des markers
sceneTrash.add(plateau);
sceneTrash.add(raquette1);
sceneTrash.add(raquette2);
for (i = 0 ; i < markersLength; i++) {
if(markers[i].id == idPlateau) {
indexPlateau = i;
scene4.add(plateau);
}
else if (markers[i].id == idRaquette1) {
indexRaquette1 = i;
plateau.add(raquette1);
}
else {
indexRaquette2 = i;
plateau.add(raquette2);
}
}
for(j = 0; j < markersLength; j++) {
if(j == indexPlateau) {
corners = markers[j].corners;
corners = centerLandMark(corners);
pose = posit.pose(corners);
//On a les matrices de transformations du repère plateau
//exprimées dans le repère caméra.
//rotationMatrix : 3x3
//translationMatrix 1x3
var rotationMatrix = pose.bestRotation;
var translationMatrix = pose.bestTranslation;
//On place lobjet dans le repère caméra.
updateObject(plateau, rotationMatrix, translationMatrix);
//On calcule les matrices de transformation du repère caméra
//exprimées dans le repère plateau
rotationInvMatrix = rotationMatrix.transpose();
//multiplcation matrice 3x3 par matrice 3x1 -> matrice 3x1
translationInvMatrix = multiplyMatrix(moins(rotationInvMatrix), transposeVector(translationMatrix));
var transformationMatrix = new THREE.Matrix4(
rotationInvMatrix[0][0], rotationInvMatrix[0][1], rotationInvMatrix[0][2], translationInvMatrix[0],
rotationInvMatrix[1][0], rotationInvMatrix[1][1], rotationInvMatrix[1][2], translationInvMatrix[1],
rotationInvMatrix[2][0], rotationInvMatrix[2][1], rotationInvMatrix[2][2], translationInvMatrix[2],
0,0,0,1);
for (k = 0; k < markersLength; k++) {
if(k != indexPlateau) {
corners = markers[k].corners;
corners = centerLandMark(corners);
pose = posit.pose(corners);
//On calcule les matrices de transformation du repère raquette
//exprimées dans le repère caméra
var raqRotationMatrix = pose.bestRotation;
var raqTranslationMatrix = new THREE.Vector3(
pose.bestTranslation[0],
pose.bestTranslation[1],
pose.bestTranslation[2]);
var raqTransformationMatrix = transformationMatrix.applyToVector3Array(raqTranslationMatrix);
if(k == indexRaquette1) {
raquette1.position.z = paddleHeight/2;
raquette1.position.x = -raqTransformationMatrix.y;
raquette1.position.y = -(fieldHeight/2) + paddleHeight/2 + decalage_raquette;
raquette1.scale.x = raq1SizeX;
}
else {
raquette2.position.z = paddleHeight/2;
raquette2.position.x = -raqTransformationMatrix.y;
raquette2.position.y = (fieldHeight/2) - paddleHeight/2 - decalage_raquette;
raquette2.scale.x = raq2SizeX;
}
}
}
}
}
}
else {
raquette1.visible = false;
raquette2.visible = false;
plateau.visible = false;
}
texture.children[0].material.map.needsUpdate = true;
};
function updateObject(object, rotation, translation){
object.rotation.x = -Math.asin(-rotation[1][2]);
object.rotation.y = -Math.atan2(rotation[0][2], rotation[2][2]);
object.rotation.z = Math.atan2(rotation[1][0], rotation[1][1]);
object.position.x = translation[0];
object.position.y = translation[1];
object.position.z = -translation[2];
deep = -translation[2];
};
function transposeVector(v) {
var newVect = [1];
newVect[0] = [];
newVect[0][0] = v[0];
newVect[0][1] = v[1];
newVect[0][2] = v[2];
return newVect;
}
function transposeColumnVectorToLineVector(v) {
var newVect = [];
newVect[0] = v[0][0];
newVect[1] = v[0][1];
newVect[2] = v[0][2];
return newVect;
}
function addVector(v1, v2) {
var newVect = [];
newVect[0] = v1[0] + v2[0];
newVect[1] = v1[1] + v2[1];
newVect[2] = v1[2] + v2[2];
return newVect;
}
function multiplyMatrix(m1, m2) {
var result = [];
var lignes1 = m1.length;
var colonnes1 = m1[0].length;
var lignes2 = m2.length;
var colonnes2 = m2[0].length;
for(var j = 0; j < lignes2; j++) {
result[j] = [];
for(var k = 0; k < colonnes1; k++) {
var sum = 0;
for(var i = 0; i < lignes1; i++) {
sum += m1[i][k] * m2[j][i];
}
result[j].push(sum);
}
}
return result;
}
function moins(m) {
var result = [];
for (var j = 0; j < m.length; j++) {
result[j] = [];
for (var i = 0; i < m[0].length; i++) {
result[j][i] = -m[j][i];
}
}
return result;
}
function centerLandMark (corners) {
var result = [];
//On centre le repère image et le repère caméra
for (j = 0; j < corners.length; ++ j){
var corner = corners[j];
corner.x = corner.x - (canvas.width / 2);
corner.y = (canvas.height / 2) - corner.y;
result[j] = corner;
}
return result;
}
window.onload = onLoad;
</script>
</head>
<body style="text-align: center; font-family: monospace;">
<video id="video" width=640 height=480 autoplay="true" style="display:none;"></video>
<div style="margin: 10px;"><strong>PONG</strong></div>
<div style="width: 100%;">
<div style="width: 1300px; margin-left:auto; margin-right:auto;">
<canvas id="canvas" style="width: 640px; height: 480px; float: left; border: solid 1px black;"></canvas>
<div id="container" style="width: 640px; height: 480px; float: left; border: solid 1px black; background: green;"></div>
<div style="clear: both;"></div>
</div>
</div>
</div>
<div id="scoreboard">
<h1>SCORE</h1>
<h1><font color="red">Joueur 1</font> VS <font color="blue">Joueur 2</font></h1>
<h1 id="scores"><font id="score1" color="red">0</font> - <font id="score2" color="blue">0</font></h1>
<h2 id="winnerBoard">Le premier joueur arrivé à 5 points gagne!</h2>
</div>
</body>
</html>