My Lightmap Setup & Images Saver scripts for Blender

Panda3D Forum Index -> Code Snippets Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
ynjh_jo


Posts: 1596
Location: Malang, Indonesia

PostPosted: Tue May 12, 2009 7:30 am    Post subject: My Lightmap Setup & Images Saver scripts for Blender Reply with quote
I use Blender too now, but it's still 2 weeks though.
At the first start, I was wondering how hard it could be to get a bunch of objects lightmapped by batch process as I used to do in Maya with MEL.
So I tried it and found it's pretty simple and straightforward.


Download :
Lightmapped Scene
Lightmap Scripts

There are 3 scripts in the zip :
1. Lightmap Setup
2. Lightmap Image Saver
3. UV Layer Switcher
NOTE : I put those scripts to Help menu, just to minimize the find & execute time.

Brief overview :
My Lightmap Setup script also works on object with multiple materials. It's demonstrated by the cube and the cylinders.
Instanced (linked duplicates) objects are not processed at all, only unique meshes are processed.
Only 1 lightmap texture is assigned to each object. The texture can be independent per object or shared by some objects.
For more info, read each script's docstring.

Detailed features of the Lightmap Setup script :
1. creates new UV layer for lightmap, if it's not already exist
2. sets the lightmap UV to be active, ready for baking
3. creates a material if the object doesn't have one already
4. duplicates material which is used by multiple objects, so each object can use its own
lightmap texture, if it's using an originally shared material
5. removes existing lightmap textures from texture slots
6. creates a new texture (+ an image) to bake to, and insert it to the material
after the last found texture in the slots
7. sets the lightmap texture's mapping to use the lightmap UV instead of the original (ORCO)
8. links the lightmap texture to the lightmap UV layer, so Chicken will write out
that UV layer's name instead of the first layer
9. turns on "tangent vector usage in shading calculation" (at Shaders panel) to get full shading
for shadow baking, otherwise some areas of faces which is facing away from the light
wouldn't be filled with shadow color.
10. turns on "auto smooth during rendering" (at Mesh panel) and sets the max angle to minimum
to fully prevent artifacts, if user wants to bake shadow
11. turns off "auto smooth during rendering" (at Mesh panel) and restores the max angle,
if user wants to bake AO
12. runs Blender's smart-projections UV unwrap script


###############################
Lightmapping steps using my scripts :

[_0_] select objects (it will be filtered to process Mesh only objects)
[_1_] run Lightmap Setup script
[_2_] fine tune the generated UV, in case any overlapping UV faces
[_3_] bake

Once the desired objects are processed, save the result :
[_4_] run Lightmap Image Saver script
[_5_] save your .blend file

You should run step 1-3 for small, medium, large, and extra large objects separately,
just to use optimum texture size for different sized objects.
###############################

I don't use "Lightmap UVPack" UV unwrap module because :
1. it can't process selected objects, it only processes the active object only,
or all objects, including camera, lights, and everything else in the scene
2. it's harder to get good proportion quality
3. it's impossible to post-process the image (eg. blur) since the faces are
scattered all over
I use the "Smart Projections" instead.


NOTE :
The Lightmap Setup script turns on 2 flags :
1. tangent vector usage for shading (at Shaders panel)
This flag is NOT good for normal rendering in Blender, but it's the only way to get
correct shadow baking calculation. It doesn't affect AO baking.
2. auto smooth during rendering (at Mesh panel)
This is only good to fully remove artifacts in shadow baking,
but BAD for AO baking and normal rendering.


Screenshots :
(all these images are included in the first zip)

The results in Blender :


The end results in Panda3D :


Some experiment results :
torus :

suzie :

(with Lightmap UVPack)


Note that those are my first Blender scripts, so I might miss something obvious, like this one :
Question I couldn't get smooth shading when baking shadow, no matter what I did.

Any input, criticism, offensive feature objection, and better implementation are welcome. Very Happy
_________________
http://ynjh.panda3dprojects.com | http://ynjh.p3dp.com
Intel P4Prescott 2.8GHz HT | Elixir 1.5GB | ATI HD4670 1GB GDDR3
MentalDisaster


Posts: 168

PostPosted: Wed May 13, 2009 12:41 pm    Post subject: Reply with quote
Great work! Smile

Thanks for sharing Very Happy

I'll take a deep look at it tommorow so I can rate it somehow.

--Your Mešča
_________________


Greetz -- Europe!
RIP Michael Jackson
clcheung


Posts: 559

PostPosted: Wed May 13, 2009 10:40 pm    Post subject: Reply with quote
I ran into the smooth problem as you describe.

I discover two points, wondering if it is related to the problem ?

First, if more than one objects is selected to run the setup script, the UV mapping of the objects seems shared with the same image no mater "share image" option is used or not. Looks like something is wrong in uvcalc_smart_project ?

If I process mesh one by one, I can get a smooth baking if "auto smooth" option is off.
ynjh_jo


Posts: 1596
Location: Malang, Indonesia

PostPosted: Thu May 14, 2009 12:42 am    Post subject: Reply with quote
The smooth problem is only when baking shadow, and not AO.
Are you sure you get smooth shading when baking shadow ?
Any screenshot ?

In uvcalc_smart_project setup, there is "Share Tex Space" option, so if you want the selected objects share the same image, then you should let them share texture space too, otherwise don't. I've mentioned it via the "Share Image" button's tooltip. Did you get a chance to read it ?
clcheung


Posts: 559

PostPosted: Thu May 14, 2009 1:26 am    Post subject: Reply with quote
oic, with ""Share Tex Space" option off, I can separate them now.

About the smooth problem, here is the screenshoot:

http://picasaweb.google.com.tw/lh/photo/a40spB4z1-sVWRTk6mDVgA?feat=directlink

http://picasaweb.google.com.tw/lh/photo/-yVW6Uat_oUqtc8iCL7BnA?feat=directlink
clcheung


Posts: 559

PostPosted: Thu May 14, 2009 2:05 am    Post subject: Reply with quote
btw, why set the max angle to 1 when auto smooth option is selected ? Shouldn't it be something big, or I misunderstand the purpose ?
ynjh_jo


Posts: 1596
Location: Malang, Indonesia

PostPosted: Thu May 14, 2009 2:58 am    Post subject: Reply with quote
As you can see in my shots, if the autosmooth is OFF, there are alot of artifacts. It's gone if I use 1 degree, but remains if I use the default 30 or even 80.
If you download the first zip, could you try the scene in /noLightmap ?
Just to be sure we start with the same thing.
_________________
http://ynjh.panda3dprojects.com | http://ynjh.p3dp.com
Intel P4Prescott 2.8GHz HT | Elixir 1.5GB | ATI HD4670 1GB GDDR3
clcheung


Posts: 559

PostPosted: Thu May 14, 2009 4:25 am    Post subject: Reply with quote
As long as the mesh is set smooth before baking, it is ok. May be I am not getting the real question. Anyway, thank you for the script, it is working very well.
ynjh_jo


Posts: 1596
Location: Malang, Indonesia

PostPosted: Thu May 14, 2009 11:02 am    Post subject: Reply with quote
Suzie is already smooth before baking. Increasing soft size and samples to maximum values doesn't make any difference. It's still flat.

BTW, I use 2.4.8 internal renderer.
_________________
http://ynjh.panda3dprojects.com | http://ynjh.p3dp.com
Intel P4Prescott 2.8GHz HT | Elixir 1.5GB | ATI HD4670 1GB GDDR3
Sprite101


Posts: 17

PostPosted: Thu Sep 17, 2009 2:21 pm    Post subject: Reply with quote
I get an error when I try to run the setup. It says my materials don't have a 'texture' attribute.
ynjh_jo


Posts: 1596
Location: Malang, Indonesia

PostPosted: Fri Sep 18, 2009 5:45 am    Post subject: Reply with quote
What material is it, and how did you create it ?
Could you post the full traceback ?
_________________
http://ynjh.panda3dprojects.com | http://ynjh.p3dp.com
Intel P4Prescott 2.8GHz HT | Elixir 1.5GB | ATI HD4670 1GB GDDR3
Display posts from previous:   
Post new topic   Reply to topic    Panda3D Forum Index -> Code Snippets All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group