The lathe geometry class in threejs can be used to create a geometry using an array of 2d points that define a line that is to be repeated along an axis to form a solid shape. For example there is creating an array of vector2 objects that form an arc of a half circle and then using that as a way to form a sphere by passing this array of Vector2 object to the THREE.LatheGeometry constructor along with additional arguments that define the number of segments, and a start and length phi value.
There are all kinds of 3d shapes that can be formed by just coming up with a new 2d path of some kind, and passing it to the lathe geometry constructor. In fact the threejs core built in capsule geometry class is just an extension of the Lathe Geometry class. When it comes to making an array of vector2 objects there is just working out some expressions and helper functions to create the array of THREE.Vector2 objects, or there is using a 2d curve and calling the getPoints method or some other means when working with curves.
In other to get started with lathe geometry there is a whole lot of things that you might want to read up more on before getting into it. Also in any case there might be some closely related subjects that you might want to refresh on, or at least be prepared to do so to say the least. I have all ready wrote a blog post on getting started with threejs that I come around to edit a bit often so I will assume that you have the very basics down at least. So in this section I will just touch base on a few topics that are very relevant to Lathe Geometry alone.
Although what needs to be passed to the late geometry is an array of Vector2 objects that can be created by a wide range of ways. I have found that the base curve class, and the various built in extensions of the base curve class are very useful tools for doing what I want to create a 2d path. The getPoints method of the base curve class, or the getSpacedPoints method of the curve path class can be used to quickly create an array of vector2 objects from a curve. Also if for some reason I need to do some custom spacing between points along the curve I can do so with the getPoint method of the base class. Just about all of the time the built in options for curves work just fine for what I want to do, and in the event that they do not there is cretaing a custom extension of the base curve class.
The source code examples that I am writing about for this post can also be found in my test threejs repo up on Github. This is also the repo where I place the source code examples for all the other blog posts that I have wrote on threejs over the years.
When I first wrote this blog post I was using r152 of threejs and this was following the style rules that I set for that revision.
For this section I will be starting out with some very simply lathe geometry examples. The first step is to one way or another find a way to create the array of vector2 objects that are to be used to be used as I will want to have that to pass as the first argument. Once I have the vector2 array there is then just the question of what the additional arguments are and what I might want to adjust when it comes to those values.
First things first, before I even call the Lath Geometry constructor function I will first need an array of Vector2 objects to pass as the first argument. For this example I am using array split, array map, and parseFloat as ways to create the data that I will then feed to a few calls of THREE.Vector2 to get the values that I want. This way if I want to start making more complex shapes this way is just a matter of punching in more values in the string that I am creating the v2 array from. If you find this to be a little to involved for a basic example no problem if you would like to just call THREE.Vector2 for each element and use the array literal syntax that will work just fine also.
For this example I am making use of an ArcCurve that is one of the built in options for 2d curves in threejs. When I use this Arc curve with certain values for the start and end radian values I can get a half circle. The use of the half circle as a way to create the Vector2 objects can then be used with the lathe geometry as another way to create a kind of sphere geometry.
The ArcCurve class is actually just an abstraction for the Ellipse Curve class, so there is skipping the middle man and just working with that in place of the ArcCurve class. If I still want to make a sphere this way I can just set the same value for both radius values. However I can also of course use differing values to end up getting Oblate spheroid shapes.
I do not have to just go with one kind of curve or another mind you, there is also curve path objects as well. This allows for me to make a curve of curves using such options as LineCurve and QuadraticBezierCurve to create for example a kind of bowl like shape.
The Late geometry class is then a very useful tool for quickly making just about any kind of shape that can be made from a 2d path that is then rotated around on an axis. There are limitations though and I have a few ideas for another kind of geometry that is like lathe geometry only with more features that have to do with having custom phi values.