mirror of
https://github.com/H-uru/korman.git
synced 2025-07-14 22:36:52 +00:00
Rudimentary material exporter
This commit is contained in:
@ -40,7 +40,7 @@ class Exporter:
|
||||
|
||||
# Step 0: Init export resmgr and stuff
|
||||
self.mgr = manager.ExportManager(globals()[self._op.version])
|
||||
self.mesh = mesh.MeshConverter(self.mgr)
|
||||
self.mesh = mesh.MeshConverter(self)
|
||||
self.report = logger.ExportAnalysis()
|
||||
|
||||
# Step 1: Gather a list of objects that we need to export
|
||||
@ -101,7 +101,7 @@ class Exporter:
|
||||
if childobj:
|
||||
parent = bo.parent
|
||||
if parent.plasma_object.enabled:
|
||||
print("\tAttaching to parent SceneObject '{}'".format(parent.name))
|
||||
print(" Attaching to parent SceneObject '{}'".format(parent.name))
|
||||
|
||||
# Instead of exporting a skeleton now, we'll just make an orphaned CI.
|
||||
# The bl_obj export will make this work.
|
||||
@ -139,7 +139,7 @@ class Exporter:
|
||||
print("WARNING: '{}' is a Plasma Object of Blender type '{}'".format(bl_obj.name, bl_obj.type))
|
||||
print("... And I have NO IDEA what to do with that! Tossing.")
|
||||
continue
|
||||
print("\tBlender Object '{}' of type '{}'".format(bl_obj.name, bl_obj.type))
|
||||
print(" Blender Object '{}' of type '{}'".format(bl_obj.name, bl_obj.type))
|
||||
|
||||
# Create a sceneobject if one does not exist.
|
||||
# Before we call the export_fn, we need to determine if this object is an actor of any
|
||||
@ -154,4 +154,7 @@ class Exporter:
|
||||
pass
|
||||
|
||||
def _export_mesh_blobj(self, so, bo):
|
||||
so.draw = self.mesh.export_object(bo)
|
||||
if bo.data.materials:
|
||||
so.draw = self.mesh.export_object(bo)
|
||||
else:
|
||||
print(" No material(s) on the ObData, so no drawables")
|
||||
|
@ -18,6 +18,13 @@ class ExportError(Exception):
|
||||
super(Exception, self).__init__(value)
|
||||
|
||||
|
||||
class TooManyUVChannelsError(ExportError):
|
||||
def __init__(self, obj, mat):
|
||||
msg = "There are too many UV Textures on the material '{}' associated with object '{}'.".format(
|
||||
mat.name, obj.name)
|
||||
super(ExportError, self).__init__(msg)
|
||||
|
||||
|
||||
class TooManyVerticesError(ExportError):
|
||||
def __init__(self, mesh, matname, vertcount):
|
||||
msg = "There are too many vertices ({}) on the mesh data '{}' associated with material '{}'".format(
|
||||
@ -41,3 +48,8 @@ class UndefinedPageError(ExportError):
|
||||
def raise_if_error(self):
|
||||
if self.mistakes:
|
||||
raise self
|
||||
|
||||
|
||||
class UnsupportedTextureError(ExportError):
|
||||
def __init__(self, texture, material):
|
||||
super(ExportError, self).__init__("Cannot export texture '{}' on material '{}' -- unsupported type '{}'".format(texture.name, texture.type, material.name))
|
||||
|
@ -90,7 +90,7 @@ class ExportManager:
|
||||
def create_builtins(self, age, textures):
|
||||
# BuiltIn.prp
|
||||
if bpy.context.scene.world.plasma_age.age_sdl:
|
||||
builtin = self.create_page(age, "BuiltIn", -1, True)
|
||||
builtin = self.create_page(age, "BuiltIn", -2, True)
|
||||
pfm = self.add_object(plPythonFileMod, name="VeryVerySpecialPythonFileMod", loc=builtin)
|
||||
pfm.filename = age
|
||||
sdl = self.add_object(plSceneObject, name="AgeSDLHook", loc=builtin)
|
||||
@ -98,7 +98,7 @@ class ExportManager:
|
||||
|
||||
# Textures.prp
|
||||
if textures:
|
||||
self.create_page(age, "Textures", -2, True)
|
||||
self.create_page(age, "Textures", -1, True)
|
||||
|
||||
def create_page(self, age, name, id, builtin=False):
|
||||
location = plLocation(self.mgr.getVer())
|
||||
|
130
korman/exporter/material.py
Normal file
130
korman/exporter/material.py
Normal file
@ -0,0 +1,130 @@
|
||||
# This file is part of Korman.
|
||||
#
|
||||
# Korman is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Korman is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Korman. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import bpy
|
||||
import korlib
|
||||
from PyHSPlasma import *
|
||||
import weakref
|
||||
|
||||
from . import explosions
|
||||
from . import utils
|
||||
|
||||
class MaterialConverter:
|
||||
_hsbitmaps = {}
|
||||
|
||||
def __init__(self, exporter):
|
||||
self._exporter = weakref.ref(exporter)
|
||||
|
||||
def export_material(self, bo, bm):
|
||||
"""Exports a Blender Material as an hsGMaterial"""
|
||||
print(" Exporting Material '{}'".format(bm.name))
|
||||
|
||||
hsgmat = self._mgr.add_object(hsGMaterial, name=bm.name, bl=bo)
|
||||
self._export_texture_slots(bo, bm, hsgmat)
|
||||
|
||||
# Plasma makes several assumptions that every hsGMaterial has at least one layer. If this
|
||||
# material had no Textures, we will need to initialize a default layer
|
||||
if not hsgmat.layers:
|
||||
layer = self._mgr.add_object(plLayer, name="{}_AutoLayer".format(bm.name), bl=bo)
|
||||
self._propagate_material_settings(bm, layer)
|
||||
hsgmat.addLayer(layer.key)
|
||||
|
||||
# Looks like we're done...
|
||||
return hsgmat.key
|
||||
|
||||
def _export_texture_slots(self, bo, bm, hsgmat):
|
||||
for slot in bm.texture_slots:
|
||||
if slot is None or not slot.use:
|
||||
continue
|
||||
|
||||
name = "{}_{}".format(bm.name, slot.name)
|
||||
print(" Exporting Plasma Layer '{}'".format(name))
|
||||
layer = self._mgr.add_object(plLayer, name=name, bl=bo)
|
||||
self._propagate_material_settings(bm, layer)
|
||||
|
||||
# UVW Channel
|
||||
for i, uvchan in enumerate(bo.data.tessface_uv_textures):
|
||||
if uvchan.name == slot.uv_layer:
|
||||
layer.UVWSrc = i
|
||||
print(" Using UV Map #{} '{}'".format(i, name))
|
||||
break
|
||||
else:
|
||||
print(" No UVMap specified... Blindly using the first one, maybe it exists :|")
|
||||
|
||||
# General texture flags and such
|
||||
texture = slot.texture
|
||||
# ...
|
||||
|
||||
# Export the specific texture type
|
||||
export_fn = "_export_texture_type_{}".format(texture.type.lower())
|
||||
if not hasattr(self, export_fn):
|
||||
raise explosions.UnsupportedTextureError(texture, bm)
|
||||
getattr(self, export_fn)(bo, hsgmat, layer, texture)
|
||||
hsgmat.addLayer(layer.key)
|
||||
|
||||
def _export_texture_type_image(self, bo, hsgmat, layer, texture):
|
||||
"""Exports a Blender ImageTexture to a plLayer"""
|
||||
|
||||
# First, let's apply any relevant flags
|
||||
state = layer.state
|
||||
if texture.invert_alpha:
|
||||
state.blendFlags |= hsGMatState.kBlendInvertAlpha
|
||||
|
||||
# Now, let's export the plBitmap
|
||||
# If the image is None (no image applied in Blender), we assume this is a plDynamicTextMap
|
||||
# Otherwise, we create a plMipmap and call into korlib to export the pixel data
|
||||
if texture.image is None:
|
||||
bitmap = self.add_object(plDynamicTextMap, name="{}_DynText".format(layer.key.name), bl=bo)
|
||||
else:
|
||||
# blender likes to create lots of spurious .0000001 objects :/
|
||||
name = texture.image.name
|
||||
name = name[:name.find('.')]
|
||||
if texture.use_mipmap:
|
||||
name = "{}.dds".format(name)
|
||||
else:
|
||||
name = "{}.bmp".format(name)
|
||||
|
||||
if name in self._hsbitmaps:
|
||||
# well, that was easy...
|
||||
print(" Using '{}'".format(name))
|
||||
layer.texture = self._hsbitmaps[name].key
|
||||
return
|
||||
else:
|
||||
location = self._mgr.get_textures_page(bo)
|
||||
bitmap = self._mgr.add_object(plMipmap, name=name, loc=location)
|
||||
korlib.generate_mipmap(texture, bitmap)
|
||||
|
||||
# Store the created plBitmap and toss onto the layer
|
||||
self._hsbitmaps[name] = bitmap
|
||||
layer.texture = bitmap.key
|
||||
|
||||
@property
|
||||
def _mgr(self):
|
||||
return self._exporter().mgr
|
||||
|
||||
def _propagate_material_settings(self, bm, layer):
|
||||
"""Converts settings from the Blender Material to corresponding plLayer settings"""
|
||||
state = layer.state
|
||||
|
||||
# Shade Flags
|
||||
if not bm.use_mist:
|
||||
state.shadeFlags |= hsGMatState.kShadeNoFog # Dead in CWE
|
||||
state.shadeFlags |= hsGMatState.kShadeReallyNoFog
|
||||
|
||||
# Colors
|
||||
layer.ambient = utils.color(bpy.context.scene.world.ambient_color)
|
||||
layer.preshade = utils.color(bm.diffuse_color)
|
||||
layer.runtime = utils.color(bm.diffuse_color)
|
||||
layer.specular = utils.color(bm.specular_color)
|
@ -15,8 +15,10 @@
|
||||
|
||||
import bpy
|
||||
from PyHSPlasma import *
|
||||
import weakref
|
||||
|
||||
from . import explosions
|
||||
from . import material
|
||||
from . import utils
|
||||
|
||||
_MAX_VERTS_PER_SPAN = 0xFFFF
|
||||
@ -32,9 +34,14 @@ class _RenderLevel:
|
||||
_MAJOR_SHIFT = 28
|
||||
_MINOR_MASK = ((1 << _MAJOR_SHIFT) - 1)
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, hsgmat, pass_index):
|
||||
# TODO: Use hsGMaterial to determine major and minor
|
||||
self.level = 0
|
||||
|
||||
# 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
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.level)
|
||||
|
||||
@ -52,11 +59,11 @@ class _RenderLevel:
|
||||
|
||||
|
||||
class _DrawableCriteria:
|
||||
def __init__(self, hsgmat):
|
||||
def __init__(self, hsgmat, pass_index):
|
||||
_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()
|
||||
self.render_level = _RenderLevel(hsgmat, pass_index)
|
||||
|
||||
def __eq__(self, other):
|
||||
if not isinstance(other, _DrawableCriteria):
|
||||
@ -81,14 +88,22 @@ class MeshConverter:
|
||||
_dspans = {}
|
||||
_mesh_geospans = {}
|
||||
|
||||
def __init__(self, mgr):
|
||||
self._mgr = mgr
|
||||
def __init__(self, exporter):
|
||||
self._exporter = weakref.ref(exporter)
|
||||
self.material = material.MaterialConverter(exporter)
|
||||
|
||||
def _create_geospan(self, bo, bm, hsgmat):
|
||||
def _create_geospan(self, bo, mesh, bm, hsgmat):
|
||||
"""Initializes a plGeometrySpan from a Blender Object and an hsGMaterial"""
|
||||
geospan = plGeometrySpan()
|
||||
geospan.material = hsgmat
|
||||
|
||||
# GeometrySpan format
|
||||
# For now, we really only care about the number of UVW Channels
|
||||
numUVWchans = len(mesh.tessface_uv_textures)
|
||||
if numUVWchans > plGeometrySpan.kUVCountMask:
|
||||
raise explosions.TooManyUVChannelsError(bo, bm)
|
||||
geospan.format = numUVWchans
|
||||
|
||||
# TODO: Props
|
||||
# TODO: RunTime lights (requires libHSPlasma feature)
|
||||
|
||||
@ -107,7 +122,7 @@ class MeshConverter:
|
||||
|
||||
for loc in self._dspans.values():
|
||||
for dspan in loc.values():
|
||||
print("\tFinalizing DSpan: '{}'".format(dspan.key.name))
|
||||
print(" Finalizing DSpan: '{}'".format(dspan.key.name))
|
||||
|
||||
# This mega-function does a lot:
|
||||
# 1. Converts SourceSpans (geospans) to Icicles and bakes geometry into plGBuffers
|
||||
@ -117,46 +132,59 @@ class MeshConverter:
|
||||
dspan.composeGeometry(True, True)
|
||||
|
||||
def _export_geometry(self, mesh, geospans):
|
||||
geodata = [None] * len(mesh.materials)
|
||||
geoverts = [None] * len(mesh.vertices)
|
||||
for i, garbage in enumerate(geodata):
|
||||
geodata[i] = {
|
||||
"blender2gs": [None] * len(mesh.vertices),
|
||||
"triangles": [],
|
||||
"vertices": [],
|
||||
}
|
||||
|
||||
# Go ahead and naively convert all vertices into TempVertices for the GeoSpans
|
||||
for i, source in enumerate(mesh.vertices):
|
||||
vertex = plGeometrySpan.TempVertex()
|
||||
vertex.color = hsColor32(red=255, green=0, blue=0, alpha=255) # FIXME trollface.jpg testing hacks
|
||||
vertex.normal = utils.vector3(source.normal)
|
||||
vertex.position = utils.vector3(source.co)
|
||||
geoverts[i] = vertex
|
||||
_geodatacls = type("_GeoData",
|
||||
(object,),
|
||||
{
|
||||
"blender2gs": [{} for i in mesh.vertices],
|
||||
"triangles": [],
|
||||
"vertices": []
|
||||
})
|
||||
geodata = [_geodatacls() for i in mesh.materials]
|
||||
|
||||
# Convert Blender faces into things we can stuff into libHSPlasma
|
||||
for tessface in mesh.tessfaces:
|
||||
for i, tessface in enumerate(mesh.tessfaces):
|
||||
data = geodata[tessface.material_index]
|
||||
face_verts = []
|
||||
|
||||
# Convert to per-material indices
|
||||
for i in tessface.vertices:
|
||||
if data["blender2gs"][i] is None:
|
||||
data["blender2gs"][i] = len(data["vertices"])
|
||||
data["vertices"].append(geoverts[i])
|
||||
face_verts.append(data["blender2gs"][i])
|
||||
for j in tessface.vertices:
|
||||
# Unpack the UV coordinates from each UV Texture layer
|
||||
uvws = []
|
||||
for uvtex in mesh.tessface_uv_textures:
|
||||
uv = getattr(uvtex.data[i], "uv{}".format(j+1))
|
||||
# In Blender, UVs have no Z coordinate
|
||||
uvws.append((uv.x, uv.y))
|
||||
|
||||
# Grab VCols (TODO--defaulting to white for now)
|
||||
# This will be finalized once the vertex color light code baking is in
|
||||
color = (0, 0, 0, 255)
|
||||
|
||||
# Now, we'll index into the vertex dict using the per-face elements :(
|
||||
# We're using tuples because lists are not hashable. The many mathutils and PyHSPlasma
|
||||
# types are not either, and it's entirely too much work to fool with all that.
|
||||
coluv = (color, tuple(uvws))
|
||||
if coluv not in data.blender2gs[j]:
|
||||
source = mesh.vertices[j]
|
||||
vertex = plGeometrySpan.TempVertex()
|
||||
vertex.position = utils.vector3(source.co)
|
||||
vertex.normal = utils.vector3(source.normal)
|
||||
vertex.color = hsColor32(*color)
|
||||
vertex.uvs = [hsVector3(uv[0], 1.0-uv[1], 0.0) for uv in uvws]
|
||||
data.blender2gs[j][coluv] = len(data.vertices)
|
||||
data.vertices.append(vertex)
|
||||
face_verts.append(data.blender2gs[j][coluv])
|
||||
|
||||
# Convert to triangles, if need be...
|
||||
if len(face_verts) == 3:
|
||||
data["triangles"] += face_verts
|
||||
data.triangles += face_verts
|
||||
elif len(face_verts) == 4:
|
||||
data["triangles"] += (face_verts[0], face_verts[1], face_verts[2])
|
||||
data["triangles"] += (face_verts[0], face_verts[2], face_verts[3])
|
||||
data.triangles += (face_verts[0], face_verts[1], face_verts[2])
|
||||
data.triangles += (face_verts[0], face_verts[2], face_verts[3])
|
||||
|
||||
# Time to finish it up...
|
||||
for i, data in enumerate(geodata):
|
||||
geospan = geospans[i]
|
||||
numVerts = len(data["vertices"])
|
||||
geospan = geospans[i][0]
|
||||
numVerts = len(data.vertices)
|
||||
|
||||
# Soft vertex limit at 0x8000 for PotS and below. Works fine as long as it's a uint16
|
||||
# MOUL only allows signed int16s, however :/
|
||||
@ -166,8 +194,8 @@ class MeshConverter:
|
||||
pass # FIXME
|
||||
|
||||
# If we're still here, let's add our data to the GeometrySpan
|
||||
geospan.indices = data["triangles"]
|
||||
geospan.vertices = data["vertices"]
|
||||
geospan.indices = data.triangles
|
||||
geospan.vertices = data.vertices
|
||||
|
||||
def export_object(self, bo):
|
||||
# Have we already exported this mesh?
|
||||
@ -195,9 +223,9 @@ class MeshConverter:
|
||||
|
||||
# Step 3: Add plGeometrySpans to the appropriate DSpan and create indices
|
||||
_diindices = {}
|
||||
for geospan in geospans:
|
||||
dspan = self._find_create_dspan(bo, geospan.material.object)
|
||||
print("\tExported hsGMaterial '{}' geometry into '{}'".format(geospan.material.name, dspan.key.name))
|
||||
for geospan, pass_index in geospans:
|
||||
dspan = self._find_create_dspan(bo, geospan.material.object, pass_index)
|
||||
print(" Exported hsGMaterial '{}' geometry into '{}'".format(geospan.material.name, dspan.key.name))
|
||||
idx = dspan.addSourceSpan(geospan)
|
||||
if dspan not in _diindices:
|
||||
_diindices[dspan] = [idx,]
|
||||
@ -213,25 +241,15 @@ class MeshConverter:
|
||||
drawables.append((dspan.key, idx))
|
||||
return drawables
|
||||
|
||||
def _export_material(self, bo, bm):
|
||||
"""Exports a single Material Slot as an hsGMaterial"""
|
||||
# FIXME HACKS
|
||||
hsgmat = self._mgr.add_object(hsGMaterial, name=bm.name, bl=bo)
|
||||
fake_layer = self._mgr.add_object(plLayer, name="{}_AutoLayer".format(bm.name), bl=bo)
|
||||
hsgmat.addLayer(fake_layer.key)
|
||||
# ...
|
||||
|
||||
return hsgmat.key
|
||||
|
||||
def _export_material_spans(self, bo, mesh):
|
||||
"""Exports all Materials and creates plGeometrySpans"""
|
||||
geospans = [None] * len(mesh.materials)
|
||||
for i, blmat in enumerate(mesh.materials):
|
||||
hsgmat = self._export_material(bo, blmat)
|
||||
geospans[i] = self._create_geospan(bo, blmat, hsgmat)
|
||||
hsgmat = self.material.export_material(bo, blmat)
|
||||
geospans[i] = (self._create_geospan(bo, mesh, blmat, hsgmat), blmat.pass_index)
|
||||
return geospans
|
||||
|
||||
def _find_create_dspan(self, bo, hsgmat):
|
||||
def _find_create_dspan(self, bo, hsgmat, pass_index):
|
||||
location = self._mgr.get_location(bo)
|
||||
if location not in self._dspans:
|
||||
self._dspans[location] = {}
|
||||
@ -241,8 +259,7 @@ class MeshConverter:
|
||||
# [... document me ...]
|
||||
# We're using pass index to do just what it was designed for. Cyan has a nicer "depends on"
|
||||
# draw component, but pass index is the Blender way, so that's what we're doing.
|
||||
crit = _DrawableCriteria(hsgmat)
|
||||
crit.render_level.level += bo.pass_index
|
||||
crit = _DrawableCriteria(hsgmat, pass_index)
|
||||
|
||||
if crit not in self._dspans[location]:
|
||||
# AgeName_[District_]_Page_RenderLevel_Crit[Blend]Spans
|
||||
@ -250,8 +267,17 @@ class MeshConverter:
|
||||
node = self._mgr.get_scene_node(location)
|
||||
name = "{}_{:08X}_{:X}{}".format(node.name, crit.render_level.level, crit.criteria, crit.span_type)
|
||||
dspan = self._mgr.add_object(pl=plDrawableSpans, name=name, loc=location)
|
||||
|
||||
dspan.criteria = crit.criteria
|
||||
# TODO: props
|
||||
dspan.renderLevel = crit.render_level.level
|
||||
dspan.sceneNode = node # AddViaNotify
|
||||
|
||||
self._dspans[location][crit] = dspan
|
||||
return dspan
|
||||
else:
|
||||
return self._dspans[location][crit]
|
||||
|
||||
@property
|
||||
def _mgr(self):
|
||||
return self._exporter().mgr
|
||||
|
@ -15,6 +15,10 @@
|
||||
|
||||
from PyHSPlasma import *
|
||||
|
||||
def color(blcolor, alpha=1.0):
|
||||
"""Converts a Blender Color into an hsColorRGBA"""
|
||||
return hsColorRGBA(blcolor.r, blcolor.g, blcolor.b, alpha)
|
||||
|
||||
def matrix44(blmat):
|
||||
"""Converts a mathutils.Matrix to an hsMatrix44"""
|
||||
hsmat = hsMatrix44()
|
||||
|
@ -31,6 +31,12 @@ properties_material.MATERIAL_PT_options.COMPAT_ENGINES.add("PLASMA_GAME")
|
||||
properties_material.MATERIAL_PT_preview.COMPAT_ENGINES.add("PLASMA_GAME")
|
||||
del properties_material
|
||||
|
||||
from bl_ui import properties_texture
|
||||
for i in dir(properties_texture):
|
||||
attr = getattr(properties_texture, i)
|
||||
if hasattr(attr, "COMPAT_ENGINES"):
|
||||
getattr(attr, "COMPAT_ENGINES").add("PLASMA_GAME")
|
||||
del properties_texture
|
||||
|
||||
@classmethod
|
||||
def _new_poll(cls, context):
|
||||
|
Reference in New Issue
Block a user