Esri CityEngine lets you create, as the name implies, cities, quite easily. As a bonus, it lets you export these creations in various formats including FBX files which can be imported into 3D game engines including Unity. You can very easily add VR support for the Oculus Rift to Unity 4 Pro.
To follow along with this tutorial we will need a few things:
- Esri CityEngine 2013: 30 day trial license may be available
- Unity 4 Pro: for Oculus Rift Support, 30 day trial license is available. You should be able to get away with the free version if you only want to add a traditional FPS camera.
- Oculus Rift SDK: free but will need to sign up for a developer’s account
- Oculus Rift Developer’s Kit: Needed to view in virtual reality though you can still follow this tutorial and navigate the city via a regular monitor.
Creating your City
Start up CityEngine and start the wizard if it does not do so automatically by clicking File > New > City Wizard. Follow the wizard to define the size, maximum elevation, and general layout of your city.
Click Finish and you’re done! A metropolis has been built in mere seconds and ready for exploration. You can edit this city or create one from scratch as well but that is outside the scope of this post.
Now select all by either hitting Ctrl-A or clicking Select > Select All from the menu. To export click File > Export and select Export Models of Selected Shapes and Terrain Layers and then Next. Select Autodesk FBX and then Next. Accept the defaults unless you want to change the Output Path (either way, make note of this path) and then Finish. After a minute or so the export will be done and you can close the application.
Importing into Unity
Open Unity and create a new project by clicking File > New Project. Name it VRCity and click Create. Right click on Assets in the Project window and then Create > Folder. Name this new folder Textures.
Open up a file explorer window and navigate to the folder where we exported the city. In addition the the FBX file you should notice a whole bunch of JPG images. These are the textures. Select these images by clicking Ctrl-A (deselect the FBX file) and drag them into the textures folder.
Click Assets > Import New Asset and navigate to the folder where the CityEngine export is located and select the .fbx file then click Import. Once the import is down there should be a new folder called Materials under as as well as the city model.
Drag the city up into the Hierarchy window and is should show up in the Scene. You should see properties for the model on the right side of Unity if you are still set up with the defaults. Change the XYZ values for Scale, which is in the Transform section, to 100. You can use a smaller number if you want to go all Godzilla like.
In order to prevent us from falling through the ground or walking through buildings we need to make the City collidable. To do this, click on the city in the Project window. In the properties click Generate Colliders and then Apply. This will take a good bit depending on the horsepower of your system and may seem like the program has locked up. Be patient.
Once Unity responds again, add a sun by clicking GameObject > Create Other and selecting Directional Light. Select it from the properties and change its Position Y value to something like 1000 to put it up in the sky or drag it around by its axis in the Scene window. You can play with the Intensity slider to get the desired brightness.
Finally click on the Main Camera in the Hierarchy window and hit the Del button to nix it.
Adding a Player Camera
Now we have a couple of ways to proceed. We can add a Player Controller for the Oculus Rift so that the city can be navigated in full VR or we can add a regular FPS Player Controller. We will start with the latter.
First Person Camera
In the menu click Assets > Import Package and select Character Controller. Leave everything checked and click Import. Now click GameObject > Create Other and then select Sphere. Click on it and in the properties click on Add Component at the bottom and select Character > FPS Input Controller. Click to add another component and select Camera-Control > Mouse Look. Add a camera by clicking GameObject > Create Other and selecting Camera. Finally drag the new camera in Hierarchy and drag it onto the Sphere.
Near the top of the Unity program you will notice a play button. Click this to “play” the city in the Game window and you should be able to navigate the city using the mouse and WASD keys.
If you fall through the world it is probably because the camera started off below ground to to the local elevation of the start area. To fix this, select the sphere and change the Y Position under transform to a large number until the game window shows you on land. Also make sure the sphere is not in the middle of a building by dragging it via the X and Z axis in the Scene window while paying attention to the Game window.
VR Player Controller
If you added a First Person Camera from the previous section delete it now by expanding Sphere from the Hierarchy window and hitting the Del button.
In order to add a VR controller for the Oculus Rift we must first download the latest SDK from their site. Unzip the SDK to some folder on your system making note of the location.
In Unity click Assets > Import Package and select Custom Package. Navigate to the Oculus SDK folderOculusUnityIntegration and select OculusUnityIntegration.unitypackage and then click Open. You should now have a folder under Assets called OVR. Expand this folder and select Prefabs and drag the OVRPlayerController up into the Hierarchy window. Position it so that it is above ground and not in the middle of a building by dragging it around via its axis in the Scene window. Also adjust the Acceleration property to something like 0.2.
Above the Game window click on the Maximize on Play button to turn it on. Click play, put on your Rift, and you are off in virtual reality to explore the city. You can navigate via the WASD keys and mouse or use a game pad such as the Xbox 360 controller.
As you can see, creating a city and exploring it in virtual reality is made quite simple with Unity without any need for programming. The same principles above can be used with any other application that exports to the Autodesk FBX format such as Maya, Metlab, 3DS Max and many more.
Check back later on how to do all of this with the Unreal Engine using UDK.
This post was written by: