Okay, this is a fun challenge! Creating a fully procedurally animated walking creature in just three .js files is tight, but achievable by focusing on simplicity and core mechanics. We'll use Three.js for rendering.
Here's a breakdown of the approach and the code. I'll explain the core concepts first, then provide the code for each file.
Core Concepts
Procedural Animation: Instead of keyframes, we'll calculate the position and rotation of body parts based on a mathematical function of time. A sine wave will be our friend for creating smooth, repeating motion.
Simplified Creature: We'll use basic geometric shapes (spheres, cylinders) to represent the creature's body, legs, and feet. This avoids the complexity of loading a model.
Walking Cycle: The key to a walking cycle is coordinating the movement of legs. We'll use a phase difference between the left and right legs to simulate alternating steps.
Basic Physics: We'll add a simple check to see when the creature hits the ground, so it doesn't walk through it.
File Structure:
index.html: Basic HTML to load the scripts and create the canvas.
creature.js: Defines the Creature class, responsible for the creature's geometry, animation logic, and updates.
app.js: Sets up the Three.js scene, camera, renderer, and the animation loop.
index.html:
Sine Wave Animation: The code utilizes Math.sin() to create smooth, cyclical motion for the legs and body. The phase offset (Math.PI) ensures the legs move in opposite directions.
Leg and Foot Creation: The createLeg and createFoot methods make it easy to adjust the leg/foot appearance. Using applyMatrix4 rotates the cylinder so it stands upright.
Ground Plane: A ground plane is added for visual context.
Lighting: Basic lighting is included to make the scene more visually appealing. MeshLambertMaterial works well with directional lights.
Forward Movement: The xPosition is updated in the update method to make the creature move forward.
Body Bob: The body bobs up and down slightly to add more realism.
Simple Ground Collision: The checkGroundCollision function prevents the creature from walking through the ground.
Ground Level Variable: The groundLevel property on the creature is now configurable, so you can easily set where the creature starts relative to the ground.
Resizing: Includes window resize handling.
Possible Enhancements:
More Body Parts: Add arms, a head, or other appendages.
More Complex Walking Cycle: Adjust the sine wave functions to create more nuanced leg movements. Consider using bezier curves for finer control.
Terrain: Create a more interesting terrain than a flat plane.
Camera Controls: Add controls to move the camera around the scene.
Shadows: Implement shadows for a more realistic look. This can be performance intensive.
More Physics: Add more realistic physics to the walking motion. Consider using a physics engine like Cannon.js or Ammo.js.
Different Gaits: Implement different walking gaits (e.g., running, hopping).
Environment Interaction: Have the creature interact with the environment (e.g., avoid obstacles).
This example provides a solid foundation for creating more complex procedurally animated creatures in Three.js. Remember to experiment and have fun!