From eb323b30c300f07727e0ab1959578148d07865c3 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Thu, 18 Jun 2015 19:40:39 -0400 Subject: [PATCH] Implement kBlendAlpha flag (and BlendSpans) --- korman/exporter/material.py | 18 ++++++++++++------ korman/exporter/mesh.py | 25 ++++++++++++++----------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/korman/exporter/material.py b/korman/exporter/material.py index ec9f1a6..b5cc567 100644 --- a/korman/exporter/material.py +++ b/korman/exporter/material.py @@ -110,15 +110,16 @@ class _Texture: assert (texture or image) if texture is not None: - self.image = texture.image + if image is None: + image = texture.image self.calc_alpha = texture.use_calculate_alpha self.mipmap = texture.use_mipmap - self.use_alpha = texture.use_alpha - if image is not None: - self.image = image + else: self.calc_alpha = False self.mipmap = False - self.use_alpha = image.use_alpha + + self.image = image + self.use_alpha = (image.channels == 4 and image.use_alpha) def __eq__(self, other): if not isinstance(other, _Texture): @@ -222,10 +223,15 @@ class MaterialConverter: def _export_texture_type_image(self, bo, hsgmat, layer, texture): """Exports a Blender ImageTexture to a plLayer""" + # Does the image have any alpha at all? + has_alpha = (texture.image.channels == 4 and texture.image.use_alpha) or texture.use_calculate_alpha + # First, let's apply any relevant flags state = layer.state - if texture.invert_alpha: + if texture.invert_alpha and has_alpha: state.blendFlags |= hsGMatState.kBlendInvertAlpha + if texture.use_alpha and has_alpha: + state.blendFlags |= hsGMatState.kBlendAlpha if texture.extension == "CLIP": state.clampFlags |= hsGMatState.kClampTexture diff --git a/korman/exporter/mesh.py b/korman/exporter/mesh.py index db60c0b..6992cc9 100644 --- a/korman/exporter/mesh.py +++ b/korman/exporter/mesh.py @@ -37,10 +37,13 @@ class _RenderLevel: _MAJOR_SHIFT = 28 _MINOR_MASK = ((1 << _MAJOR_SHIFT) - 1) - def __init__(self, hsgmat, pass_index): - # TODO: Use hsGMaterial to determine major and minor + def __init__(self, hsgmat, pass_index, blendSpan=False): self.level = 0 + # Naive... BlendSpans (any blending on the first layer) are MAJOR_BLEND + if blendSpan: + self.major = self.MAJOR_BLEND + # We use the blender material's pass index (which we stashed in the hsGMaterial) to increment # the render pass, just like it says... self.level += pass_index @@ -52,15 +55,15 @@ class _RenderLevel: return hash(self.level) def _get_major(self): - return self.level >> _MAJOR_SHIFT + return self.level >> self._MAJOR_SHIFT def _set_major(self, value): - self.level = ((value << _MAJOR_SHIFT) & 0xFFFFFFFF) | self.minor + self.level = ((value << self._MAJOR_SHIFT) & 0xFFFFFFFF) | self.minor major = property(_get_major, _set_major) def _get_minor(self): - return self.level & _MINOR_MASK + return self.level & self._MINOR_MASK def _set_minor(self, value): - self.level = ((self.major << _MAJOR_SHIFT) & 0xFFFFFFFF) | value + self.level = ((self.major << self._MAJOR_SHIFT) & 0xFFFFFFFF) | value minor = property(_get_minor, _set_minor) @@ -69,7 +72,7 @@ class _DrawableCriteria: _layer = hsgmat.layers[0].object # better doggone well have a layer... self.blend_span = bool(_layer.state.blendFlags & hsGMatState.kBlendMask) self.criteria = 0 # TODO - self.render_level = _RenderLevel(hsgmat, pass_index) + self.render_level = _RenderLevel(hsgmat, pass_index, self.blend_span) def __eq__(self, other): if not isinstance(other, _DrawableCriteria): @@ -105,10 +108,10 @@ class MeshConverter: self._dspans = {} self._mesh_geospans = {} - def _create_geospan(self, bo, mesh, bm, hsgmat): + def _create_geospan(self, bo, mesh, bm, hsgmatKey): """Initializes a plGeometrySpan from a Blender Object and an hsGMaterial""" geospan = plGeometrySpan() - geospan.material = hsgmat + geospan.material = hsgmatKey # GeometrySpan format # For now, we really only care about the number of UVW Channels @@ -289,8 +292,8 @@ class MeshConverter: """Exports all Materials and creates plGeometrySpans""" geospans = [None] * len(mesh.materials) for i, blmat in enumerate(mesh.materials): - hsgmat = self.material.export_material(bo, blmat) - geospans[i] = (self._create_geospan(bo, mesh, blmat, hsgmat), blmat.pass_index) + matKey = self.material.export_material(bo, blmat) + geospans[i] = (self._create_geospan(bo, mesh, blmat, matKey), blmat.pass_index) return geospans def _export_static_lighting(self, bo):