In this tutorial, we create a 3D game with Three.js and use HTML Canvas to build textures. If you haven’t used Three.js before don’t worry. This is an introductory course.
We start with the basics, we set up the scene, the lights, and the camera. Then we build a minimalistic car by putting together boxes. We also add texture to it to paint the windows. For the textures, we not just going to import an image, we are going to code our textures with HTML canvas.
Once finishing with the car, we create the race track. We talk about how to draw two-dimensional shapes in Three.js and how to turn them into extruded geometries. This part will also contain some trigonometry as we need to calculate some angles to draw the track. Don’t worry though, we are going to cover everything in detail.
And once we know how to create a car and the track in the 3D space we will add the game logic. We add event handlers and the main animation loop that will move around the vehicles and take care of the game logic.
This tutorial is for beginners. The only thing you need as a prerequisite is a basic understanding of JavaScript, and you shouldn’t be afraid of using some trigonometry. We won’t do anything crazy with JavaScript, but we won’t go through the code line by line either.
👉 You can find the whole game and its source code on Codepen:
https://codepen.io/HunorMarton/pen/ExNzWqm
The game in the video is heavily inspired by the mobile game Loop Driver: https://apps.apple.com/us/app/loop-drive-crash-race/id992442150
#game-development #threejs #three #html