Friday, May 2, 2008

Render to Texture, or Texture baking in 3dStudio max for Papervision3d

Here's a small tutorial on how to Bake a texture in 3d studio Max (2009) for use with papervision3d.

Show the result

First off, let me state that I am not particularly skilled in 3dMax, I'm more of a lightwave person myself, but since Lightwave cannot produce content I can use in Papervision, 3dMax it is :)

This tutorial shows you how to do Texture baking very basically. It doesn't go into Mental Ray or Vray. It's just to show you how its done, so you can use it as a starting point for more detailed experimentation :)

I'll sum up the steps I do to make objects, light them and render them to texture. I've tried to use Mental ray and Vray too, but today for some reason 3dMax only wishes to spit out completely white texture maps, while it displays them correctly in the render preview window.

We'll be exporting our scene as a .3ds file, because I've tried using Collada export (using http://www.feelingsoftware.com/ 's Collada exported. But as soon as I started baking textures, my scenes won't render correctly.


Step 1 - make your model

I've created 3 boxes and an omni light. In this Tutorial I haven't given them a material, this will be automatically done when we're baking the texture. You can do this manually as well if you wish.

Step 2 - Set the filetype to PNG
You can skip this step if your 3dMax already renders to PNG, or if you like using .TGA's and arent using Papervision3d :)

In my setup, 3dMax automatically renders all files as Targa (.tga), but I want to use PNG's which Papervision can read so we need to change this.

I use the following method.
1 - select only 1 object in the scene.
2 - open the "Render to Texture" dialog (press 0)
3 - under "Output" Delete any entries if present.
4 - ADD a "Diffuse map"
5 - click on the dots-button to the right of "File name and Type"
6 - Select a filename, and select PNG as a filetype.
7 - After the file dialog is closed, you'll see that the file extension is .png now.
8 - Delete the output entry again, 3dMax will now save .png's as default when you create new Output maps.

In my case, 3dMax only goes back to saving .tga's after the program has quit/crashed and has been restarted. So If you clear your scene, and render to texture again, the output should still be .png and you can skil this step.

If there's a setting somewhere where you can change this in 3dMax, please let me know :)

Step 3 - Render to Texture dialog

Select all your objects on stage and press the 0 key, or use the menu "Rendering - Render to Texture" to bring up the "Render to texture" Dialog.
Here you can select where your textures will be saved.
Because you selected all your objects, they should all appear in the "Objects to bake" tab. If the list is empty, you apparantly havent selected any objects.


Make sure "Use Automatic unwrap" is selected. You can also use manually created UV-mappings if you wish (Make sure to set it to the correct channel if you do), but this example has no UVmapping yet, so we'll leave this to Automatic.


Step 4 - Selecting which type of map
Make sure "All selected" is checked, because we want to make a map for every object in the scene.
Click on the ADD button, and select "Diffuse map"

Since we'll be exporting to .3ds, you need to know that 3dMax automatically renames texture filenames to a 8.3 filename type. So that means 8 characters for the filename, 3 for the extention.
This is a problem, because our baked textures arent limited to that specification and will happily render out to filenames that won't fit in this scheme.
Result : Your 3ds file will want to open box01Dif.png while your texture was rendered to box01DiffuseMap.png.

To fix this we do the following :
1 - Make sure your object names are smaller than 8 characters.
2 - In the "Selected Element Common Settings"-tab, clear the "DiffuseMap" from the 'Name' inputbox.



So your window should look like this:


Select the map size you want, we'll use 256x256 in this tutorial.

Step 5 - preparing the render
Select "Output into Source" and "Keep Source Materials"

This will replace the material of your objects when you bake the textures. We need this because we'll be exporting this scene, with the baked materials for Papervision.

Also select "Lighting" and "Shadows" in the "Selected Elements Unique setting" tab.
This will enable the texture to be rendered with, you guessed it, shadows and lighting :)




Step 6 - Render the texture
This is the easiest step.
Click on "Render" and wait for 3dMax to finish.


Step 7 - Export to a .3ds file.

All done in 3dMax, if you take a look at your Materials editor, you can see that the materials now contain texturemaps with lighting and shadows.
If you dont see your materials, select an object in your scene and click on the eyedropper button to get the material from the object.




Finished.
You can now load your .3ds file into your Papervision flash, make sure it knows where to find the texture images, and it should work.

I've included a small example, made with FlashDevelop.
Click here to download it


I've found an error/missing feature in the 3dsParser in papervision. Apparantly a "LOAD_COMPLETE" event was missing from the 3dsParser, so I've added that.

To do this, edit org.papervision3d.objects.parsers.Max3DS.as


and change the OnFileLoadComplete function to :

private function onFileLoadComplete(event:Event=null):void
{
var loader:URLLoader = event.target as URLLoader;

parse(ByteArray(loader.data));
// added by whizzkid
var fileEvent:FileLoadEvent = new FileLoadEvent( FileLoadEvent.LOAD_COMPLETE, filename );
this.dispatchEvent( fileEvent );

}


And if you're not using FlashDevelop, manually add the following import as well:


import org.papervision3d.events.FileLoadEvent;



I've included this file in my example.



This is my first tutorial ever, so please let me know what you think about it, and if you find an error or have any suggestions, leave a message :)

Regards,
Martijn

13 comments:

  1. Thanks for the tutorial. Pretty useful. Any thoughts why you've used 3ds instead of collada ? Any advantages, disadvantages ? Cheers

    ReplyDelete
  2. Yes, I couldn't get the scene exported to collada from 3dmax correctly, I've installed the Feeling software plugin, but as soon as i baked the textures in 3dMax and exported them to Collada, the scene would be pretty messed up in Papervision3d.

    ReplyDelete
  3. I'm getting the pretty messed up textures as well, but for a couple of models thing went ok. I don't understant why yet. I'm trying to figure out the right settings for Collada.

    ReplyDelete
  4. I found that some settings in the render to texture dialog work really well with Lathe objects or similar to those. Also, I've found something that maps the baked textured properly to collada models. If you use Automatic Unwrap, make sure the channel on the right matches the channel in the UnwrapUVW modifier. I tested first without baking. 1.create the model, 2. add the unwrap, select polygons, flatten mapping ( with 0 spacing ) and render UVW Template from the tools in Edit > Unrwap UVW . I've added the rendered template into the diffuse map, just to make sure the texture is set to the unrwap and noticed it only set up properly on Channel 1. The next step was to remove the template image from the diffuse map and render to texture. It worked fine with the Collada. If the model is complet, there will be some face sorting issues in Flash, but the texture will be mapped properly on the Collada model. I haven't tested trying different sorting options in Papervision yet. That's the next step

    ReplyDelete
  5. to fix the texture problem, it's enough to just export the scene to .3ds .. then import the .3ds file into 3dsmax again .. and then export it to .dae

    ReplyDelete
  6. What about if I wanna just export the 3ds, and add the textures dynamically via AS3 any ideas of how do this?.

    ReplyDelete
  7. It's probably possible, but I've never tried that. So unfortunately I can't help you there.

    ReplyDelete
  8. 1st Thks for the tutorial, it is gr8 ...

    but i've a problem while importing my .3ds into Flash, the textures does look fine, as u say the automatic UV didn't be affected on Flash! any idea about that!?

    thks again,

    ReplyDelete
  9. I don't quite understand your question ?

    ReplyDelete
  10. hey, the images are not displaying, your tutorial is great but, without images it's difficult to verify if i'm rigth or not

    ReplyDelete