<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple Voxel Editor RGB Share</title>
<style>
body {
font-family: sans-serif;
margin: 0;
padding: 1rem;
background: #222;
color: #eee;
display: flex;
flex-direction: column;
align-items: center;
}
#editor {
display: grid;
grid-template-columns: repeat(8, 32px);
grid-template-rows: repeat(8, 32px);
gap: 2px;
margin-bottom: 1rem;
user-select: none;
}
.voxel {
width: 32px;
height: 32px;
background: #111;
border: 1px solid #333;
box-sizing: border-box;
cursor: pointer;
transition: background-color 0.2s;
}
.voxel.empty {
background: #111;
}
#controls {
margin-bottom: 0.5rem;
display: flex;
gap: 0.5rem;
align-items: center;
}
button, input[type="color"] {
font-size: 1rem;
padding: 0.25em 0.6em;
border-radius: 4px;
border: none;
cursor: pointer;
}
button:hover {
background: #333;
}
#info {
margin-top: 1rem;
text-align: center;
font-size: 0.9rem;
line-height: 1.4em;
max-width: 400px;
}
/* Layer selector */
#layerSelector {
margin-left: 1rem;
}
</style>
</head>
<body>
<h1>Simple Voxel Editor RGB Share</h1>
<div id="controls">
<label for="colorPicker">Current Color:</label>
<input type="color" id="colorPicker" value="#ff0000" title="Pick a color" />
<button id="clearBtn" title="Clear current layer">Clear Layer</button>
<button id="shareBtn" title="Share scene via URL">Share</button>
<label for="layerSelector">Layer:</label>
<input type="range" id="layerSelector" min="0" max="7" value="0" title="Select layer (Z)" />
<span id="layerLabel">0</span>
</div>
<div id="editor"></div>
<div id="info">
Click voxels to toggle color on current layer.<br />
Use the slider to change layers.<br />
Share button copies URL with scene.<br />
URL automatically loads saved scenes.
</div>
<script>
(() => {
// Grid size
const SIZE = 8; // 8x8x8
const editor = document.getElementById('editor');
const colorPicker = document.getElementById('colorPicker');
const shareBtn = document.getElementById('shareBtn');
const clearBtn = document.getElementById('clearBtn');
const layerSelector = document.getElementById('layerSelector');
const layerLabel = document.getElementById('layerLabel');
// Store voxel colors: array of SIZE*SIZE*SIZE elements
// Each element is either null (empty) or [r,g,b]
// r,g,b in 0-255
const voxels = new Array(SIZE * SIZE * SIZE).fill(null);
// Current editing layer
let currentLayer = 0; // Z
// Map (x,y,z) to index
function idx(x, y, z) {
return z * SIZE * SIZE + y * SIZE + x;
}
// Create the 2D editor for current layer (x/y plane at z = currentLayer)
// We only render one layer at a time to keep UI manageable
function renderLayer(z) {
editor.innerHTML = '';
editor.style.gridTemplateColumns = `repeat(${SIZE}, 32px)`;
editor.style.gridTemplateRows = `repeat(${SIZE}, 32px)`;
for (let y = 0; y < SIZE; y++) {
for (let x = 0; x < SIZE; x++) {
const voxelDiv = document.createElement('div');
voxelDiv.classList.add('voxel');
voxelDiv.dataset.x = x;
voxelDiv.dataset.y = y;
voxelDiv.dataset.z = z;
const c = voxels[idx(x, y, z)];
if (c) {
voxelDiv.style.backgroundColor = `rgb(${c[0]},${c[1]},${c[2]})`;
voxelDiv.classList.remove('empty');
} else {
voxelDiv.style.backgroundColor = '#111';
voxelDiv.classList.add('empty');
}
voxelDiv.addEventListener('click', () => {
// Toggle voxel color at (x,y,z)
if (voxels[idx(x, y, z)]) {
// Currently colored, remove
voxels[idx(x, y, z)] = null;
voxelDiv.style.backgroundColor = '#111';
voxelDiv.classList.add('empty');
} else {
// Add color from picker
const hex = colorPicker.value;
const rgb = hexToRgb(hex);
voxels[idx(x, y, z)] = rgb;
voxelDiv.style.backgroundColor = `rgb(${rgb[0]},${rgb[1]},${rgb[2]})`;
voxelDiv.classList.remove('empty');
}
});
editor.appendChild(voxelDiv);
}
}
}
// Convert hex #rrggbb to [r,g,b]
function hexToRgb(hex) {
const bigint = parseInt(hex.slice(1), 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;
return [r, g, b];
}
// Convert [r,g,b] to hex #rrggbb
function rgbToHex(rgb) {
return (
'#' +
rgb
.map((x) => {
const s = x.toString(16);
return s.length === 1 ? '0' + s : s;
})
.join('')
);
}
// Serialize voxel data to a compact string for URL hash
// We serialize only non-empty voxels: "x,y,z,r,g,b;"
// Then encode with base64 or URI component
function serializeVoxels() {
const parts = [];
for (let z = 0; z < SIZE; z++) {
for (let y = 0; y < SIZE; y++) {
for (let x = 0; x < SIZE; x++) {
const c = voxels[idx(x, y, z)];
if (c) {
parts.push(
`${x},${y},${z},${c[0]},${c[1]},${c[2]}`
);
}
}
}
}
const raw = parts.join(';');
// URI encode for safety
return encodeURIComponent(raw);
}
// Deserialize from string in hash to voxels array
function deserializeVoxels(str) {
// Reset voxels
for (let i = 0; i < voxels.length; i++) voxels[i] = null;
if (!str) return;
const decoded = decodeURIComponent(str);
if (!decoded) return;
const entries = decoded.split(';');
for (const entry of entries) {
if (!entry) continue;
const parts = entry.split(',');
if (parts.length !== 6) continue;
const [x, y, z, r, g, b] = parts.map(Number);
if (
x >= 0 &&
x < SIZE &&
y >= 0 &&
y < SIZE &&
z >= 0 &&
z < SIZE
) {
voxels[idx(x, y, z)] = [r, g, b];
}
}
}
// Update URL hash with current scene
function updateHash() {
const data = serializeVoxels();
location.hash = data;
}
// Load from URL hash if any
function loadFromHash() {
if (location.hash.length > 1) {
const data = location.hash.substring(1);
try {
deserializeVoxels(data);
} catch (e) {
console.warn('Failed to load voxels from hash', e);
}
}
}
// Copy share URL to clipboard
function copyShareURL() {
updateHash();
const url = location.href;
if (navigator.clipboard) {
navigator.clipboard.writeText(url).then(() => {
alert('Share URL copied to clipboard!');
}, () => {
alert('Failed to copy URL. You can copy it manually:\n' + url);
});
} else {
prompt('Copy the URL below:', url);
}
}
// Clear current layer
function clearCurrentLayer() {
for (let y = 0; y < SIZE; y++) {
for (let x = 0; x < SIZE; x++) {
voxels[idx(x, y, currentLayer)] = null;
}
}
renderLayer(currentLayer);
}
// Initialize
function init() {
loadFromHash();
renderLayer(currentLayer);
layerSelector.min = 0;
layerSelector.max = SIZE - 1;
layerSelector.value = currentLayer;
layerLabel.textContent = currentLayer;
layerSelector.addEventListener('input', (e) => {
currentLayer = Number(e.target.value);
layerLabel.textContent = currentLayer;
renderLayer(currentLayer);
});
shareBtn.addEventListener('click', () => {
copyShareURL();
});
clearBtn.addEventListener('click', () => {
if (confirm(`Clear all voxels on layer ${currentLayer}?`)) {
clearCurrentLayer();
}
});
window.addEventListener('hashchange', () => {
loadFromHash();
renderLayer(currentLayer);
});
}
init();
})();
</script>
</body>
</html>