/
redsnow.html
388 lines (342 loc) · 9.88 KB
/
redsnow.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
<!--
original
必要なのはスマホと1,000円のGoogle Cardboardだけ! WebGLではじめるお手軽VR入門 - ICS MEDIA
https://ics.media/entry/7712
The MIT License (MIT) Copyright (c) 2016 ICS INC.
-->
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>redrain - WebMegane</title>
<meta property="og:image" content="redrain.png">
<link rel="apple-touch-icon" href="redrain.png"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r71/three.min.js"></script>
<script src="js/StereoEffect.js"></script>
<script src="js/DeviceOrientationControls.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.17.0/TweenMax.min.js"></script>
<script>
//
(function() {
// 一辺の長さ
var STEP = 100;
var GRID_STEP = STEP * 4;
var BOX_STEP = 30;
var N_BOX = 2400;
// シーン
var scene;
// カメラ
var camera;
// レンダラー
var renderer;
// 描画領域
var container;
// VR表示へ変換
var effect;
// DOM
var element;
// 操作
var controls;
// 時間
var clock;
// 立方体の輪郭線を格納する配列
var edgesPool;
/**
* Initialize
*/
function init() {
// to user motion sensor, orientation sensor for iOS13
document.body.onclick = function() {
if (DeviceMotionEvent && DeviceMotionEvent.requestPermission && typeof DeviceMotionEvent.requestPermission === 'function') {
DeviceMotionEvent.requestPermission()
}
if (DeviceOrientationEvent && DeviceOrientationEvent.requestPermission && typeof DeviceOrientationEvent.requestPermission === 'function') {
DeviceOrientationEvent.requestPermission();
}
}
// シーン
scene = new THREE.Scene();
// カメラ
camera = new THREE.PerspectiveCamera(90, window.innerWidth / window.innerHeight, 1, 3000);
camera.position.set(0, 700, 0);
camera.far *= 10;
scene.add(camera);
// レンダラー
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
//renderer.setClearColor(0x0);
renderer.setClearColor(0x000000, 0); // the default
renderer.setSize(window.innerWidth, window.innerHeight);
// ピクセルレートを設定
if(window.devicePixelRatio) {
renderer.setPixelRatio(window.devicePixelRatio);
}
// DOM
element = renderer.domElement;
// 描画領域
container = document.getElementById("container");
container.appendChild(element);
// VR表示へ変換
effect = new THREE.StereoEffect(renderer);
effect.focalLength = 50;
// PCで閲覧時にマウスドラッグで操作
controls = new THREE.OrbitControls(camera, element);
controls.rotateUp(Math.PI / 4);
controls.target.set(
camera.position.x + 0.15,
camera.position.y,
camera.position.z
);
controls.noZoom = true;
controls.noPan = true;
clock = new THREE.Clock();
// スマートフォンの場合はジャイロセンサーでの操作へ変更
window.addEventListener("deviceorientation", setOrientationControls, true);
// windowのリサイズ処理
window.addEventListener("resize", resize, false);
resize();
// 立方体を生成
createBox();
// 床を生成
createBrid();
// 繰り返し処理を開始
loop();
}
/**
* ジャイロセンサーでの操作へ変更します。
*/
function setOrientationControls(e) {
if (!e.alpha) {
return;
}
controls = new THREE.DeviceOrientationControls(camera, true);
controls.connect();
controls.update();
element.addEventListener("click", fullscreen, false);
window.removeEventListener("deviceorientation", setOrientationControls, true);
}
/**
* 立方体の生成します。
*/
function createBox() {
edgesPool = [];
var geometry = new THREE.BoxGeometry(BOX_STEP, BOX_STEP, BOX_STEP, 1, 1, 1);
var material = new THREE.MeshBasicMaterial({ color: 0xFF0000 });
for(var i = 0; i < N_BOX; i++) {
var mesh = new THREE.Mesh(geometry, material);
// 立方体を作る
var egh = new THREE.EdgesHelper(mesh, 0xFF0000);
egh.material.linewidth = 1;
egh.updateMatrix();
scene.add(egh);
edgesPool.push(egh);
startDrop(egh);
}
}
/**
* 落下アニメーションを停止します。
*/
function startDrop(egh) {
// ランダムに立方体を配置
egh.position.x = STEP * Math.round(3 * 6000 * (Math.random() - 0.5) / STEP) + STEP / 2;
egh.position.y = 5000 * 4;
egh.position.z = STEP * Math.round(3 * 6000 * (Math.random() - 0.5) / STEP) + STEP / 2;
egh.updateMatrix();
// 秒数
var sec = 3 * Math.random() + 3;
TweenMax.to(egh.position, sec, {
y: 20, //STEP / 2 + 10,
//ease: Bounce.easeOut,
onComplete: endDrop,
onCompleteParams: [ egh ]
});
}
/**
* 落下アニメーションが終了しました。
*/
function endDrop(egh) {
// 再度アニメーション
setTimeout(function() {
startDrop(egh)
}, 1000);
}
/**
* 地面を生成します。
*/
function createBrid() {
var grid = new THREE.GridHelper(10000, GRID_STEP);
var gcolor = 0xFF0000; // 0x444444
grid.setColors(gcolor, gcolor);
scene.add(grid);
}
/**
* windowがリサイズしました。
*/
function resize() {
// var width = container.offsetWidth;
// var height = container.offsetHeight;
var width = window.innerWidth;
var height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
effect.setSize(width, height);
}
/**
* 繰り返し実行される処理です。
*/
function loop() {
requestAnimationFrame(loop);
update(clock.getDelta());
render();
}
/**
* 更新します。
*/
function update(dt) {
if (edgesPool) {
for (var i = 0; i < edgesPool.length; i++) {
edgesPool[i].updateMatrix();
}
}
camera.updateProjectionMatrix();
controls.update(dt);
}
/**
* レンダリングします。
*/
function render() {
effect.render(scene, camera);
}
/**
* フルスクリーン表示へ切り替えます。
*/
function fullscreen() {
if (container.requestFullscreen) {
container.requestFullscreen();
} else if (container.msRequestFullscreen) {
container.msRequestFullscreen();
} else if (container.mozRequestFullScreen) {
container.mozRequestFullScreen();
} else if (container.webkitRequestFullscreen) {
container.webkitRequestFullscreen();
}
}
window.addEventListener("DOMContentLoaded", init, false);
})();
window.onload = function() {
const USE_CAMERA_FRONT = false;
// var videoop = USE_CAMERA_FRONT ? true : { facingMode : { exact : "environment" } };
// var videoop = USE_CAMERA_FRONT ? true : { facingMode : { ideal : "environment" } };
var videoop = USE_CAMERA_FRONT ? true : {
facingMode : { ideal : "environment" },
width: { min: 640, ideal: 1920 },
height: { min: 480, ideal: 1080 }
// width: { min: 640, ideal: 1280, max: 1920 },
// height: { min: 480, ideal: 720, max: 1080 }
};
const medias = { audio: false, video: videoop };
// dump(navigator.mediaDevices.getSupportedConstraints());
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia
if (navigator.getUserMedia) {
navigator.getUserMedia(medias, function(stream) {
video.srcObject = stream;
}, function(err) {
alert(err);
}
)
} else {
navigator.mediaDevices.getUserMedia(medias).then(function(stream) {
video.srcObject = stream;
}).catch(function(err) {
alert(err);
})
}
var g = canvas.getContext("2d");
var draw = function() {
const cw = window.innerWidth * window.devicePixelRatio;
const ch = window.innerHeight * window.devicePixelRatio;
canvas.width = cw;
canvas.height = ch;
const vw = video.videoWidth;
const vh = video.videoHeight;
const cw2 = cw / 2;
const caspect = ch / cw2;
const vaspect = vh / vw;
const fitwidth = caspect < vaspect; // auto fit screen
// const fitwidth = true; // force fit width
// const fitwidth = false; // force fit height
if (fitwidth) {
const vh2 = vw * caspect;
if (vh2 > vh) {
const cy = (vh2 - vh) / 2 / (vw / cw2);
const ch2 = cw2 * vaspect;
g.drawImage(video, 0, 0, vw, vh, 0, cy, cw2, ch2);
g.drawImage(video, 0, 0, vw, vh, cw2, cy, cw2, ch2);
} else {
const vy = (vh - vh2) / 2;
g.drawImage(video, 0, vy, vw, vh2, 0, 0, cw2, ch);
g.drawImage(video, 0, vy, vw, vh2, cw2, 0, cw2, ch);
}
} else {
const vw2 = vh / caspect;
if (vw2 > vw) {
const cx = (vw2 - vw) / 2 / (vh / ch);
const cw3 = ch / vaspect;
console.log(cx);
g.drawImage(video, 0, 0, vw, vh, cx, 0, cw3, ch);
g.drawImage(video, 0, 0, vw, vh, cw2 + cx, 0, cw3, ch);
} else {
const vx = (vw - vw2) / 2;
g.drawImage(video, vx, 0, vw2, vh, 0, 0, cw2, ch);
g.drawImage(video, vx, 0, vw2, vh, cw2, 0, cw2, ch);
}
}
requestAnimationFrame(draw);
};
draw();
var mode = 0;
var filters = [ "", "grayscale(100%)", "sepia(100%)", "contrast(200%)", "hue-rotate(90deg)", "invert(100%)" ];
canvas.onmousedown = function() {
mode = (mode + 1) % filters.length;
canvas.style.filter = filters[mode];
};
};
</script>
<style>
body {
margin: 0;
overflow: hidden;
}
#video {
position: absolute;
display: block;
width: 100%;
transform: scale(0.01);
z-index: -1;
}
#canvas {
z-index: 0;
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#container {
z-index: 4;
position: absolute;
top: 0;
left: 0;
x-opacity: 0.5;
}
</style>
</head>
<body>
<div id="container"></div>
<canvas id="canvas"></canvas>
<video id="video" autoplay playsinline></video>
</body>
</html>