Project Goal: Create an interactive 3D web application using Three.js to demonstrate advanced procedural spider animation.
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.
Output as one unified .html file.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a complete interactive 3D web application featuring a procedurally animated spider that adapts to uneven terrain. The spider has 8 multi-jointed legs that automatically adjust to the ground surface, and you can control its movement with keyboard inputs (WASD) while viewing from any angle with an orbital camera.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Procedural Spider Animation</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #f0f0f0;
}
canvas {
display: block;
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background-color: rgba(0,0,0,0.5);
padding: 10px;
font-family: Arial, sans-serif;
border-radius: 5px;
}
</style>
</head>
<body>
<div id="info">
<h2>Procedural Spider Animation</h2>
<p>Controls: WASD to move, Mouse to rotate camera</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/examples/js/controls/OrbitControls.js"></script>
<script>
// Scene setup
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
// Camera setup
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 5, 10);
// Renderer setup
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// Orbit controls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(5, 10, 7);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
scene.add(directionalLight);
// Ground plane with varied heights
const groundGeometry = new THREE.PlaneGeometry(50, 50, 50, 50);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x3a5f0b,
roughness: 0.8,
metalness: 0.2
});
// Create a height map for the ground
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
// Add some noise to the ground
const positions = groundGeometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const z = positions.getZ(i);
const height = Math.sin(x * 0.2) * Math.cos(z * 0.2) * 2 + Math.random() * 2 - 1;
positions.setY(i, height);
}
positions.needsUpdate = true;
scene.add(ground);
// Add some obstacles
const obstacles = [];
const obstacleGeometry = new THREE.SphereGeometry(1, 16, 16);
const obstacleMaterial = new THREE.MeshStandardMaterial({
color: 0x8b4513,
roughness: 0.7
});
for (let i = 0; i < 10; i++) {
const obstacle = new THREE.Mesh(obstacleGeometry, obstacleMaterial);
obstacle.position.set(
(Math.random() - 0.5) * 30,
Math.random() * 2,
(Math.random() - 0.5) * 30
);
obstacle.castShadow = true;
obstacle.receiveShadow = true;
scene.add(obstacle);
obstacles.push(obstacle);
}
// Spider class
class Spider {
constructor() {
this.body = new THREE.Group();
this.legs = [];
this.bodyPosition = new THREE.Vector3(0, 0, 0);
this.targetPosition = new THREE.Vector3(0, 0, 0);
this.speed = 0.1;
this.legLength = 3;
this.legSegments = 3;
this.legWidth = 0.2;
this.legColor = 0x3a3a3a;
// Create body
const bodyGeometry = new THREE.SphereGeometry(1, 16, 16);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0x5a2d0c,
roughness: 0.5,
metalness: 0.3
});
this.bodyMesh = new THREE.Mesh(bodyGeometry, bodyMaterial);
this.bodyMesh.castShadow = true;
this.bodyMesh.receiveShadow = true;
this.body.add(this.bodyMesh);
// Create legs
for (let i = 0; i < 8; i++) {
const leg = this.createLeg(i);
this.legs.push(leg);
this.body.add(leg.group);
}
scene.add(this.body);
}
createLeg(index) {
const leg = {
group: new THREE.Group(),
segments: [],
targetAngles: [0, 0, 0],
currentAngles: [0, 0, 0],
basePosition: new THREE.Vector3(),
targetPosition: new THREE.Vector3(),
groundPosition: new THREE.Vector3()
};
// Calculate base position for each leg
const angle = (index * Math.PI * 2) / 8;
const radius = 1.5;
leg.basePosition.set(
Math.cos(angle) * radius,
0,
Math.sin(angle) * radius
);
// Create leg segments
for (let i = 0; i < this.legSegments; i++) {
const segment = new THREE.Group();
const geometry = new THREE.CylinderGeometry(
this.legWidth * (0.8 - i * 0.1),
this.legWidth * (0.8 - i * 0.1),
this.legLength / this.legSegments,
8
);
const material = new THREE.MeshStandardMaterial({
color: this.legColor,
roughness: 0.7
});
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
segment.add(mesh);
leg.segments.push(segment);
leg.group.add(segment);
}
return leg;
}
update(deltaTime) {
// Move body toward target position
this.bodyPosition.lerp(this.targetPosition, this.speed * deltaTime);
this.body.position.copy(this.bodyPosition);
// Update each leg
for (let i = 0; i < this.legs.length; i++) {
const leg = this.legs[i];
// Calculate target position for the leg's foot
this.calculateLegTargetPosition(leg, i);
// Update leg angles to reach the target position
this.updateLegAngles(leg, deltaTime);
// Position the leg segments
this.positionLegSegments(leg);
}
}
calculateLegTargetPosition(leg, index) {
// Raycast to find ground position
const rayOrigin = new THREE.Vector3(
this.bodyPosition.x + leg.basePosition.x,
this.bodyPosition.y + 2, // Start above the body
this.bodyPosition.z + leg.basePosition.z
);
const rayDirection = new THREE.Vector3(0, -1, 0);
const ray = new THREE.Raycaster(rayOrigin, rayDirection);
const intersects = ray.intersectObjects([ground]);
if (intersects.length > 0) {
leg.groundPosition.copy(intersects[0].point);
} else {
// If no ground found, place foot at a reasonable position
leg.groundPosition.set(
this.bodyPosition.x + leg.basePosition.x,
this.bodyPosition.y - 2,
this.bodyPosition.z + leg.basePosition.z
);
}
// Calculate target position for the leg's foot
leg.targetPosition.copy(leg.groundPosition);
// Add some randomness to make it look more natural
const randomOffset = new THREE.Vector3(
(Math.random() - 0.5) * 0.5,
0,
(Math.random() - 0.5) * 0.5
);
leg.targetPosition.add(randomOffset);
}
updateLegAngles(leg, deltaTime) {
// Simple IK to position the leg
const target = leg.targetPosition;
const base = this.bodyPosition.clone().add(leg.basePosition);
// Calculate direction from base to target
const direction = new THREE.Vector3()
.subVectors(target, base)
.normalize();
// Calculate angles for each joint
for (let i = 0; i < this.legSegments; i++) {
const segmentLength = this.legLength / this.legSegments;
const remainingLength = direction.clone().length();
if (remainingLength > segmentLength) {
const angle = Math.acos(
(remainingLength * remainingLength + segmentLength * segmentLength - this.legWidth * this.legWidth) /
(2 * remainingLength * segmentLength)
);
// Update target angle
leg.targetAngles[i] = angle;
} else {
leg.targetAngles[i] = 0;
}
// Smoothly interpolate to target angle
leg.currentAngles[i] += (leg.targetAngles[i] - leg.currentAngles[i]) * 0.1 * deltaTime;
}
}
positionLegSegments(leg) {
const base = this.bodyPosition.clone().add(leg.basePosition);
let currentPosition = base.clone();
let currentDirection = new THREE.Vector3(0, -1, 0); // Start pointing down
for (let i = 0; i < this.legSegments; i++) {
const segment = leg.segments[i];
const segmentLength = this.legLength / this.legSegments;
// Rotate the segment
segment.rotation.x = leg.currentAngles[i];
segment.rotation.z = (Math.random() - 0.5) * 0.2; // Add some side-to-side movement
// Position the segment
const nextPosition = new THREE.Vector3()
.copy(currentDirection)
.multiplyScalar(segmentLength)
.add(currentPosition);
segment.position.copy(nextPosition);
segment.position.sub(currentPosition);
// Update for next segment
currentPosition.copy(nextPosition);
currentDirection = new THREE.Vector3()
.copy(segment.position)
.normalize();
}
}
move(direction) {
this.targetPosition.add(direction);
}
}
// Create spider
const spider = new Spider();
// Animation loop
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const deltaTime = clock.getDelta();
// Update spider
spider.update(deltaTime);
// Update controls
controls.update();
// Render scene
renderer.render(scene, camera);
}
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Keyboard controls
const keys = {
w: false,
a: false,
s: false,
d: false
};
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;
}
});
// Movement direction
const direction = new THREE.Vector3();
function updateMovement() {
direction.set(0, 0, 0);
if (keys.w) direction.z -= 1;
if (keys.s) direction.z += 1;
if (keys.a) direction.x -= 1;
if (keys.d) direction.x += 1;
// Normalize diagonal movement
if (direction.length() > 0) {
direction.normalize();
}
// Apply movement
if (direction.length() > 0) {
spider.move(direction);
}
}
// Add movement update to animation loop
const originalAnimate = animate;
animate = function() {
originalAnimate();
updateMovement();
};
// Start animation
animate();
</script>
</body>
</html>
Features Implemented:
Procedural Spider Animation:
8 multi-jointed legs with 3 segments each
Legs automatically adapt to the ground surface using raycasting
Simple inverse kinematics for leg movement
User Controls:
WASD keys for movement in 4 directions
Mouse-controlled orbital camera for 360° viewing
Test Environment:
Non-flat ground plane with hills and valleys
Scattered obstacles (spheres) on the terrain
Proper lighting and shadows for better visualization
Visual Elements:
Realistic spider body and legs with proper materials
Ambient and directional lighting
Shadow casting for better depth perception
The spider's legs will automatically adjust to the terrain as it moves, demonstrating the procedural animation system. The camera can be rotated with the mouse to view the spider from any angle.