Tuesday, February 11, 2014

How to Voxelate Vectors Like a Boss... Part 1

First, what the frimp is a voxel?  
voxel: n. (in computer-based modeling or graphic simulation) each of an array of elements of volume that constitute a notional three-dimensional space, esp. each of an array of discrete elements into which a representation of a three-dimensional object is divided.

In other words, a voxel is a volumetric pixel.  In the context of games like StarMade or MineCraft, voxels are the cubic blocks that we place that compose the structures we build in game

Some folks have figured out that it's possible to convert vector based 3d models into voxels that can then be imported into games like StarMade.

How does it work?
The voxelator program reads the 3d model and projects the model into a 3d grid.  Then it looks at each block in the volumetric grid.  If a certain threshhold of the volume of the block is filled, the block is turned "on", or "filled" with a voxel.  When the whole volume of the blocks is read and analyzed, the resulting voxel file is written to disk, and readied for import into the game of your choice.

So that's it?  Instant Ship?!?!
Not so much.  A voxel import is not a ship, just a shape.  It's imprecise.  it's asymmetrical.  It has a LOT of extra blocks (and mass).  Yes, you CAN slap some power, engines and weapons on it, but it's like dropping a volkswagon into a beat-up fiberglass outline of a Rolls Royce. 


Well, what IS it good for then?
Voxel imports are great for getting the general design and proportions of a ship with a really complex structure.  Struts and spars and 3d compound curves are really tricky to figure out.  A voxel import can provide a sort of stencil where you can more easily figure out where the key structures are located and roughly what the surface looks like.


Is this how you do your ships?
Mostly no.  My Nova was based on a voxel import.  My planned Galaxy and Constellation class ships will be based on voxel imports.   The rest of my ships were built using detailed schematics, careful measurements, and a lot of effort.   It's not that there's anything wrong with voxel importing; it's just a different kind of challenge. It's sculpting with a chisel instead of a welder. Understand that this is a multi-step convoluted and uncertain process.  It MAY in fact be easier to do it the old fashioned way with a high-res schematic, ruler and calculator, at least if you want quality results.

OK... I get it.  This can help me get the look right, but ship-building is still work.  How do I do it?



Step 1: Find your mesh file.
Try googling for (name of ship) "3d mesh" or (name of ship) "object file"  there are lots of sources and repositories. (yay!)   There are lots of weird and incompatible formats (boo!).   What you are looking for are "Object files" or ".obj"  You might find versions of differing quality made by different 3d artists.  For our purposes, the level of detail really isn't important, only the gross proportions of the main structural members.  Remember, any detail finer than 1 cubic meter is going to be lost in translation before we're done.

Note: You may find exactly the ship you want in the wrong format.  It might be possible to translate it into a .obj file... but doing so is a thouroughly dodgy process.  It might work.  It might just fail.  It might render a random assortment of ship-parts.  Mesh translation is beyond the scope of this tutorial.  Google it yourself, and good luck.  (edit):  Maybe try Meshconv? http://www.cs.princeton.edu/~min/meshconv/  I've never used it, but it might work for you.  Let me know)

Step 2: in which size matters
In StarMade, like in MineCraft, the voxels are displayed in game as cubes of a bout 1 meter.  We will need to tell our voxelator program how finely to dice our 3d model.  So what we need to discover is the longest dimension of our ship.  For MOST ships the longest dimension is going to be the length from stem to stern.  For this example I used the Nebula class starship from StarTrek TNG.
I googled "nebula class length"  and my second link (http://memory-beta.wikia.com/wiki/Nebula_class) showed the length of the Nebula to be 442m in length.  That will be important in a minute.


Step 3: Voxelization
The program you want is called "binvox".  This is the program that turns your nice smooth vector object into chunky blocky voxels.  

  • Download here: http://www.cs.princeton.edu/~min/binvox/#Download
  • If you are on windows, download the vox_package.zip file.  If you are on other platforms, the instructions are close enough that you should be able to follow along.
  • Unzip the file, and move/rename the resulting directory to something handy like "c:/binvox"
  • Copy your .obj file into the directory you just created.
  • Create a batch file named "shipname.bat" so our example will be "Nebula.bat"
Here is the batch file for my object file (Nebula.obj)  rendering the ship into voxels with 442 voxels on the longest dimension:
--------------------------------------------------------------------------



   rem : Set Variables Here
   set ShipName=Nebula
   set ImportFileType=obj
   set LongestDimension=442

   rem : The actual commands
   del %ShipName%.binvox
   binvox -d %LongestDimension% -t binvox -v -c  -ri -cb %ShipName%.%ImportFileType%
   viewvox %ShipName%.binvox

----------------------------------------------------

Running the batch file above renders the object file into voxels, and saves the result as Nebula.binvox


Step 4: Import to Starmade
The program you want is called "SMedit"
Unfortunately I'm not going to be able to demonstrate this step just now as SMedit seems to be broken.  Schema is a lovely programmer, but he's apparently not good at sharing his toys, or his code.   Just now, the recent structural changes to StarMade have rendered SMedit unusable.   I'm certain it will be back, but first the author jjaquinta is going to have to reverse-engineer whatever schema's done to the ship file format

Here's the SMedit page, with some lovely tutorials from the guys at MushroomFleet.
http://www.starmadewiki.com/wiki/SMEdit

SMedit tries to position the ship core near the middle of a ship... It often misses.  Sometimes the imported voxel file has the ship oriented in the wrong direction.  Use the functions of SMedit to rotate the structure around the core and place the core nice and deep in a well defended part of the ships structure.  Then, Import your ship into StarMade like the tutorials tell you.

One more thing... Use SMedit to paint your voxel import your least favorite color.

End part 1.  Part2 coming soon






No comments:

Post a Comment