BJ Patel is an expert user of Umbraco. Always keen to share hints and tips on getting the best out of Umbraco.
Welcome to our step-by-step guide on getting started with Three.js! Whether you are a seasoned developer or a curious beginner, this post will help you understand the basics of Three.js and how to create engaging 3D graphics for your web applications. Dive in and let’s explore the exciting world of Three.js together!
Understanding Three.js
Three.js is a powerful JavaScript library that simplifies the creation of 3D graphics in the browser using WebGL. It allows you to create complex 3D scenes, animations, and interactive experiences with ease.
Initializing Three.js
To start using Three.js, you need to set up a scene, camera, and renderer. Here’s a basic example to get you started:
Initialization
- Import Three.js: This imports the entire Three.js library which contains all the necessary functions and objects for 3D rendering.
- Create a scene: THREE.Scene is the container for all 3D objects, lights, and cameras in your 3D environment.
- Set up a camera: THREE.PerspectiveCamera creates a camera with perspective projection. The parameters are field of view, aspect ratio, near clipping plane, and far clipping plane.
- Create a renderer: THREE.WebGLRenderer creates a renderer that uses WebGL to draw the scene. setSize adjusts the rendering area to the size of the window.
- Attach the renderer to the document: This appends the renderer’s output (a canvas element) to the HTML document so it is visible in the browser.
// Import Three.js
import * as THREE from 'three';
// Create a scene
const scene = new THREE.Scene();
// Set up a camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
// Create a renderer and attach it to our document
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
Adding a 3D Object:
- Create a cube geometry: THREE.BoxGeometry creates a cube shape with specified width, height, and depth.
- Create a material: THREE.MeshBasicMaterial is a material for drawing geometries in a simple shaded (flat or wireframe) way. The color is set to green.
- Combine them into a mesh: THREE.Mesh combines the geometry and material into a 3D object that can be added to the scene.
- Add the cube to the scene: scene.add(cube) adds the cube mesh to the scene.
- Move the camera: The camera is moved back along the z-axis so the cube is visible in the frame.
// Create a cube geometry and a basic material and combine them into a mesh
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
// Add the cube to the scene
scene.add(cube);
// Move the camera away from the origin to view the cube
camera.position.z = 5;
- Animation loop: requestAnimationFrame is a browser API for performing animations. It tells the browser to call the specified function before the next repaint.
- Rotate the cube: Each frame, the cube’s rotation is incremented slightly around the x and y axes, causing it to spin.
- Render the scene: renderer.render(scene, camera) draws the scene from the perspective of the camera.
- Start the animation: Calling animate() starts the loop.
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Rotate the cube for some basic animation
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
// Render the scene from the perspective of the camera
renderer.render(scene, camera);
}
// Start the animation loop
animate();
Interactivity
- HTML Buttons: Two buttons are created in the HTML to start and stop the cube’s rotation.
- JavaScript Variables and Event Listeners:
- isRotating is a boolean variable that controls whether the cube should rotate.
- The click event listener on the "Start" button sets isRotating to true and starts the animation.
- The click event listener on the "Stop" button sets isRotating to false and stops the animation.
- Modified Animation Function:
- The animate function checks if isRotating is true before rotating the cube and calling requestAnimationFrame again.
- If isRotating is false, the function will stop the animation loop.
let isRotating = false;
document.getElementById('startButton').addEventListener('click', () = >{
isRotating = true;
animate();
});
document.getElementById('stopButton').addEventListener('click', () = >{
isRotating = false;
});
function animate() {
if (isRotating) {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
}
HTML
<button id="startButton">Start</button>
<button id="stopButton">Stop</button>
By understanding these code sections, you can start creating interactive 3D graphics using Three.js
Join Our Community
Your contributions help us continue creating valuable content. Consider supporting us by sharing our content.
Junagadh, Gujarat
Latest Blog Posts