
๐ webcam_interaction.html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Webcam Interactive Three.js Art</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="info">์น์บ ๊ถํ์ ํ์ฉํด์ฃผ์ธ์...</div>
<canvas id="videoCanvas"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="sphere_fog_with_friends.js"></script>
</body>
</html>
๐ style.css
body {
margin: 0;
overflow: hidden;
background: #000;
}
canvas {
display: block;
}
#videoCanvas {
display: none;
}
#info {
position: absolute;
top: 20px;
left: 20px;
color: white;
font-family: monospace;
font-size: 14px;
background: rgba(0, 0, 0, 0.3);
padding: 10px;
border-radius: 5px;
z-index: 10;
}
๐ basic_sphere.js
let scene, camera, renderer, sphere;
let video, videoCanvas, videoContext;
let brightnessValue = 0;
async function initWebcam() {
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: { width: 320, height: 240 },
});
video = document.createElement("video");
video.srcObject = stream;
video.play();
videoCanvas = document.getElementById("videoCanvas");
videoCanvas.width = 320;
videoCanvas.height = 240;
videoContext = videoCanvas.getContext("2d");
document.getElementById("info").innerHTML =
'๋ฐ๊ธฐ: <span id="brightness">0</span>%<br>๊ตฌ ํฌ๊ธฐ: <span id="size">0</span>';
return true;
} catch (err) {
document.getElementById("info").innerHTML =
"์น์บ ์ ๊ทผ ์คํจ: " + err.message;
return false;
}
}
function analyzeVideoFrame() {
if (!video || video.readyState !== video.HAVE_ENOUGH_DATA) return 0;
videoContext.drawImage(video, 0, 0, 320, 240);
const imageData = videoContext.getImageData(0, 0, 320, 240);
const data = imageData.data;
let totalBrightness = 0;
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
totalBrightness += (r + g + b) / 3;
}
const avgBrightness = totalBrightness / (320 * 240);
return avgBrightness / 255;
}
function initThree() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = 5;
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.SphereGeometry(1, 32, 32);
const material = new THREE.MeshStandardMaterial({
color: 0x00ffff,
wireframe: false,
});
sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
}
function animate() {
requestAnimationFrame(animate);
brightnessValue = analyzeVideoFrame();
const brightElem = document.getElementById("brightness");
const sizeElem = document.getElementById("size");
if (brightElem) {
brightElem.textContent = (brightnessValue * 100).toFixed(1);
}
if (sphere) {
const targetScale = brightnessValue * 5;
sphere.scale.set(targetScale, targetScale, targetScale);
if (sizeElem) {
sizeElem.textContent = targetScale.toFixed(2);
}
sphere.rotation.x += 0.01;
sphere.rotation.y += 0.01;
}
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
async function init() {
const webcamOk = await initWebcam();
if (webcamOk) {
initThree();
animate();
}
}
init();
๐ sphere_fog_with_friends.js
let scene, camera, renderer, sphere;
let video, videoCanvas, videoContext;
let brightnessValue = 0;
let currentScale = 0;
const smoothingFactor = 0.15;
let smallPrimitives = [];
async function initWebcam() {
try {
const stream = await navigator.mediaDevices.getUserMedia({
video: { width: 320, height: 240 },
});
video = document.createElement("video");
video.srcObject = stream;
video.play();
videoCanvas = document.getElementById("videoCanvas");
videoCanvas.width = 320;
videoCanvas.height = 240;
videoContext = videoCanvas.getContext("2d");
document.getElementById("info").innerHTML =
'๋ฐ๊ธฐ: <span id="brightness">0</span>%<br>๊ตฌ ํฌ๊ธฐ: <span id="size">0</span>';
return true;
} catch (err) {
document.getElementById("info").innerHTML =
"์น์บ ์ ๊ทผ ์คํจ: " + err.message;
return false;
}
}
function analyzeVideoFrame() {
if (!video || video.readyState !== video.HAVE_ENOUGH_DATA) return 0;
videoContext.drawImage(video, 0, 0, 320, 240);
const imageData = videoContext.getImageData(0, 0, 320, 240);
const data = imageData.data;
let totalBrightness = 0;
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
totalBrightness += (r + g + b) / 3;
}
const avgBrightness = totalBrightness / (320 * 240);
return avgBrightness / 255;
}
function initThree() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e8e8);
scene.fog = new THREE.Fog(0xe8e8e8, 1, 20);
camera = new THREE.PerspectiveCamera(
55,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = 8;
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.SphereGeometry(1, 64, 64);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.5,
metalness: 0.1,
});
sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
const primitiveGeometries = [
new THREE.BoxGeometry(0.2, 0.2, 0.2),
new THREE.TetrahedronGeometry(0.2),
new THREE.OctahedronGeometry(0.2),
new THREE.IcosahedronGeometry(0.2),
];
const primitiveMaterial = new THREE.MeshStandardMaterial({
color: 0x6482ad,
roughness: 0.5,
metalness: 0.1,
});
for (let i = 0; i < 30; i++) {
const geom =
primitiveGeometries[
Math.floor(Math.random() * primitiveGeometries.length)
].clone();
const primitive = new THREE.Mesh(geom, primitiveMaterial);
const distance = 4 + Math.random() * 2;
const theta = Math.random() * Math.PI * 2;
const phi = Math.random() * Math.PI;
primitive.position.x = distance * Math.sin(phi) * Math.cos(theta);
primitive.position.y = distance * Math.sin(phi) * Math.sin(theta);
primitive.position.z = distance * Math.cos(phi);
primitive.rotation.x = Math.random() * Math.PI;
primitive.rotation.y = Math.random() * Math.PI;
primitive.rotation.z = Math.random() * Math.PI;
primitive.userData.rotationSpeed = {
x: (Math.random() - 0.5) * 0.02,
y: (Math.random() - 0.5) * 0.02,
z: (Math.random() - 0.5) * 0.02,
};
scene.add(primitive);
smallPrimitives.push(primitive);
}
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(5, 5, 5);
scene.add(directionalLight);
const directionalLight2 = new THREE.DirectionalLight(0xffffff, 0.4);
directionalLight2.position.set(-5, -5, 5);
scene.add(directionalLight2);
}
function animate() {
requestAnimationFrame(animate);
brightnessValue = analyzeVideoFrame();
const brightElem = document.getElementById("brightness");
const sizeElem = document.getElementById("size");
if (brightElem) {
brightElem.textContent = (brightnessValue * 100).toFixed(1);
}
if (sphere) {
const targetScale = brightnessValue * 5;
currentScale += (targetScale - currentScale) * smoothingFactor;
sphere.scale.set(currentScale, currentScale, currentScale);
if (sizeElem) {
sizeElem.textContent = currentScale.toFixed(2);
}
sphere.rotation.x += 0.01;
sphere.rotation.y += 0.01;
}
smallPrimitives.forEach((primitive) => {
primitive.rotation.x += primitive.userData.rotationSpeed.x;
primitive.rotation.y += primitive.userData.rotationSpeed.y;
primitive.rotation.z += primitive.userData.rotationSpeed.z;
});
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
async function init() {
const webcamOk = await initWebcam();
if (webcamOk) {
initThree();
animate();
}
}
init();