-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch_multi.js
More file actions
406 lines (359 loc) · 13.7 KB
/
sketch_multi.js
File metadata and controls
406 lines (359 loc) · 13.7 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
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
let faceapi;
let faceapi2;
let video;
let detections;
let detections2;
let capture;
let theirVideo;
let detections_list_me = [];
let fukkin_ct_me = 0;
let detections_list_you = [];
let fukkin_ct_you = 0;
var conn;
let score_end;
let start_check = false;
// by default all options are set to true
const detection_options = {
withLandmarks: true,
withDescriptors: false,
};
function setup() {
canvas = createCanvas(1280, 960);
canvas.parent("p5-canvas-wrapper");
// load up your video
capture = createCapture({video: {width: 640, height: 960}, audio: false});
//video.size(width, height);
// video.hide(); // Hide the video element, and just show the canvas
capture.hide(); // ビデオを消した
faceapi = ml5.faceApi(capture, detection_options, modelReady);
textAlign(RIGHT);
// skywayのインスタンスを作成
let peer = new Peer({
key: "a3dadfd7-94d5-466e-92fb-84bba5f87981",
});
// skywayでドメインを許可していれば実行される
peer.on("open", () => {
console.log("open! id=" + peer.id);
createP("Your id: " + peer.id);
});
// id入力タグの生成
let idInput = createInput("");
// 送信ボタンの生成
createButton("Call").mousePressed(() => {
// ボタンが押されたら
fukkin_ct_me = 0;
fukkin_ct_you = 0;
const callId = idInput.value(); //id入力欄の値を取得
console.log("call! id=" + peer.id);
const call = peer.call(callId, capture.elt.srcObject); //id先を呼び出し
addVideo(call);
});
// // 相手から呼び出された実行される
peer.on("call", (call) => {
fukkin_ct_me = 0;
fukkin_ct_you = 0;
console.log("be called!");
call.answer(capture.elt.srcObject); //呼び出し相手に対して返す
addVideo(call);
});
// 相手からデータ通信の接続要求イベントが来た場合、このconnectionイベントが呼ばれる
// - 渡されるconnectionオブジェクトを操作することで、データ通信が可能
peer.on("connection", function (connection) {
// データ通信用に connectionオブジェクトを保存しておく
conn = connection;
// 接続が完了した場合のイベントの設定
// conn.on("open", function() {
// // 相手のIDを表示する
// // - 相手のIDはconnectionオブジェクトのidプロパティに存在する
// $("#peer-id").text(conn.id);
// });
// メッセージ受信イベントの設定
conn.on("data", onRecvMessage);
});
function onRecvMessage(data) {
// 画面に受信したメッセージを表示
console.log("受け取り回数" + data);
//$("#messages").append($("<p>").text(conn.id + ": " + data).css("font-weight", "bold"));
}
// 相手の映像を追加処理
function addVideo(call) {
call.on("stream", (theirStream) => {
console.log("stream!");
//相手のビデオを作成
theirVideo = createVideo();
theirVideo.elt.autoplay = true;
theirVideo.elt.srcObject = theirStream;
theirVideo.hide(); //キャンバスで描くので非表示
//相手側のビデオ映像に対してfaceAPIをする
faceapi2 = ml5.faceApi(theirVideo, detection_options, modelReady);
});
}
}
function modelReady() {
console.log("ready!");
console.log(faceapi);
console.log(faceapi2);
faceapi.detect(gotResults);
if (faceapi2) {
faceapi2.detect(gotResults2);
}
}
// 自分の映像用
function gotResults(err, result) {
if (err) {
console.log(err);
return;
}
detect_fukkin_me(result);
// 自分の映像の情報を取得
detections = result;
faceapi.detect(gotResults);
}
// 相手の動画用
function gotResults2(err, result) {
if (err) {
console.log(err);
return;
}
detect_fukkin_you(result);
// 相手の映像の情報を取得する
detections2 = result;
faceapi2.detect(gotResults2);
}
function draw() {
background(255);
// 自分の映像を表示
if (capture) image(capture, 0, 0, 640, 960);
// 相手の映像をx軸に640pxずらして表示する
if (theirVideo) image(theirVideo, 640, 0, 640, 960);
// 自分の映像に加工を入れる処理
if (detections) {
if (detections.length > 0) {
// 自分の映像の場合は引数で1を渡すことにしてる
drawLandmarks(detections, 1);
}
}
// 自分の映像に加工を入れる処理
if (detections2) {
if (detections2.length > 0) {
// 相手の映像の場合は引数で2を渡すことにしてる
drawLandmarks(detections2, 2);
}
}
}
// マスクとバーチャルディスタンスのアラート画像の読み込み
// let img;
// let img2;
// function preload() {
// img = loadImage('mask.png');
// img2 = loadImage('alert.png');
// }
// 口の位置や目隠しをする処理
// 引数はそれぞれの映像から得られた情報と自分(1)or相手(2)という情報
function drawLandmarks(detections, part) {
noFill();
stroke(255, 0, 0);
strokeWeight(2);
let mouthx = 0;
let mouthy = 0;
let lefteye1x, lefteye1y, righteye1x, righteye1y;
let lefteye2x, lefteye2y, righteye2x, righteye2y;
for (let i = 0; i < detections.length; i++) {
const mouth = detections[i].parts.mouth;
// // 口の重心をとるためにトータルを取得する
// mouthx = mouthx + detections[i].parts.mouth[0].x;
// mouthy = mouthy + detections[i].parts.mouth[0].y;
// ///////// とりあえず目を隠す実装をする。不要なら外す /////////
// if (part == 1) { // 1なので自分の映像のとき。
// // 左目
// lefteye1x = detections[i].parts.leftEye[0].x - 50;
// lefteye1y = detections[i].parts.leftEye[0].y;
// // 右目
// righteye1x = detections[i].parts.rightEye[0].x + 50;
// righteye1y = detections[i].parts.rightEye[0].y;
// }
// //相手がいた時用
// ///////// とりあえず目を隠す実装をする。不要なら外す /////////
// if (part == 2) { // 2なので相手の映像のとき
// lefteye2x = detections[i].parts.leftEye[0].x - 50 + 640;
// lefteye2y = detections[i].parts.leftEye[0].y;
// // 右目
// righteye2x = detections[i].parts.rightEye[0].x + 50 + 640;
// righteye2y = detections[i].parts.rightEye[0].y;
// }
// // 口の位置に合わせてマスクを表示する
// if (part == 1) image(img, detections[i].parts.mouth[0].x - 60, detections[i].parts.mouth[0].y - 60, 200, 170);
// if (part == 2) image(img, detections[i].parts.mouth[0].x + 580, detections[i].parts.mouth[0].y - 60, 200, 170);
// // 口の輪郭をかく
// if (part == 1) drawPart(mouth, true);
// if (part == 2) drawPart2(mouth, true);
}
// それぞれの重心
const mouth_px = mouthx / detections.length - 50;
////////// 目を隠す処理 不要だったら消す//////////
// eyeline(lefteye1x, lefteye1y, righteye1x, righteye1y);
// if (theirVideo) eyeline(lefteye2x, lefteye2y, righteye2x, righteye2y);
// 画面上で相手に近づき過ぎた時に警告する
//// 今は自分の画面で右端に240px(640-400)より近づいた時に警告を出す。必要に応じて数字は変える
// if (mouth_px > 400) {
// image(img2, 20, 20, 600, 100);
// }
}
// 口の輪郭をかく処理(自分用)
function drawPart(feature, closed) {
beginShape();
for (let i = 0; i < feature.length; i++) {
const x = feature[i]._x;
const y = feature[i]._y;
vertex(x, y);
}
if (closed === true) {
endShape(CLOSE);
} else {
endShape();
}
}
// 口の輪郭をかく処理(相手用)
// function drawPart2(feature, closed) {
// beginShape();
// for (let i = 0; i < feature.length; i++) {
// const x = feature[i]._x + 640;
// const y = feature[i]._y;
// vertex(x, y);
// }
// if (closed === true) {
// endShape(CLOSE);
// } else {
// endShape();
// }
// }
// // 目線を隠すための関数
// function eyeline(x1, y1, x2, y2) {
// strokeWeight(30); //線の太さ
// stroke(0, 0, 0); //線の色 R,G,B
// line(
// x1, y1, x2, y2
// );
// }
function detect_fukkin_me(result) {
detections_list_me.push(result);
if (result.length > 0) {
// 現フレーム顔あり
let i = 10; // 顔がない時の連続フレーム数
let fukkin = detections_list_me
.slice(-i, detections_list_me.length - 1)
.every(function (val) {
return val == 0;
});
if (fukkin && start_check) {
// 腹筋あり
fukkin_ct_me = fukkin_ct_me + 1;
console.log("私" + fukkin_ct_me + "回目");
document.querySelector('#fukkin-me-ct').textContent = fukkin_ct_me;
document.hanabi_1.src = "img/hanabi_pinkBig.svg";
document.hanabi_5.src = "img/hanabi_pink.svg";
document.querySelector('#score').textContent = score_end;
document.getElementById("audioElement_hanabi").currentTime = 0;
document.getElementById("audioElement_hanabi").play();
}
} else {
// 現フレーム顔なし
}
}
function detect_fukkin_you(result) {
detections_list_you.push(result);
if (result.length > 0) {
// 現フレーム顔あり
let i = 10; // 顔がない時の連続フレーム数
let fukkin = detections_list_you
.slice(-i, detections_list_you.length - 1)
.every(function (val) {
return val == 0;
});
if (fukkin && start_check) {
// 腹筋あり
fukkin_ct_you = fukkin_ct_you + 1;
console.log("相手" + fukkin_ct_you + "回目");
document.querySelector("#fukkin-you-ct").textContent = fukkin_ct_you;
document.hanabi_2.src = "img/hanabi_orange.svg";
document.hanabi_4.src = "img/hanabi_pink.svg";
// 送信
// conn.send(fukkin_ct_you);
}
} else {
// 現フレーム顔なし
}
}
document.getElementById("start-button").onclick = function () {
document.getElementById("audioElement_start").currentTime = 0;
document.getElementById("audioElement_start").play();
start_check = true;
document.getElementById("audioElement_battle").volume = 0.05;
document.getElementById("audioElement_battle").currentTime = 0;
document.getElementById("audioElement_battle").play();
this.classList.toggle("start");
var sub_wrap = document.getElementById("sub-wrap");
var end_wrap = document.getElementById("end-wrap");
var count_wrap = document.getElementById("timer-wrap");
count_wrap.classList.add("start");
document
.getElementsByTagName("body")[0]
.getElementsByTagName("button")[1].style.display = "none";
document
.getElementsByTagName("body")[0]
.getElementsByTagName("input")[0].style.display = "none";
document.getElementById("btn-change-mode").style.display = "none";
window.setTimeout(function () {
count_wrap.classList.add("end");
sub_wrap.classList.add("start");
document.getElementById("wave-wrap").classList.add("start");
document.getElementById("orange").style.display = "none";
}, 3000);
window.setTimeout(async function () {
// 3秒待機
const sleep = (waitTime) =>
new Promise((resolve) => setTimeout(resolve, waitTime));
document.getElementById("audioElement_battle").pause();
document.getElementById("audioElement_finish").currentTime = 0;
document.getElementById("audioElement_finish").play();
sub_wrap.classList.remove("start");
end_wrap.classList.add("end");
score_end = fukkin_ct_me;
document.querySelector("#score").textContent = score_end;
start_check = false;
await sleep(3000);
if (fukkin_ct_me > fukkin_ct_you) {
document.getElementById("audioElement_win").currentTime = 0;
document.getElementById("audioElement_win").play();
document.getElementById("result_comment").textContent = "You Win!";
document.hanabi_2.src = "img/hanabi_orange.svg";
document.hanabi_1.src = "img/hanabi_orange.svg";
} else if (fukkin_ct_me === fukkin_ct_you) {
document.getElementById("result_comment").textContent = "Draw!";
} else {
document.getElementById("result_comment").textContent = "You Lose..";
document.getElementById("audioElement_lose").currentTime = 0;
document.getElementById("audioElement_lose").play();
}
}, 33000);
};
window.onload = function () {
document
.querySelector("#start-button")
.addEventListener("click", function (e) {
e.preventDefault();
var count = 3;
var id = setInterval(function () {
count--;
document.querySelector("#timer").textContent = count;
if (count <= 1) clearInterval(id);
}, 1000);
e.preventDefault();
var count2 = 33;
var id2 = setInterval(function () {
count2--;
document.querySelector("#timer2").textContent = count2;
if (count <= 1) clearInterval(id);
}, 1000);
});
};