Train Track threejs module project example
When it comes to my beta world collection of videos I have started a timer video project that involves a train that goes along a track. There is a lot that I like about this project, but also a whole lot that I would change if I where to start over. Anyway one thing about the project is that I have the land all as one big solid geometry, then I worked out a curve path for a train to go along on top of the single geometry in the single mesh. This seems to work okay, but if I where to start to make another video project like this, and then another, and so forth I would like to make some other kind of system for this. Mainly I do not think that I would want to have one solid geometry, but rather a collection of source objects to which I clone, and adjust one by one as a way to create an over all scene. So with that said this threejs project example is about a module that has some methods that can be used to create such a project.
The tracks threejs module example and what to know first
This is a post on a javaScript module that I use on top of threejs to create a kind of train track like project that can be used to create a single curve path. I can then use this curve path to move one or more mesh objects along to create a kind of train set like project then. This is not a post for people that are new to threejs and javaScript then, but rather for people that have at least some background with these things are a looking for project ideas. I will not be getting into the very basics of what should be known before hand here then, but I think I should at least write about a few things that you might want to read up more on here in this opening section.
Check out more on curves if you have not done so yet
There are a few built in options for classes that extend the base curve class in threejs that work great and as such help to save a lot of time compared to making a custom curve class. In fact what I am writing about here is me continuing with what I started with my post on the cubic bezier curve3 class that I wrote last week.
Know a thing or two about the user data object, and other object3d class features
My tracks module makes use of the user data object of the object3d class as a way to park some data that is used by the various methods of the tracks module. The object3d class is the base class of mesh objects, cameras, and anything else that would be added to a scene object as a child. Also scene objects themselves are examples of objhect3d class based objects. However not everything in threejs is an object3d based object such as curves, which is where I make use of the user data object as a way to store a curve for each source object. However when it comes to using the curve object from a cloned copy the position of the curve will be relative to worked space, so to fix this I create new curves from this curve objects but add the position of the parent object to the values of the curve stored in the user data object to do so.
Source code is also up on Github
The source code exmaples here can also be found in my test threejs repo on Github. This is also where I store the source code for all the various other blog posts that I have wrote on threejs thus far as well.
Version numbers matter
When I was working on this project last I was using r146 of threejs.
1 - The first version of the tracks module, and some demos
For this first section I will be writing about what I have when it comes to the very first version of this tracks module. Also I will want to have at least a few demos to test out that the core features that I have in place all ready work okay before even toying with the idea of making my first revision of this or not. There is a lot more that I would want to add and change at this point, but for this first version at least I wanted to just make sure that the very crude basic idea that I had in mind will work out okay. Thus far it would seem that it does.
1.a - The tracks module ( R0 )
This first version of the tracks module is then yet another one of my modules that follows the tired old IIFE pattern. There are three public methods of the module, one to create source objects, another to create a track object from a collection of source objects, and one last method to create the final track curve. Each source object has a curve that is attached to the user data object of the source object. The process of creating a final track curve then is to run over all the objects created from the source objects, and then create a copy of the curve stored in the track object. There is then creating a new curve from the values of the source curve but adding the position of the track object as well.
|
|
1.1 - A tracks module hello world demo in which I have a kind of oval loop
For my first hello world type example I would like to make a simple kind of oval loop track using just two source objects. One is a kind of straight track object, and the other is a single kind of turn object. To create my track then I just need to work out an array of argument values to pass to the create track object method of the track module.
|
|
1.2 - Testing out the flip feature
So there is rotating the source objects, but I have found that I am also going to want to negate the curves as well while I am at it. For this demo I am then testing out not just the rotation of the curves, but also the features that have to do with flipping the curves by making use of the vector3 negate method.
|
|
Conclusion
Although what I have thus far with this is looking good I am sure that there is a lot that I will want to refine, and add with at least one revision before I even think about making any kind of real project with this. I hope that i do get around to making at least an R1 of this as I find this to be a fun project thus far. However I do have a lot of other things that I need to focus on, so I might not get the time.