Creating a full voxel editor with 3D voxel placement, resizing, color selection, copy-paste, line selection, position shortcuts, deletion, and sharing features is a bit complex but totally doable with modern web technologies like HTML, CSS, and JavaScript using Three.js for 3D rendering.
Below is a minimal functional voxel editor covering:
3D voxel grid (X, Y, Z)
Add/remove voxels by clicking
Color picker for voxels
Select voxel by clicking
Copy & paste voxels
Line drawing mode between two voxels
Keyboard shortcuts for reset, copy, paste, delete, and moving selected voxels
Share via URL encoding (hash fragment)
Resize grid (simple UI with input fields)
Display position and color info
How it works
Click on voxels to select/deselect.
CTRL + Click to add adjacent voxel.
Delete key removes selected voxel.
Copy (Ctrl+C) and Paste (Ctrl+V).
Shift + click start/end to draw line of voxels.
W/A/S/D/Q/E to move selected voxels.
Color picker changes color of new or selected voxels.
Resize grid inputs change dimension and reset grid.
URL hash stores voxel data as compressed JSON.
Code example
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Editor 3D</title>
<style>
body, html {
margin:0; padding:0; overflow:hidden; font-family:sans-serif;
}
#ui {
position: fixed;
top:10px; left:10px;
background:#222a;
color:#eee;
padding:10px;
border-radius:6px;
z-index:10;
user-select:none;
}
#ui input[type=number] {
width:50px;
}
#color-picker {
width: 60px;
height: 30px;
border:none;
cursor:pointer;
}
#info {
margin-top:8px;
font-size:14px;
line-height:1.3em;
}
#share-link {
width: 100%;
}
</style>
</head>
<body>
<div id="ui">
<div>
Grid Size:
X<input id="sizeX" type="number" min="1" max="32" value="16" />
Y<input id="sizeY" type="number" min="1" max="32" value="16" />
Z<input id="sizeZ" type="number" min="1" max="32" value="16" />
<button id="resize">Resize</button>
</div>
<div style="margin-top:8px;">
Color: <input type="color" id="color-picker" value="#ff0000" />
</div>
<div style="margin-top:8px;">
<button id="clear">Clear</button>
<button id="copy">Copy</button>
<button id="paste">Paste</button>
<button id="line-mode">Line Mode: OFF</button>
</div>
<div id="info">
<div>Selected Voxels: <span id="selected-count">0</span></div>
<div>Position(s): <span id="selected-pos">-</span></div>
<div>Use CTRL+Click to add voxel, Shift+Click to draw a line.<br>
W/A/S/D/Q/E to move selection.<br>
Delete to remove selected voxels.<br>
Ctrl+C / Ctrl+V to copy/paste.<br>
Share by URL hash auto-updates.</div>
</div>
<div style="margin-top:8px;">
Share link:<br>
<textarea id="share-link" readonly rows="4"></textarea>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/three@0.150.1/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.150.1/examples/js/controls/OrbitControls.js"></script>
<script>
(() => {
// Basic 3D Voxel Editor
// Vars
let scene, camera, renderer, controls;
let gridHelper;
let raycaster, mouse;
let voxelSize = 1;
let gridSize = { x: 16, y: 16, z: 16 };
let voxels = new Map(); // key = "x,y,z", val = {mesh, color}
let selected = new Set();
let copyBuffer = [];
let lineMode = false;
// Colors
let currentColor = '#ff0000';
// UI Elements
const sizeXInput = document.getElementById('sizeX');
const sizeYInput = document.getElementById('sizeY');
const sizeZInput = document.getElementById('sizeZ');
const resizeBtn = document.getElementById('resize');
const colorPicker = document.getElementById('color-picker');
const clearBtn = document.getElementById('clear');
const copyBtn = document.getElementById('copy');
const pasteBtn = document.getElementById('paste');
const lineModeBtn = document.getElementById('line-mode');
const selectedCount = document.getElementById('selected-count');
const selectedPos = document.getElementById('selected-pos');
const shareLink = document.getElementById('share-link');
// Three.js setup
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x111111);
camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set(gridSize.x*1.5, gridSize.y*1.5, gridSize.z*1.5);
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.target.set(gridSize.x/2, gridSize.y/2, gridSize.z/2);
controls.update();
raycaster = new THREE.Raycaster();
mouse = new THREE.Vector2();
gridHelper = new THREE.GridHelper(Math.max(gridSize.x, gridSize.z), Math.max(gridSize.x, gridSize.z));
scene.add(gridHelper);
// Add axis helper
const axesHelper = new THREE.AxesHelper(Math.max(gridSize.x, gridSize.y, gridSize.z) + 2);
scene.add(axesHelper);
window.addEventListener('resize', onWindowResize);
renderer.domElement.addEventListener('pointerdown', onPointerDown);
window.addEventListener('keydown', onKeyDown);
animate();
}
function onWindowResize() {
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Return key string for Map key
function key(x,y,z) {
return `${x},${y},${z}`;
}
// Parse key string
function parseKey(k) {
return k.split(',').map(n => parseInt(n));
}
// Create a voxel mesh at position with color
function createVoxel(x,y,z,color) {
const geometry = new THREE.BoxGeometry(voxelSize*0.95, voxelSize*0.95, voxelSize*0.95);
const material = new THREE.MeshStandardMaterial({color: color, roughness: 0.4, metalness: 0.1});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x+0.5, y+0.5, z+0.5);
return mesh;
}
// Add voxel
function addVoxel(x,y,z,color) {
if(x < 0 || y < 0 || z < 0) return;
if(x >= gridSize.x || y >= gridSize.y || z >= gridSize.z) return;
const k = key(x,y,z);
if(voxels.has(k)) return; // Exists
const mesh = createVoxel(x,y,z,color);
scene.add(mesh);
voxels.set(k, {mesh, color});
updateShareLink();
}
// Remove voxel
function removeVoxel(x,y,z) {
const k = key(x,y,z);
if(!voxels.has(k)) return;
const {mesh} = voxels.get(k);
scene.remove(mesh);
mesh.geometry.dispose();
if (mesh.material) mesh.material.dispose();
voxels.delete(k);
selected.delete(k);
updateSelectionInfo();
updateShareLink();
}
// Select voxel
function selectVoxel(k, additive=false) {
if(!voxels.has(k)) return;
if(!additive) selected.clear();
if(selected.has(k)) selected.delete(k);
else selected.add(k);
updateSelectionInfo();
}
// Update selection visuals
function updateSelectionVisuals() {
voxels.forEach(({mesh}, k) => {
if(selected.has(k)){
mesh.material.emissive = new THREE.Color(0x4444ff);
} else {
mesh.material.emissive = new THREE.Color(0x000000);
}
});
}
// Update UI selection info
function updateSelectionInfo() {
selectedCount.textContent = selected.size;
if(selected.size === 0){
selectedPos.textContent = '-';
} else {
const positions = Array.from(selected).map(k => {
const [x,y,z] = parseKey(k);
return `(${x},${y},${z})`;
}).join(' ');
selectedPos.textContent = positions;
}
updateSelectionVisuals();
}
// Find voxel under mouse
function getIntersectedVoxel(event) {
const rect = renderer.domElement.getBoundingClientRect();
mouse.x = ((event.clientX - rect.left) / rect.width) * 2 -1;
mouse.y = -((event.clientY - rect.top) / rect.height) * 2 +1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(Array.from(voxels.values()).map(v=>v.mesh));
return intersects.length ? intersects[0] : null;
}
// Find voxel adjacent face to add voxel on
function addVoxelAdjacent(intersect) {
if(!intersect) return;
const point = intersect.point;
const normal = intersect.face.normal;
const voxel = intersect.object;
const pos = voxel.position;
const x = Math.floor(pos.x - 0.5);
const y = Math.floor(pos.y - 0.5);
const z = Math.floor(pos.z - 0.5);
const nx = x + normal.x;
const ny = y + normal.y;
const nz = z + normal.z;
addVoxel(nx, ny, nz, currentColor);
}
// Clear all voxels
function clearAll() {
voxels.forEach(({mesh}) => {
scene.remove(mesh);
mesh.geometry.dispose();
if(mesh.material) mesh.material.dispose();
});
voxels.clear();
selected.clear();
updateSelectionInfo();
updateShareLink();
}
// Copy selected voxels to buffer
function copySelected() {
copyBuffer = Array.from(selected).map(k => {
const v = voxels.get(k);
const [x,y,z] = parseKey(k);
return {x,y,z,color: v.color};
});
}
// Paste buffer voxels offset to center of grid
function pasteBuffer() {
if(copyBuffer.length === 0) return;
// Compute offset to place in center
let minX = Infinity, minY=Infinity, minZ=Infinity;
copyBuffer.forEach(v => {
if(v.x < minX) minX = v.x;
if(v.y < minY) minY = v.y;
if(v.z < minZ) minZ = v.z;
});
// Offset to grid center (floor)
const centerX = Math.floor(gridSize.x/2);
const centerY = Math.floor(gridSize.y/2);
const centerZ = Math.floor(gridSize.z/2);
selected.clear();
copyBuffer.forEach(v => {
const px = centerX + (v.x - minX);
const py = centerY + (v.y - minY);
const pz = centerZ + (v.z - minZ);
if(px<0||py<0||pz<0) return;
if(px>=gridSize.x||py>=gridSize.y||pz>=gridSize.z) return;
addVoxel(px, py, pz, v.color);
selected.add(key(px,py,pz));
});
updateSelectionInfo();
}
// Move selected voxels by dx,dy,dz if within bounds and empty
function moveSelection(dx,dy,dz) {
if(selected.size === 0) return;
// Check if move is valid
for(const k of selected){
const [x,y,z] = parseKey(k);
const nx = x+dx, ny=y+dy, nz=z+dz;
if(nx<0||ny<0||nz<0) return;
if(nx>=gridSize.x||ny>=gridSize.y||nz>=gridSize.z) return;
if(voxels.has(key(nx,ny,nz)) && !selected.has(key(nx,ny,nz))){
// Blocked by voxel not in selection
return;
}
}
// Move all selected voxels
const newVoxels = new Map();
for(const k of selected){
const [x,y,z] = parseKey(k);
const voxel = voxels.get(k);
removeVoxel(x,y,z);
const nx = x+dx, ny=y+dy, nz=z+dz;
const newKey = key(nx,ny,nz);
newVoxels.set(newKey, {mesh:null, color:voxel.color});
}
// Re-create voxels & update selection
selected.clear();
newVoxels.forEach(({color}, k) => {
const [x,y,z] = parseKey(k);
addVoxel(x,y,z, color);
selected.add(k);
});
updateSelectionInfo();
}
// Draw line of voxels between two points using Bresenham's 3D line algorithm
function drawLine(start, end, color) {
const points = [];
let x1 = start[0], y1 = start[1], z1 = start[2];
const x2 = end[0], y2 = end[1], z2 = end[2];
let dx = Math.abs(x2 - x1);
let dy = Math.abs(y2 - y1);
let dz = Math.abs(z2 - z1);
let xs = (x2 > x1) ? 1 : -1;
let ys = (y2 > y1) ? 1 : -1;
let zs = (z2 > z1) ? 1 : -1;
// Driving axis is X-axis
if (dx >= dy && dx >= dz) {
let p1 = 2 * dy - dx;
let p2 = 2 * dz - dx;
while (x1 != x2) {
if(x1>=0 && x1<gridSize.x && y1>=0 && y1<gridSize.y && z1>=0 && z1<gridSize.z)
points.push([x1, y1, z1]);
x1 += xs;
if (p1 >= 0) {
y1 += ys;
p1 -= 2 * dx;
}
if (p2 >= 0) {
z1 += zs;
p2 -= 2 * dx;
}
p1 += 2 * dy;
p2 += 2 * dz;
}
}
// Driving axis is Y-axis
else if (dy >= dx && dy >= dz) {
let p1 = 2 * dx - dy;
let p2 = 2 * dz - dy;
while (y1 != y2) {
if(x1>=0 && x1<gridSize.x && y1>=0 && y1<gridSize.y && z1>=0 && z1<gridSize.z)
points.push([x1, y1, z1]);
y1 += ys;
if (p1 >= 0) {
x1 += xs;
p1 -= 2 * dy;
}
if (p2 >= 0) {
z1 += zs;
p2 -= 2 * dy;
}
p1 += 2 * dx;
p2 += 2 * dz;
}
}
// Driving axis is Z-axis
else {
let p1 = 2 * dy - dz;
let p2 = 2 * dx - dz;
while (z1 != z2) {
if(x1>=0 && x1<gridSize.x && y1>=0 && y1<gridSize.y && z1>=0 && z1<gridSize.z)
points.push([x1, y1, z1]);
z1 += zs;
if (p1 >= 0) {
y1 += ys;
p1 -= 2 * dz;
}
if (p2 >= 0) {
x1 += xs;
p2 -= 2 * dz;
}
p1 += 2 * dy;
p2 += 2 * dx;
}
}
// Add end point
if(x2>=0 && x2<gridSize.x && y2>=0 && y2<gridSize.y && z2>=0 && z2<gridSize.z)
points.push([x2,y2,z2]);
// Add voxels
points.forEach(([x,y,z]) => addVoxel(x,y,z,color));
}
// Mouse click state for line mode endpoints
let lineStart = null;
// Handler pointer down
function onPointerDown(e) {
e.preventDefault();
const intersect = getIntersectedVoxel(e);
if(lineMode){
if(intersect){
const pos = intersect.object.position;
const x = Math.floor(pos.x-0.5);
const y = Math.floor(pos.y-0.5);
const z = Math.floor(pos.z-0.5);
if(!lineStart){
lineStart = [x,y,z];
lineModeBtn.textContent = 'Line Mode: STARTED - select endpoint';
} else {
drawLine(lineStart, [x,y,z], currentColor);
lineStart = null;
lineModeBtn.textContent = 'Line Mode: ON';
updateShareLink();
}
}
return; // ignore other handlers in line mode
}
if(intersect){
const voxelMesh = intersect.object;
const pos = voxelMesh.position;
const x = Math.floor(pos.x - 0.5);
const y = Math.floor(pos.y - 0.5);
const z = Math.floor(pos.z - 0.5);
const k = key(x,y,z);
if(e.ctrlKey){
// CTRL + click add voxel adjacent
addVoxelAdjacent(intersect);
} else if(e.shiftKey){
// SHIFT + click toggle selection additive
selectVoxel(k, true);
} else {
// Click select voxel
selectVoxel(k, false);
}
} else {
selected.clear();
updateSelectionInfo();
}
}
// Keyboard shortucts and actions
function onKeyDown(e) {
if(e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
const key = e.key.toLowerCase();
if(e.ctrlKey && key === 'c') {
e.preventDefault();
copySelected();
} else if(e.ctrlKey && key === 'v'){
e.preventDefault();
pasteBuffer();
} else if(key === 'delete' || key === 'backspace'){
e.preventDefault();
selected.forEach(k => {
const [x,y,z] = parseKey(k);
removeVoxel(x,y,z);
});
selected.clear();
updateSelectionInfo();
} else if(key === 'w'){
e.preventDefault();
moveSelection(0,1,0);
} else if(key === 's'){
e.preventDefault();
moveSelection(0,-1,0);
} else if(key === 'a'){
e.preventDefault();
moveSelection(-1,0,0);
} else if(key === 'd'){
e.preventDefault();
moveSelection(1,0,0);
} else if(key === 'q'){
e.preventDefault();
moveSelection(0,0,1);
} else if(key === 'e'){
e.preventDefault();
moveSelection(0,0,-1);
}
}
// Update current color
colorPicker.addEventListener('input', e => {
currentColor = e.target.value;
});
// Resize button
resizeBtn.addEventListener('click', () => {
clearAll();
const sx = parseInt(sizeXInput.value);
const sy = parseInt(sizeYInput.value);
const sz = parseInt(sizeZInput.value);
if(sx > 0 && sy>0 && sz>0 && sx<=32 && sy<=32 && sz<=32){
gridSize = {x: sx, y: sy, z: sz};
updateGridHelper();
controls.target.set(gridSize.x/2, gridSize.y/2, gridSize.z/2);
controls.update();
updateShareLink();
}
});
// Clear button
clearBtn.addEventListener('click', clearAll);
// Copy button
copyBtn.addEventListener('click', () => {
copySelected();
alert('Copied ' + copyBuffer.length + ' voxels');
});
// Paste button
pasteBtn.addEventListener('click', pasteBuffer);
// Toggle line mode
lineModeBtn.addEventListener('click', () => {
lineMode = !lineMode;
lineStart = null;
lineModeBtn.textContent = 'Line Mode: ' + (lineMode ? 'ON' : 'OFF');
});
// Update grid helper size and divisions
function updateGridHelper(){
scene.remove(gridHelper);
const size = Math.max(gridSize.x, gridSize.z);
gridHelper = new THREE.GridHelper(size, size);
scene.add(gridHelper);
}
// Animate loop
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
// Save voxels data compressed to JSON base64 in URL hash for sharing
function updateShareLink() {
const data = {
gridSize,
voxels: Array.from(voxels.entries()).map(([k,v]) => {
return {pos: k, color: v.color};
})
};
const json = JSON.stringify(data);
// simple base64 encode (no compression to keep simple)
const enc = btoa(unescape(encodeURIComponent(json)));
location.hash = enc;
shareLink.value = location.href;
}
// Load voxels data from URL hash
function loadFromHash() {
if(location.hash.length < 2) return false;
try {
const dec = decodeURIComponent(escape(atob(location.hash.slice(1))));
const data = JSON.parse(dec);
gridSize = data.gridSize;
sizeXInput.value = gridSize.x;
sizeYInput.value = gridSize.y;
sizeZInput.value = gridSize.z;
updateGridHelper();
controls.target.set(gridSize.x/2, gridSize.y/2, gridSize.z/2);
controls.update();
data.voxels.forEach(v => {
const [x,y,z] = v.pos.split(',').map(Number);
addVoxel(x,y,z, v.color);
});
return true;
} catch(e) {
console.warn('Failed to load from hash', e);
return false;
}
}
// Lighting
function addLights() {
const ambient = new THREE.AmbientLight(0xcccccc, 0.5);
scene.add(ambient);
const directional = new THREE.DirectionalLight(0xffffff, 0.8);
directional.position.set(1,2,1);
scene.add(directional);
}
// Init & start
init();
addLights();
if(!loadFromHash()){
// Default start voxel
addVoxel(Math.floor(gridSize.x/2), Math.floor(gridSize.y/2), Math.floor(gridSize.z/2), currentColor);
}
updateSelectionInfo();
})();
</script>
</body>
</html>
Features & Usage
Orbit camera with mouse drag.
Click voxel to select.
CTRL + click voxel face: add voxel adjacent in that direction.
SHIFT + click: additive selection toggle.
Delete removes selected.
W/A/S/D/Q/E moves selection in 3D grid.
Copy/Paste buttons and Ctrl+C / Ctrl+V shortcuts.
Line mode lets you pick two voxels to draw a colored line of voxels between them.
Color picker changes color for new voxels.
Resize grid button to change 3D voxel workspace.
Clear button clears all voxels.
Voxel data stored in URL hash for sharing.
This code provides a base you can build on with more features like:
Undo/redo
Advanced selection tools
More complex sharing (compressed)
Export/import files
If you want me to help extend this further, just ask!