Below is a single, self-contained HTML file you can save as “spider.html” and open in any modern browser.
It pulls Three.js from a CDN, generates a wavy test-terrain, drops a few obstacles, and spawns a procedurally animated eight-legged spider you can drive around with W A S D while orbiting the camera with the mouse.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Procedural Spider – Three.js Demo</title>
<style>
html,body{margin:0;height:100%;overflow:hidden;background:#000;}
#info{
position:fixed;top:10px;left:10px;color:#fff;font-family:monospace;
background:rgba(0,0,0,.4);padding:8px 12px;border-radius:4px;
}
</style>
</head>
<body>
<div id="info">
W A S D – move<br/>
Mouse – orbit / zoom
</div>
<script type="module">
/* ──────────────────────────────────────────────────────────────
Imports
─────────────────────────────────────────────────────────── */
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.158/build/three.module.js';
import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.158/examples/jsm/controls/OrbitControls.js';
/* ──────────────────────────────────────────────────────────────
Basic Scene Setup
─────────────────────────────────────────────────────────── */
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x202020);
const camera = new THREE.PerspectiveCamera(60,innerWidth/innerHeight,0.1,1000);
camera.position.set(0,25,35);
const renderer= new THREE.WebGLRenderer({antialias:true});
renderer.setSize(innerWidth,innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera,renderer.domElement);
controls.enablePan = false;
controls.target.set(0,5,0);
/* ──────────────────────────────────────────────────────────────
Lights
─────────────────────────────────────────────────────────── */
const hemi = new THREE.HemisphereLight(0xffffff,0x444444,0.6);
scene.add(hemi);
const dir = new THREE.DirectionalLight(0xffffff,0.8);
dir.position.set(60,80,40);
dir.castShadow = true;
dir.shadow.mapSize.set(2048,2048);
dir.shadow.camera.near = 10;
dir.shadow.camera.far = 200;
dir.shadow.camera.left = -60;
dir.shadow.camera.right = 60;
dir.shadow.camera.top = 60;
dir.shadow.camera.bottom= -60;
scene.add(dir);
/* ──────────────────────────────────────────────────────────────
Terrain
─────────────────────────────────────────────────────────── */
const terrainSize = 250;
const terrainDiv = 200;
const tGeo = new THREE.PlaneGeometry(terrainSize,terrainSize,terrainDiv,terrainDiv);
tGeo.rotateX(-Math.PI/2);
const pos = tGeo.attributes.position;
for(let i=0;i<pos.count;i++){
const x = pos.getX(i);
const z = pos.getZ(i);
// Simple height function (could swap for perlin noise)
const h = Math.sin(x*0.06)*6 + Math.cos(z*0.07)*5 + Math.sin((x+z)*0.13)*2;
pos.setY(i,h);
}
tGeo.computeVertexNormals();
const tMat = new THREE.MeshStandardMaterial({color:0x556b2f,side:THREE.DoubleSide});
const terrain = new THREE.Mesh(tGeo,tMat);
terrain.receiveShadow=true;
scene.add(terrain);
/* ──────────────────────────────────────────────────────────────
Obstacles
─────────────────────────────────────────────────────────── */
const obstacles = new THREE.Group();
scene.add(obstacles);
const rayTemp = new THREE.Raycaster();
for(let i=0;i<20;i++){
const isBox = Math.random()<0.5;
const size = THREE.MathUtils.randFloat(2,5);
const geom = isBox
? new THREE.BoxGeometry(size,size,size)
: new THREE.SphereGeometry(size*0.5,18,18);
const mat = new THREE.MeshStandardMaterial({color:isBox?0x8b4513:0x8888ff});
const o = new THREE.Mesh(geom,mat);
o.castShadow=true;
o.receiveShadow=true;
// pick a random XZ and drop onto ground
let x = THREE.MathUtils.randFloatSpread(terrainSize*0.8);
let z = THREE.MathUtils.randFloatSpread(terrainSize*0.8);
rayTemp.set(new THREE.Vector3(x,100,z), new THREE.Vector3(0,-1,0));
const hit = rayTemp.intersectObject(terrain)[0];
if(hit) o.position.set(x, hit.point.y + size/2, z);
obstacles.add(o);
}
/* ──────────────────────────────────────────────────────────────
Spider Class
─────────────────────────────────────────────────────────── */
class Spider extends THREE.Group{
constructor(){
super();
scene.add(this);
/* Body */
const bodyGeo = new THREE.SphereGeometry(3.5,20,16);
const bodyMat = new THREE.MeshStandardMaterial({color:0x111111});
this.bodyMesh = new THREE.Mesh(bodyGeo,bodyMat);
this.bodyMesh.castShadow=true;
this.add(this.bodyMesh);
/* Parameters */
this.speed = 20; // units per second
this.rise = 6; // body offset from ground
this.legCount = 8;
this.legLenUpper = 4;
this.legLenLower = 4;
this.stepRadius = 3.5;
this.stepHeight = 2.0;
this.stepTime = 0.15; // seconds
/* Helpers */
this.ray = new THREE.Raycaster();
/* Legs */
this.legs = [];
const segGeo = new THREE.CylinderGeometry(0.3,0.6,1,8);
segGeo.translate(0,-0.5,0); // pivot at top
const upperMat = new THREE.MeshStandardMaterial({color:0x222222});
const lowerMat = new THREE.MeshStandardMaterial({color:0x333333});
const anchors = [];
const rad = 4; // radial distance from body center
const ys = 1;
for(let i=0;i<this.legCount;i++){
const side = i<4?-1:1; // -1 left, +1 right
const idx = i%4;
const ang = THREE.MathUtils.lerp(-Math.PI/2*0.7,Math.PI/2*0.7,idx/3);
const ax = Math.cos(ang)*rad*side;
const az = Math.sin(ang)*rad;
anchors.push(new THREE.Vector3(ax,ys,az));
}
anchors.forEach((offset,i)=>{
const legGroup = new THREE.Group();
this.add(legGroup);
const upper = new THREE.Mesh(segGeo,upperMat);
const lower = new THREE.Mesh(segGeo,lowerMat);
upper.castShadow=lower.castShadow=true;
legGroup.add(upper);
legGroup.add(lower);
this.legs.push({
anchor: offset.clone(),
group : legGroup,
upper ,
lower ,
footTarget : new THREE.Vector3(),
prevTarget : new THREE.Vector3(),
stepping : false,
progress : 0
});
});
this.position.set(0,10,0);
this.updateFootTargets(true);
}
/* --- find ground Y under any X/Z ------------------------------------------------- */
groundHeightAt(x,z){
this.ray.set(new THREE.Vector3(x,100,z), new THREE.Vector3(0,-1,0));
const hit=this.ray.intersectObject(terrain)[0];
return hit?hit.point.y:0;
}
/* --- initial / rest foot placement ---------------------------------------------- */
updateFootTargets(first=false){
this.legs.forEach((leg,i)=>{
const worldAnchor = leg.anchor.clone().applyMatrix4(this.matrixWorld);
const dir = new THREE.Vector3().subVectors(worldAnchor,this.position).setY(0).normalize();
const desiredPos = worldAnchor.clone().add(dir.multiplyScalar(this.stepRadius));
desiredPos.y = this.groundHeightAt(desiredPos.x,desiredPos.z);
if(first){
leg.footTarget.copy(desiredPos);
}else if(!leg.stepping && leg.footTarget.distanceTo(desiredPos)>this.stepRadius*0.8){
// start step
leg.stepping = true;
leg.progress = 0;
leg.prevTarget.copy(leg.footTarget);
leg.newTarget = desiredPos.clone();
}
});
}
/* --- per-frame update ------------------------------------------------------------ */
update(delta,moveDir){
/* Move body */
if(moveDir.lengthSq()>0){
const move = moveDir.clone().setLength(this.speed*delta);
this.position.add(move);
}
/* Stick body to terrain */
const groundY = this.groundHeightAt(this.position.x,this.position.z);
this.position.y = groundY + this.rise;
/* Turn body toward heading */
if(moveDir.lengthSq()>0){
const targetYaw = Math.atan2(moveDir.x,moveDir.z);
this.rotation.y = THREE.MathUtils.lerpAngle(this.rotation.y,targetYaw,0.1);
}
/* Foot placement logic */
this.updateFootTargets(false);
/* Animate legs */
this.legs.forEach((leg)=>{
/* stepping animation */
if(leg.stepping){
leg.progress += delta/this.stepTime;
const p = THREE.MathUtils.clamp(leg.progress,0,1);
const foot = new THREE.Vector3().lerpVectors(leg.prevTarget,leg.newTarget,p);
foot.y += Math.sin(Math.PI*p)*this.stepHeight;
leg.footTarget.copy(foot);
if(p>=1){
leg.stepping=false;
}
}
/* segment transforms (very simple 2-segment faux IK) */
const root = leg.anchor.clone().applyMatrix4(this.matrixWorld);
const foot = leg.footTarget;
// knee heuristic: midpoint lifted up a bit to create bend
const mid = root.clone().lerp(foot,0.5);
mid.y += 1.5;
positionCylinder(leg.upper, root, mid);
positionCylinder(leg.lower, mid , foot);
});
}
}
/* Helper function: orient & scale a cylinder so that its Y-axis
runs between two points. */
function positionCylinder(cyl, start, end){
const vec = new THREE.Vector3().subVectors(end,start);
const len = vec.length();
cyl.scale.set(1,len,1);
cyl.position.copy(start);
cyl.lookAt(end);
cyl.rotateX(Math.PI/2);
}
/* ──────────────────────────────────────────────────────────────
Instantiate Spider
─────────────────────────────────────────────────────────── */
const spider = new Spider();
/* ──────────────────────────────────────────────────────────────
User Input
─────────────────────────────────────────────────────────── */
const keys = {};
addEventListener('keydown',e=>{keys[e.key.toLowerCase()]=true;});
addEventListener('keyup' ,e=>{keys[e.key.toLowerCase()]=false;});
/* ──────────────────────────────────────────────────────────────
Main Loop
─────────────────────────────────────────────────────────── */
let prevTime = performance.now()/1000;
function animate(){
requestAnimationFrame(animate);
const now = performance.now()/1000, dt=now-prevTime; prevTime=now;
/* build desired movement dir in world space, relative to camera heading */
const dir = new THREE.Vector3(
(keys['a']?-1:0)+(keys['d']?1:0),
0,
(keys['w']?-1:0)+(keys['s']?1:0)
);
if(dir.lengthSq()>0){
dir.normalize();
// rotate dir by camera yaw so W = forward from cam view
const eul = new THREE.Euler(0,camera.rotation.y,0);
dir.applyEuler(eul);
}
spider.update(dt,dir);
controls.update();
renderer.render(scene,camera);
}
animate();
/* ──────────────────────────────────────────────────────────────
Resize
─────────────────────────────────────────────────────────── */
addEventListener('resize',()=>{
camera.aspect=innerWidth/innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth,innerHeight);
});
</script>
</body>
</html>
What’s implemented?
• A wavy plane terrain (200×200 subdivisions) acts as non-flat ground.
• ~20 random spheres/boxes are scattered as obstacles.
• An eight-leg spider (body + 2-segment legs) lives at run-time in Spider class:
– Simple procedural stepping: each leg keeps a “comfort radius”; when the body moves far enough, that leg lifts, arcs upward, and places its foot at a new ray-cast ground position, adapting to slopes.
– Two cylinders per leg are re-oriented each frame to stretch between anchor-knee-foot, giving a jointed look.
• Keyboard (W A S D) moves the spider; movement direction is aligned with camera heading for intuitive control.
• Full mouse orbit / zoom courtesy of OrbitControls.
• Shadows, lighting, and basic materials are enabled for depth.
Save, open, and enjoy skittering your procedural arachnid around hills & obstacles!