We discussed meshes in class. Here are the things you need to know.
The main concepts you need to know (from class):
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
Geometry, but a
Geometry is the collection of triangles that is more like the common graphics usage of the term.
Three has two types of Geometry. The regular
Geometry class and the
BufferGeometry class. We can't really talk about
BufferGeometry until we learn about buffers, so we'll use regular old
Geometry for now. A warning: most examples in THREE use
BufferGeometry, so it can be hard to find good examples. The examples in this workbook should be good enough to help you get started.
Geometry stores a Mesh as vertex-indexed set of faces (where the faces are triangles). The
Geometry class (documentation) stores a list of vertices (as
Vector3 positions). It also stores a list of Faces (as
Face3). Each face, refers to 3 vertices (by their index in the vertex array).
Faces have properties you would expect - like a color and a normal. However, they also contain properties of the vertices (colors and normals). This allows for "vertex splitting" (the same vertex can have different properties for different faces). Vertices don't have properties like color or normals by themselves (only inside of faces). To make things more confusing, texture coordinates (which are properties of vertices that are split like colors and normals) are stored in a completely different manner.
THREE has materials specifically designed to work with meshes. If you want materials such as
MeshStandardMaterial to use vertex or face colors, you need to tell it to do so. As far as I can tell, if you tell
MeshStandardMaterial that you want to use vertex colors, but you provide face colors instead, that's OK. Vertex colors override face colors (if you are in vertex color mode).
For normals, you can provide a normal for either the face or for each vertex on the face. THREE has a way to automatically compute the face normals (so they are normal to the triangle).
In this example, I've made a Mesh (
Geometry) with two triangles. For the first one, it uses the material color (yellow). For the second one, I've given it face colors. For the third, I'm using vertex colors.
Be sure to read the code in
1-colortriangles.js and understand how the meshes are put together.
This example uses the same two triangles. Except that this time, the normals are done differently for each of the 3 versions. On the left, THREE computes the normals for me (see the line
geometry.computeFaceNormals();). In the middle one, I assign each vertex on each face a normal value. This is the same normal that would have been computed for each triangle. The triangle on the right has the vertices in the middle (along the edge between the two triangles) pointing forward - to give 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 your the lighting behaves as if this were a curved object.
Be sure to read the code in
1-normaltriangles.js and understand how the meshes are put together.
This is a reminder of what we learned about Meshes, and an example of how to make a very simple one (with two triangles). On the next page, you'll get to try to make a more interesting mesh.