Learning to Work with Geometries
local graph
Chapter 5 — Learning to Work with Geometries
Section titled “Chapter 5 — Learning to Work with Geometries”In the previous chapters, you learned a lot about how to work with Three.js. Now you know how to create a basic scene, add lighting, and configure the material for your meshes. In Chapter 2, The Basic Components That Make Up a Three.js Scene, we touched upon (but didn’t really go into) the details of the available geometries that Three.js provides and that you can use to create your 3D objects. In this chapter and Chapter 6, Exploring Advanced Geometries, we’ll walk you through all the geometries that Three.js provides out of the box (except THREE.Line, which we discussed in Chapter 4, Working with Three.js Materials).
In this chapter, we’ll look at the following geometries:
THREE.CircleGeometryTHREE.RingGeometryTHREE.PlaneGeometryTHREE.ShapeGeometryTHREE.BoxGeometryTHREE.SphereGeometryTHREE.CylinderGeometryTHREE.ConeGeometryTHREE.TorusGeometryTHREE.TorusKnotGeometryTHREE.PolyhedronGeometryTHREE.IcosahedronGeometryTHREE.OctahedronGeometryTHREE.TetraHedronGeometryTHREE.DodecahedronGeometry
Before we look into the geometries provided by Three.js, we’ll first look a bit deeper into how Three.js represents geometries internally as THREE.BufferGeometry. In some documentation, you might still encounter THREE.Geometry as a base object for all geometries. This has been replaced completely in the newer version by THREE.BufferGeometry, which generally provides better performance, since it can easily get its data to the GPU. It is, however, a bit more difficult to use than the old THREE.Geometry.
With THREE.BufferGeometry, all properties of geometry are identified by a set of attributes. An attribute is basically an array with some additional metadata that contains information about the position of the vertices. Attributes are also used to store additional information about a vertex – for example, its color. To use an attribute to define a vertex and a face, you use the following two properties of THREE.BufferGeometry:
attributes: Theattributesproperties are used to store information that can be directly passed to the GPU. For instance, for defining a shape, you define aFloat32Array, where every three values define the position of a vertex.
Each of the three vertices is then interpreted as a face. This can be defined in THREE.BufferGeometry like this: geometry.setAttribute( 'position', new THREE.BufferAttribute( arrayOfVertices, 3 ) );.
index: By default, faces don’t need to be explicitly defined (every three consecutive positions are interpreted as a single face), but using theindexproperty, we can explicitly define which vertices together form a face:geometry.setIndex( ``indicesArray );.
For using the geometries in this chapter, you don’t need to think about these internal attributes, since Three.js takes care of setting them correctly when you construct a geometry. If you want to create a geometry from scratch, though, you need to use the properties shown in the previous list.
In Three.js, we have a couple of geometries that result in a 2D mesh, and a larger number of geometries that create a 3D mesh. In this chapter, we will discuss the following topics:
- 2D geometries
- 3D geometries
2D geometries
Section titled “2D geometries”2D objects look like flat objects and, as the name implies, only have two dimensions. In this section, we’ll first look at the 2D geometries: THREE.CircleGeometry, THREE.RingGeometry, THREE.PlaneGeometry, and THREE.ShapeGeometry.
THREE.PlaneGeometry
Section titled “THREE.PlaneGeometry”A THREE.PlaneGeometry object can be used to create a very simple 2D rectangle. For an example of this geometry, look at the plane-geometry.html example in the sources for this chapter. A rectangle that was created using THREE.PlaneGeometry is shown in the following screenshot:

Figure 5.1 – Plane geometry
In the examples for this chapter, we’ve added a control GUI that you can use to control the properties of the geometry (in this case, width, height, widthSegments, and heightSegments), and also change the material (and its properties), disable shadows, and hide the ground plane. For instance, if you want to see the individual faces of this shape, you can easily show them by disabling the ground plane and enabling the wireframe property of the selected material:

Figure 5.2 – Plane geometry as a wireframe
Creating a THREE.PlaneGeometry object is very simple, and is done as follows:
new THREE.PlaneGeometry(width, height, widthSegments, heightSegments)In this example for THREE.PlaneGeometry, you can change these properties and directly see the effect it has on the resulting 3D object. An explanation of these properties is shown in the following list:
width: This is the width of the rectangle.height: This is the height of the rectangle.widthSegments: This is the number of segments that the width should be divided into. This defaults to 1.heightSegments: This is the number of segments the height should be divided into. This defaults to 1.
As you can see, this is not a very complex geometry. You just specify the size, and you’re done. If you want to create more faces (for example, when you want to create a checkered pattern), you can use the widthSegments and heightSegments properties to divide the geometry into smaller faces.
Note
If you want to access the properties of a geometry after it has been created, you can’t just say plane.width. To access the properties of a geometry, you have to use the parameters property of the object. So, to get the width property of the plane object we created in this section, you’d have to use plane.parameters.width.
THREE.CircleGeometry
Section titled “THREE.CircleGeometry”You can probably already guess what THREE.CircleGeometry creates. With this geometry, you can create a very simple 2D circle (or part of a circle). First, let’s look at the example for this geometry, circle-geometry.html.
In the following screenshot, you can find an example where we created a THREE.CircleGeometry object with a thetaLength value that is smaller than 2 * PI:

Figure 5.3 – 2D circle geometry
In this example, you can see and control a mesh that’s been created by using THREE.CircleGeometry. 2 * PI represents a complete circle in radians. If you’d rather work with degrees than radians, converting between them is very easy.
The following two functions can help you to convert between radians and degrees, as follows:
const deg2rad = (degrees) => (degrees * Math.PI) / 180const rad2deg = (radians) => (radians * 180) / Math.PIWhen you create THREE.CircleGeometry, you can specify a few properties that define what the circle looks like, as follows:
radius: The radius of a circle defines its size. The radius is the distance from the center of the circle to its edge. The default value is 50.segments: This property defines the number of faces that are used to create the circle. The minimum number is 3, and if not specified, this number defaults to 8. A higher value means a smoother circle.thetaStart: This property defines the position from which to start drawing the circle. This value can range from 0 to2 * PI, and the default value is 0.thetaLength: This property defines to what extent the circle is complete. This defaults to2 * PI(a full circle) when not specified. For instance, if you specify0.5 * PIfor this value, you’ll get a quarter circle. Use this property together with thethetaStartproperty to define the shape of a circle.
You can create a full circle by just specifying the radius and the segments:
new THREE.CircleGeometry(3, 12)If you wanted to create half a circle from this geometry, you’d use something like this:
new THREE.CircleGeometry(3, 12, 0, Math.PI);This creates a circle with a radius of 3 that is split into 12 segments. The circle starts at a default of 0 and is only drawn halfway since we specify thetaLength as Math.PI, which is half a circle.
Before moving on to the next geometry, here’s a quick note on the orientation that Three.js uses when creating these 2D shapes (THREE.PlaneGeometry, THREE.CircleGeometry, THREE.RingGeometry, and THREE.ShapeGeometry): Three.js creates these objects standing up, so they are aligned along the x - y plane. This is very logical since they are 2D shapes. However, often, especially with THREE.PlaneGeometry, you want to have the mesh lying down on the ground (the x - z plane), as some sort of ground area on which you can position the rest of your objects. The easiest way to create a 2D object that is horizontally orientated instead of vertical is by rotating the mesh a quarter rotation backward (-PI/2) around its x- axis, as follows:
mesh.rotation.x =- Math.PI/2;That’s all for THREE.CircleGeometry. The next geometry, THREE.RingGeometry, looks a lot like THREE.CircleGeometry.
THREE.RingGeometry
Section titled “THREE.RingGeometry”With THREE.RingGeometry, you can create a 2D object that not only closely resembles THREE.CircleGeometry, but also allows you to define a hole in the center (see ring-geometry.html):

Figure 5.4 – 2D ring geometry
When creating a THREE.RingGeometry object, you can use the following properties:
innerRadius: The inner radius of a circle defines the size of the center hole. If this property is set to0, no hole will be shown. The default value is 0.outerRadius: The outer radius of a circle defines its size. The radius is the distance from the center of the circle to its edge. The default value is 50.thetaSegments: This is the number of diagonal segments that will be used to create the circle. A higher value means a smoother ring. The default value is 8.phiSegments: This is the number of segments required to be used along the length of the ring. The default value is 8. This doesn’t really affect the smoothness of the circle but increases the number of faces.thetaStart: This defines the position from which to start drawing the circle. This value can range from 0 to2 * PI, and the default value is 0.thetaLength: This defines the extent to which the circle is complete. This defaults to2 * PI(a full circle) when not specified. For instance, if you specify0.5 * PIfor this value, you’ll get a quarter circle. Use this property together with thethetaStartproperty to define the shape of the ring.
The following screenshot shows how thetaStart and thetaLength work with THREE.RingGeometry:

Figure 5.5 – 2D ring geometry with different theta
In the next section, we’ll look at the last of the 2D shapes: THREE.ShapeGeometry.
THREE.ShapeGeometry
Section titled “THREE.ShapeGeometry”THREE.PlaneGeometry and THREE.CircleGeometry have limited ways of customizing their appearance. If you want to create custom 2D shapes, you can use THREE.ShapeGeometry.
With THREE.ShapeGeometry, you have a couple of functions you can call to create your own shapes. You can compare this functionality with the <path/> element functionality, which is also available to the HTML canvas element and SVG. Let’s start with an example and after that, we’ll show you how you can use the various functions to draw your own shape. The shape-geometry.html example can be found in the sources of this chapter.
The following screenshot shows this example:

Figure 5.6 – A custom shape geometry
In this example, you can see a custom-created 2D shape. Before going into the description of the properties, first, let’s look at the code that is used to create this shape. Before we create a THREE.ShapeGeometry object, we first have to create a THREE.Shape object. You can trace these steps by looking at the previous screenshot, where we start in the bottom-right corner. Here’s how we created a THREE.Shape object:
const drawShape = () => { // create a basic shape const shape = new THREE.Shape() // startpoint // straight line upwards shape.lineTo(10, 40) // the top of the figure, curve to the right shape.bezierCurveTo(15, 25, 25, 25, 30, 40) // spline back down shape.splineThru([new THREE.Vector2(32, 30), new THREE.Vector2(28, 20), new THREE.Vector2(30, 10)]) // add 'eye' hole one const hole1 = new THREE.Path() hole1.absellipse(16, 24, 2, 3, 0, Math.PI * 2, true) shape.holes.push(hole1) // add 'eye hole 2' const hole2 = new THREE.Path() hole2.absellipse(23, 24, 2, 3, 0, Math.PI * 2, true) shape.holes.push(hole2) // add 'mouth' const hole3 = new THREE.Path() hole3.absarc(20, 16, 2, 0, Math.PI, true) shape.holes.push(hole3) return shape}In this piece of code, you can see that we created the outline of this shape using lines, curves, and splines. After that, we punched a number of holes into this shape by using the holes property of THREE.Shape.
In this section, though, we’re talking about THREE.ShapeGeometry and not THREE.Shape – to create a geometry from THREE.Shape, we need to pass in THREE.Shape (returned, in our case, from the drawShape() function) as the argument to THREE.ShapeGeometry. You can also pass in an array of a THREE.Shape object, but in our example, we only use one object:
new THREE.ShapeGeometry(drawShape())The result of this function is a geometry that can be used to create a mesh. Besides the shape you want to convert into THREE.ShapeGeometry, you can also pass in a number of additional options objects as the second argument:
curveSegments: This property determines how smooth the curves created from the shape are. The default value is 12.material: This is thematerialIndexproperty that is used for the faces that are created for the specified shapes. So, if you pass in multiple materials, you can specify which of those materials should be applied to the faces of the shape that is created.UVGenerator: When you use a texture with your material, the UV mapping determines what part of a texture is used for a specific face. With theUVGeneratorproperty, you can pass in your own object, which will create the UV settings for the faces that are created for the shapes passed in. More information on UV settings can be found in Chapter 10, Loading and Working with Textures. If none are specified,THREE.ExtrudeGeometry.WorldUVGeneratoris used.
The most important part of THREE.ShapeGeometry is THREE.Shape, which you use to create the shape, so let’s look at the list of drawing functions you can use to create THREE.Shape:
moveTo(x,y): Move the drawing position to thex-andy-coordinates that are specified.lineTo(x,y): Draw a line from the current position (for example, set by themoveTofunction) to thex-andy-coordinates that have been provided.quadraticCurveTo(aCPx, aCPy, x, y): There are two different ways of specifying curves. You can use thequadraticCurveTofunction, or you can use thebezierCurveTofunction. The difference between these two functions is how you specify the curvature of the curve.
For a quadratic curve, we need to specify one additional point (using the aCPx and aCPy arguments), and the curve is based solely on that point and, of course, the specified endpoint (from the x and y arguments). For a cubic curve (used by the bezierCurveTo function), you specify two additional points to define the curve. The starting point is the current position of the path.
The following diagram explains the differences between these two options:

Figure 5.7 – Quadratic bezier and cubic bezier
bezierCurveTo(aCPx1, aCPy1, aCPx2, aCPy2, x, y): This draws a curve based on the arguments supplied. For an explanation, see the previous list entry. The curve is drawn based on the two coordinates that define the curve (aCPx1,aCPy1,aCPx2, andaCPy2) and the end coordinates (xandy). The starting point is the current position of the path.splineThru(pts): This function draws a fluid line through the set of coordinates provided, (pts). This argument should be an array ofTHREE.Vector2objects. The starting point is the current position of the path.arc(aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise): This draws a circle (or part of a circle). The circle starts from the current position of the path. Here,aXandaYare used as offsets from the current position. Note thataRadiussets the size of the circle andaStartAngleandaEndAngledefine how large a part of the circle is drawn. The BooleanaClockwiseproperty determines whether the circle is drawn clockwise or counterclockwise.absArc(aX, aY, aRadius, aStartAngle, aEndAngle, AClockwise): See the description of thearcproperty. The position is absolute instead of relative to the current position.ellipse(aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise): See the description of thearcproperty. As an addition, with theellipsefunction, we can separately set thexradius and theyradius.absEllipse(aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise): See the description of theellipseproperty. The position is absolute instead of relative to the current position.fromPoints(vectors): If you pass in an array ofTHREE.Vector2(orTHREE.Vector3) objects into this function, Three.js will create a path using straight lines from the supplied vectors.holes: Theholesproperty contains an array ofTHREE.Shapeobjects. Each of the objects in this array is rendered as a hole. A good example of this is the example we saw at the beginning of this section. In that code fragment, we added threeTHREE.Shapeobjects to this array: one for the left eye, one for the right eye, and one for the mouth of our mainTHREE.Shapeobject.
Just like for a lot of examples, to understand how the various properties affect the final shape, the easiest way is to just enable the wireframe property on the material and play around with the settings. For example, the following screenshot shows what happens when you use a low value for curveSegments:

Figure 5.8 – The wireframe of a shape geometry
As you can see, the shape loses its nice, round edges, but uses a lot fewer faces in the process. That’s it for the 2D shapes. The following sections will show and explain the basic 3D shapes.
3D geometries
Section titled “3D geometries”In this section on basic 3D geometries, we’ll start with a geometry we’ve already seen a couple of times: THREE.BoxGeometry.
THREE.BoxGeometry
Section titled “THREE.BoxGeometry”THREE.BoxGeometry is a very simple 3D geometry that allows you to create a box by specifying its width, height, and depth properties. We’ve added an example, box-geometry.html, where you can play around with these properties. The following screenshot shows this geometry:

Figure 5.9 – Basic 3D box geometry
As you can see in this example, by changing the width, height, and depth properties of THREE.BoxGeometry, you can control the size of the resulting mesh. These three properties are also mandatory when you create a new cube, as follows:
new THREE.BoxGeometry(10,10,10);In the example, you can also see a couple of other properties that you can define on the cube. The following list explains all the properties:
width: This is the width of the cube. This is the length of the vertices of the cube along the x- axis.height: This is the height of the cube. This is the length of the vertices of the cube along the y- axis.depth: This is the depth of the cube. This is the length of the vertices of the cube along the z- axis.widthSegments: This is the number of segments into which we divide a face along the cube’s x- axis. The default value is 1. The more segments you define, the more faces a side has. If this property and the next two are set to1, each side of the cube will just have 2 faces. If this property is set to2, the face will be divided into 2 segments, resulting in 4 faces.heightSegments: This is the number of segments into which we divide a face along the cube’s y- axis. The default value is 1.depthSegments: This is the number of segments into which we divide a face along the cube’s z- axis. The default value is 1.
By increasing the various segment properties, you divide the six main faces of the cube into smaller faces. This is useful if you want to set specific material properties on parts of the cube using THREE.MeshFaceMaterial.
THREE.BoxGeometry is a very simple geometry. Another simple one is THREE.SphereGeometry.
THREE.SphereGeometry
Section titled “THREE.SphereGeometry”With THREE.SphereGeometry, you can create a 3D sphere. Let’s dive straight into the example, sphere-geometry.html:

Figure 5.10 – Simple 3D sphere geometry
In the previous screenshot, we have shown you a half-open sphere that was created based on THREE.SphereGeometry. This geometry is a very flexible one and can be used to create all kinds of sphere-related geometries. A basic THREE.SphereGeometry instance, though, can be created as easily as this: new THREE.SphereGeometry().
The following properties can be used to tune what the resulting mesh looks like:
radius: This is used to set the radius for the sphere. This defines how large the resulting mesh will be. The default value is 50.widthSegments: This is the number of segments to be used vertically. More segments mean a smoother surface. The default value is 8 and the minimum value is 3.heightSegments: This is the number of segments to be used horizontally. The more segments, the smoother the surface of the sphere. The default value is 6 and the minimum value is 2.phiStart: This determines where to start drawing the sphere along its x- axis. This can range from 0 to2 * PI. The default value is0.phiLength: This determines how far fromphiStartthe sphere is to be drawn.2 * PIwill draw a full sphere and0.5 * PIwill draw an open quarter-sphere. The default value is2 *PI.thetaStart: This determines where to start drawing the sphere along its x- axis. This can range from 0 to2 * PI, and the default value is 0.thetaLength: This determines how far fromthetaStartthe sphere is drawn. The2 * PIvalue is a full sphere, whereasPIwill draw only half of the sphere. The default value is2 *PI.
The radius, widthSegments, and heightSegments properties should be clear. We’ve already seen these kinds of properties in other examples. The phiStart, phiLength, thetaStart, and thetaLength properties are a bit harder to understand without looking at an example. Luckily, though, you can experiment with these properties from the menu in the sphere-geometry.html example and create interesting geometries such as these:

Figure 5.11 – Sphere geometry with different phi and theta properties
The next one on the list is THREE.CylinderGeometry.
THREE.CylinderGeometry
Section titled “THREE.CylinderGeometry”With this geometry, we can create cylinders and cylindrical objects. As for all the other geometries, we also have an example (cylinder-geometry.html) that lets you experiment with the properties of this geometry, the screenshot for which is as follows:

Figure 5.12 – 3D cylinder geometry
When you create THREE.CylinderGeometry, there aren’t any mandatory arguments, so you can create a cylinder by just calling new THREE.CylinderGeometry(). You can pass in a number of properties, as you can see in the preceding example, to alter the appearance of this cylinder. The properties are explained in the following list:
radiusTop: This sets the size that this cylinder will be at the top. The default value is 20.radiusBottom: This sets the size that this cylinder will be at the bottom. The default value is 20.height: This property sets the height of the cylinder. The default height is 100.radialSegments: This determines the number of segments along the radius of the cylinder. This defaults to 8. More segments mean a smoother cylinder.heightSegments: This determines the number of segments along the height of the cylinder. The default value is 1. More segments mean more faces.openEnded: This determines whether or not the mesh is closed at the top and the bottom. The default value isfalse.thetaStart: This determines where to start drawing the cylinder along its x- axis. This can range from 0 to2 * PI, and the default value is 0.thetaLength: This determines how far fromthetaStartthe cylinder is drawn. The2 * PIvalue is a full cylinder, whereasPIwill draw only half of a cylinder. The default value is2 *PI.
These are all very basic properties that you can use to configure a cylinder. One interesting aspect, though, is when you use a negative radius value for the top (or for the bottom). If you do this, you can use this geometry to create an hourglass-like shape, as shown in the following screenshot:

Figure 5.13 – 3D cylinder geometry as an hourglass
One thing to note here is that the top half, in this case, is turned inside out. If you use a material that isn’t configured with THREE.DoubleSide, you won’t see the top half.
The next geometry is THREE.ConeGeometry, which provides the basic functionalities of THREE.CylinderGeometry, but has the top radius fixed to zero.
THREE.ConeGeometry
Section titled “THREE.ConeGeometry”THREE.ConeGeometry is pretty much the same as THREE.CylinderGeometry. It uses all the same properties, except it only allows you to set the radius instead of separate radiusTop and radiusBottom values:

Figure 5.14 – Simple 3D cone geometry
The following properties can be set on THREE.ConeGeometry:
radius: This sets the size that this cylinder will be at the bottom. The default value is20.height: This property sets the height of the cylinder. The default height is 100.radialSegments: This determines the number of segments along the radius of the cylinder. This defaults to 8. More segments mean a smoother cylinder.heightSegments: This determines the number of segments along the height of the cylinder. The default value is 1. More segments mean more faces.openEnded: This determines whether or not the mesh is closed at the top and the bottom. The default value isfalse.thetaStart: This determines where to start drawing the cylinder along its x- axis. This can range from 0 to2 * PI, and the default value is 0.thetaLength: This determines how far fromthetaStartthe cylinder is drawn. The2 * PIvalue is a full cylinder, whereasPIwill draw only half of a cylinder. The default value is2 *PI.
The next geometry, THREE.TorusGeometry, allows you to create a donut-like shaped object.
THREE.TorusGeometry
Section titled “THREE.TorusGeometry”A torus is a simple shape that looks like a donut. The following screenshot, which you can get yourself by opening the torus-geometry.html example, shows THREE.TorusGeometry in action:

Figure 5.15 – 3D torus geometry
Just like most of the simple geometries, there aren’t any mandatory arguments when creating THREE.TorusGeometry. The following list mentions the arguments you can specify when you create this geometry:
radius: This sets the size of the complete torus. The default value is 100.tube: This sets the radius of the tube (the actual donut). The default value for this attribute is 40.radialSegments: This determines the number of segments to be used along the length of the torus. The default value is 8. See the effect of changing this value in the example.tubularSegments: This determines the number of segments to be used along the width of the torus. The default value is 6. See the effect of changing this value in the example.arc: With this property, you can control whether the torus draws a full circle. The default of this value is2 * PI(a full circle).
Most of these are very basic properties that you’ve already seen. The arc property, however, is a very interesting one. With this property, you can define whether the donut makes a full circle or only a partial one. By experimenting with this property, you can create very interesting meshes, such as the following one with arc set to a value lower than 2 * PI:

Figure 5.16 – 3D torus geometry with a smaller arc property
THREE.TorusGeometry is a very straightforward geometry. In the next section, we’ll look at a geometry that almost shares its name but is much less straightforward: THREE.TorusKnotGeometry.
THREE.TorusKnotGeometry
Section titled “THREE.TorusKnotGeometry”With THREE.TorusKnotGeometry, you can create a torus knot. A torus knot is a special kind of knot that looks like a tube that winds around itself a couple of times. The best way to explain this is by looking at the torus-knot-geometry.html example. The following screenshot shows this geometry:

Figure 5.17 – Torus knot geometry
If you open this example and play around with the p and q properties, you can create all kinds of beautiful geometries. The p property defines how often the knot winds around its axis, and q defines how much the knot winds around its interior.
If this sounds a bit vague, don’t worry. You don’t need to understand these properties to create beautiful knots, such as the one shown in the following screenshot (for those interested in the details, Wolfram has a good article on this subject at https://mathworld.wolfram.com/TorusKnot.html):

Figure 5.18 – Torus knot geometry with different p and q values
With the example for this geometry, you can play around with the following properties and see the effect that various combinations of p and q have on this geometry:
radius: This sets the size of the complete torus. The default value is 100.tube: This sets the radius of the tube (the actual donut). The default value for this attribute is 40.radialSegments: This determines the number of segments to be used along the length of the torus knot. The default value is 64. See the effect of changing this value in the demo.tubularSegments: This determines the number of segments to be used along the width of the torus knot. The default value is 8. See the effect of changing this value in the demo.p: This defines the shape of the knot, and the default value is 2.q: This defines the shape of the knot, and the default value is 3.heightScale: With this property, you can stretch out the torus knot. The default value is 1.
The next geometry on the list is the last one of the basic geometries: THREE.PolyhedronGeometry.
THREE.PolyhedronGeometry
Section titled “THREE.PolyhedronGeometry”With this geometry, you can easily create polyhedrons. A polyhedron is a geometry that has only flat faces and straight edges. Most often, though, you won’t use THREE.PolyhedronGeometry directly. Three.js provides a number of specific polyhedrons you can use without having to specify the vertices and faces of THREE.PolyhedronGeometry. We’ll discuss these polyhedrons later on in this section.
If you do want to use THREE.PolyhedronGeometry directly, you have to specify the vertices and the faces (just as we did for the cube in Chapter 3, Working with Light Sources in Three.js). For instance, we can create a simple tetrahedron (also see the section on THREE.TetrahedronGeometry in this chapter) like this:
const vertices = [ 1, 1, 1,-1, -1, 1,-1, 1, -1, 1, -1, -1];const indices = [2, 1, 0,0, 3, 2,1, 3, 0,2, 3, 1];new THREE.PolyhedronBufferGeometry(vertices, indices, radius, detail)To construct THREE.PolyhedronGeometry, we pass in the vertices, indices, radius, and detail properties. The resulting THREE.PolyhedronGeometry object is shown in the polyhedron-geometry.html example:

Figure 5.19 – Custom polyhedron
When you create a polyhedron, you can pass in the following four properties:
vertices: These are the points that make up the polyhedron.indices: These are the faces that need to be created from the vertices.radius: This is the size of the polyhedron. This defaults to 1.detail: With this property, you can add additional detail to the polyhedron. If you set this to1, each triangle in the polyhedron will be split into 4 smaller triangles. If you set this to2, those 4 smaller triangles will each be split into 4 smaller triangles once more, and so on.
The following screenshot shows the same custom mesh, but now with a higher level of detail:

Figure 5.20 – A custom polyhedron with a higher level of detai
At the beginning of this section, we mentioned that Three.js comes with a couple of polyhedrons out of the box. In the following subsections, we’ll quickly show you these. All of these polyhedron types can be viewed by looking at the polyhedron-geometry.html example.
THREE.IcosahedronGeometry
Section titled “THREE.IcosahedronGeometry”THREE.IcosahedronGeometry creates a polyhedron that has 20 identical triangular faces created from 12 vertices. When creating this polyhedron, all you need to specify are the radius and the detail level. This screenshot shows a polyhedron that was created using THREE.IcosahedronGeometry:

Figure 5.21 – 3D icosahedron geometry
The next polyhedron we’re going to look at is THREE.TetrahedronGeometry.
THREE.TetrahedronGeometry
Section titled “THREE.TetrahedronGeometry”The tetrahedron is one of the simplest polyhedrons. This polyhedron only contains four triangular faces that are created from four vertices. You can create THREE.TetrahedronGeometry just like the other polyhedrons provided by Three.js, by specifying the radius and detail levels. Here’s a screenshot that shows a tetrahedron that was created using THREE.TetrahedronGeometry:

Figure 5.22 – 3D tetrahedron geometry
Next, we will see a polyhedron with eight faces: an octahedron.
THREE.OctahedronGeometry
Section titled “THREE.OctahedronGeometry”Three.js also provides an implementation of an octahedron. As the name implies, this polyhedron has eight faces. These faces are created from six vertices. The following screenshot shows this geometry:

Figure 5.23 – 3D octahedron geometry
The last of the polyhedrons we’re going to look at is a dodecahedron.
THREE.DodecahedronGeometry
Section titled “THREE.DodecahedronGeometry”The final polyhedron geometry provided by Three.js is THREE.DodecahedronGeometry. This polyhedron has 12 faces. The following screenshot shows this geometry:

Figure 5.24 – 3D dodecahedron geometry
As you can see, Three.js provides a large number of 3D geometries, from directly useful ones such as spheres and cubes to geometries that probably aren’t that useful in practice, such as the set of polyhedrons. In any case, these 3D geometries will give you a good starting point for creating and experimenting with materials, geometries, and 3D scenes.
Summary
Section titled “Summary”In this chapter, we discussed all of the standard geometries that Three.js has to offer. As you saw, there are a whole lot of geometries you can use right out of the box. To best learn how to use the geometries, experiment with them. Use the examples in this chapter to get to know the properties you can use to customize the standard set of geometries available from Three.js.
For 2D shapes, it’s important to remember that they are placed on the x - y plane. If you want to have a 2D shape horizontally, you’ll have to rotate the mesh around the x- axis for -0.5 * PI. And finally, take care that if you’re rotating a 2D shape, or a 3D shape that is open (for example, a cylinder or a tube), remember to set the material to THREE.DoubleSide. If you don’t do this, the inside or the back of your geometry won’t be shown.
In this chapter, we focused on simple, straightforward meshes. Three.js also provides ways to create complex geometries, which we will cover in Chapter 6.