Lab: Torus
In this chapter, you've been learning how to represent triangle meshes and generate algorithmic shapes like cones, cylinders, and spheres. Let's apply this knowledge by writing a renderer that generates a torus—a donut.
Form a group of no more than three people. Host, open your top-level repository directory in Visual Studio Code and run npm run dev
to start up a web server. Then open the command palette via View / Command Palette and enter Live Share: Start Collaboration Session. Share the link with your group.
Torus
Your challenge is to render a spinnable torus colored by its normals. Complete these steps:
transfit
renderer to make a new renderer named torus
. We use transfit
because it deals with the viewport distortion and has no HTML inputs.center
variable in resizeCanvas
to [0, 0]
and size
to 1.torus
. Have it accept four parameters: the inner radius, the outer radius, the longitude count, and the latitude count. Generate the torus geometry using a similar approach to how we generated a sphere. Unlike the sphere, wrap both latitude and longitude when forming triangles. Outline an algorithm before writing code.initialize
with code like this:
window.addEventListener('keydown', event => {
if (event.key === 'ArrowLeft') {
// ...
}
});
window.addEventListener('keydown', event => { if (event.key === 'ArrowLeft') { // ... } });
Submission
To receive credit for your lab work, follow these steps:
torus
folder before the host closes the session.#lab
channel in Discord. No voiceover is necessary. Tag your group members with @
.Only labs submitted on time will be granted credit. Late labs or forgot-to-submits are not accepted because Monday at noon is when your instructor has time to grade.