<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel 3D Editor RGB share</title>
<style>
body, html { margin:0; overflow:hidden; }
#colorPicker {
position: absolute; top: 10px; left: 10px;
z-index: 10;
background: rgba(255,255,255,0.8);
padding: 5px;
border-radius: 4px;
}
#shareLink {
position: absolute; top: 45px; left:10px;
z-index: 10;
background: rgba(255,255,255,0.8);
padding: 5px;
font-family: monospace;
max-width: 300px;
word-break: break-all;
}
</style>
</head>
<body>
<input type="color" id="colorPicker" value="#ff0000" />
<div id="shareLink"></div>
<script src="https://cdn.jsdelivr.net/npm/three@0.152.2/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.152.2/examples/js/controls/OrbitControls.js"></script>
<script>
(() => {
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.1;
controls.minDistance = 5;
controls.maxDistance = 50;
camera.position.set(10, 10, 10);
controls.update();
// Add grid helper
const gridHelper = new THREE.GridHelper(20, 20);
scene.add(gridHelper);
// Light
const ambient = new THREE.AmbientLight(0xffffff, 0.7);
scene.add(ambient);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(10,20,10);
scene.add(directionalLight);
// Voxel data: key = "x_y_z", value = hex color string e.g. "#ff0000"
let voxels = {};
// Geometry + material cache
const geometry = new THREE.BoxGeometry(1,1,1);
function hexToThreeColor(hex) {
return new THREE.Color(hex);
}
// Store mesh references keyed by position for efficient removal
let voxelMeshes = new Map();
// Create a single material per color (to reuse)
const materialCache = new Map();
function getMaterial(colorHex) {
if(!materialCache.has(colorHex)) {
let mat = new THREE.MeshLambertMaterial({color: colorHex});
materialCache.set(colorHex, mat);
}
return materialCache.get(colorHex);
}
// Add voxel at position with color
function addVoxel(pos, color) {
const key = pos.join('_');
if(voxels[key]) return; // already exists
voxels[key] = color;
const mesh = new THREE.Mesh(geometry, getMaterial(color));
mesh.position.set(...pos);
scene.add(mesh);
voxelMeshes.set(key, mesh);
}
// Remove voxel
function removeVoxel(pos) {
const key = pos.join('_');
if(!voxels[key]) return;
voxels[key] = null;
delete voxels[key];
const mesh = voxelMeshes.get(key);
if(mesh) {
scene.remove(mesh);
voxelMeshes.delete(key);
}
}
// Serialize voxels to compact string for url
// Format: > "x,y,z,color;..."
// Color stripped "#" and shortened
function serializeVoxels() {
const parts = [];
for(let key in voxels) {
const color = voxels[key];
if (!color) continue;
parts.push(key + ',' + color.replace('#',''));
}
return btoa(parts.join(';'));
}
// Deserialize voxel string
function deserializeVoxels(str) {
voxels = {};
voxelMeshes.forEach(mesh => scene.remove(mesh));
voxelMeshes.clear();
materialCache.clear();
if(!str) return;
try {
const decoded = atob(str);
if(!decoded) return;
const parts = decoded.split(';');
for(let p of parts) {
if(!p) continue;
const [x,y,z,color] = p.split(',');
if(x !== undefined && y !== undefined && z !== undefined && color) {
addVoxel([+x, +y, +z], '#'+color);
}
}
} catch(e) {
console.warn("Failed to deserialize voxels", e);
}
}
// Input handlers:
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
function coordsToArray(str) {
return str.split('_').map(Number);
}
function getVoxelAtPos(pos) {
const key = pos.join('_');
return voxels[key] || null;
}
// On click: if intersects voxel, remove it
// else if intersects face of voxel or grid, add voxel adjacent
function onClick(event) {
event.preventDefault();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = - (event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
// All voxel meshes + grid for intersect test
const intersectObjects = Array.from(voxelMeshes.values());
intersectObjects.push(gridHelper);
const intersects = raycaster.intersectObjects(intersectObjects, false);
if(intersects.length > 0) {
const intersect = intersects[0];
if(voxelMeshes.has(intersect.object.uuid ? intersect.object.uuid : '')) {
// Intersected with a voxel mesh
// Find voxel by position
const pos = [Math.round(intersect.point.x), Math.round(intersect.point.y), Math.round(intersect.point.z)];
// But intersection point will be near face, so use object position
const voxelPos = [intersect.object.position.x, intersect.object.position.y, intersect.object.position.z].map(Math.round);
removeVoxel(voxelPos);
} else {
// Intersected with grid or else => add voxel adjacent
// intersection point on face, face normal tells where to add voxel
const faceNormal = intersect.face.normal;
const voxelPos = intersect.point.clone().addScaledVector(faceNormal, 0.5).floor();
addVoxel([voxelPos.x, voxelPos.y, voxelPos.z], document.getElementById('colorPicker').value);
}
} else {
// Missed everything - do nothing
}
updateURL();
}
// Setup event listeners
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
window.addEventListener('click', onClick);
// Update URL hash with voxel data
function updateURL() {
const encoded = serializeVoxels();
location.hash = '#/' + encoded;
document.getElementById('shareLink').textContent = location.href;
}
// On load, parse hash and restore
function loadFromURL() {
const h = location.hash;
if(h.startsWith('#/')) {
const encoded = h.substring(2);
deserializeVoxels(encoded);
}
}
loadFromURL();
updateURL();
// Render loop
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
})();
</script>
</body>
</html>