Courses / Three.js
1. Creating a Scene
Learn how to create a basic scene in Three.js.
How It Works
Think of a scene like a stage in a theater where you place actors (objects), lights, and a camera to capture the performance.
First, you create a scene object.
Next, you add objects like geometries and materials to the scene.
Then, you set up a camera to view the scene from a specific angle.
Finally, you render the scene with a renderer.
See It
The code sets up a basic Three.js scene with a green cube rendered in front of the camera. You could try changing the cube's color in the material definition to see how it affects the scene.
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
renderer.render(scene, camera);Use It
Creating interactive 3D visualizations for web applications.
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.SphereGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
camera.position.z = 10;
renderer.render(scene, camera);This concept matters because it provides a foundation for creating engaging 3D experiences on the web.
Break It
What will this render?
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
renderer.render(scene, camera);The camera is positioned at the default location, and without adjusting its position or the cube's position, it may not be able to view the cube properly.
To fix this, adjust the camera's position to ensure it can view the cube.
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5; // Adjusted camera position
renderer.render(scene, camera);Level Up
Adding Interactivity: This technique allows users to interact with objects in the scene, enhancing engagement.
Animating Objects: This technique enables the creation of dynamic scenes where objects move over time.
Using Textures: Applying textures to objects can create a more realistic appearance in your 3D scenes.
Prove It
Create a scene with two cubes, one red and one blue, positioned apart.
JavaScript
Creating a Scene
Creating a scene in Three.js allows you to build 3D environments easily.
"I want to bring my ideas to life in 3D without getting lost in complex code."
Creating a scene in Three.js involves setting up a 3D space where objects, lights, and cameras interact to form a visual experience.
"I want to bring my ideas to life in 3D without getting lost in complex code."
Creating a scene in Three.js involves setting up a 3D space where objects, lights, and cameras interact to form a visual experience.