๐Ÿงฟ 251210 - Interactive White Sphere

BamgasiJMยท2025๋…„ 12์›” 10์ผ

Three.js

๋ชฉ๋ก ๋ณด๊ธฐ
5/22
post-thumbnail

๐Ÿ“ 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;
    /* display: 1; */
}

#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; // 0~1 ์‚ฌ์ด ๊ฐ’์œผ๋กœ ์ •๊ทœํ™”
}

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();

	// UI ์—…๋ฐ์ดํŠธ
	const brightElem = document.getElementById("brightness");
	const sizeElem = document.getElementById("size");
	if (brightElem) {
		brightElem.textContent = (brightnessValue * 100).toFixed(1);
	}

	// ๋ฐ๊ธฐ์— ๋”ฐ๋ผ ๊ตฌ ํฌ๊ธฐ ๋ณ€ํ™”
	if (sphere) {
		// ๋ฐ๊ธฐ 0(๊ฒ€์€์ƒ‰) = ํฌ๊ธฐ 0, ๋ฐ๊ธฐ 1(ํฐ์ƒ‰) = ํฌ๊ธฐ 5
		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; // 0์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ๋ณ€ํ™”
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; // 0~1 ์‚ฌ์ด ๊ฐ’์œผ๋กœ ์ •๊ทœํ™”
}

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);

	// ์Šคํ”ผ์–ด ์ƒ์„ฑ - ๋ถ€๋“œ๋Ÿฌ์šด ํ‘œ๋ฉด์„ ์œ„ํ•ด MeshStandardMaterial ์‚ฌ์šฉ
	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),
		// new THREE.TorusGeometry(0.15, 0.05, 8, 16),
	];

	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);

		// xyz ์ถ•์— ๊ณจ๊ณ ๋ฃจ ๋ฐฐ์น˜
		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();

	// UI ์—…๋ฐ์ดํŠธ
	const brightElem = document.getElementById("brightness");
	const sizeElem = document.getElementById("size");
	if (brightElem) {
		brightElem.textContent = (brightnessValue * 100).toFixed(1);
	}

	// ๋ฐ๊ธฐ์— ๋”ฐ๋ผ ๊ตฌ ํฌ๊ธฐ ๋ณ€ํ™”
	if (sphere) {
		// ๋ฐ๊ธฐ 0(๊ฒ€์€์ƒ‰) = ํฌ๊ธฐ 0, ๋ฐ๊ธฐ 1(ํฐ์ƒ‰) = ํฌ๊ธฐ 5
		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();

profile
Coding Art with Blender / Three.js / p5.js / GLSL Shader / Processing / nannou

0๊ฐœ์˜ ๋Œ“๊ธ€