Code Structure Used in the Course
Explore the essential code structure used throughout this course to create Three.js scenes. Learn about scene initialization, camera setup, lighting configuration, and reusable helper modules. Gain an understanding of how to organize and modularize Three.js projects for cleaner and more maintainable 3D graphics code.
A general structure for creating a new scene
As we know, most scenes will need pretty much the same setup. They all need some lights, a camera, a scene, and maybe a ground floor. To avoid having to add all this to each example, we’ve externalized most of these common elements to a set of helper libraries. Examples throughout the course imports functions and modules from the Three.js library and folders like utils, bootstrap, helpers, and control.
Let’s take an example of chapter-01/porsche.html:
Explanation
Lines 1–17: First we import modules and functions from different libraries then we set up configuration properties for the scene, such as background and fog colors. We also initialize a graphical user interface (GUI) using the
lil-guilibrary.Lines 19–54: Second, we initialized the scene using the
initScenefunction, and the callback receives thescene,camera,renderer, andorbitControlsparameters. We set the camera position, a floating floor, and a 3D model of a Porsche is loaded using theGLTFLoader. We set the different properties of loaded Porsche models like colors, metalness, and roughness.Lines 56–64: Finally, we add an animation loop that continuously renders ...