I purchased a 3D model of a complex scene off of Turbo Squid. I'm a bit of a novice when it comes to 3D modeling so I decided to purchase an already made scene and modify it to suit my needs. As my game's intended platforms are Android and iOS devices I knew optimization would be key to good game experience. Following the standard Unity instructions on how to import a model from 3ds Max yielded me a few problems:
- Importing the .max file directly into my Unity project's assets timed out after Unity gave up 3 minutes later trying to internally convert the .max file to an .fbx file.
- Manually exporting the scene from 3ds to an .fbx file and then importing that into Unity gave me the scene, and despite the textures being imported into Unity no textures were being applied (everything was just gray).
- Importing the .fbx into Unity took a very long time.
- Deploying Unity app to my Android with just the gray scene crashed the app (more precisely, as soon as a Vuforia target was found in the camera frame).
- A bunch of the Vray materials and lights were not exportable to .fbx.
The answer? Merge a bunch of objects all with their own materials/textures into a single mesh object and bake a rendered view of that mesh into a single texture (containing all the other textures/lighting/shadows) that then need be the only texture applied to the mesh as a material. In other words, we merge a bunch of multiple objects with different images attached to their surfaces into a single object with a single image file (texture).
Importing a multi-textured 3ds Max scene into Unity
I wrote the instructions below aimed at complete newbies who want to optimize their complex 3D scene suitable for a low spec handheld device like Android or iPhone. This is achieved by combining several objects into one mesh and baking a single texture image to apply to the mesh as the only material. The baked texture image will come from a rendered output of the object complete with its standard or Vray materials, any multisub object materials, shadows, lighting etc. The scene I happened to buy used the Vray renderer but the same instructions should be applicable to models that use the standard renderer as well.
NB. There is a plugin for 3ds Max called Flatiron that be used to bake entire scenes into one image texture. The full version costs a bit but if you have luck with the trial version you may wish to purchase it. I didn't have much luck so I decided to go the manual route, as detailed below.
- Open your 3D model scene in 3DS Max. I use 3ds Max 2010 with the trial version of Vray 1.5 SP4.
- Select the objects that have their own standard (and/or multi/sub) materials that you want to turn into a single mesh object with a single material.
* To select objects, make sure the Select Object icon is the highlighted icon on the top horizontal toolbar (just below the row of main menus you should see a drop down list of All, Geometry, Shapes, Cameras, Lights etc. and beside that the Select Object icon which is a cube with a mouse pointer - in Max 2010 at least).
Press Ctrl+left mouse click to select multiple objects within your scene. You also can click on the Select by Name icon and choose the objects from their names.
Ctrl+A (or Edit -> Select All) will select all objects currently displayed in the viewport. - Optional: press Alt+Q or Tools -> Isolate Selection to display only the selected objects in the current viewport.
- Select Group -> Ungroup as many times until all the selected objects are ungrouped. The Ungrouped menu item will become grayed out. This is to make sure you can Attach all items (covered below).
- Select one object, right click it and choose Convert To -> Convert to Editable Mesh.
- With the same object still selected, on the right hand panel of Max, select the Modify icon (2nd icon from the left on the top of the right-hand panel in 2010) and directly below that is the object's name, Modifier List (a drop down menu) and then the stack list (think layers of operations done on the object). Currently the only layer in the stack list should be Editable Mesh. After clicking Editable Mesh in the stack list you will see a few Editable Mesh related configuration sections appear below it.
- In the Edit Geometry section, click Attach List and the Attach List dialog window will popup. Assuming you're in isolated mode (Alt+Q a few steps back) then you can happily select all objects in that list and click the Attach button in the dialog window. This operation will effectively make all your objects into one single mesh object. Alternatively, click the Attach button next to Attach List and select objects individually if you're not in isolated mode. With the newly created Editable Mesh object selected, select Unwrap UVW from the Modifier List. You should now see in order from bottom to top, Editable Mesh and Unwrap UVW in your object's stack list.
- In the Unwrap UVW's Parameters panel, look for the section called Channel and change Map Channel to 2. If you get a warning about changing channels with the options to Move or Abandon, go with Move. I noticed this started appearing after I upgraded to 3ds Max 2013 and after trial and error Move seemed to be the best option.
- Click the small + icon next to Unwrap UVW in the stack list, select Face and press Ctrl+A (or Edit -> Select All) to select all faces.
- Click the Edit... button in the same Parameters panel and the UVW Editor window will open.
- Open Mapping -> Flatten Mapping and with the default values press OK.
- Depending on how many faces your object has, a while later you should see a bunch of green/white wireframe like figures neatly laid out in a square region representing the unwrapped version of all faces of your object. This so-called UV map will be the map of coordinates that tell how the 2D texture image is to be wrapped around the 3D mesh object.
- Close the UVW Editor.
- Select Unwrap UVW in the stack list.
- Making sure the object is still selected, press 0 or Rendering -> Render To Texture to open the Render To Texture dialog window. Here is where we generate the texture image by rendering the 3D model in Max and following the unwrapped UV map coordinates, the object's faces' materials will be baked onto that area of the texture image file.
- Under the General Settings section, Output set Path to a folder where you want the output image file saved.
- Under the Objects to Bake section, you should see the mesh object's name in the list.
- Under the Mapping Coordinates section, select Use Existing Channel next to Object.
- Select 2 from the drop down list next to Channel. If there is no 2, then it means the UV map was not unwrapped properly to channel 2.
- Under the Output section, click the Add button. If you are using the Vray renderer, select VRayCompleteMap. If you are using the standard renderer you can probably use CompleteMap but I haven't tried with that one. Using the complete map will ensure not only materials, but also lighting and shadows etc are baked in.
- Under the Selected Element Common Settings section, you should see something like:
- Name: VRayCompleteMap
- File Name and Type: <object name>VRayCompleteMap.xxx
- Target Map Slot: Diffuse Map (select Diffuse map if its in the list)
- Element Type: VRayCompleteMap
- Width/height dimensions: the resolution of the output texture image.
A few notes about this section: - If you select multiple mesh objects to render to texture at the same time (which is completely possible, just select them all before opening the Render To Texture dialog and they will all be listed under Objects to Bake), then the file name and type cannot be changed from the UI.
- You can change the default type by:
- Open C:\Program Files\Autodesk\3ds Max 2010\ui\macroscripts\Macro_BakeTextures.mcr in a text editor.
- Change local defaultFileType = ".tga" to local defaultFileType = ".png"
TGA seems to be the Max default, I prefer PNG but you can use JPG or TIF too. - Restart Max.
- Choosing the right resolution is a bit of an experiment and I'm still experimenting and discovering. It all comes down to the specs of the target devices that your game will run on, how many objects you have in total, how many faces in one mesh object, the actual content of the object (is it a far away stone in the background or a closeup thing requiring more detail?). So far I've been playing around with 512x512 to 1024x1024 resolutions with 10-30 objects and the scene has been rendering on my Android okay. This is a static environment though and those numbers still feel too high. You'll just have to experiment here. My approach is bake all your textures to high resolution and then scale them down as you test the level of detail of actual content rendered on a device until you're happy with the lowest image size vs content detail.
- When selecting multiple objects to bake at the same time you may not be able to select Diffuse map in the Target Map Slot setting and may get some warnings about it not being set for some objects. So far I've been fine just ignoring these warnings or just having the Target Map Slot for multiple objects set to "varies".
- Down the very bottom, I set both Views and Render to Original. Then click the Render button.
- The render process may take some time. When it's finished it's not obvious with a "Render finished" message or anything. You should just see the preview window of the rendered texture image appear to be finished (and the fact your computer is no longer slowed down to a snail's pace and you can actually open the menus in Max etc!)
- Back on the stack list with Unwrap UVW selected, change the Map Channel that you set to 2 back to 1.
- Right-click Unwrap UVW and select Collapse All. This collapses all the layers in your stack into one layer.
- Open the Material Editor (4th icon from right on top toolbar in 2010 - the icon with a chequered circle with a small square bottom right).
- In one of the free slots (the spheres), drag/drop the texture image that was created from Windows Explorer. You will now have a new standard material with the image file attached as a bitmap texture on the diffuse channel.
- Now drag/drop that material slot (the sphere) onto the actual mesh object in your scene. And presto! You now have a single mesh object with a single rendered-to-texture image material.
- Things may look a bit fuzzy in 3ds Max viewport more than they actually do in Unity, I'm not sure why.
- Next I simply export the selected object(s) as an FBX (ensuring Insert Media is selected in the FBX settings window) and then import that FBX file into Unity and I have the same textured object to play with in Unity!
What about a youtube tutorial please?
ReplyDeleteGood tutorial. Thank you~
ReplyDeleteBut it a bit hard to understand...
You'd better take some screenshots whit the procedures(^^♪
Why do you collapse the Unwrap UVW modifier? You can export your mesh with the Unwrap UVW modifier applied and it works in Unity. I was just wondering what the reasoning is behind collapsing the modifiers, I can't see the difference.
ReplyDeleteOh, about the fuzziness in the Max Viewport you can use the Nitrous Texture Size Limit over at Scriptspot to increase the resolution of your textures. Cheers!
Hey!
ReplyDeleteTrying my hands on Unity2d Development I was implementing to import 3DS Max Models into Unity 3D. This post is quite helpful. Thanks a lot for sharing.