Spaces:
Running
Running
File size: 21,616 Bytes
c1771b2 |
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 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2D to 3D STL Generator</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/stl-export.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<style>
.dropzone {
border: 2px dashed #4b5563;
transition: all 0.3s ease;
}
.dropzone.active {
border-color: #3b82f6;
background-color: rgba(59, 130, 246, 0.1);
}
#previewCanvas {
background-color: #f3f4f6;
border-radius: 0.5rem;
}
#3dViewer {
width: 100%;
height: 400px;
background-color: #f3f4f6;
border-radius: 0.5rem;
}
.slider-thumb::-webkit-slider-thumb {
-webkit-appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
background: #3b82f6;
cursor: pointer;
}
.slider-thumb::-moz-range-thumb {
width: 20px;
height: 20px;
border-radius: 50%;
background: #3b82f6;
cursor: pointer;
}
</style>
</head>
<body class="bg-gray-100 min-h-screen">
<div class="container mx-auto px-4 py-8">
<header class="text-center mb-8">
<h1 class="text-4xl font-bold text-gray-800 mb-2">2D to 3D STL Generator</h1>
<p class="text-gray-600">Convert your 2D images into printable 3D STL files</p>
</header>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
<!-- Image Upload Section -->
<div class="bg-white rounded-xl shadow-md p-6">
<h2 class="text-2xl font-semibold text-gray-800 mb-4">1. Upload 2D Image</h2>
<div id="dropzone" class="dropzone rounded-lg p-8 text-center cursor-pointer mb-6">
<div class="flex flex-col items-center justify-center">
<i class="fas fa-cloud-upload-alt text-4xl text-blue-500 mb-3"></i>
<p class="text-gray-600 mb-2">Drag & drop your image here</p>
<p class="text-sm text-gray-500 mb-4">or</p>
<label for="fileInput" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg cursor-pointer transition">
Select Image
</label>
<input type="file" id="fileInput" accept="image/*" class="hidden">
</div>
</div>
<div id="imagePreviewContainer" class="hidden">
<h3 class="text-lg font-medium text-gray-700 mb-3">Image Preview</h3>
<div class="flex justify-center">
<canvas id="previewCanvas" class="w-full max-w-md h-auto"></canvas>
</div>
</div>
</div>
<!-- 3D Settings Section -->
<div class="bg-white rounded-xl shadow-md p-6">
<h2 class="text-2xl font-semibold text-gray-800 mb-4">2. 3D Generation Settings</h2>
<div class="space-y-4">
<div>
<label for="heightRange" class="block text-sm font-medium text-gray-700 mb-1">3D Height (mm)</label>
<div class="flex items-center">
<input type="range" id="heightRange" min="1" max="50" value="10" class="w-full slider-thumb">
<span id="heightValue" class="ml-3 text-gray-700 w-12 text-center">10</span>
</div>
</div>
<div>
<label for="smoothnessRange" class="block text-sm font-medium text-gray-700 mb-1">Edge Smoothness</label>
<div class="flex items-center">
<input type="range" id="smoothnessRange" min="1" max="10" value="5" class="w-full slider-thumb">
<span id="smoothnessValue" class="ml-3 text-gray-700 w-12 text-center">5</span>
</div>
</div>
<div>
<label for="baseThicknessRange" class="block text-sm font-medium text-gray-700 mb-1">Base Thickness (mm)</label>
<div class="flex items-center">
<input type="range" id="baseThicknessRange" min="0" max="10" value="2" class="w-full slider-thumb">
<span id="baseThicknessValue" class="ml-3 text-gray-700 w-12 text-center">2</span>
</div>
</div>
<div class="pt-4">
<button id="generateBtn" class="w-full bg-blue-500 hover:bg-blue-600 text-white py-3 px-4 rounded-lg font-medium transition flex items-center justify-center">
<i class="fas fa-cube mr-2"></i> Generate 3D Model
</button>
</div>
</div>
</div>
</div>
<!-- 3D Preview Section -->
<div id="3dPreviewSection" class="hidden mt-8 bg-white rounded-xl shadow-md p-6">
<div class="flex justify-between items-center mb-4">
<h2 class="text-2xl font-semibold text-gray-800">3D Model Preview</h2>
<button id="downloadBtn" class="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded-lg flex items-center">
<i class="fas fa-download mr-2"></i> Download STL
</button>
</div>
<div id="3dViewer"></div>
<div class="mt-4 grid grid-cols-1 md:grid-cols-3 gap-4">
<div class="bg-gray-50 p-4 rounded-lg">
<h3 class="font-medium text-gray-700 mb-2">Model Info</h3>
<p class="text-sm text-gray-600">Height: <span id="modelHeight" class="font-medium">10</span> mm</p>
<p class="text-sm text-gray-600">Base: <span id="modelBase" class="font-medium">2</span> mm</p>
</div>
<div class="bg-gray-50 p-4 rounded-lg">
<h3 class="font-medium text-gray-700 mb-2">Estimated Print Time</h3>
<p class="text-sm text-gray-600">Approx. <span id="printTime" class="font-medium">2-4</span> hours</p>
</div>
<div class="bg-gray-50 p-4 rounded-lg">
<h3 class="font-medium text-gray-700 mb-2">File Size</h3>
<p class="text-sm text-gray-600"><span id="fileSize" class="font-medium">1.2</span> MB</p>
</div>
</div>
</div>
<!-- How It Works Section -->
<div class="mt-12 bg-white rounded-xl shadow-md p-6">
<h2 class="text-2xl font-semibold text-gray-800 mb-4">How It Works</h2>
<div class="grid grid-cols-1 md:grid-cols-3 gap-6">
<div class="text-center">
<div class="bg-blue-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-3">
<i class="fas fa-upload text-blue-500 text-2xl"></i>
</div>
<h3 class="font-medium text-gray-800 mb-2">Upload Image</h3>
<p class="text-gray-600 text-sm">Upload any 2D image in JPG, PNG, or GIF format.</p>
</div>
<div class="text-center">
<div class="bg-blue-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-3">
<i class="fas fa-sliders-h text-blue-500 text-2xl"></i>
</div>
<h3 class="font-medium text-gray-800 mb-2">Adjust Settings</h3>
<p class="text-gray-600 text-sm">Customize the height, smoothness, and base thickness.</p>
</div>
<div class="text-center">
<div class="bg-blue-100 w-16 h-16 rounded-full flex items-center justify-center mx-auto mb-3">
<i class="fas fa-cube text-blue-500 text-2xl"></i>
</div>
<h3 class="font-medium text-gray-800 mb-2">Download STL</h3>
<p class="text-gray-600 text-sm">Generate and download your 3D model in STL format.</p>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
// DOM Elements
const dropzone = document.getElementById('dropzone');
const fileInput = document.getElementById('fileInput');
const previewCanvas = document.getElementById('previewCanvas');
const ctx = previewCanvas.getContext('2d');
const imagePreviewContainer = document.getElementById('imagePreviewContainer');
const generateBtn = document.getElementById('generateBtn');
const downloadBtn = document.getElementById('downloadBtn');
const heightRange = document.getElementById('heightRange');
const heightValue = document.getElementById('heightValue');
const smoothnessRange = document.getElementById('smoothnessRange');
const smoothnessValue = document.getElementById('smoothnessValue');
const baseThicknessRange = document.getElementById('baseThicknessRange');
const baseThicknessValue = document.getElementById('baseThicknessValue');
const modelHeight = document.getElementById('modelHeight');
const modelBase = document.getElementById('modelBase');
const printTime = document.getElementById('printTime');
const fileSize = document.getElementById('fileSize');
const dPreviewSection = document.getElementById('3dPreviewSection');
const dViewer = document.getElementById('3dViewer');
// Variables
let uploadedImage = null;
let generatedMesh = null;
let scene, camera, renderer, controls;
// Initialize Three.js viewer
function init3DViewer() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0xf3f4f6);
camera = new THREE.PerspectiveCamera(75, dViewer.clientWidth / dViewer.clientHeight, 0.1, 1000);
camera.position.z = 5;
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(dViewer.clientWidth, dViewer.clientHeight);
dViewer.appendChild(renderer.domElement);
// Add lights
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
// Add orbit controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
// Handle window resize
window.addEventListener('resize', onWindowResize);
// Start animation loop
animate();
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = dViewer.clientWidth / dViewer.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(dViewer.clientWidth, dViewer.clientHeight);
}
// Initialize the 3D viewer
init3DViewer();
// Update slider values display
heightRange.addEventListener('input', function() {
heightValue.textContent = this.value;
});
smoothnessRange.addEventListener('input', function() {
smoothnessValue.textContent = this.value;
});
baseThicknessRange.addEventListener('input', function() {
baseThicknessValue.textContent = this.value;
});
// Handle drag and drop
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropzone.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
['dragenter', 'dragover'].forEach(eventName => {
dropzone.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropzone.addEventListener(eventName, unhighlight, false);
});
function highlight() {
dropzone.classList.add('active');
}
function unhighlight() {
dropzone.classList.remove('active');
}
dropzone.addEventListener('drop', handleDrop, false);
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
handleFiles(files);
}
fileInput.addEventListener('change', function() {
handleFiles(this.files);
});
function handleFiles(files) {
if (files.length > 0) {
const file = files[0];
if (file.type.match('image.*')) {
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.onload = function() {
uploadedImage = img;
displayImagePreview(img);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
} else {
alert('Please upload an image file (JPG, PNG, GIF)');
}
}
}
function displayImagePreview(img) {
// Set canvas dimensions
const maxWidth = 500;
const maxHeight = 400;
let width = img.width;
let height = img.height;
if (width > maxWidth) {
height = (maxWidth / width) * height;
width = maxWidth;
}
if (height > maxHeight) {
width = (maxHeight / height) * width;
height = maxHeight;
}
previewCanvas.width = width;
previewCanvas.height = height;
// Draw image on canvas
ctx.drawImage(img, 0, 0, width, height);
// Show preview container
imagePreviewContainer.classList.remove('hidden');
}
// Generate 3D model
generateBtn.addEventListener('click', function() {
if (!uploadedImage) {
alert('Please upload an image first');
return;
}
// Show loading state
generateBtn.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i> Generating...';
generateBtn.disabled = true;
// Simulate generation process (in a real app, this would be more complex)
setTimeout(function() {
generate3DModel();
// Update model info
modelHeight.textContent = heightRange.value;
modelBase.textContent = baseThicknessRange.value;
// Estimate print time and file size (these would be calculated in a real app)
const height = parseInt(heightRange.value);
const base = parseInt(baseThicknessRange.value);
const estTime = Math.round(height * 0.2 + base * 0.1);
printTime.textContent = `${estTime}-${estTime + 2}`;
const fileSizeEst = (height * width * 0.001).toFixed(1);
fileSize.textContent = fileSizeEst;
// Show 3D preview section
dPreviewSection.classList.remove('hidden');
// Reset button
generateBtn.innerHTML = '<i class="fas fa-cube mr-2"></i> Generate 3D Model';
generateBtn.disabled = false;
// Scroll to 3D preview
dPreviewSection.scrollIntoView({ behavior: 'smooth' });
}, 1500);
});
function generate3DModel() {
// Clear previous mesh
if (generatedMesh) {
scene.remove(generatedMesh);
}
// Create a simple 3D shape from the image (simplified for demo)
// In a real app, you would use more sophisticated algorithms
const geometry = new THREE.BoxGeometry(
3,
parseFloat(baseThicknessRange.value) * 0.1,
parseFloat(heightRange.value) * 0.1
);
// Create texture from image
const texture = new THREE.Texture(previewCanvas);
texture.needsUpdate = true;
const material = new THREE.MeshPhongMaterial({
map: texture,
side: THREE.DoubleSide
});
generatedMesh = new THREE.Mesh(geometry, material);
scene.add(generatedMesh);
// Reset camera position
camera.position.z = 5;
controls.reset();
}
// Download STL
downloadBtn.addEventListener('click', function() {
if (!generatedMesh) {
alert('Please generate a 3D model first');
return;
}
// Show loading state
downloadBtn.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i> Preparing Download...';
downloadBtn.disabled = true;
// Simulate STL export (in a real app, you would use a proper STL exporter)
setTimeout(function() {
// Create a simplified STL export (demo only)
const exporter = new THREE.STLExporter();
const stlString = exporter.parse(generatedMesh);
// Create download link
const blob = new Blob([stlString], { type: 'application/octet-stream' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = '3d-model.stl';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Reset button
downloadBtn.innerHTML = '<i class="fas fa-download mr-2"></i> Download STL';
downloadBtn.disabled = false;
}, 1000);
});
});
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=Mathieu2025/2dto3d-converter" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html> |