-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
239 lines (215 loc) · 7.37 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Webcam Pixelation</title>
<style>
body, html {
margin: 0;
}
canvas {
display: block;
}
.hide {
display: none;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<video class="hide"></video>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script>
// Function to scroll to the bottom of the page
function scrollToBottom() {
window.scrollTo(0, document.body.scrollHeight);
}
// Function to request webcam access
async function requestWebcamAccess() {
try {
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
// Handle the stream (e.g., attach it to a video element)
console.log('Webcam access granted');
} catch (error) {
console.error('Error accessing webcam:', error);
}
}
// When the window loads, scroll to bottom and request webcam access
window.onload = function() {
scrollToBottom();
requestWebcamAccess();
};
</script>
<script>
// 2d and web cam stuff
let canvas;
let ctx;
let w;
let h;
let size = 8;
let video;
// three.js stuff
let colors;
let scene;
let camera;
let renderer;
let cubes;
let nrOfCubesX;
let nrOfCubesY;
function setup() {
setupColors();
setupScene();
setupRenderer();
setupEventListeners();
setupCanvas();
setupWebCamera().then(() => {
// We need to call these after
// the web cam is setup so we
// know the width and height
// of the video feed
reset();
setupCamera();
setupCubes();
setupLights();
draw();
});
}
function setupCanvas() {
canvas = document.querySelector("#canvas");
ctx = canvas.getContext("2d");
}
function reset() {
w = canvas.width = video.videoWidth;
h = canvas.height = video.videoHeight;
nrOfCubesX = w / size;
nrOfCubesY = h / size;
}
function setupWebCamera() {
return new Promise((resolve, reject) => {
let constraints = { audio: false, video: true };
navigator.mediaDevices.getUserMedia(constraints)
.then(mediaStream => {
video = document.querySelector("video");
video.srcObject = mediaStream;
video.onloadedmetadata = () => {
video.play();
resolve();
};
})
.catch(err => {
console.log(err.name + ": " + err.message);
reject(err);
});
});
}
// Trying to be clever and keep all grayscale colors
// in a lookup table. Will we avoid some GCs?
function setupColors() {
colors = new Map();
for(let i = 0; i < 256; i++) {
let c = new THREE.Color(`rgb(${i}, ${i}, ${i})`);
colors.set(i, c);
}
}
function setupScene() {
scene = new THREE.Scene();
}
function setupRenderer() {
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(
window.innerWidth,
window.innerHeight);
document.body.appendChild(renderer.domElement);
}
function setupCamera() {
let res = window.innerWidth / window.innerHeight;
let z = 1/size*500;
camera = new THREE.PerspectiveCamera(
75,
res,
0.1,
1000);
camera.position.set(nrOfCubesX/2, nrOfCubesY/2, z);
let controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(nrOfCubesX/2, nrOfCubesY/2, 0);
controls.update();
}
function setupCubes() {
let geometry = new THREE.BoxGeometry(1, 1, 1);
cubes = [];
let color = new THREE.Color(`rgb(128, 128, 128)`);
for(let x = 0; x < nrOfCubesX; x++) {
for(let y = 0; y < nrOfCubesY; y++) {
let material = new THREE.MeshStandardMaterial({
roughness: 0.5,
color: color,
});
let cube = new THREE.Mesh(geometry, material);
cube.position.set(x, y, 0);
scene.add(cube);
cubes.push(cube);
}
}
}
function setupLights() {
let ambientLight = new THREE.AmbientLight(0x777777);
scene.add(ambientLight);
let spotLight = new THREE.SpotLight(0xbbbbbb);
spotLight.position.set(0, nrOfCubesY, 100);
spotLight.castShadow = true;
scene.add(spotLight);
}
function draw() {
requestAnimationFrame(draw);
ctx.drawImage(video, 0, 0, w, h);
pixelate();
renderer.render(scene, camera);
}
function pixelate() {
let imageData = ctx.getImageData(0, 0, w, h);
let pixels = imageData.data;
cubes.forEach(cube => {
let x = cube.position.x;
let y = cube.position.y;
let col = getAverage(pixels, w-x*size, h-y*size);
let c = Math.round(col);
cube.material.color = colors.get(c);
let z = col/10 + 0.01;
cube.scale.z = z;
cube.position.z = z / 2;
});
}
function getAverage(pixels, x0, y0) {
let r = 0;
let g = 0;
let b = 0;
for(let x = x0; x < x0 + size; x += 1) {
for(let y = y0; y < y0 + size; y += 1) {
let index = (x + w*y) * 4;
r += pixels[index];
g += pixels[index + 1];
b += pixels[index + 2];
}
}
let val = (0.2126*r + 0.7152*g + 0.0722*b)/(size*size)
return isNaN(val) ? 1 : val;
}
function setupEventListeners() {
window.addEventListener("resize", onWindowResize);
}
function onWindowResize() {
camera.aspect =
window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(
window.innerWidth,
window.innerHeight);
}
setup();
</script>
</body>
</html>