Sphere circles Lines threejs example
When it comes to making lines in threejs I wanted to make a threejs example in which I have a collection of lines that form a sphere like shape. So the general idea is to make a JavaScript module that has a create method that will return a group of lines, where each line is one circle that forms something that will look like a sphere when it comes to the over all shape of the collection of lines.
I can then also add a method that can be used to update the state of this group of lines with a new set of options as a way to make various kinds of animations. So there is starting out with the basic idea of what i want to do, and then also maybe make a kind of module form of this example. Once I have a module form of this genera idea I can then make all kinds of additional demos that make use of the module to create, and update the state of the lines.
Sphere Circle Lines threejs example and what to know first
This is a post on some javaScript code built on top of the library known as threejs that has to do with making a collection of lines that look like a sphere. This is one of my many threejs project examples that I have made thus far that is for developers that have a fair amount of experience with javaScript and threejs and are now looking into ideas for projects. This is then not a post for people that are still fairly new with threejs. As such I will not be getting into basic things that you should know about at this point, however in this section I will be going over a few things you might want to read up more on that are related to what I am doing here.
Read up more on the THREE.Line constructor, and buffer geometry
In this example I am creating an instance of THREE.Group and adding a bunch of Lines as children for this group. The THREE.Line object is one of several alternative options to the usual THREE.Mesh object when it comes to adding objects to a scene that contain some kind of content. What is nice about lines is that I only have to worry about the position attribute of the buffer geometry objects that I make when it comes to custom geometry. This allows for me to just focus on how to go about placing points in space in a certain order, and not working about everything else that needs to happen in order to make a mesh friendly geometry.
Check out Buffer geometry and Vector3 classes
In this example I am creating an array of Vector3 class instances and then passing this array to the set from points method of the buffer geometry class as a way to create a geometry. I am then passing the resulting geometry along with a line material as arguments of the three line constructor. So then there is taking a moment tot read up more on the buffer geometry class as well as the Vetor3 class if yo have not done so before hand. There are a lot of useful methods in the buffer geometry class such as this set from points method, and the Vector3 class also has a lot of great stuff to work with when it comes to playing around with vectors.
The source code is also on Github
The source code examples that I am writing about in this post can also be found in my test threejs repo.
Version Numbers matter
I was using r135 when I first wrote this post, and the last time I cam around to do some editing I was using r146.
1 - Getting the general idea working first ( r0 )
When I started a new prototype folder in which to just get the basic idea of what I wanted working I had everything in just one javaScript file which is often the case when first starting out with one of these ideas. In this single javaScript file I create my usual scene object, camera, renderer and so forth, but the main thing of interest are the methods that I made to create an array or points, and a group of lines where each line if a collection of these points.
|
|
2 - Stand alone module, and for point methods for mutation of points ( r1 )
In the first revision of this example I got the basic idea of what I wanted working okay, but I wanted to make at least one more additional revision of this example to further refine the core idea here. In the first version of the example I have all my code in a single javaScript file which is fine when first starting out with an idea but when it comes to making some kind of example that is a little more potable the next step is to make a kind of module with what I worked out and that is what I did in this first revision of the example.
2.1 - A stand alone module for this
The javaScript module that I then made based off of my first version of this example is then a module that has just two public functions for now. The public methods have to do with creating a group that is an array of lines that from the sphere like shape, and the other public method has to do with setting the state of the lines by way of set by frame method.
|
|
2.1 - A seaShell like shape demo
So now when it comes to making a main javaScript file I can just call a few lines of code to create a group of lines using the module rather than having it all in one file. After setting up the usual scene object and various other usual suspects I can then just call the create method of my Line Sphere Circles module. When doing so I can now provide a whole bunch of options that can be used to adjust things like the array of colors to use for the lines, the width of the lines for browsers that support thicker lines, and so forth.
One of the coolest feature thus far though is the for point option that I can use to mutate the state of the array of points to use to create the lines. I have a few built in options for this that I can use by providing a string for the name of the built in for point method, but I can also work out custom methods as needed as well by using a function for the value of the option. In this demo of the module I cam also using my set by frame method as a way to mutate the state of a group of lines that uses the built in sea shell for point method that I made while playing around with things.
|
|
2.2 - Random points example
Another option then is to use the seeded random built in for point method of the module. The seeded random method is one of many methods to work with in the math utils object of the core of the threejs library. This seeded random method is like that of the math random method only it will give the same result when called for the first time.
|
|
2.3 - Can still do a plain sphere like shape
I can still to just a plain sphere like shape by just going with the default for point method of the module. So for this example I went with just making a very simple static scene that does not even involve an app loop, or the updating of the geometry over time. When making the options object that will be passed to the create method I can just not give any option at all for the for point method which will result in the default sphere like shape for all the lines. I can then adjust things when it comes to radius, the number of circles in the group, points per circle, and the thickness of the lines.
|
|
2.3 - Rotaiton of lines, whole group, and custom for point method
For this example I just wanted to make a cool looking effect where I rotate and scale each line in the group. On top of that i also made a custom for point method when it comes to setting up the options object to use for this one. The end result is more or less what I wanted which is a pretty cool looking over all effect.
Conclusion
I was able to get the general idea of what i wanted up and running, but there is maybe a bit more to think about when it comes to the idea of working on this more, or maybe starting some other kind of project that has to do with lines. I was thinking that this project was a nice start with it comes to making something interesting just using the THREE.Line constructor in threejs, but maybe it is called for to make another project based on this example but in a more general kind of way. That is to make a project where the goal is to make a javaScript module that I can use to create a group of lines, but not just lines that form a circle.