Lightmap baking ? Pfff, easy.

Help bring our custom Ages to life! Share tips and tricks, as well as code samples with other developers.

Lightmap baking ? Pfff, easy.

Postby Sirius » Thu Feb 05, 2015 3:04 pm

Ah, lightmaps ! Creating a UV layer, carefully unticking all of the material's textures, creating a new image... Telling Blender to bake lights to this image, then dutyfully re-ticking the textures, setting up the new lightmap in the material for the #249th object in your scene... Then swearing when you see some shadow is wrong and you have to do it all over again !

...

Personally, I think that's a bit too long.
Seriously, what about transforming a shadeless, textureless, materialless, but HUGE scene...
...into THIS :
Show Spoiler
...in less than a minute ? :D

Alright, lightmap size isn't too big, quality isn't top-notch, but still, it took ONE button press and a few seconds to get it !

...But, how ?
Mix Blender, a bit of magic, and Python's awesomeness. Praise Python. http://xkcd.com/353/

Alright, so this doesn't mean it's bug-free, or that it will eventually be useful for Age building, but still, food for thought.


I'll go into more details later, but right now I need some sleep :P
User avatar
Sirius
 
Posts: 1016
Joined: Mon Jul 26, 2010 4:46 am
Location: France

Re: Lightmap baking ? Pfff, easy.

Postby janaba » Thu Feb 05, 2015 3:58 pm

This looks and sounds really great, Sirius ... and this will remain uneffected by adding the materials, other objects, textures, puzzles etc., and can be adjusted afterwards as easily as it was created beforehand? ... :P

The Python comic is so lovely and inspiring btw ... :)
Image
User avatar
janaba
 
Posts: 918
Joined: Sun Feb 05, 2012 3:27 pm
Location: Berlin, Germany

Re: Lightmap baking ? Pfff, easy.

Postby Sirius » Fri Feb 06, 2015 11:05 am

Okay, so some more details about this...

It's a small addon I wrote for Blender 2.7, while I was testing the other program I'm working on that bakes light in existing fan-Ages.
I was thinking of how engines like Source or Unreal had utilities to bake light automatically before exporting an environment. Neither Blender nor Plasma can do that, obviously, but at least Blender can render lightmaps.
The problem with Blender, is having to setup the materials by hand for each object in the scene, which can be tedious and cannot be reverted once it's done.


Basically, what my script does, is:
  • make sure all objects have a material and have a lightmap UV layer
  • store into the object's properties informations about the lightmap it must create (such as the size and name)
  • [allow the user to modify any of these properties or correct the UV unwrapping]
  • copy the whole scene, making sure all meshes and materials are single-user (very important since each object needs its own material)
  • put all objects in the oven to bake their lightmaps at once
  • and apply these lightmaps to the new objects.


Here are all the pros of using this method:
  • (do I even need to mention it ?) faster, and awesome lighting quality without losing time setting up materials. Possibility to use features that Plasma doesn't support (you can even bake normalmapping if your lightmap is big enough :shock: )
  • the Blender-render becomes almost WYSIWYG.
  • no need to stay near the computer while it's baking. Start the script, go to work, and don't waste time.
  • subsequent bakings are a lot faster, since previously rendered lightmaps are kept (you can of course choose to rebake them).
  • no need to know how to apply a lightmap. The best lightmap size is chosen according to the object's size (on small object it can use vertex color painting instead). Even UV-unwrapping is automated.
  • can manually edit lightmap size or UV wrapping for better precision/quality.
  • and as Janaba said:
    janaba wrote:this will remain uneffected by adding the materials, other objects, textures, puzzles etc., and can be adjusted afterwards as easily as it was created beforehand? ... :P
    Entirely right ! Original objects are kept, so you can easily modify these and re-run the script.


Now, obviously, there are some cons (I like lists, so there comes another one :P ):
  • Errr, absolutely no way to export to Plasma currently. The only options are Korman (still in development, not sure it's compatible yet), exporting to some file format before reimporting in 2.49 (but most of the plugins don't like multitexturing), and downgrading the Blender file by saving it in different versions of Blender (means having something like 3 versions of Blender on your PC, and you'll still lose faces that are not triangles).
  • baking lights is still as long, and can take up to a few hours for bigger Ages.
  • small performance impact in game (not much, in my opinion).
  • the script is just a proof of concept. It sucks at guessing what the lightmap size should be. Input from the artist is required in most cases (for lightmap size and better UV unwrapping).
  • it's quite likely complex texturing or objects with multiple materials will mess the script up too.



janaba wrote:The Python comic is so lovely and inspiring btw ... :)
Yes, I found it thanks to Deledrius' signature :P Very nice comics, although some require good knowledge of mathematics or computing to be understood :shock:
User avatar
Sirius
 
Posts: 1016
Joined: Mon Jul 26, 2010 4:46 am
Location: France

Re: Lightmap baking ? Pfff, easy.

Postby Yali » Fri Feb 06, 2015 9:13 pm

This is awesome! Great age design!
User avatar
Yali
 
Posts: 151
Joined: Tue Oct 02, 2007 7:59 am

Re: Lightmap baking ? Pfff, easy.

Postby Deledrius » Wed Feb 11, 2015 8:52 pm

The 3DS Max plugin automates the lightmap generation (well, when it's working correctly it does, anyway), and it's a shame that PyPRP doesn't. This looks pretty promising as a first step (well, several first steps ;) ) to getting some automation for it in Blender. Is this a process that could work as part of the export step? It would be immensely useful to merge this into the existing tools!
User avatar
Deledrius
Gehn Shard Admin
 
Posts: 1106
Joined: Mon Oct 01, 2007 1:21 pm

Re: Lightmap baking ? Pfff, easy.

Postby Sirius » Thu Feb 12, 2015 5:18 am

Deledrius wrote:Is this a process that could work as part of the export step? It would be immensely useful to merge this into the existing tools!
Sure ! With a few tweaks, it should be compatible with Korman.

It still needs polishing though. It uses some unsafe hacks (for instance, when duplicating lightgroups), and the UI isn't really pretty (Blender refuses to display a user-friendly name in the "Custom Properties" plane, resulting in funny settings names). That, and the issue about determining lightmap size, UV mapping island margin, etc (honestly I'm not really sure how best to code this).

Another feature I'd like to add, is an option to bypass baking when exporting (for instance, by baking only to vertex color without creating new lightmaps). Because it's often required to export the scene quickly when doing some tests, without any need for high visual quality.
User avatar
Sirius
 
Posts: 1016
Joined: Mon Jul 26, 2010 4:46 am
Location: France


Return to Scripting

Who is online

Users browsing this forum: No registered users and 3 guests

cron