So far, we've been in control of our camera's perspective programmatically. But for a truly engaging 3D experience, users need to be able to explore the scene themselves! This section dives into how we can give your users intuitive camera controls like orbiting around an object, panning across the scene, and zooming in and out. Three.js provides a fantastic set of pre-built controls that make this surprisingly easy to implement.
The most common and versatile camera control in 3D applications is the OrbitControls. As the name suggests, it allows users to orbit around a target point (usually the center of your scene), pan to move the camera's position sideways or up/down, and zoom by moving the camera closer or further away from the target. Let's get started with setting this up.
First, you'll need to include the OrbitControls module. These are typically found in the examples/jsm/controls/OrbitControls.js directory of your Three.js installation or CDN. We'll import it like this:
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';Next, after you've created your scene, camera, and renderer, you'll instantiate OrbitControls, passing in your camera and renderer.domElement (which is the HTML canvas where your scene is rendered):
const controls = new OrbitControls(camera, renderer.domElement);That's it for the basic setup! Now, if you run your application, you should be able to use your mouse to interact with the scene. Typically, the left mouse button allows you to orbit, the right mouse button allows you to pan, and the mouse wheel enables zooming. However, the exact behavior can be customized.
To ensure the controls update correctly on every frame, you need to call the update() method of OrbitControls within your animation loop. This is crucial for the controls to process user input and adjust the camera accordingly.
function animate() {
requestAnimationFrame(animate);
// Update the orbit controls
controls.update();
renderer.render(scene, camera);
}The OrbitControls offer a wealth of customization options. Here are a few common ones:
controls.target: You can explicitly set the point the camera orbits around. By default, it orbits around the origin (0, 0, 0). If your model is at a different position, you'll want to set this to match its center.controls.enableZoom: Set tofalseto disable zooming.controls.enablePan: Set tofalseto disable panning.controls.enableRotate: Set tofalseto disable orbiting.controls.minDistance,controls.maxDistance: Set limits on how close or far the camera can zoom.controls.minPolarAngle,controls.maxPolarAngle: Control the vertical angle of the camera, preventing it from going above or below certain points (useful for preventing the camera from flipping upside down).controls.autoRotate: Set totrueto make the camera automatically rotate around the target. You can also control the speed withcontrols.autoRotateSpeed.
controls.enablePan = false;
controls.minDistance = 5;
controls.maxDistance = 50;
controls.target.set(0, 2, 0); // Orbit around a point at y=2Let's visualize the basic flow of how OrbitControls work with your scene. It's a continuous loop of user input, control updates, and rendering.
graph TD
A[User Input (Mouse/Wheel)] --> B{OrbitControls.update()}
B --> C[Camera Position & Rotation Updated]
C --> D[Renderer Renders Scene]
D --> E[Display Updated Frame on Screen]
E --> A
Beyond OrbitControls, Three.js offers other control types for different interaction needs. For instance, TrackballControls is an older alternative that uses a virtual ball on your screen for more freeform rotation, and FlyControls simulates a first-person flight experience. However, for most general-purpose 3D scenes, OrbitControls is the go-to solution due to its intuitive and familiar behavior.
Experiment with the various properties of OrbitControls to fine-tune the user experience. The ability for users to freely navigate your 3D world is a hallmark of a compelling web experience, and OrbitControls is your key to unlocking that interactivity.