<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Interactive 3D Spider Animation with THREE.IK</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three-ik@0.1.0/build/three-ik.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<script>
// Set up the scene
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 5, 10);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add lights
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
// Orbit Controls for mouse-controlled camera
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Smooth camera movement
// Generate terrain with varied heights
const terrainGeometry = new THREE.PlaneGeometry(20, 20, 32, 32); // Subdivided plane for hills
const positionAttribute = terrainGeometry.attributes.position;
for (let i = 0; i < positionAttribute.count; i++) {
const x = positionAttribute.getX(i);
const y = positionAttribute.getY(i);
const height = Math.sin(x * 0.5) * Math.cos(y * 0.5) * 1.5; // Simple hills and valleys
positionAttribute.setZ(i, height);
}
terrainGeometry.computeVertexNormals(); // For proper lighting
const terrainMaterial = new THREE.MeshLambertMaterial({ color: 0x228B22 }); // Green color
const terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.rotation.x = -Math.PI / 2; // Lay flat on XZ plane
scene.add(terrain);
// Add obstacles
const obstacleGeometry = new THREE.SphereGeometry(0.5, 16, 16);
const obstacleMaterial = new THREE.MeshLambertMaterial({ color: 0xFF0000 }); // Red color
const obstacle1 = new THREE.Mesh(obstacleGeometry, obstacleMaterial);
obstacle1.position.set(2, 0.5, 3); // Positioned on terrain
scene.add(obstacle1);
const obstacle2 = new THREE.Mesh(obstacleGeometry, obstacleMaterial);
obstacle2.position.set(-1, 0.5, -2);
scene.add(obstacle2);
// Create spider model
const bodyGeometry = new THREE.SphereGeometry(1, 16, 16);
const bodyMaterial = new THREE.MeshLambertMaterial({ color: 0x000000 }); // Black body
const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
body.position.set(0, 1, 0); // Start above ground
scene.add(body);
const legs = []; // Array to hold leg segments
const ikSolvers = []; // Array to hold IK solvers
const numLegs = 8;
const segmentsPerLeg = 3; // Each leg has 3 segments
const legLength = 2; // Total length per leg
for (let i = 0; i < numLegs; i++) {
const leg = []; // Array for each leg's segments
const angle = (i / numLegs) * Math.PI * 2; // Circular positioning
const basePosition = new THREE.Vector3(Math.cos(angle) * 1.2, 0, Math.sin(angle) * 1.2); // Relative to body
for (let j = 0; j < segmentsPerLeg; j++) {
const geometry = new THREE.CylinderGeometry(0.1, 0.1, legLength / segmentsPerLeg, 8);
const material = new THREE.MeshLambertMaterial({ color: 0x8B4513 }); // Brown color for legs
const segment = new THREE.Mesh(geometry, material);
segment.position.copy(basePosition).add(new THREE.Vector3(0, j * (legLength / segmentsPerLeg) / 2, 0)); // Initial positioning
segment.rotation.z = Math.PI / 2; // Orient cylinder along Y-axis
scene.add(segment);
leg.push(segment);
}
legs.push(leg);
// Set up IK for this leg
const chain = new THREE.IKChain();
for (let j = 0; j < segmentsPerLeg; j++) {
const bone = new THREE.IKBone(legs[i][j], legLength / segmentsPerLeg); // Create bone for each segment
chain.addBone(bone, legs[i][j].position); // Add bone to chain
}
const solver = new THREE.CCDIKSolver(chain); // Use CCD IK solver
ikSolvers.push(solver);
}
// Keyboard controls for spider movement
const keys = { w: false, a: false, s: false, d: false };
document.addEventListener('keydown', (event) => {
if (event.key === 'w') keys.w = true;
if (event.key === 'a') keys.a = true;
if (event.key === 's') keys.s = true;
if (event.key === 'd') keys.d = true;
});
document.addEventListener('keyup', (event) => {
if (event.key === 'w') keys.w = false;
if (event.key === 'a') keys.a = false;
if (event.key === 's') keys.s = false;
if (event.key === 'd') keys.d = false;
});
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Handle spider movement
const speed = 0.1;
if (keys.w) body.position.z -= speed; // Move forward
if (keys.s) body.position.z += speed; // Move backward
if (keys.a) body.position.x -= speed; // Move left
if (keys.d) body.position.x += speed; // Move right
// Update legs based on body position
for (let i = 0; i < numLegs; i++) {
const angle = (i / numLegs) * Math.PI * 2;
const baseOffset = new THREE.Vector3(Math.cos(angle) * 1.2, 0, Math.sin(angle) * 1.2);
legs[i][0].position.copy(body.position).add(baseOffset); // Update base of each leg
// Procedural adaptation: Raycast to ground for target
const raycaster = new THREE.Raycaster(legs[i][0].position, new THREE.Vector3(0, -1, 0));
const intersects = raycaster.intersectObject(terrain);
if (intersects.length > 0) {
const targetPoint = intersects[0].point; // Target on ground
ikSolvers[i].chain.target.copy(targetPoint); // Set IK target
ikSolvers[i].update(); // Solve IK for this leg
}
}
controls.update(); // Update camera controls
renderer.render(scene, camera); // Render the scene
}
animate(); // Start animation
</script>
</body>
</html>
This unified HTML file sets up a 3D scene with a procedurally animated spider using THREE.IK. The spider's legs adapt to the uneven terrain via inverse kinematics, and users can control movement with W, A, S, D keys. The orbital camera allows viewing from any angle using the mouse. The terrain includes hills and valleys, with scattered obstacles for testing.