Spaces:
Running
Running
File size: 15,269 Bytes
96121f0 | 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 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | <!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Cube Data Visualization</title>
<style>
/*
* MODERN CSS RESET & BASE STYLES
*/
:root {
--bg-color: #eeeeee;
--text-color: #000000;
--accent-color: #3b82f6;
--ui-bg: rgba(255, 255, 255, 0.9);
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
user-select: none; /* Prevent text selection during interaction */
-webkit-user-drag: none;
}
body {
font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
background-color: var(--bg-color);
color: var(--text-color);
overflow: hidden; /* Prevent scrollbars */
width: 100vw;
height: 100vh;
}
/*
* 3D CANVAS CONTAINER
*/
#canvas-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
outline: none;
}
/*
* UI OVERLAY LAYER
*/
#ui-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 2;
pointer-events: none; /* Let clicks pass through to canvas */
display: flex;
flex-direction: column;
justify-content: space-between;
}
/*
* HEADER
*/
header {
padding: 20px;
display: flex;
justify-content: space-between;
align-items: center;
pointer-events: auto;
background: linear-gradient(to bottom, rgba(238,238,238,0.8), transparent);
}
h1 {
font-size: 1.2rem;
font-weight: 600;
letter-spacing: -0.5px;
opacity: 0.8;
}
.built-with {
font-size: 0.8rem;
color: #555;
text-decoration: none;
transition: color 0.3s ease;
}
.built-with:hover {
color: var(--accent-color);
font-weight: bold;
}
/*
* INFO PANEL (Bottom Left)
*/
.info-panel {
padding: 20px;
pointer-events: auto;
background: var(--ui-bg);
border-radius: 12px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
max-width: 300px;
backdrop-filter: blur(10px);
transform: translateY(20px);
opacity: 0;
animation: slideUp 0.8s ease-out forwards 0.5s;
}
@keyframes slideUp {
to {
transform: translateY(0);
opacity: 1;
}
}
.info-panel h2 {
font-size: 1rem;
margin-bottom: 8px;
}
.info-panel p {
font-size: 0.85rem;
line-height: 1.5;
color: #444;
}
/*
* LOADING OVERLAY
*/
#loader {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: var(--bg-color);
z-index: 10;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
transition: opacity 0.5s ease;
}
.spinner {
width: 40px;
height: 40px;
border: 4px solid #ccc;
border-top: 4px solid var(--text-color);
border-radius: 50%;
animation: spin 1s linear infinite;
margin-bottom: 15px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/*
* ANNOTATION LABELS (CSS 2D Renderer)
*/
.label {
color: #000;
font-family: sans-serif;
font-size: 12px;
font-weight: 500;
background: rgba(255, 255, 255, 0.8);
padding: 2px 6px;
border-radius: 4px;
pointer-events: none; /* Let clicks pass through */
white-space: nowrap;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
opacity: 0.7;
transition: opacity 0.2s;
}
.label.hidden {
opacity: 0;
}
</style>
<!-- Import Three.js and Addons via ES Modules -->
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.160.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.160.0/examples/jsm/"
}
}
</script>
</head>
<body>
<!-- Loading Screen -->
<div id="loader">
<div class="spinner"></div>
<p>๋ฐ์ดํฐ๋ฅผ ๋ก๋ฉ ์ค์
๋๋ค...</p>
</div>
<!-- 3D Scene Container -->
<div id="canvas-container"></div>
<!-- UI Overlay -->
<div id="ui-layer">
<header>
<h1>3D Cube Data Points</h1>
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" class="built-with">Built with anycoder</a>
</header>
<div class="info-panel">
<h2>์ธํฐ๋์
๊ฐ์ด๋</h2>
<p>โข <strong>ํ์ :</strong> ๋ง์ฐ์ค ๋๋๊ทธ</p>
<p>โข <strong>์ค:</strong> ๋ง์ฐ์ค ํ </p>
<p>โข <strong>์ค์ธ:</strong> ํฌ์ธํธ ํด๋ฆญ</p>
</div>
</div>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { CSS2DRenderer, CSS2DObject } from 'three/addons/renderers/CSS2DRenderer.js';
// --- Configuration ---
const CONFIG = {
pointCount: 100,
pointColor: 0x000000,
lineColor: 0x000000,
bgColor: 0xeeeeee,
cubeSize: 10,
cameraZ: 25
};
// --- Data Generation (100 Unique Animal Names) ---
const animalNames = [
"Tiger", "Lion", "Bear", "Wolf", "Fox", "Rabbit", "Deer", "Horse",
"Eagle", "Shark", "Whale", "Dolphin", "Panda", "Koala", "Kangaroo",
"Penguin", "Owl", "Falcon", "Eagle", "Crab", "Turtle", "Snake", "Lizard",
"Frog", "Toad", "Cat", "Dog", "Bird", "Fish", "Shrimp", "Cricket",
"Ant", "Bee", "Butterfly", "Dragonfly", "Spider", "Scorpion", "Centipede",
"Mosquito", "Beetle", "Wasp", "Hornet", "Fly", "Moth", "Ladybug",
"Bee", "Honey", "Hive", "Nest", "Honey", "Bee", "Honey", "Bee"
];
// Generate unique pairs if needed, or just pick random ones
const uniqueNames = Array.from(new Set(animalNames)).slice(0, CONFIG.pointCount);
// --- Scene Setup ---
const container = document.getElementById('canvas-container');
const scene = new THREE.Scene();
scene.background = new THREE.Color(CONFIG.bgColor);
scene.fog = new THREE.Fog(CONFIG.bgColor, 20, 50);
// --- Camera ---
const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, CONFIG.cameraZ);
// --- Renderers ---
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
container.appendChild(renderer.domElement);
const labelRenderer = new CSS2DRenderer();
labelRenderer.setSize(window.innerWidth, window.innerHeight);
labelRenderer.domElement.style.position = 'absolute';
labelRenderer.domElement.style.top = '0px';
labelRenderer.domElement.style.pointerEvents = 'none'; // Crucial for clicks
container.appendChild(labelRenderer.domElement);
// --- Controls ---
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.enablePan = false;
controls.minDistance = 2; // Don't get too close
controls.maxDistance = 50;
controls.autoRotate = true;
controls.autoRotateSpeed = 0.5;
// --- Lighting ---
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
dirLight.position.set(10, 20, 10);
dirLight.castShadow = true;
scene.add(dirLight);
// --- Objects ---
// 1. The Cube (Wireframe)
const geometry = new THREE.BoxGeometry(CONFIG.cubeSize, CONFIG.cubeSize, CONFIG.cubeSize);
const edges = new THREE.EdgesGeometry(geometry);
const lineMaterial = new THREE.LineBasicMaterial({ color: CONFIG.lineColor, linewidth: 2 });
const wireframeCube = new THREE.LineSegments(edges, lineMaterial);
scene.add(wireframeCube);
// 2. The Points and Labels
const pointsGroup = new THREE.Group();
scene.add(pointsGroup);
const pointGeometry = new THREE.SphereGeometry(0.15, 16, 16);
const pointMaterial = new THREE.MeshBasicMaterial({ color: CONFIG.pointColor });
// Helper to calculate position on surface
// We use a simple rejection sampling to place points on the faces of the cube
function getRandomPointOnCube() {
const halfSize = CONFIG.cubeSize / 2;
let x, y, z;
// Randomly choose a face (0: +x, 1: -x, 2: +y, 3: -y, 4: +z, 5: -z)
const face = Math.floor(Math.random() * 6);
switch(face) {
case 0: x = halfSize; y = (Math.random() - 0.5) * CONFIG.cubeSize; z = (Math.random() - 0.5) * CONFIG.cubeSize; break;
case 1: x = -halfSize; y = (Math.random() - 0.5) * CONFIG.cubeSize; z = (Math.random() - 0.5) * CONFIG.cubeSize; break;
case 2: x = (Math.random() - 0.5) * CONFIG.cubeSize; y = halfSize; z = (Math.random() - 0.5) * CONFIG.cubeSize; break;
case 3: x = (Math.random() - 0.5) * CONFIG.cubeSize; y = -halfSize; z = (Math.random() - 0.5) * CONFIG.cubeSize; break;
case 4: x = (Math.random() - 0.5) * CONFIG.cubeSize; y = (Math.random() - 0.5) * CONFIG.cubeSize; z = halfSize; break;
case 5: x = (Math.random() - 0.5) * CONFIG.cubeSize; y = (Math.random() - 0.5) * CONFIG.cubeSize; z = -halfSize; break;
}
return new THREE.Vector3(x, y, z);
}
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
const clickablePoints = []; // Store meshes for raycasting
uniqueNames.forEach((name, index) => {
// Create 3D Point
const pos = getRandomPointOnCube();
const pointMesh = new THREE.Mesh(pointGeometry, pointMaterial);
pointMesh.position.copy(pos);
pointMesh.userData = { id: index, name: name }; // Store data for interaction
pointsGroup.add(pointMesh);
clickablePoints.push(pointMesh);
// Create HTML Label
const div = document.createElement('div');
div.className = 'label';
div.textContent = name;
const label = new CSS2DObject(div);
label.position.set(0, 0.3, 0); // Offset slightly above point
pointMesh.add(label);
});
// --- Interaction Logic ---
function onMouseClick(event) {
// Calculate mouse position in normalized device coordinates
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
// Intersect with our clickable points
const intersects = raycaster.intersectObjects(clickablePoints);
if (intersects.length > 0) {
const target = intersects[0].object;
const targetPos = target.position.clone();
// Calculate distance to target
const distance = camera.position.distanceTo(targetPos);
// Desired distance (zoom in)
// We want to be closer to the surface of the cube, minus the radius of the point
const targetDistance = CONFIG.cubeSize / 2 + 2;
// Animate Camera Position (Simple Linear Interpolation via TWEEN logic or manual loop)
// Here we use a simple GSAP-like manual tween for zero-dependency
const startPos = camera.position.clone();
const endPos = targetPos.clone().normalize().multiplyScalar(targetDistance);
let alpha = 0;
const duration = 1000; // ms
const startTime = performance.now();
function animateZoom(currentTime) {
const elapsed = currentTime - startTime;
alpha = Math.min(elapsed / duration, 1);
// Ease out cubic
const ease = 1 - Math.pow(1 - alpha, 3);
camera.position.lerpVectors(startPos, endPos, ease);
controls.target.lerp(targetPos, ease * 0.5); // Look at target
if (alpha < 1) {
requestAnimationFrame(animateZoom);
} else {
// Stop auto-rotation when zoomed in for better UX
controls.autoRotate = false;
}
}
requestAnimationFrame(animateZoom);
}
}
// --- Event Listeners ---
window.addEventListener('resize', onWindowResize);
window.addEventListener('click', onMouseClick);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
labelRenderer.setSize(window.innerWidth, window.innerHeight);
}
// --- Animation Loop ---
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
labelRenderer.render(scene, camera);
}
// --- Init ---
// Hide loader when scene is ready
setTimeout(() => {
document.getElementById('loader').style.opacity = '0';
setTimeout(() => {
document.getElementById('loader').style.display = 'none';
}, 500);
animate();
}, 1000);
</script>
</body>
</html> |