SELECTING 3D
MINECRAFT MODELS
By Shane Brumback · · Updated · Free Three.js tutorial · Level 1
To select a 3D model in Three.js, turn the click position into a ray with Raycaster.setFromCamera, test it against your models with intersectObjects(models, true), and walk from the mesh you hit back to the model it belongs to. Here five animated Minecraft zombies glow green, hop and get a ring under their feet when you click or tap them.
A beginner walkthrough of loading a GLTF / GLB model, cloning it with SkeletonUtils, playing its animations, and click picking with a Three.js Raycaster. The zombies behind this page are the live demo.
LIVE DEMO
The zombie line-up behind this page is a live Three.js scene. Launch the full-screen demo to click or tap a zombie to select it, drag to orbit and scroll to zoom. Launch asks for a free Google sign-in first. Press Esc or Exit to return.
Free Google sign-in · Click to select · Drag to orbit · Scroll to zoom · Esc to exit
How Clicking a 3D Model Works
Your screen is flat but the scene is 3D, so a click on its own does not know which object you meant. A raycaster solves that: it shoots an invisible laser from the camera through the exact pixel you clicked and reports every triangle the laser passes through, nearest first.
A Minecraft model is not one mesh. It is a little family tree: a head, a body, two arms and two legs, each its own mesh under one parent. So we ask the raycaster to search recursively (the true argument), then use a tag we stored on every part, userData.zombieIndex, to know which zombie owns the arm or leg we hit.
Why Clone With SkeletonUtils?
The zombie is a rigged, animated model. A normal clone() would leave every copy pointing at the same skeleton, so they would all move as one. SkeletonUtils.clone copies the bones too, which lets every zombie play its own walk or idle animation with its own AnimationMixer.
We also clone each material, so making one zombie glow does not light up all five. New to models? Start with animating 3D GLTF models, or pick plain cubes first in selecting a 3D cube with raycasters.
Step-by-Step Code Breakdown
Every excerpt below is copied exactly from the full code further down.
1. Set up the scene, camera, renderer and orbit controls
In plain words: we make a dark world with a little fog, a camera, a renderer with shadows, and mouse controls to look around. Create a Scene with fog, a PerspectiveCamera, a WebGLRenderer with shadow maps, and OrbitControls attached to renderer.domElement with damping and limits.
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x04050a);
scene.fog = new THREE.Fog(0x04050a, 18, 45);
const camera = new THREE.PerspectiveCamera(50, innerWidth / innerHeight, 0.1, 200);
camera.position.set(0, 4, 12);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
document.body.style.margin = '0';
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1.2, 0);
controls.enableDamping = true;
controls.maxPolarAngle = Math.PI * 0.49;
controls.minDistance = 5;
controls.maxDistance = 25;
2. Add lights and a neon grid floor
In plain words: we light the scene from the sky and the sun, and lay down a dark floor with a glowing grid on top. Add a HemisphereLight and a shadow-casting DirectionalLight, a large dark PlaneGeometry floor, and a cyan GridHelper just above it.
scene.add(new THREE.HemisphereLight(0xbfd4ff, 0x202030, 1.6));
const sun = new THREE.DirectionalLight(0xffffff, 2.2);
sun.position.set(5, 10, 6);
sun.castShadow = true;
sun.shadow.camera.left = sun.shadow.camera.bottom = -10;
sun.shadow.camera.right = sun.shadow.camera.top = 10;
scene.add(sun);
const floor = new THREE.Mesh(new THREE.PlaneGeometry(200, 200), new THREE.MeshStandardMaterial({ color: 0x07080f, roughness: 0.9 }));
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(80, 40, 0x2ad1d6, 0x1c2a44);
grid.position.y = 0.01;
scene.add(grid);
3. Load the Minecraft zombie and clone five copies
In plain words: we load the zombie file once, measure it, then make five independent animated copies in a half circle. Load the GLB with GLTFLoader, size it from a Box3, clone it with SkeletonUtils.clone, clone each material, tag every mesh with userData.zombieIndex, and play walk or idle clips with an AnimationMixer.
const zombies = [];
const loader = new GLTFLoader();
loader.load(MODEL_URL, (gltf) => {
const box = new THREE.Box3().setFromObject(gltf.scene);
const scale = 2.2 / (box.max.y - box.min.y);
const walk = gltf.animations.find((a) => /walk/i.test(a.name)) || gltf.animations[0];
const idle = gltf.animations.find((a) => /idle/i.test(a.name)) || walk;
for (let i = 0; i < 5; i++) {
const model = SkeletonUtils.clone(gltf.scene);
model.scale.setScalar(scale);
const angle = (i - 2) * 0.42;
model.position.set(Math.sin(angle) * 6, -box.min.y * scale, -Math.cos(angle) * 6 + 5);
model.rotation.y = -angle;
model.traverse((child) => {
if (child.isMesh) {
child.material = child.material.clone();
child.castShadow = true;
child.userData.zombieIndex = i;
}
});
const mixer = new THREE.AnimationMixer(model);
const action = mixer.clipAction(i % 2 ? walk : idle);
action.time = Math.random() * action.getClip().duration;
action.play();
scene.add(model);
// Invisible box proxy: skinned-mesh bounds do not follow the animation, so pick against this.
const hitbox = new THREE.Mesh(
new THREE.BoxGeometry(1.3 / scale, 2.4 / scale, 1.0 / scale),
new THREE.MeshBasicMaterial({ visible: false })
);
hitbox.position.y = box.min.y + 1.2 / scale;
hitbox.userData.zombieIndex = i;
model.add(hitbox);
zombies.push({ model, mixer, hitbox, name: 'Zombie ' + (i + 1), baseY: model.position.y, hop: 0 });
}
new THREE.TextureLoader().load('/images/za-zombie-rotten-townsfolk.png?rev=20261113a', (tex) => {
tex.flipY = false;
tex.magFilter = THREE.NearestFilter;
tex.minFilter = THREE.NearestFilter;
if ('colorSpace' in tex && THREE.SRGBColorSpace) tex.colorSpace = THREE.SRGBColorSpace;
for (const z of zombies) {
z.model.traverse((child) => {
if (child.isMesh && child.material && child.material.map) {
child.material.map = tex;
child.material.needsUpdate = true;
}
});
}
});
});
4. Make a selection ring and an on-screen label
In plain words: we make a green ring to sit under the chosen zombie and a small text box that says who is selected. Build a flat RingGeometry with a MeshBasicMaterial, hide it until something is selected, and add a fixed-position div label to the page.
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.75, 0.95, 48),
new THREE.MeshBasicMaterial({ color: 0x39ff14, transparent: true, opacity: 0.9, side: THREE.DoubleSide })
);
ring.rotation.x = -Math.PI / 2;
ring.visible = false;
scene.add(ring);
const label = document.createElement('div');
label.style.cssText = 'position:fixed;left:16px;bottom:16px;padding:10px 16px;border-radius:10px;background:rgba(0,0,0,0.65);color:#39ff14;font:600 15px system-ui,sans-serif;border:1px solid rgba(57,255,20,0.4);pointer-events:none;z-index:1100;';
label.textContent = 'Click or tap a zombie to select it';
document.body.appendChild(label);
5. Highlight the selected model
In plain words: selecting a zombie turns off the old glow, makes the new one glow green, starts a little hop and updates the label. Walk every mesh with traverse to set emissive color and intensity, remember the selected zombie, show the ring and update the label text.
let selected = null;
function setGlow(zombie, color, amount) {
zombie.model.traverse((child) => {
if (child.isMesh && child.material.emissive) { // skips the invisible hitbox
child.material.emissive.setHex(color);
child.material.emissiveIntensity = amount;
}
});
}
function selectZombie(zombie) {
if (selected) setGlow(selected, 0x000000, 0);
selected = zombie;
if (!zombie) { ring.visible = false; label.textContent = 'Nothing selected'; return; }
setGlow(zombie, 0x39ff14, 0.45);
zombie.hop = 1;
ring.visible = true;
label.textContent = 'Selected: ' + zombie.name;
}
6. Raycast on click or tap
In plain words: when you let go of the mouse or lift your finger, we shoot a ray through that spot and select whatever zombie it hits first. Track each pointer by id, ignore drags longer than 6 pixels (14 for a finger) and multi-touch, convert the pointer to normalized device coordinates using the canvas rect, call setFromCamera, test the invisible hitbox proxies, and read zombieIndex from the first hit.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const canvas = renderer.domElement;
const downs = new Map(); // pointerId -> where it went down
let lastClick = -10;
canvas.style.touchAction = 'none';
canvas.addEventListener('pointerdown', (e) => { downs.set(e.pointerId, { x: e.clientX, y: e.clientY }); });
canvas.addEventListener('pointercancel', (e) => { downs.delete(e.pointerId); });
canvas.addEventListener('pointerup', (e) => {
const start = downs.get(e.pointerId);
downs.delete(e.pointerId);
if (!start || downs.size) return; // ignore pinch / multi-touch
const slop = e.pointerType === 'mouse' ? 6 : 14; // fingers wobble more than mice
if (Math.hypot(e.clientX - start.x, e.clientY - start.y) > slop) return;
const rect = canvas.getBoundingClientRect();
pointer.set(((e.clientX - rect.left) / rect.width) * 2 - 1, -((e.clientY - rect.top) / rect.height) * 2 + 1);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(zombies.map((z) => z.hitbox), false);
selectZombie(hits.length ? zombies[hits[0].object.userData.zombieIndex] : null);
lastClick = clock.elapsedTime;
});
7. Animate, hop and auto-pick in the render loop
In plain words: every frame the zombies animate, the chosen one hops and its ring pulses, and if nobody has clicked for a while the demo picks one for you. Update each AnimationMixer, ease the hop with a sine, pulse the ring scale, auto-select a random zombie every 2.5 seconds when idle, update controls and render.
const clock = new THREE.Clock();
let autoTimer = 0;
function render() {
requestAnimationFrame(render);
const dt = Math.min(clock.getDelta(), 0.05);
const t = clock.elapsedTime;
for (const z of zombies) {
z.mixer.update(dt);
z.hop = Math.max(0, z.hop - dt * 1.6);
z.model.position.y = z.baseY + Math.sin(z.hop * Math.PI) * 0.8;
}
if (selected) {
ring.position.set(selected.model.position.x, 0.03, selected.model.position.z);
ring.scale.setScalar(1 + Math.sin(t * 5) * 0.08);
}
autoTimer += dt;
if (zombies.length && t - lastClick > 8 && autoTimer > 2.5) {
autoTimer = 0;
selectZombie(zombies[Math.floor(Math.random() * zombies.length)]);
}
controls.update();
renderer.render(scene, camera);
}
render();
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
Using pointerup with a small movement check means orbiting the camera never selects by accident. Ready for more? Try selecting multiple 3D objects or build a full Minecraft-style zombie FPS.
The Full Code
Paste this module into a page with the same import map as this one (Three.js 0.160 from unpkg). It loads the zombie from /models/gltf/minecraft-zombie/zombie-001.glb on this site; if you run it on your own server, download that GLB and point MODEL_URL at your copy.
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js';
const MODEL_URL = '/models/gltf/minecraft-zombie/zombie-001.glb';
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x04050a);
scene.fog = new THREE.Fog(0x04050a, 18, 45);
const camera = new THREE.PerspectiveCamera(50, innerWidth / innerHeight, 0.1, 200);
camera.position.set(0, 4, 12);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
document.body.style.margin = '0';
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1.2, 0);
controls.enableDamping = true;
controls.maxPolarAngle = Math.PI * 0.49;
controls.minDistance = 5;
controls.maxDistance = 25;
scene.add(new THREE.HemisphereLight(0xbfd4ff, 0x202030, 1.6));
const sun = new THREE.DirectionalLight(0xffffff, 2.2);
sun.position.set(5, 10, 6);
sun.castShadow = true;
sun.shadow.camera.left = sun.shadow.camera.bottom = -10;
sun.shadow.camera.right = sun.shadow.camera.top = 10;
scene.add(sun);
const floor = new THREE.Mesh(new THREE.PlaneGeometry(200, 200), new THREE.MeshStandardMaterial({ color: 0x07080f, roughness: 0.9 }));
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(80, 40, 0x2ad1d6, 0x1c2a44);
grid.position.y = 0.01;
scene.add(grid);
const zombies = [];
const loader = new GLTFLoader();
loader.load(MODEL_URL, (gltf) => {
const box = new THREE.Box3().setFromObject(gltf.scene);
const scale = 2.2 / (box.max.y - box.min.y);
const walk = gltf.animations.find((a) => /walk/i.test(a.name)) || gltf.animations[0];
const idle = gltf.animations.find((a) => /idle/i.test(a.name)) || walk;
for (let i = 0; i < 5; i++) {
const model = SkeletonUtils.clone(gltf.scene);
model.scale.setScalar(scale);
const angle = (i - 2) * 0.42;
model.position.set(Math.sin(angle) * 6, -box.min.y * scale, -Math.cos(angle) * 6 + 5);
model.rotation.y = -angle;
model.traverse((child) => {
if (child.isMesh) {
child.material = child.material.clone();
child.castShadow = true;
child.userData.zombieIndex = i;
}
});
const mixer = new THREE.AnimationMixer(model);
const action = mixer.clipAction(i % 2 ? walk : idle);
action.time = Math.random() * action.getClip().duration;
action.play();
scene.add(model);
// Invisible box proxy: skinned-mesh bounds do not follow the animation, so pick against this.
const hitbox = new THREE.Mesh(
new THREE.BoxGeometry(1.3 / scale, 2.4 / scale, 1.0 / scale),
new THREE.MeshBasicMaterial({ visible: false })
);
hitbox.position.y = box.min.y + 1.2 / scale;
hitbox.userData.zombieIndex = i;
model.add(hitbox);
zombies.push({ model, mixer, hitbox, name: 'Zombie ' + (i + 1), baseY: model.position.y, hop: 0 });
}
new THREE.TextureLoader().load('/images/za-zombie-rotten-townsfolk.png?rev=20261113a', (tex) => {
tex.flipY = false;
tex.magFilter = THREE.NearestFilter;
tex.minFilter = THREE.NearestFilter;
if ('colorSpace' in tex && THREE.SRGBColorSpace) tex.colorSpace = THREE.SRGBColorSpace;
for (const z of zombies) {
z.model.traverse((child) => {
if (child.isMesh && child.material && child.material.map) {
child.material.map = tex;
child.material.needsUpdate = true;
}
});
}
});
});
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.75, 0.95, 48),
new THREE.MeshBasicMaterial({ color: 0x39ff14, transparent: true, opacity: 0.9, side: THREE.DoubleSide })
);
ring.rotation.x = -Math.PI / 2;
ring.visible = false;
scene.add(ring);
const label = document.createElement('div');
label.style.cssText = 'position:fixed;left:16px;bottom:16px;padding:10px 16px;border-radius:10px;background:rgba(0,0,0,0.65);color:#39ff14;font:600 15px system-ui,sans-serif;border:1px solid rgba(57,255,20,0.4);pointer-events:none;z-index:1100;';
label.textContent = 'Click or tap a zombie to select it';
document.body.appendChild(label);
let selected = null;
function setGlow(zombie, color, amount) {
zombie.model.traverse((child) => {
if (child.isMesh && child.material.emissive) { // skips the invisible hitbox
child.material.emissive.setHex(color);
child.material.emissiveIntensity = amount;
}
});
}
function selectZombie(zombie) {
if (selected) setGlow(selected, 0x000000, 0);
selected = zombie;
if (!zombie) { ring.visible = false; label.textContent = 'Nothing selected'; return; }
setGlow(zombie, 0x39ff14, 0.45);
zombie.hop = 1;
ring.visible = true;
label.textContent = 'Selected: ' + zombie.name;
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const canvas = renderer.domElement;
const downs = new Map(); // pointerId -> where it went down
let lastClick = -10;
canvas.style.touchAction = 'none';
canvas.addEventListener('pointerdown', (e) => { downs.set(e.pointerId, { x: e.clientX, y: e.clientY }); });
canvas.addEventListener('pointercancel', (e) => { downs.delete(e.pointerId); });
canvas.addEventListener('pointerup', (e) => {
const start = downs.get(e.pointerId);
downs.delete(e.pointerId);
if (!start || downs.size) return; // ignore pinch / multi-touch
const slop = e.pointerType === 'mouse' ? 6 : 14; // fingers wobble more than mice
if (Math.hypot(e.clientX - start.x, e.clientY - start.y) > slop) return;
const rect = canvas.getBoundingClientRect();
pointer.set(((e.clientX - rect.left) / rect.width) * 2 - 1, -((e.clientY - rect.top) / rect.height) * 2 + 1);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(zombies.map((z) => z.hitbox), false);
selectZombie(hits.length ? zombies[hits[0].object.userData.zombieIndex] : null);
lastClick = clock.elapsedTime;
});
const clock = new THREE.Clock();
let autoTimer = 0;
function render() {
requestAnimationFrame(render);
const dt = Math.min(clock.getDelta(), 0.05);
const t = clock.elapsedTime;
for (const z of zombies) {
z.mixer.update(dt);
z.hop = Math.max(0, z.hop - dt * 1.6);
z.model.position.y = z.baseY + Math.sin(z.hop * Math.PI) * 0.8;
}
if (selected) {
ring.position.set(selected.model.position.x, 0.03, selected.model.position.z);
ring.scale.setScalar(1 + Math.sin(t * 5) * 0.08);
}
autoTimer += dt;
if (zombies.length && t - lastClick > 8 && autoTimer > 2.5) {
autoTimer = 0;
selectZombie(zombies[Math.floor(Math.random() * zombies.length)]);
}
controls.update();
renderer.render(scene, camera);
}
render();
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
Frequently Asked Questions
How do you select a 3D object with the mouse in Three.js?
Convert the click to normalized device coordinates, call raycaster.setFromCamera(pointer, camera), then raycaster.intersectObjects(objects, true). The first entry in the result is the closest object under the mouse.
Why does the raycaster return a child mesh instead of my model?
GLTF models are made of many meshes in a hierarchy. Raycasting tests each mesh, so you get the arm or head you hit. Store an id in userData on every mesh, or walk up the parent chain, to find the model it belongs to.
How do I clone an animated GLTF model in Three.js?
Use SkeletonUtils.clone from three/addons/utils/SkeletonUtils.js. It copies the skeleton and bones so each copy can play its own animation with its own AnimationMixer. A plain clone() shares bones and breaks skinned animation.
How do I highlight a selected model?
Clone the materials per model so they are not shared, then set material.emissive to a color and raise emissiveIntensity on every mesh of the selected model. Reset the previous selection to black first.
How do I stop orbit drags from selecting objects?
Record the pointer position on pointerdown and only raycast on pointerup if it moved fewer than a few pixels. That separates a click from a drag.
Why do I need to sign in to launch the live demo?
The tutorial and its full code are free to read. The full-screen Live Demo asks for a free Google sign-in first, the same gate used across the site.
Play It and Keep Learning
Blocky Minecraft-style models and raycast picking power Zombie Attack, a free Three.js browser FPS and Super Soldier Battle. Browse every Three.js tutorial, read the articles, or search the site.
Need Three.js or WebGL work for your site or game? I take freelance and contract work: contact me.
Play Game