+import itertools
+
class MaterialExporter:
def export_technique_resources(self, ctx, material, resources):
from .export_texture import SamplerExporter, TextureExporter
if type(material)!=Material:
material = Material(material)
- textured_props = [p for p in material.properties if p.texture]
+ textured_props = [p for p in itertools.chain(material.properties, *(s.properties for s in material.sub_materials)) if p.texture]
ctx.set_slices(len(textured_props)+1)
for p in textured_props:
else:
resources[mat_name] = None
- if material.cast_shadows and material.alpha_cutoff>0.0:
- for detail in ("", "_thsm"):
- shader_name = "occluder{}_masked.shader".format(detail)
+ for thsm in (False, True):
+ occluder_variant, occluder_spec_values = self.get_occluder_shader_variant(material, thsm)
+ if occluder_spec_values:
+ base_variant = "_thsm" if thsm else ""
+ shader_name = "occluder{}.shader".format(occluder_variant)
if shader_name not in resources:
- resources[shader_name] = self.export_shader(shader_name, "occluder{}.glsl".format(detail), {"use_alpha_cutoff":True}, resources)
+ resources[shader_name] = self.export_shader(shader_name, "occluder{}.glsl".format(base_variant), occluder_spec_values, resources)
def export_technique(self, material, resources):
from .material import Material
if blend_st:
st.sub.append(blend_st)
+ elif material.alpha_cutoff>0.0:
+ ss = Statement("blend")
+ ss.sub.append(Statement("alpha_to_coverage", True))
+ st.sub.append(ss)
if material.receive_shadows:
st.sub.append(Statement("receive_shadows", True))
if material.image_based_lighting:
st.sub.append(Statement("image_based_lighting", True))
+ if material.instancing:
+ st.sub.append(Statement("instancing", True))
if material.face_cull=='BACK':
st.sub.append(Statement("face_cull", Token("CULL_BACK")))
tech_res.statements.append(st)
if material.cast_shadows:
- tech_res.statements.append(self.create_shadow_method(tech_res, material, resources, ""));
- tech_res.statements.append(self.create_shadow_method(tech_res, material, resources, "_thsm"));
+ tech_res.statements.append(self.create_shadow_method(tech_res, material, resources, False));
+ tech_res.statements.append(self.create_shadow_method(tech_res, material, resources, True));
return tech_res
- def create_shadow_method(self, tech_res, material, resources, detail):
+ def create_shadow_method(self, tech_res, material, resources, thsm):
from .datafile import Statement, Token
- color_prop = next((p for p in material.properties if p.keyword and "color" in p.keyword), None)
+ occluder_variant, occluder_spec_values = self.get_occluder_shader_variant(material, thsm)
- st = Statement("method", "shadow"+detail)
- if material.alpha_cutoff>0.0 and color_prop and 'A' in color_prop.tex_channels:
- st.sub.append(tech_res.create_reference_statement("shader", resources["occluder{}_masked.shader".format(detail)]))
+ shader_stem = "occluder{}.shader" if occluder_spec_values else "occluder{}.glsl.shader"
+ shader_name = shader_stem.format(occluder_variant)
+
+ st = Statement("method", "shadow"+("_thsm" if thsm else ""))
+ if occluder_spec_values:
+ st.sub.append(tech_res.create_reference_statement("shader", resources[shader_name]))
+ else:
+ st.sub.append(Statement("shader", shader_name))
+
+ if occluder_spec_values.get("use_alpha_cutoff"):
ss = Statement("texture", "alpha_map")
from .export_texture import TextureExporter
texture_export = TextureExporter()
sampler_export = SamplerExporter()
+ color_prop = material.properties[0]
tex_res = resources[texture_export.get_texture_name(color_prop.texture, color_prop.tex_channels)]
ss.sub.append(tech_res.create_reference_statement("texture", tex_res))
ss.sub.append(tech_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(color_prop.texture)]))
ss = Statement("uniforms")
ss.sub.append(Statement("uniform", "alpha_cutoff", material.alpha_cutoff))
st.sub.append(ss)
- else:
- st.sub.append(Statement("shader", "occluder{}.glsl.shader".format(detail)))
if material.face_cull=='BACK':
st.sub.append(Statement("face_cull", Token("CULL_BACK")))
return st;
+ def get_occluder_shader_variant(self, material, thsm):
+ variant = ""
+ if thsm:
+ variant += "_thsm"
+
+ spec_values = {}
+
+ if material.cast_shadows and material.alpha_cutoff>0.0:
+ spec_values["use_alpha_cutoff"] = True
+ variant += "_masked"
+ if material.instancing:
+ spec_values["use_instancing"] = True
+ variant += "_instanced"
+
+ return (variant, spec_values)
+
def export_material(self, material, resources):
from .datafile import Resource, Statement, Token
mat_res = Resource(material.name+".mat", "material")
- if material.type!="pbr" and material.type!="unlit":
+ if material.type!="pbr" and material.type!="unlit" and material.type!="splat":
raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
mat_res.statements.append(Statement("type", Token(material.type)));
st = self.create_property_statement(mat_res, p, resources)
if st:
mat_res.statements.append(st)
- textures = [p.texture for p in material.properties if p.texture]
+
+ if material.sub_materials:
+ for k, s in material.array_storage.items():
+ mat_res.statements.append(Statement(k.replace("_map", "_storage"), Token(s[0]), s[1], s[2]))
+ for s in material.sub_materials:
+ st = Statement("sub")
+ for p in s.properties:
+ ss = self.create_property_statement(mat_res, p, resources, raw_texture=True)
+ if ss:
+ st.sub.append(ss)
+ mat_res.statements.append(st)
+
+ textures = [p.texture for p in itertools.chain(material.properties, *(s.properties for s in material.sub_materials)) if p.texture]
if textures:
from .export_texture import SamplerExporter
sampler_export = SamplerExporter()
return mat_res
- def create_property_statement(self, mat_res, prop, resources):
+ def create_property_statement(self, mat_res, prop, resources, *, raw_texture=False):
from .datafile import Statement
if prop.texture:
from .export_texture import TextureExporter
texture_export = TextureExporter()
tex_res = resources[texture_export.get_texture_name(prop.texture, prop.tex_channels)]
+ if raw_texture:
+ for s in tex_res.statements:
+ if s.keyword.startswith("external_data"):
+ return mat_res.create_reference_statement(prop.tex_keyword, s.args[0])
+ elif s.keyword.startswith("external_image"):
+ return Statement(prop.tex_keyword, s.args[0])
return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
elif not prop.keyword:
return