A mesh object in three.js is used to create an object with a buffer geometry, and a material such as the mesh basic material. This resulting mesh object can then be added as a child of a main scene object, and then the scene object can be used along with a camera to render a view of the scene which will include one or more of these mesh objects.
The Mesh Constructor is one of many constructor functions that I find myself using often as I get into making threejs projects. What is great about mesh objects is that they are one of many objects in threejs that have the Object3d class as a base class. So then when it comes to something like learning how to use the position property of a mesh object, what one is really learning about is an object3d class feature. Knowledge of this object3d class feature can then also be applied to cameras, groups, and anything else in threejs that is based off of the object3d class. Still there are some things that are just a part of mesh objects alone, mainly the buffer geometry and material properties of such objects.
The mesh is then an object that contains references to the geometry and materials that are used to compose the over all content of the mesh object. So on top of knowing a thing or two about geometry it also pays to learn more about what the options are when it comes to materials. I think that a good starting option for materials might be the normal material as it will help to show some sense of depth and also does not require a light source to work. However in the long run there is learning how to use the basic material with textures or lines, or better yet maybe the Phong material that is a good choice for using one or more light sources.
A mesh object is based off of the Object3d class, and there are many additional objects in threejs that are based off of it beyond just that of mesh objects. The Vector3 class also comes up a lot in code examples of threejs which is used for creating and working with a vector, or point in 3d space. With a mesh object the position property of a mesh is an instance of vector3 and that is what can be used to set and change the position of a mesh Object. Another property of instance for mesh objects is the scale property which is also an instance of this Vector3 class.
There is also the Euler class that is like vector3 only we are dealing with angles rather than a position. So with that said the rotation property of a mesh object, as well as anything else based off of object 3d class.
The source code examples that I am writing about here can be found in my test threejs Github repository.
The last time I edited this post I was using version r135 of three.js, and when I first wrote this post I was using version r91 of threejs. Between r91 and r135 a whole lot of code breaking changes have happened, so always check what version of three.js you are using when looking at old code examples of three.js on the open web.
A Basic example of using a mesh would involve creating an instance of a Mesh with the THREE.Mesh constructor, passing it the geometry that I want to use. Be default the basic material will be used with a random color, so if I want to use something else for a material then you I want to pass that to the Mesh Constructor as the second argument. The result can then be saved to a variable, or just directly added to the scene as there are ways of still getting a reference to the mesh by way of the children property of the scene object.
So then the Basic idea here is to create a scene object, then create and add a Mesh object to the scene object. However in order to see the mesh I am going to need a camera, for this there are a few options but I typically like to go with the perspective camera. When I add the camera to the scene I am going to want to make sure that I position the camera away from the mesh so that it is not inside the mesh. After that I am going to need some kind of Renderer such as the built in WebGLRenderer. I then just need to call the render method of the renderer and pass the scene and camera to use.
So then this is a basic hello world type example of three.js where I am just looking at a cube. Now that I have that out of the way I can start to get to some more complex examples.
It is important to note that THREE.Mesh is just one of many constructors in three.js that inherit from Object3D which would be worth checking out in detail because much of what applies for a mesh will also apply for a camera, groups, a light source, and even a whole scene because all those things are built on top of Object3d. However for now it is a good idea to just know that Object3D brings a whole bunch of methods, and properties to THREE.Mesh that can be used to do things like moving the mesh around, and changing its rotation.
Here I am using the Object3D position property that stores an instance of Vector3 that can be used to change what should be the center point of the Mesh geometry assuming it has been normalized. That might come off as a mouth full so maybe another way of explaining it is that there is a point in space in which the geometry of the mesh is relative to. The position property can be used to change the value of that point in space.
Also In this demo I am using the rotation property, which is another useful property that is inherited from, use guessed it, Object3D. This rotation property stores an instance of the Euler class which is like vercor3 only we are taking angles rather than a matrix position.
One thing about a mesh is that a material index can be with and array of materials when skinning a geometry of a mesh. So then it is possible to pass an array of materials rather than just a single instance of some kind of mesh material. When doing this the material index value of the face instances in the geometry is of interest when it comes to assigning what material is used for what face of the geometry.
I have a post on this in which I get into this in detail but I can also provide a basic example of this here.
So then the process of skinning a mesh is just a matter of passing an array of materials to the mesh constructor rather than just a single material. After that it is just a question of making sure that the material index values are what they should be when it comes to the instance of the geometry that is being used with the mesh.
There will comes times now and then in which I will want to make a single mesh object and then make many copies of that single mesh object. For this task there is the clone method of the mesh object that can be used to create a kind of shallow copy of a mesh object. What I mean by a sallow copy is that this will be a copy of the mesh object itself but not always all nested objects of that mesh object, at least not when it comes to the material as this example will demonstrate.
Another method of interest that I think I should touch base with in this post is the look at method of the object 3d class. This is a method that I have used with the camera object in just about all of the examples in this post thus far. Because it is often used with a camera object new developers might assume that this is a method of a camera object, but it is actually a method of the object3d class. because the mesh object like a camera is also based off of object 3d I can also use this look at method as a way to have a mesh object face a position in word space.
However when it comes to a mesh object I might often need to adjust what the ‘front’ of the geometry is, for this tasks I will want to use the rotate methods of an instance of buffer geometry.
There is not much more to write about with Mesh, at least not at this time. However that is not at all the case with many other topics that branch off from Mesh such as geometry, materials, Object3D, Vector3, the Scene object, and many more just when it comes to the basics of three.js.
Once the basics are out of the way though it is then time to look into starting some actual projects of some kind. There is getting into making games, and also all kinds of fun animation type projects.