Three.js Basics and Your First 3D Scene

Understand the core objects in Three.js and render a visible 3D cube in a Vue 3 page.

🎯 Introduction

By the end of this article, you will be able to create a 3D scene in Vue 3, explain how scenes, cameras, renderers, geometries, materials, and meshes work together, and render your first cube.

🧱 What is Three.js?

Three.js is a JavaScript library for browser-based 3D graphics. It wraps WebGL drawing details in accessible objects such as scenes, cameras, and materials. You can use it for product showcases, 3D data visualizations, model viewers, and interactive animations.

Start with these relationships:

Scene + Camera + Renderer → Rendered image
Geometry + Material → Mesh → Add to the scene
  • Scene contains objects and lights.
  • Camera determines the viewpoint and visible area.
  • Renderer draws the scene viewed by the camera onto a <canvas>.
  • Geometry describes an object's shape.
  • Material describes its color and surface appearance.
  • Mesh combines geometry and material into a renderable object.

🚀 Installation and a minimal scene

This course uses Node.js v22.0.0 and Three.js 0.185.1. Run this command in an existing Vue 3 project:

npm install three@0.185.1

The following code renders a single frame so you can focus on the initialization sequence.

components/FirstScene.vue
<template>
    <div ref="containerRef" class="scene"></div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import * as THREE from 'three';

const containerRef = ref(null);

onMounted(() => {
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(60, 320 / 240, 0.1, 100);
    camera.position.set(2, 2, 4);
    camera.lookAt(0, 0, 0);

    const geometry = new THREE.BoxGeometry(1, 1, 1);
    const material = new THREE.MeshStandardMaterial({ color: 0x44aa88 });
    const cube = new THREE.Mesh(geometry, material);
    cube.rotation.set(0.5, 0.7, 0);
    scene.add(cube);

    scene.add(new THREE.HemisphereLight(0xffffff, 0x445566, 2));

    const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
    renderer.setSize(320, 240);
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
    renderer.outputColorSpace = THREE.SRGBColorSpace;
    containerRef.value.appendChild(renderer.domElement);
    renderer.render(scene, camera);
});
</script>

The arguments to renderer.render(scene, camera) are always the scene to render followed by the camera to use. If an object has not been added to scene, or the camera cannot see it, the canvas may appear but remain empty.

Here is the rendered result:

Three.js relies on the browser's canvas and WebGL environment. Run initialization after the component mounts in the browser. With server-side rendering, do not create Scene or WebGLRenderer directly during server rendering.

🧾 Summary

  • Scene, Camera, and Renderer work together to render a frame.
  • Geometry + Material form a renderable Mesh.
  • Add objects to the scene and make sure the camera can see them.
  • Three.js units are not pixels. The camera and viewing distance determine an object's apparent size.
  • renderer.render() renders only the current frame. Continuous rendering is covered in the animation article.

❓ Questions and answers

Q1: How are Three.js and WebGL related?

WebGL is the browser's low-level API for 3D drawing. Three.js builds more accessible scene and object abstractions on top of it.

Q2: Why does a standard material need light?

MeshStandardMaterial calculates shading from lights. Without a light source, objects may appear very dark. The materials and lighting article explains this in detail.

Q3: Why is the camera often placed on the positive z-axis?

By default, a Three.js camera looks along the negative z-axis. Placing it on the positive z-axis lets it see objects near the origin.

🧪 Exercise

Change the example into a box with a width of 2, a height of 1, and a depth of 0.5. Then change the camera position and observe the result.

// Write your code here

🎉 You have created your first 3D scene. Next, you will learn to move, rotate, and scale objects.