The THREE.ExtrudeGeometry class in threejs is one of the built in geometry classes that can be used to create a buffer geometry from a THREE.Shape object. The other built in option to create a geometry form a shape is the THREE.ShapeGeometry class that is just a simple plain of the 2d shape.
Getting started with this feature of threejs is easy enough when it comes to just creating a simple shape object and passing that as the first argument and just going with that. However there is a whole lot more beyond just that of course and just like everything else in threejs this can quickly turn into a major time and energy sucking black hole if one allows it to turn into that. This might prove to be the case with far more advanced topics that branch off from Extrude geometry when it comes to the topic of the custom UV generator option of the geometry constructor for example.
In order to create an istance of THREE.ExtrudeGeometry you will first need a THREE.Shape instance to pass as the first argument. I will be wriitng about THREE.Shape a little in this post as I have to, however you might still want to look into this feature of threejs a bit more. While you are at it there is also reading more on the path class as well sense that is the base class of Shape to which Shape extends from.
The Vector2 class is also something that you might want to read more about as there are a lot of methods to work with in that class that and prove to be useful in the process of creating paths, or curves, than can then be used to make shapes that can then be used to make extrude geometry.
The SVG loader is an optional add on to the core threejs module that helps with the process of loading, and then parsing SVG files into Shape Objects that can then be used to create extrude geometry. Often I might want to create extrude geometry this way actually at least when it comes to the 2d shape part that will be used to create such a geometry.
The code exmaples that I made for this post can also be found in my test threejs repo on Github. This is also where I place the source code examples for the many other blog posts that I have wrote on threejs thus far.
The version of threejs that I was using when I first wrote this blog post was r152.
For this Basic section I am going to just write about a few basic examples of extrude geometry. This will then have to involve at least a few examples of how to create a Shape Object to begin with, as well as touch base on some of the additional options when calling THREE.ExtrudeGeometry. However I will not be writing about some of the options, mainly the option for a custom UV generator as that is without question way to complex for a basic section.
One way or another, in order to create an Extrude Geometry I first need to have a Shape object to pass as the first argument. One way to create a shape object is to call THREE.Shape, and pass an array of vector2 objects as the first argument when calling that to create the shape. Once I have the shape I can then just pass that to THREE.ExtrudeGeometry to ge the geometry that I can then in turn use with the usual mesh object.
For this demo I am not going to do anything fancy when it comes to creating the array of Vector2 objects. This is after all the very first demo of this basic section so I am just directly calling THREE.Vector2 for each point that will compose the shape object. There are a lot of other ways to create this kind of array actually, but when it comes to just getting started this will do for now when it comes to just getting started. Also for this demo I have not got into any of the options of extrude geometry just yet, so lets take a look at some more basic, simple, static examples.
Taking a look at the threejs source code for THREE.Shape it would seem that the Shape class does in fact extend the base THREE.Path class. So another way to create the shape to begin with is to just create a blank THREE.Shape object and just start using these Path class prototype methods by just calling them off of the shape object. This Path class is very similar to that of the paths of the 2D drawing context, so if you know how to use that this will be easy for you. Otherwise you start out by using the move to method to pick a start location for the path and then start using path prototype methods like lineTo and bezierCurveTo to create the path. Again once I have the shape set up just the way that I like it I can then just pass that to THREE.ExtrudeGeometry.
I think that the most note worthy advanced feature of THREE.ExtrudeGeometry would be the UVGenerator option. To use this I need to give an object with two functions that both return an array of vector2 objects that will in turn be used to create the UV attribute for the buffer geometry that is created by THREE.ExtrudeGeometry. If you have no idea at all what a UV attribute is I have my main blog post on the subject in which I get into detail with this, but to save you a click the skinny is that it is a way to map a 2D image to a 3d object.
I have found that the default UV Generator just about never works the way that I would like it to. So then it would make sense to take advantage of this option and see about making at least a few custom UV generators. Doing so is a little involved, and there are some tools that I would say help to make the process of working out logic for this sort of thing easier. In my main blog post on UV Mapping I have a whole section on some code that has to do with creating a kind of minimap that helps me get a way better idea of what is going on with the current state of the UV attribute. That feature will also be showing up in these demos as well.
This is then the demo that I have that is just a start point for this custom UV Generator feature of extrude geometry. For this first demo I will just want to have a very basic shape, and I am also going to want to just have static UV values for each triangle. This just seems like the best way to get started. Also in some cases it will also be more or less what it is that I will want to do anyway. If the nature of the texture is just this repeating pattern, and if it is okay for all triangles to make use of the same area of the texture something like this will work just fine and I can move on. In any case one needs to start somewhere with this, and if they are lucky this will also be the end of it as well.
I wanted to start working out some decent logic for the top parts ate least for this next demo. For now I think it will still be okay for both sides of the shape to overlap the same area, however I would like for that to not be the case for each triangle of each shape at least. When and if I get to it I might further improve these demos when it comes to scaling and positioning the various parts of the uvs for the top part, as well as work out some logic for the side parts as well.
This time I now have a helper function that will create and return the object that will then be passed in the options object of the extrude geometry constructor.
The Extrude geometry is then often the main constructor if interest that I might want to use when it comes to doing something with a Shape object that was created by one means or another. When it comes to a real project though there is a whole lot of other features of the library that I would want to use. Also things can get a little time consuming when it comes to certain advanced features of the extrude geometry class such as working out a custom UV Generator.