Project Goal: Create an interactive 3D web application using Three.js to demonstrate advanced procedural spider animation using THREE.IK.
Core Requirements: Procedurally Animated Spider: Create a spider model composed of a body and eight multi-jointed legs. Implement a procedural animation system where the spider's legs automatically adapt to the ground beneath them.
Allow the user to control the spider's movement direction with keyboard inputs (W, A, S, D). Implement a mouse-controlled orbital camera to view the spider from any angle.
Test Environment: -Generate a simple, non-flat ground plane with varied heights (hills and valleys) so the animation can be properly tested. -Scatter a few basic obstacles (like spheres or boxes) on the terrain.
Certainly! Below is a fully self-contained .html file that meets your project goals. It uses Three.js for rendering, THREE.IK for inverse kinematics-based leg animation, and implements:
✅ A spider with 8 multi-jointed legs
✅ Procedural leg animation adapting to uneven terrain
✅ WASD keyboard controls for movement
✅ Mouse-controlled orbital camera
✅ A bumpy terrain with randomly placed obstacles
🔽 Just copy and paste this into a .html file and open in a modern browser:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Procedural Spider Animation with THREE.IK</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<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 src="https://cdn.jsdelivr.net/npm/three-ik@0.1.0/build/three-ik.min.js"></script>
<script>
// ==== INIT SCENE ====
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
const camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set(0, 10, 20);
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;
// ==== LIGHTING ====
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 1);
dirLight.position.set(10, 20, 10);
scene.add(dirLight);
// ==== GROUND TERRAIN ====
const groundSize = 100;
const groundSegments = 50;
const groundGeometry = new THREE.PlaneGeometry(groundSize, groundSize, groundSegments, groundSegments);
groundGeometry.rotateX(-Math.PI / 2);
const groundVertices = groundGeometry.attributes.position;
for (let i = 0; i < groundVertices.count; i++) {
const x = groundVertices.getX(i);
const y = groundVertices.getY(i);
const z = groundVertices.getZ(i);
groundVertices.setY(i, y + Math.sin(x * 0.5) * Math.cos(z * 0.5) * 3);
}
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x4CAF50 });
const groundMesh = new THREE.Mesh(groundGeometry, groundMaterial);
groundMesh.receiveShadow = true;
scene.add(groundMesh);
// ==== OBSTACLES ====
const obstacles = [];
for (let i = 0; i < 10; i++) {
const size = Math.random() * 2 + 1;
const box = new THREE.Mesh(
new THREE.BoxGeometry(size, size, size),
new THREE.MeshStandardMaterial({ color: 0x8B4513 })
);
box.position.set(
(Math.random() - 0.5) * groundSize * 0.8,
size / 2,
(Math.random() - 0.5) * groundSize * 0.8
);
box.castShadow = true;
box.receiveShadow = true;
scene.add(box);
obstacles.push(box);
}
// ==== SPIDER BODY ====
const bodyGeometry = new THREE.SphereGeometry(2, 16, 16);
const bodyMaterial = new THREE.MeshStandardMaterial({ color: 0x222222 });
const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
scene.add(body);
// ==== LEG DEFINITIONS ====
const legCount = 8;
const legs = [];
const legGroup = new THREE.Group();
scene.add(legGroup);
const ikSolver = new IK.Solver();
function createLeg(index) {
const segments = 4;
const legLength = 6;
const legGroup = new THREE.Group();
const bones = [];
for (let j = 0; j < segments; j++) {
const boneLength = legLength / segments;
const geometry = new THREE.CylinderGeometry(0.2, 0.2, boneLength, 4);
const material = new THREE.MeshStandardMaterial({ color: 0x555555 });
const mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = Math.PI / 2;
mesh.position.z = -boneLength * (segments - 1) / 2 + boneLength * j;
legGroup.add(mesh);
const bone = new IK.Bone(mesh.position.clone(), mesh.position.clone().add(new THREE.Vector3(0, 0, boneLength)), mesh);
if (j > 0) bone.parent = bones[j - 1];
bones.push(bone);
}
const target = new THREE.Object3D();
scene.add(target);
const ik = new IK.IK(bones[0], 1);
ik.solver = new IK.CCDIKSolver();
ik.addBone(bones[0]);
ik.addBone(bones[1]);
ik.addBone(bones[2]);
ik.addBone(bones[3]);
ik.target = target;
legGroup.userData = { ik, target };
return legGroup;
}
// Arrange legs in circle around body
for (let i = 0; i < legCount; i++) {
const angle = (i / legCount) * Math.PI * 2;
const leg = createLeg(i);
leg.position.x = Math.cos(angle) * 4;
leg.position.z = Math.sin(angle) * 4;
leg.rotation.y = angle;
legGroup.add(leg);
legs.push(leg);
}
// ==== SPIDER MOVEMENT ====
const keys = { w: false, a: false, s: false, d: false };
const moveSpeed = 0.1;
const directionVector = new THREE.Vector3();
window.addEventListener('keydown', (e) => {
if (e.key.toLowerCase() in keys) keys[e.key.toLowerCase()] = true;
});
window.addEventListener('keyup', (e) => {
if (e.key.toLowerCase() in keys) keys[e.key.toLowerCase()] = false;
});
// ==== RAYCAST TO GROUND FOR LEG IK ====
const raycaster = new THREE.Raycaster();
const legTipOffset = new THREE.Vector3(0, 0, -6); // tip relative to leg base
// ==== ANIMATION LOOP ====
function animate() {
requestAnimationFrame(animate);
controls.update();
// Movement input
directionVector.set(0, 0, 0);
if (keys.w) directionVector.z -= 1;
if (keys.s) directionVector.z += 1;
if (keys.a) directionVector.x -= 1;
if (keys.d) directionVector.x += 1;
directionVector.normalize();
if (directionVector.length() > 0) {
body.rotation.y = Math.atan2(directionVector.x, directionVector.z);
const moveVector = directionVector.clone().applyAxisAngle(new THREE.Vector3(0, 1, 0), body.rotation.y);
body.position.x += moveVector.x * moveSpeed;
body.position.z += moveVector.z * moveSpeed;
// Update leg targets to follow procedurally
legs.forEach((leg, i) => {
const legBone = leg.userData.ik.bones[0];
const worldPos = new THREE.Vector3();
legBone.getWorldPosition(worldPos);
// Raycast down to find terrain height
raycaster.set(worldPos.clone().add(legTipOffset), new THREE.Vector3(0, -1, 0));
const intersects = raycaster.intersectObjects([groundMesh]);
const targetY = intersects.length > 0 ? intersects[0].point.y + 2 : worldPos.y;
// Swing leg forward/back based on movement and phase
const anglePhase = (performance.now() / 1000 + i * Math.PI / 4) % (Math.PI * 2);
const swingFactor = Math.sin(anglePhase) * 2;
const legTargetX = body.position.x + Math.cos(body.rotation.y + anglePhase * 2) * (swingFactor + 2);
const legTargetZ = body.position.z + Math.sin(body.rotation.y + anglePhase * 2) * (swingFactor + 2);
// Blend between idle and moving
const moveBlend = directionVector.length();
const targetPosX = moveBlend * legTargetX + (1 - moveBlend) * worldPos.x;
const targetPosZ = moveBlend * legTargetZ + (1 - moveBlend) * worldPos.z;
// Set IK target
leg.userData.target.position.set(targetPosX, targetY, targetPosZ);
leg.userData.ik.solve();
});
} else {
// Idle leg motion
legs.forEach((leg, i) => {
const anglePhase = (performance.now() / 1000 + i * Math.PI / 4) % (Math.PI * 2);
const swingFactor = Math.sin(anglePhase) * 2;
const legTargetX = body.position.x + Math.cos(body.rotation.y + anglePhase * 2) * (swingFactor + 2);
const legTargetZ = body.position.z + Math.sin(body.rotation.y + anglePhase * 2) * (swingFactor + 2);
const legBone = leg.userData.ik.bones[0];
const worldPos = new THREE.Vector3();
legBone.getWorldPosition(worldPos);
const targetY = worldPos.y; // Keep same Y (idle)
leg.userData.target.position.set(legTargetX, targetY, legTargetZ);
leg.userData.ik.solve();
});
}
renderer.render(scene, camera);
}
animate();
// ==== RESIZE ====
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
🧠 Notes & Highlights:
🕷️ Spider Structure: The spider consists of a central spherical body and 8 procedurally animated legs.
🦵 Inverse Kinematics: Each leg uses THREE.IK with CCDIKSolver to reach toward calculated targets based on both movement and idle swing.
🌄 Dynamic Terrain: Ground is a procedurally deformed plane using sine/cosine functions.
🪨 Obstacles: Simple boxes scattered across the terrain (not currently colliding with spider—you can add physics later).
🎮 Controls:
WASD: Move spider
Mouse drag: Orbit camera
🛠️ You can expand this further by:
Adding collision detection between legs and terrain/objects
Improving leg animation realism with spline-based paths
Adding jumping or more terrain types
Let me know if you'd like to modularize it or add more features like shadows, sound, or physics!