Browse Source

Fix sharp lightmap edges

See this thread for more information:
http://forum.guildofwriters.org/viewtopic.php?f=7&t=6088
pull/6/head
Adam Johnson 10 years ago
parent
commit
cce4533a25
  1. 3
      korman/exporter/material.py
  2. 2
      korman/exporter/mesh.py
  3. 84
      korman/operators/op_lightmap.py
  4. 5
      korman/properties/modifiers/render.py
  5. 4
      korman/ui/modifiers/render.py

3
korman/exporter/material.py

@ -139,10 +139,7 @@ class _Texture:
if name.find(".") == -1: if name.find(".") == -1:
return "{}{}".format(name, newext) return "{}{}".format(name, newext)
name, end = os.path.splitext(name) name, end = os.path.splitext(name)
if name.find(".") == -1:
return "{}{}".format(name, newext) return "{}{}".format(name, newext)
name, oldext = os.path.splitext(name)
return "{}{}{}".format(name, end, newext)
def _update(self, other): def _update(self, other):
"""Update myself with any props that might be overridable from another copy of myself""" """Update myself with any props that might be overridable from another copy of myself"""

2
korman/exporter/mesh.py

@ -298,9 +298,7 @@ class MeshConverter:
lm = bo.plasma_modifiers.lightmap lm = bo.plasma_modifiers.lightmap
if lm.enabled: if lm.enabled:
print(" Baking lightmap...") print(" Baking lightmap...")
print("====")
bpy.ops.object.plasma_lightmap_autobake(light_group=lm.light_group) bpy.ops.object.plasma_lightmap_autobake(light_group=lm.light_group)
print("====")
else: else:
for vcol_layer in bo.data.vertex_colors: for vcol_layer in bo.data.vertex_colors:
name = vcol_layer.name.lower() name = vcol_layer.name.lower()

84
korman/operators/op_lightmap.py

@ -37,6 +37,7 @@ class _LightingOperator:
toggle.track(render, "use_envmaps", False) toggle.track(render, "use_envmaps", False)
toggle.track(render, "use_raytrace", True) toggle.track(render, "use_raytrace", True)
toggle.track(render, "bake_type", "FULL") toggle.track(render, "bake_type", "FULL")
toggle.track(render, "use_bake_clear", True)
def _generate_lightgroups(self, mesh, user_lg=None): def _generate_lightgroups(self, mesh, user_lg=None):
"""Makes a new light group for the baking process that excludes all Plasma RT lamps""" """Makes a new light group for the baking process that excludes all Plasma RT lamps"""
@ -87,13 +88,32 @@ class LightmapAutobakeOperator(_LightingOperator, bpy.types.Operator):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
def _associate_image_with_uvtex(self, uvtex, im):
# Associate the image with all the new UVs
# NOTE: no toggle here because it's the artist's problem if they are looking at our
# super swagalicious LIGHTMAPGEN uvtexture...
for i in uvtex.data:
i.image = im
def _get_base_uvtex(self, mesh, modifier):
if modifier.uv_map:
return mesh.uv_textures[modifier.uv_map]
for i in mesh.uv_textures:
if i.name != "LIGHTMAPGEN":
return i
return None
def execute(self, context): def execute(self, context):
obj = context.active_object
mesh = obj.data
modifier = obj.plasma_modifiers.lightmap
uv_textures = mesh.uv_textures
with GoodNeighbor() as toggle: with GoodNeighbor() as toggle:
# We need to ensure that we bake onto the "BlahObject_LIGHTMAPGEN" image # We need to ensure that we bake onto the "BlahObject_LIGHTMAPGEN" image
obj = context.active_object
data_images = bpy.data.images data_images = bpy.data.images
im_name = "{}_LIGHTMAPGEN".format(obj.name) im_name = "{}_LIGHTMAPGEN.png".format(obj.name)
size = obj.plasma_modifiers.lightmap.resolution size = modifier.resolution
im = data_images.get(im_name) im = data_images.get(im_name)
if im is None: if im is None:
@ -104,36 +124,42 @@ class LightmapAutobakeOperator(_LightingOperator, bpy.types.Operator):
data_images.remove(im) data_images.remove(im)
im = data_images.new(im_name, width=size, height=size) im = data_images.new(im_name, width=size, height=size)
# This just wraps Blender's internal lightmap UV whatchacallit... # If there is a cached LIGHTMAPGEN uvtexture, nuke it
# We want to ensure that we use the UV Layer "LIGHTMAPGEN" and fetch the size from uvtex = uv_textures.get("LIGHTMAPGEN", None)
# the lightmap modifier. What fun... if uvtex is not None:
mesh = context.active_object.data uv_textures.remove(uvtex)
# Search for LIGHTMAPGEN # Originally, we used the lightmap unpack UV operator to make our UV texture, however,
for i in mesh.uv_textures: # this tended to create sharp edges. There was already a discussion about this on the
if i.name == "LIGHTMAPGEN": # Guild of Writers forum, so I'm implementing a code version of dendwaler's process,
uvtex = i # as detailed here: http://forum.guildofwriters.org/viewtopic.php?p=62572#p62572
break uv_base = self._get_base_uvtex(mesh, modifier)
else: if uv_base is not None:
toggle.track(i, "active_render", False) uv_textures.active = uv_base
# this will copy the UVs to the new UV texture
uvtex = uv_textures.new("LIGHTMAPGEN")
uv_textures.active = uvtex
self._associate_image_with_uvtex(uvtex, im)
# here we go...
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="SELECT")
bpy.ops.uv.average_islands_scale()
bpy.ops.uv.pack_islands()
else: else:
uvtex = mesh.uv_textures.new("LIGHTMAPGEN") # same thread, see Sirius's suggestion RE smart unwrap. this seems to yield good
uvtex.active_render = False # results in my tests. it will be good enough for quick exports.
toggle.track(mesh.uv_textures, "active", uvtex) uvtex = uv_textures.new("LIGHTMAPGEN")
toggle.track(uvtex, "active_render", True) self._associate_image_with_uvtex(uvtex, im)
# Now, enter edit mode on this mesh and unwrap.
bpy.ops.object.mode_set(mode="EDIT") bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action="SELECT") bpy.ops.mesh.select_all(action="SELECT")
bpy.ops.uv.lightmap_pack(PREF_CONTEXT="ALL_FACES", PREF_IMG_PX_SIZE=size) bpy.ops.uv.smart_project()
bpy.ops.object.mode_set(mode="OBJECT") bpy.ops.object.mode_set(mode="OBJECT")
mesh.update()
# Associate the image with all the new UVs # Now, set the new LIGHTMAPGEN uv layer as what we want to render to...
# NOTE: no toggle here because it's the artist's problem if they are looking at our for i in uv_textures:
# super swagalicious LIGHTMAPGEN uvtexture... value = i.name == "LIGHTMAPGEN"
for i in mesh.uv_textures.active.data: i.active = value
i.image = im i.active_render = value
# Bake settings # Bake settings
render = context.scene.render render = context.scene.render
@ -168,7 +194,7 @@ class LightmapAutobakePreviewOperator(_LightingOperator, bpy.types.Operator):
if tex is None: if tex is None:
tex = bpy.data.textures.new("LIGHTMAPGEN_PREVIEW", "IMAGE") tex = bpy.data.textures.new("LIGHTMAPGEN_PREVIEW", "IMAGE")
tex.extension = "CLIP" tex.extension = "CLIP"
tex.image = bpy.data.images["{}_LIGHTMAPGEN".format(context.active_object.name)] tex.image = bpy.data.images["{}_LIGHTMAPGEN.png".format(context.active_object.name)]
return {"FINISHED"} return {"FINISHED"}

5
korman/properties/modifiers/render.py

@ -39,13 +39,16 @@ class PlasmaLightMapGen(PlasmaModifierProperties):
light_group = StringProperty(name="Light Group", light_group = StringProperty(name="Light Group",
description="Group that defines the collection of lights to bake") description="Group that defines the collection of lights to bake")
uv_map = StringProperty(name="UV Texture",
description="UV Texture used as the basis for the lightmap")
def created(self, obj): def created(self, obj):
self.display_name = "{}_LIGHTMAPGEN".format(obj.name) self.display_name = "{}_LIGHTMAPGEN".format(obj.name)
def export(self, exporter, bo, so): def export(self, exporter, bo, so):
mat_mgr = exporter.mesh.material mat_mgr = exporter.mesh.material
materials = mat_mgr.get_materials(bo) materials = mat_mgr.get_materials(bo)
lightmap_im = bpy.data.images.get("{}_LIGHTMAPGEN".format(bo.name)) lightmap_im = bpy.data.images.get("{}_LIGHTMAPGEN.png".format(bo.name))
# Find the stupid UVTex # Find the stupid UVTex
uvw_src = 0 uvw_src = 0

4
korman/ui/modifiers/render.py

@ -18,6 +18,8 @@ import bpy
def lightmap(modifier, layout, context): def lightmap(modifier, layout, context):
layout.row(align=True).prop(modifier, "quality", expand=True) layout.row(align=True).prop(modifier, "quality", expand=True)
layout.prop_search(modifier, "light_group", bpy.data, "groups", icon="GROUP") layout.prop_search(modifier, "light_group", bpy.data, "groups", icon="GROUP")
layout.prop_search(modifier, "uv_map", context.active_object.data, "uv_textures")
operator = layout.operator("object.plasma_lightmap_preview", "Preview Lightmap", icon="RENDER_STILL") operator = layout.operator("object.plasma_lightmap_preview", "Preview Lightmap", icon="RENDER_STILL")
operator.light_group = modifier.light_group operator.light_group = modifier.light_group
@ -27,6 +29,6 @@ def lightmap(modifier, layout, context):
# well... It was nice knowing you! # well... It was nice knowing you!
tex = bpy.data.textures.get("LIGHTMAPGEN_PREVIEW") tex = bpy.data.textures.get("LIGHTMAPGEN_PREVIEW")
if tex is not None: if tex is not None:
im_name = "{}_LIGHTMAPGEN".format(context.active_object.name) im_name = "{}_LIGHTMAPGEN.png".format(context.active_object.name)
if tex.image.name == im_name: if tex.image.name == im_name:
layout.template_preview(tex, show_buttons=False) layout.template_preview(tex, show_buttons=False)

Loading…
Cancel
Save