Building Your First Browser Game with Three.js and React: Part 1 - Getting Started
Introduction
In this series, we're going to build a 3D web game from scratch: a basketball game that runs in the browser, made with Three.js and React Three Fiber. You launch the ball by clicking buttons, and the goal is to score as many baskets as possible.
By the end of the series, you'll have a working game and a good idea of how to build 3D scenes with Three.js and React. You can play the final version of the game to see what we're building. The game is simple, which makes it a good first project in web game development. Here's a preview:
Today, we start with the basics: setting up the project with Vite, a fast way to create a React project.
Step 1: Setting Up Your Environment
First, make sure Node.js is installed on your computer. We need it to manage the project's dependencies and run the development server. You can download and install Node.js from here.
Once Node.js is ready, we're going to set up our project using Vite. Open your terminal and run the following commands:
# Create a new directory for your project and navigate into it
mkdir basketball-game
cd basketball-game
# Initialize a new project using Vite with a React template
npm create vite@latest . -- --template react
This command creates a new React project with Vite, with all the boilerplate code you need to get started.
Step 2: Installing Dependencies
Our game uses Three.js and React Three Fiber, so we need to add them to the project. We'll also install drei, a collection of helpers for React Three Fiber that makes working with Three.js easier.
Run the following command in your terminal:
npm install three @react-three/fiber @react-three/drei
With these libraries, we can create 3D objects and manage them as React components.
Step 3: Verify Your Project Setup
Once the dependencies are installed, check that everything works. Start the development server:
npm run dev
This command will launch your application in a local development server.
Open your browser and navigate to http://localhost:5173 to see your new React application running. You should see a basic React welcome screen.
Step 4: Creating a Basic 3D Scene with a Cube
The project is ready and the libraries are installed, so let's get to the fun part: our first 3D scene with Three.js and React Three Fiber. In this step, we'll set up a simple scene with a cube and a camera to see how Three.js works inside a React application.
Setting Up the Experience Component
Inside your src/ directory, create a new file named Experience.jsx.
This component will house our 3D scene. Add the following code to initialize a basic scene with a cube:
import React from 'react';
import { Box, OrbitControls } from '@react-three/drei';
const Experience = () => {
return (
<>
<ambientLight intensity={0.5} />
<pointLight position={[10, 10, 10]} />
<OrbitControls makeDefault />
<Box>
<meshStandardMaterial attach="material" color="orange" />
</Box>
</>
);
}
export default Experience;
In this component:
<ambientLight>and<pointLight>add basic lighting to the scene, which helps in viewing the cube.<OrbitControls>a basic control to rotate/move around the scene.<Box>is a component fromdreithat represents a 3D cube. We color it orange.
Incorporating the Experience into Our App
Next, the main application needs to render our scene.
Delete the existing App.js file in the src folder and update the main.jsx file to include our Experience component:
import React from 'react'
import ReactDOM from 'react-dom/client'
import { Canvas } from '@react-three/fiber'
import Experience from './Experience'
import './index.css'
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<Canvas>
<Experience />
</Canvas>
</React.StrictMode>
)
This adds the Experience component to the main application, inside a Canvas component,
which creates the canvas element where our 3D scene renders.
Create the base style for the 3D Experience
Now, update the existing index.css file to add some base styles:
* {
box-sizing: border-box;
}
html,
body {
background-color: #ddc28d;
}
html,
body,
#root,
#main {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
user-select: none;
}
You can now delete the App.jsx file and the App.css file also, we don’t need them.
Testing Your Scene
Once you have made these changes, save your files and ensure your development server is still running. Refresh your browser page at http://localhost:5173.
You should now see a cube rendered on the screen, as shown in the screenshot below. You can rotate around, zooming in/out and move pressing CTRL.
If it's not appearing, make sure your development server is running correctly and that there are no errors in your console.
If you run into problems or have questions, you can contact me using the links on my About page.
Conclusion
You've just created your first 3D scene with Three.js and React Three Fiber. The next posts in the series build on this setup to make the scene more complex and interactive.
In the next part, we will add the 3D models of the game.
See you in part 2! 🏀