This week I have been taking a deeper look into what there is to work with when it comes to the Vector3 class in threejs, and today I thought I would work out a few demos with the apply to axis angle method. This is a prototype method of the Vector3 class, which will mutate the value of the Vector in place, and as the name suggests is has to do with rotating the vector along an axis that is defined with another vector that is the direction of the axis, and the second argument is then angle to apply with this given direction.
The thing to keep in mind here is that this is a Vector3 prototype method, so it has to do with changing the value of a vector, and not the state of a Euler, or Quaternion object when it comes to setting local rotation of objects. Vectors can be used to represent direction, and there is some over lap between Vectors and Euler angles, bit it is still a good idea to be aware of what the limitations are here. There will be situations now and then where I will want to do something like what the apply to axis method does, but by mutating the state of a Euler class instance.
In this post the focal point is just one little method in the Vector3 class prototype, there are many others that you should become familiar with at one point or another. There are also some basic things you should be aware of at this point such as the fact that the position property of the Object3d class is an instance of the Vector3 class. Also there are things like the fact that the object3d class is a base class for a whole lot of object in threejs such as Mesh objects, Cameras, and so forth. So it would make sense to read up more on the Vector3 class in general, and not just stop with this post when doing so.
The Vector3 class is used for, well, vectors in what might often be called Vector space. There is some overlap between position, and rotation, but when it comes to the rotation property of an Object3d class instance that is an instance of the Euler class. The Euler class id often a good start when it comes to working out things with local rotation of objects, but when it comes to really getting into rotations there is the Quaternion Class.
The source code examples that I am writing about in this post can also be found up on Github. This is also the repo where I store all the source code examples for my many other blog posts on threejs that I have wrote over the years.
When I wrote this post I was testing one the source code examples in r127 of threejs, and the last time I came around to do a little editing I was able to make updated examples that work well with r146. Always be mindful of the version of threejs you are using, and the version that the author of a source code examples was suing when it comes to threejs examples on the open web. This library is still moving very fast compare to many other projects, and code braking changes happen often.
For this section I will be starting out with a few basic examples of the apply axis angle method. Although I will be keeping these examples as simple as possible I still assume that you know enough as to how to get started with code examples like this. I will also be touching base on a lot of other features of the Vector3 class in this section as well as the usual features of threejs that are needed to see anything at all.
Here I have a mesh object that makes use of the cone geometry constructor, and the Mesh normal material. I am using the set method of the Vector3 class instance of the position property of the mesh to set the position to something other than that of 0,0,0.
After I have my mesh object added to the scene, and positioned, I am using the apply axis angle method using a vector with a direction that is going straight up on the y axis, and with a vector unit length of 1, but I am sure any length can be used. In addition to the vector that will serve as the axis, I can also pass an angle, and for now I am just pulling a full 180 for this example.
The first example in this section might have been an okay starting point for this sort of thing. However I have to say that I do not thing that I would often use the apply axis angle method alone but rather I might choose to use it along with a bunch of other vector3 class features. There is for example the copy method which is what I can use to copy one vector3 object to another, the normalize method that will set the vector unit length to 1, and then multiply scalar that will then scale a Vector to a desired unit length. You see this apply axis angle method is useful in the process of setting the direction of a vector, but there is also the length, magnitude, or distance if you prefer as well.
In this example then I have a helper function that I can use to create a mesh object of a cone, and then I am creating a whole bunch of cones by calling the array for each method off of a data array. This data array then contains data that I am feeding to another method that I am using to set the position of each of these cone objects that I am creating and adding to the scene object. This update cone helper the makes use of the apply axis angle, but then also method like copy that I use to set the start direction and unit length of the vector first. I am then also using normalize to set the length to 1 while preserving the direction, then setting the desired direction from there using the multiply scalar method.
So now that I have the basic example out of the way I think I should make at least one more demo of this method that will involve an animation loop.
In this example I am doing more or less the same thing as in the basic example only now I am just adding in a loop method that makes use of request animation frame to create a loop method. In this loop method I am calling an update function, that will use a values in seconds as the argument to update the state of something in this case the position property of a mesh using, you guessed it the apply axis angle Vector3 method.
For this animation loop example I will be working out something for the first demo video for this blog post. This will then just be a quick demo of the method that involves a group of mesh objects rather than just one mesh object. Also this time around I did a better job of starting to break things down more by having a few helper functions. With that said I have one function that creates a single mesh object, another that creates a group of mesh objects, and a final update method that will update the position of the mesh objects of the group. The update helper function is then the main function of interest here as this is where I am using the apply axis angle method to set the position of the mesh objects.
I am not sure if I will be using this method that often in future projects, not because I do not thing that it brings something of value, but because there are just many other ways to get a similar effect. Often I might want to change the orientation of an object in a way in which it is rotating on an axis, and to do that typically I will want to do something like this but with one of the angles of a Euler class instance rather than this method. Also even if I want to do something like this with a Vector3 class instance I might still want to do so with another method that might prove to be a little more robust, with additional options. With that said I have found that I do prefer to use the apply Euler method of the Vector3 class rather than this method.
Still taking a moment to look into the various methods to work with in the Vector3 class ins worth while, I am sure that I might not use all of them all the time, but there are many basic ones that will come in handy once in a while.