From 2a693a03c25bc0c963606cf56171f42356f03d86 Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Fri, 30 Dec 2016 19:05:49 -0800 Subject: [PATCH] More cleanups --- korman/exporter/manager.py | 12 +++++++----- korman/exporter/material.py | 21 +++++++++++---------- korman/exporter/mesh.py | 24 +++++++++++++++--------- korman/korlib/texture.py | 12 ++++++------ 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/korman/exporter/manager.py b/korman/exporter/manager.py index 4541354..e331aa8 100644 --- a/korman/exporter/manager.py +++ b/korman/exporter/manager.py @@ -174,14 +174,16 @@ class ExportManager: key = self.add_object(pl=pClass, name=name, bl=bl, so=so).key return key - def find_key(self, pClass, bl=None, name=None, so=None): + def find_key(self, pClass, bl=None, name=None, so=None, loc=None): """Given a blender Object and a Plasma class, find (or create) an exported plKey""" - assert bl or so + assert loc or bl or so - if so is None: - location = self._pages[bl.plasma_object.page] - else: + if loc is not None: + location = loc + elif so is not None: location = so.key.location + else: + location = self._pages[bl.plasma_object.page] if name is None: if bl is not None: diff --git a/korman/exporter/material.py b/korman/exporter/material.py index bf8fa0c..0dba72e 100644 --- a/korman/exporter/material.py +++ b/korman/exporter/material.py @@ -110,7 +110,6 @@ class MaterialConverter: def __init__(self, exporter): self._obj2mat = {} self._bumpMats = {} - self._bumpLUT = None self._exporter = weakref.ref(exporter) self._pending = {} self._alphatest = {} @@ -240,16 +239,18 @@ class MaterialConverter: dw_layer.UVWSrc = du_uv | plLayerInterface.kUVWNormal dv_layer.UVWSrc = du_uv + 1 - if self._bumpLUT is None: - self._bumpLUT = plMipmap("BumpLutTexture", 16, 16, 1, plBitmap.kUncompressed, plBitmap.kRGB8888) - GLTexture.create_bump_LUT(self._bumpLUT) + page = self._mgr.get_textures_page(du_layer.key) + LUT_key = self._mgr.find_key(plMipmap, loc=page, name="BumpLutTexture") - page = self._mgr.get_textures_page(du_layer.key) - self._mgr.AddObject(page, self._bumpLUT) + if LUT_key is None: + bumpLUT = plMipmap("BumpLutTexture", 16, 16, 1, plBitmap.kUncompressed, plBitmap.kRGB8888) + GLTexture.create_bump_LUT(bumpLUT) + self._mgr.AddObject(page, bumpLUT) + LUT_key = bumpLUT.key - du_layer.texture = self._bumpLUT.key - dw_layer.texture = self._bumpLUT.key - dv_layer.texture = self._bumpLUT.key + du_layer.texture = LUT_key + dw_layer.texture = LUT_key + dv_layer.texture = LUT_key return (du_layer, dw_layer, dv_layer) @@ -673,7 +674,7 @@ class MaterialConverter: def get_materials(self, bo): return self._obj2mat.get(bo, []) - def has_bump_layer(self, bo): + def get_bump_layer(self, bo): return self._bumpMats.get(bo, None) def get_texture_animation_key(self, bo, bm, tex_name=None, tex_slot=None): diff --git a/korman/exporter/mesh.py b/korman/exporter/mesh.py index 859c3ed..15cfa52 100644 --- a/korman/exporter/mesh.py +++ b/korman/exporter/mesh.py @@ -175,7 +175,7 @@ class MeshConverter: def _export_geometry(self, bo, mesh, materials, geospans): geodata = [_GeoData(len(mesh.vertices)) for i in materials] - has_bumpmap = self.material.has_bump_layer(bo) + has_bumpmap = self.material.get_bump_layer(bo) # Locate relevant vertex color layers now... color, alpha = None, None @@ -225,13 +225,19 @@ class MeshConverter: if len(tessface.vertices) != 3: gradPass.append([tessface.vertices[0], tessface.vertices[1], tessface.vertices[2]]) gradPass.append([tessface.vertices[0], tessface.vertices[2], tessface.vertices[3]]) - gradUVWs.append([tuple([uvw[0] for uvw in tessface_uvws]), tuple([uvw[1] for uvw in tessface_uvws]), tuple([uvw[2] for uvw in tessface_uvws])]) - gradUVWs.append([tuple([uvw[0] for uvw in tessface_uvws]), tuple([uvw[2] for uvw in tessface_uvws]), tuple([uvw[3] for uvw in tessface_uvws])]) + gradUVWs.append((tuple((uvw[0] for uvw in tessface_uvws)), + tuple((uvw[1] for uvw in tessface_uvws)), + tuple((uvw[2] for uvw in tessface_uvws)))) + gradUVWs.append((tuple((uvw[0] for uvw in tessface_uvws)), + tuple((uvw[2] for uvw in tessface_uvws)), + tuple((uvw[3] for uvw in tessface_uvws)))) else: gradPass.append(tessface.vertices) - gradUVWs.append([tuple([uvw[0] for uvw in tessface_uvws]), tuple([uvw[1] for uvw in tessface_uvws]), tuple([uvw[2] for uvw in tessface_uvws])]) + gradUVWs.append((tuple((uvw[0] for uvw in tessface_uvws)), + tuple((uvw[1] for uvw in tessface_uvws)), + tuple((uvw[2] for uvw in tessface_uvws)))) - for p,vids in enumerate(gradPass): + for p, vids in enumerate(gradPass): dPosDu += self._get_bump_gradient(has_bumpmap[1], gradUVWs[p], mesh, vids, has_bumpmap[0], 0) dPosDv += self._get_bump_gradient(has_bumpmap[1], gradUVWs[p], mesh, vids, has_bumpmap[0], 1) dPosDv = -dPosDv @@ -301,7 +307,7 @@ class MeshConverter: # If we're bump mapping, we need to normalize our magic UVW channels if has_bumpmap is not None: geospan.format += 2 # We dded 2 special bumpmapping UV layers - for v, vtx in enumerate(data.vertices): + for vtx in data.vertices: uvMap = vtx.uvs uvMap[geospan.format - 2].normalize() uvMap[geospan.format - 1].normalize() @@ -317,9 +323,9 @@ class MeshConverter: v1 = hsVector3(*mesh.vertices[vIds[1]].co) v2 = hsVector3(*mesh.vertices[vIds[2]].co) - uv0 = [(uv[0], uv[1], 0.0) for uv in uvws[0]][uvIdx] - uv1 = [(uv[0], uv[1], 0.0) for uv in uvws[1]][uvIdx] - uv2 = [(uv[0], uv[1], 0.0) for uv in uvws[2]][uvIdx] + uv0 = (uvws[0][uvIdx][0], uvws[0][uvIdx][1], 0.0) + uv1 = (uvws[1][uvIdx][0], uvws[1][uvIdx][1], 0.0) + uv2 = (uvws[2][uvIdx][0], uvws[2][uvIdx][1], 0.0) notUV = int(not iUV) kRealSmall = 1.e-6 diff --git a/korman/korlib/texture.py b/korman/korlib/texture.py index baa30e9..2d4e4f7 100644 --- a/korman/korlib/texture.py +++ b/korman/korlib/texture.py @@ -201,26 +201,26 @@ class GLTexture: doneH = 0 doneH = startH * kLUTWidth * 4 - buf[0:doneH] = [b for x in range(kLUTWidth) for b in [0, 0, int((x / denom) * 255.9), 255]] * startH + buf[0:doneH] = [b for x in range(kLUTWidth) for b in (0, 0, int((x / denom) * 255.9), 255)] * startH startH = doneH doneH += delH * kLUTWidth * 4 - buf[startH:doneH] = [b for x in range(kLUTWidth) for b in [127, 127, int((x / denom) * 255.9), 255]] * delH + buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (127, 127, int((x / denom) * 255.9), 255)] * delH startH = doneH doneH += delH * kLUTWidth * 4 - buf[startH:doneH] = [b for x in range(kLUTWidth) for b in [0, int((x / denom) * 255.9), 0, 255]] * delH + buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (0, int((x / denom) * 255.9), 0, 255)] * delH startH = doneH doneH += delH * kLUTWidth * 4 - buf[startH:doneH] = [b for x in range(kLUTWidth) for b in [127, int((x / denom) * 255.9), 127, 255]] * delH + buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (127, int((x / denom) * 255.9), 127, 255)] * delH startH = doneH doneH += delH * kLUTWidth * 4 - buf[startH:doneH] = [b for x in range(kLUTWidth) for b in [int((x / denom) * 255.9), 0, 0, 255]] * delH + buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (int((x / denom) * 255.9), 0, 0, 255)] * delH startH = doneH doneH += delH * kLUTWidth * 4 - buf[startH:doneH] = [b for x in range(kLUTWidth) for b in [int((x / denom) * 255.9), 127, 127, 255]] * startH + buf[startH:doneH] = [b for x in range(kLUTWidth) for b in (int((x / denom) * 255.9), 127, 127, 255)] * startH mipmap.setRawImage(bytes(buf))