Make a fully procedurally animated walking creature in three .js
- User
Make a fully procedurally animated walking creature in three .js
Assistant
Procedural Walking Creature in Three.js
Here's a complete implementation of a procedurally animated walking creature using Three.js. This example creates a simple quadruped creature with procedural walking animation.
HTML Setup (index.html)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Procedural Walking Creature</title> <style> body { margin: 0; overflow: hidden; } canvas { display: block; } </style> </head> <body> <script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script> <script src="creature.js"></script> <script src="animation.js"></script> <script src="main.js"></script> </body> </html>Creature Definition (creature.js)
class ProceduralCreature { constructor() { this.scene = new THREE.Scene(); this.scene.background = new THREE.Color(0x87CEEB); // Create lights const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); this.scene.add(ambientLight); const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); directionalLight.position.set(1, 1, 1); this.scene.add(directionalLight); // Create ground const groundGeometry = new THREE.PlaneGeometry(20, 20); const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x3a5f0b, roughness: 0.8, metalness: 0.2 }); const ground = new THREE.Mesh(groundGeometry, groundMaterial); ground.rotation.x = -Math.PI / 2; ground.receiveShadow = true; this.scene.add(ground); // Create creature this.creature = this.createCreature(); this.scene.add(this.creature); // Camera setup this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); this.camera.position.set(0, 2, 5); this.camera.lookAt(0, 1, 0); // Renderer this.renderer = new THREE.WebGLRenderer({ antialias: true }); this.renderer.setSize(window.innerWidth, window.innerHeight); this.renderer.shadowMap.enabled = true; document.body.appendChild(this.renderer.domElement); // Animation variables this.animationTime = 0; this.speed = 0.5; this.direction = 1; this.turnSpeed = 0.02; this.turnDirection = 0; // Event listeners window.addEventListener('resize', this.onWindowResize.bind(this)); window.addEventListener('keydown', this.onKeyDown.bind(this)); } createCreature() { const creature = new THREE.Group(); // Body const bodyGeometry = new THREE.CylinderGeometry(0.5, 0.7, 1.5, 16); const bodyMaterial = new THREE.MeshStandardMaterial({ color: 0x5a3a2a, roughness: 0.7, metalness: 0.1 }); const body = new THREE.Mesh(bodyGeometry, bodyMaterial); body.position.y = 0.75; body.castShadow = true; body.receiveShadow = true; creature.add(body); // Head const headGeometry = new THREE.SphereGeometry(0.4, 16, 16); const head = new THREE.Mesh(headGeometry, bodyMaterial); head.position.set(0, 1.2, 0); head.castShadow = true; creature.add(head); // Legs (4 legs) this.legs = []; const legPositions = [ [0.3, 0, 0.3], // Front right [-0.3, 0, 0.3], // Front left [0.3, 0, -0.3], // Back right [-0.3, 0, -0.3] // Back left ]; for (let i = 0; i < 4; i++) { const leg = new THREE.Group(); leg.position.set(legPositions[i][0], 0, legPositions[i][2]); // Upper leg const upperLegGeometry = new THREE.CylinderGeometry(0.15, 0.1, 0.5, 8); const upperLeg = new THREE.Mesh(upperLegGeometry, bodyMaterial); upperLeg.position.y = 0.25; upperLeg.castShadow = true; leg.add(upperLeg); // Lower leg const lowerLegGeometry = new THREE.CylinderGeometry(0.1, 0.08, 0.5, 8); const lowerLeg = new THREE.Mesh(lowerLegGeometry, bodyMaterial); lowerLeg.position.y = -0.25; lowerLeg.castShadow = true; leg.add(lowerLeg); creature.add(leg); this.legs.push(leg); } return creature; } onWindowResize() { this.camera.aspect = window.innerWidth / window.innerHeight; this.camera.updateProjectionMatrix(); this.renderer.setSize(window.innerWidth, window.innerHeight); } onKeyDown(event) { switch (event.key) { case 'ArrowUp': this.speed = 0.5; break; case 'ArrowDown': this.speed = -0.2; break; case 'ArrowLeft': this.turnDirection = -1; break; case 'ArrowRight': this.turnDirection = 1; break; } } animate() { requestAnimationFrame(this.animate.bind(this)); // Update animation time this.animationTime += 0.01 * this.speed; // Update creature position based on direction this.creature.position.x += Math.sin(this.creature.rotation.y) * this.speed * 0.01; this.creature.position.z += Math.cos(this.creature.rotation.y) * this.speed * 0.01; // Update creature rotation this.creature.rotation.y += this.turnDirection * this.turnSpeed; // Reset turn direction if no key is pressed if (Math.abs(this.turnDirection) < 0.1) { this.turnDirection = 0; } // Update leg animation this.updateLegs(); // Update camera to follow creature this.camera.position.x = this.creature.position.x; this.camera.position.z = this.creature.position.z + 5; this.camera.lookAt(this.creature.position.x, 1, this.creature.position.z); // Render this.renderer.render(this.scene, this.camera); } updateLegs() { const legPairs = [ [0, 2], // Front and back right [1, 3] // Front and back left ]; for (let i = 0; i < legPairs.length; i++) { const [leg1, leg2] = legPairs[i]; // Calculate phase offset for alternating movement const phase = i * Math.PI; // Calculate lift and swing based on animation time const lift = Math.sin(this.animationTime + phase) * 0.2; const swing = Math.sin(this.animationTime + phase) * 0.1; // Update first leg this.legs[leg1].rotation.x = lift; this.legs[leg1].position.x = swing; // Update second leg (opposite phase) this.legs[leg2].rotation.x = -lift; this.legs[leg2].position.x = -swing; } } }Animation Logic (animation.js)
class CreatureAnimation { constructor(creature) { this.creature = creature; this.animationTime = 0; this.speed = 0.5; this.direction = 1; this.turnSpeed = 0.02; this.turnDirection = 0; } update(deltaTime) { this.animationTime += deltaTime * this.speed; // Update creature position based on direction this.creature.position.x += Math.sin(this.creature.rotation.y) * this.speed * deltaTime; this.creature.position.z += Math.cos(this.creature.rotation.y) * this.speed * deltaTime; // Update creature rotation this.creature.rotation.y += this.turnDirection * this.turnSpeed * deltaTime; // Reset turn direction if no key is pressed if (Math.abs(this.turnDirection) < 0.1) { this.turnDirection = 0; } // Update leg animation this.updateLegs(deltaTime); } updateLegs(deltaTime) { const legPairs = [ [0, 2], // Front and back right [1, 3] // Front and back left ]; for (let i = 0; i < legPairs.length; i++) { const [leg1, leg2] = legPairs[i]; // Calculate phase offset for alternating movement const phase = i * Math.PI; // Calculate lift and swing based on animation time const lift = Math.sin(this.animationTime + phase) * 0.2; const swing = Math.sin(this.animationTime + phase) * 0.1; // Update first leg this.legs[leg1].rotation.x = lift; this.legs[leg1].position.x = swing; // Update second leg (opposite phase) this.legs[leg2].rotation.x = -lift; this.legs[leg2].position.x = -swing; } } setSpeed(speed) { this.speed = speed; } setTurnDirection(direction) { this.turnDirection = direction; } }Main Application (main.js)
// Main application const creature = new ProceduralCreature(); creature.animate(); // Handle keyboard input document.addEventListener('keydown', (event) => { switch (event.key) { case 'ArrowUp': creature.speed = 0.5; break; case 'ArrowDown': creature.speed = -0.2; break; case 'ArrowLeft': creature.turnDirection = -1; break; case 'ArrowRight': creature.turnDirection = 1; break; } }); document.addEventListener('keyup', (event) => { if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') { creature.turnDirection = 0; } });How It Works
-
Creature Structure: The creature is built with a body, head, and four legs. Each leg has two segments (upper and lower).
-
Procedural Animation: The walking animation is created by:
- Alternating the lifting of opposite leg pairs (front-right with back-left, front-left with back-right)
- Adding a slight side-to-side swing to each leg
- Using sine waves to create smooth, cyclic motion
-
Controls:
- Arrow Up: Move forward
- Arrow Down: Move backward
- Arrow Left/Right: Turn left or right
-
Camera: The camera follows the creature from behind.
To use this, simply create an HTML file with the first code block, and then create three JavaScript files (creature.js, animation.js, and main.js) with the respective code. Open the HTML file in a browser to see the animated creature.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.