World Machine Tutorial #2
Machine Worlds in Unity
Install World Machine following these instructions, start World Machine, build a world, and get ready to do something with your dreamworld! :)
In this short tutorial, I assume you have a world set up and are ready to export. If that is not the case, please review this tutorial on how to get to that point.
World Machine vs. Unity
Ok, so you've got a world and you're happy with it-- it looks sleek, it looks cool, seems like something you'd like to explore in a different environment and in a different way, say a game engine. Unity happens to be a particularly suitable game engine, that is, its able to provide you with a great virtual experience either in VR or 3D of the world you just built.
In fact, Unity has built-in terrain generation tools that allow you to create terrains as well. There are a number of extensions (assets) available through which you can do procedural generation; and yes, World Machine is therefore a bit redundant though still provides a powerful tool in addition, not to mention, it's free!
World Machine: Exporting
We'll first export our data from World Machine in a format suitable for Unity.
With your world file open and built, double-click the Height Output device to open up the export dialog. Set the folder for the output, which generally is a good idea to be a terrain folder inside your Unity project's assets folder.
Unity understands RAW 16-bit format, but does not read the format World Machine exports. You've got two options: select RAW under Low Precision (8-bit) or RAW16 under High Precision (16bit+).
Press Write output to disk!. If you selected RAW16, the file's extension will be .r16; Unity does not like this format, so we will need to change it. To do so, locate the file where you saved it on your disk and change the file-type to .raw. Note that renaming the file is not the same as changing its file-type!
Ok, badass! We exported our elevation model. Next up, our texture! :)
Double-click the Bitmap Output device and another dialog box opens up. This time it's slightly easier, since Unity does like these file-types. I recommend going with a 16-bit type (either .tiff or .png).
Set your folder - probably somewhere inside your Unity project's assets folder - and
Write output to disk!
Unity: Importing
The two files you just created need to be imported into Unity.
Open Unity and start a new project;
From Windows explorer drag and drop your height and texture files (.raw and .tiff or .png) into your project;
Inside Unity also create a new Terrain (GameObject > 3D Object > Terrain);
Organize your file(s) and new Terrain in the Project explorer;
Select your Terrain in the Hierarchy and check out the new options in the inspector on the left;
Under Terrain open up Terrain Settings (the little cog to the right), which will expand a range of options below.
Scroll down to Heightmap where you will find two buttons: Import Raw... and Export Raw...
Click Import Raw... and locate and open the height map file you've exported (which should now be in your Unity project assets terrains folder).
Unity will throw a small dialog box at you, which should automatically give you the right metadata of the file:
Bit 16;
513 x 513;
Make sure the platform you work on is correct set;
Check Flip Vertically;
Press Import;
Unity will now model and shape your Terrain according to the elevation data that you exported from World Machine (Figs. 1-2).
Fig. 1 New Terrain GameObject in Unity
Fig. 2 Terrain GameObject in Unity with elevation data applied
Unity: Texturing
Kinky! However, it looks a bit pale and coloring this in manually would be a royal pain in your body's lower parts. Thankfully we also exported the color information from World Machine!
Select the Terrain and click Paint Texture (the paintbrush, three icons over from the cog you pressed earlier) under the Inspector;
Below you will see Textures and a message saying No terrain textures defined. Let's change that by pressing the little button with the cog that says Edit Textures…
Select Add Texture, which will open a small dialog box with two smaller boxes; select your texture from your Project explorer and drop it onto the left box marked Smoothness (A) and make sure you adjust the size to fit your texture map: 512 x 512. This way your texture map will cover the entirety of your Terrain (Fig. 3).
Note: if things don't show up quite in the right place, that is your texture does not properly match up with your Terrain (e.g. your river might be atop of a hill and your mountains may be in the middle of a lake!), you may have forgotten to check Flip Vertically on import or change some of the size options. In that case, just import the RAW file again and trace your steps to make sure it works out the second time around :)
At this point you can incorporate your landscape into your game! Start placing trees and sprinkle some awesome-sauce on yourself; you've earned it!
Fig. 3 Elevation data with texture draped over