Page 5: Meshes in Three.js
Spring 2026 Sample Solution
Confusingly, THREE.js uses the term Geometry to mean a collection of triangles, and uses the term Mesh to mean a Geometry (collection of triangles) that has a material associated with it, and all of the other stuff that a “Graphics Object” has (e.g., transformations and hierarchy). A THREE Mesh “has a” Geometry, but a Geometry is the collection of triangles that is more like the common graphics usage of “mesh”.
THREE’s terminology gets more confusing… Geometry doesn’t exist in three.js anymore. We only have BufferGeometry.
In the old days, THREE had a Geometry class. This was a very flexible way to represent a set of triangles. Geometry had an API that would allow a programmer to specify collections of triangles in many different ways. It also had a class called BufferGeometry that organizes triangle data using data structures that are inflexible, but efficient to send to graphics hardware. Three.js deprecated and removed Geometry, so now we only have BufferGeometry. All of the workbook code has been updated, but sometimes I might refer to Geometry by mistake. The term Geometry is even part of some of Three’s code and documentation.
In this workbook, we are using BufferGeometry. Some of the old lecture videos may refer to the old-fashioned geometry.
Buffers, Attributes, and Geometry
BufferGeometry is designed to use data structures that are efficient for the hardware to use for drawing. Information is stored per-vertex. The only information we store per-triangle is the list of three vertices. We store the same information for all vertices on an object.
Because all vertices are the same, we can make simple blocks of memory that store the values for all the vertices as one-dimensional arrays. For example, the positions of vertices are just a long list of numbers (3 per vertex). We call blocks of memory “buffers”. We call a block of memory that is meant to store a list of attributes (per vertex) an attribute buffer. THREE calls them BufferAttributes. But they are just simple arrays of values - stored in a slightly special way to allow them to be transferred efficiently to the graphics hardware. This has to do with some technical details on how JavaScript handles types.
THREE’s BufferGeometry class stores a collection of triangles. It can have a number of BufferAttributes for the different attributes that you want associated with the vertices. It can handle the triangle indices in two ways: either it will assume that your vertices are in order (basically, your triangles are (0,1,2), (3,4,5), etc.), or you can give it a list of 3n (n=number of triangles) integers which are the indices of the vertices in the vertex list (3 per triangle).
A First Example
Here is a simple triangle:
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
05-05-01.html 05-05-01.js
Here are the important lines from the code (comments removed, so the line numbers don’t match):
1const geometry = new THREE.BufferGeometry();
2
3const vertices = new Float32Array([
4 -1.0, -1.0, 0.0, // bottom-left
5 1.0, -1.0, 0.0, // bottom-right
6 0.0, 1.0, 0.0 // top-center
7]);
8
9const positionBuffer = new THREE.BufferAttribute(vertices, 3);
10geometry.setAttribute('position', positionBuffer);
11
12const material = new THREE.MeshBasicMaterial({ color: 0xffff00, side: THREE.DoubleSide });
13
14const triangle = new THREE.Mesh(geometry, material);
15scene.add(triangle);Because this triangle is drawn using MeshBasicMaterial, the only information we need is the positions. We need to create a BufferGeometry object that has a single attribute for position. Notice that there is nothing special about position: it is just an attribute (something that we will have values for each vertex).
The code first creates the geometry object, as type BufferGeometry (line 1). We then create a buffer of the positions. To do this, it creates an array of positions (line 3-7). Notice that it is a 1D array with 9 numbers, x, y, and z for each of 3 vertices. Also, notice that it uses the special JavaScript type Float32Array. This is a special kind of array where all values must be floating point numbers. Using this data structure is important: it puts the numbers in memory in a form that can be used efficiently by the hardware.
Once we have the memory array of values, we need to create the BufferAttribute object (line 9). This adds information to help three.js know how to interpret the numbers in the array. Here, we tell it that each vertex has 3 values. We assign this as the position attribute in the BufferGeometry we had created (line 10). The BufferGeometry can be used to create a Mesh that combines it with a material (line 14).
Here is an extended version of the program. We will use lighting, so we need normal vectors, which need to be assigned per-vertex. We will also give each vertex its own color. This means we need to tell the material to use the per-vertex color, not a per-object color.
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
05-05-02.html 05-05-02.js
Notice how we can create other attributes just as we created the position attribute. Three numbers per vertex.
|
|
More than one triangle
If we have more than one triangle, we simply have bigger arrays.
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
05-05-03.html 05-05-03.js
All we had to change from the first example is to add more data.
|
|
Notice that we repeat the vertex at (0,0,0).
By default, three.js interprets the list of vertices as being consecutive for each triangle. Instead, we can specify which vertices each triangle should use. We do this by specifying an index array, which has 3 integers per triangle, each specifying which vertex to use.
The default would have been equivalent to [0,1,2, 3,4,5, 6,7,8]. But, using indices, we can do vertex sharing, or re-order things as we wish.
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
05-05-04.html 05-05-04.js
This example is identical to the previous, except that it shares the vertex at the origin. Notice how we have fewer vertices in the array, and we use the setIndex method to specify which triangles connect to which other ones.
|
|
In the code, I did things the “correct” way (by creating a BufferAttribute), but also the concise way of just passing an array. Three.js is smart enough to figure out how to convert things when needed.
Normals
Specifying normals can be laborious. Fortunately, Three has a nice method to compute them for us. This works well when the triangles convey the shape.
The computeVertexNormals function is a method of BufferGeometry documented here. It creates a buffer for the normals, fills it with reasonable values, and attaches it to the BufferGeometry.
For non-indexed geometries, each vertex belongs to one triangle, so computeVertexNormals computes the normal for the triangle and uses that.
For indexed geometries, a vertex can be shared among several triangles. So computeVertexNormals computes the normal vector for each triangle, and then sets each vertex to be the average of all the triangles it is part of. This helps create a smooth-looking shape.
Another Example
Here is another example where we make two triangles, with different colorings. Color per mesh, color per triangle, and color per vertex.
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
05-05-05.html 05-05-05.js
The example (05-05-05.html,
05-05-05.js
) shown uses BufferGeometry. Here it is again:
Read the code. You will see that I make objects (using the class framework) for “TwoTriangles” in three different ways.
Here is the version of the code using BufferGeometry. This is the code for the two-color version:
|
|
Notice that we had to split the vertices (so we have 6 of them) - for example (0,0,0) is repeated, once for each triangle. And note that we need to specify the colors for each of the 6 vertices.
You can also see how we have to put the data for the vertices in special “typed” arrays (the Float32Array) on 88 and 102, and then tell THREE to use these as BufferAttributes. Note how for the positions and the colors, each vertex has 3 numbers (x,y,z or r,b,g), so we need to tell this to THREE when we create the BufferAttribute (lines 96 and 110).
In this case, each triangle is made up of 3 distinct vertices. Triangle 1 is vertices 0,1 and 2; Triangle 2 is 3,4,5. If we don’t tell THREE otherwise, it will assume this and will figure out the indices for us.
THREE has materials specifically designed to work with meshes. If you want materials such as MeshStandardMaterial to use vertex colors, you need to tell it to do so (see vertexColors: true on line 114). Otherwise, it makes the whole mesh one color.
Example: Mesh Normals
This example uses the same two triangles. However, this time, the normals are done differently for each of the 3 versions. On the left, we use the actual normals of the triangles (which requires vertex splitting). The other two have the center vertices “shared” between the triangles. These vertices have their normals set to be the average of the two faces they touch. This gives the same normals as if it were a smooth, curved piece (rather than two triangles with a crease). Because the lights are on the sides, the middle of this object is dark. But, as you move it, notice the lighting behaves as if this were a curved object. The object in the middle manually specified the normals; on the right, the normals are computed by THREE.
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
05-05-06.html 05-05-06.js
Be sure to read the code in 05-05-06.js and understand how the meshes are put together.
In the “curved” examples, we use the same normal (and color) for the vertex on each of its triangles, so we can re-use the vertex for both triangles. Notice how these examples only have 4 points. In the third example, THREE computes the normals: when it computes them for the vertices in the middle, it averages the normals of all the triangles the vertex touches.
When we use 4 vertices to make three triangles, we need to tell THREE which vertices to use for each triangle. If you look at the code, you’ll see we use setIndex to tell it which vertices to use for each triangle.
A Note on BufferGeometry
While BufferGeometry is the preferred way to do things in THREE, it can be less convenient if we want to see our triangles (use their “real” normals and/or have triangle colors). In practice, we don’t usually want to see individual triangles, so this isn’t a big problem.
One trick: the documentation doesn’t have a good list of what attributes we can make. If you noticed, so far we’ve made buffers for position, normal, and color. There are other choices (we’ll see “uv” later in this workbook). The answers to this get a little tricky, and depend on how the graphics hardware works. Basically, the names of the buffers are variables that get used in the programs that run on the graphics hardware to transform vertices and color pixels.
Backfacing Triangles
If you remember from the beginning… if you rotate the triangles too much so you see their “backside,” they disappear!
By default, THREE uses an optimization called backface culling. Basically, if a triangle points away from the camera, it doesn’t get drawn. With backface culling, we only see the “fronts” of triangles.
The idea of backface culling is that most of the time, we make solid objects that are surrounded by surfaces. Triangles are on the surface of the object, and their normals should only face outwards. Most objects have fronts and backs - different triangles, which would face different directions. Since you can’t see the back side, which is facing away from the camera, there is (usually) no reason to draw it.
The problem here is that we made an object that is only 1 triangle “thick” - it is infinitely thin, so the same triangle is both the front and the back. If we turn the object around, we see the back of a triangle (which THREE doesn’t draw by default).
There are two ways to deal with this:
- We tell THREE that we want it to draw both sides of the object. We do this by adding an option to the material.
- We make our objects have some thickness, so they have fronts and backs. This is more “realistic” but also more work.
Here is an example - this time, the object is 8 triangles. Some of the triangles face forward, some face backwards. As you spin the object around, note how when you look at the “back” you see different triangles. The object on the right is set to draw front and back facing triangles.
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
05-05-07.html 05-05-07.js
| Kind
Kind of rubric items: standard - expected from all students advanced - used to get a better grade creative - an opportunity to do something creative levels - rubric item graded by selected achievement level optional - not required, but encouraged |
Description | |
| standard | triangles correctly oriented (no holes in grid) |
If you look at the code, you’ll notice something unexpected: when I specify the normals, I specify the normals all going the same way! (on line 41, all the normals face the Z direction).
For backface culling, the graphics hardware doesn’t use the normals to determine which direction the triangle is facing. It uses the orientation of the triangle. It effectively uses the right-hand rule to determine which direction the normal is facing - it has to do this because not all triangles have their normals specified.
Specifically, it uses the winding direction of the triangle. After projection, the hardware determines if the vertices are clockwise or counter-clockwise. (counter-clockwise is facing towards the camera). If you look at the code, you’ll see that some triangles are clockwise, others are counter-clockwise. This is a good exercise. Note that the vertices form a grid, and the triangles (specified by the index) are in different orders.
So, to practice… (this is a rubric item!) fix the orientations of the triangles. You’ll need to re-order the vertices in 05-05-07.js to be consistently all clockwise or counter-clockwise. (if you make them all clockwise, you’ll have to rotate the object to see it on the left). A hint: the positions of the vertices are:
6 7 8
3 4 5
0 1 2
You might also wonder: why in the unfixed version with two-sided triangles are some triangles bright yellow and other triangles dark yellow? We would expect THREE to use the specified normal - so the triangle orientation doesn’t matter. But, THREE does care about triangle orientation - if the triangle is going the wrong way, THREE flips the normal. So, the short lesson: when you make a mesh, orient the triangles correctly!
Summary: Meshes
This is a reminder of what we learned about meshes, and an example of how to make a very simple one (with two triangles).
Next: Page 6 - Fake Normals