From 08942dd73918a51baefbe7344b62afc0cad42e55 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 11 Oct 2021 14:55:02 +0300 Subject: [PATCH] Support creating fully custom techniques in Blender This replaces the custom shader, which was getting unwieldy with different kinds of shadow maps needing different shaders. --- blender/io_mspgl/export_material.py | 60 ++++++++++++++++------------- blender/io_mspgl/material.py | 31 ++++++++++----- blender/io_mspgl/operators.py | 27 ++++++++++++- blender/io_mspgl/properties.py | 43 +++++++++++++++++---- 4 files changed, 116 insertions(+), 45 deletions(-) diff --git a/blender/io_mspgl/export_material.py b/blender/io_mspgl/export_material.py index 19c22d1f..69782b64 100644 --- a/blender/io_mspgl/export_material.py +++ b/blender/io_mspgl/export_material.py @@ -7,40 +7,46 @@ def create_technique_resource(material, resources): mat_res = resources[material.name+".mat"] - st = Statement("method", "") - if mat_res: - st.sub.append(tech_res.create_embed_statement("material", mat_res)) - if material.render_mode=='CUSTOM': - shader = material.shader - if shader.endswith(".glsl"): - shader += ".shader" - st.sub.append(Statement("shader", shader)) - - if material.uniforms: - ss = Statement("uniforms") - for u in material.uniforms: - ss.sub.append(Statement("uniform", u.name, *u.values[:u.size])) - st.sub.append(ss) - else: - if material.receive_shadows: - st.sub.append(Statement("receive_shadows", True)) - if material.image_based_lighting: - st.sub.append(Statement("image_based_lighting", True)) + for m in material.render_methods: + st = Statement("method", m.tag) + if mat_res and m.use_material: + st.sub.append(tech_res.create_reference_statement("material", mat_res)) - tech_res.statements.append(st) - - if material.shadow_method!='NONE': - st = Statement("method", "shadow") - if material.render_mode=='CUSTOM': - shader = material.shadow_shader or material.shader + shader = m.shader if shader.endswith(".glsl"): shader += ".shader" st.sub.append(Statement("shader", shader)) - else: - st.sub.append(Statement("shader", "occluder.glsl.shader")) + + if material.uniforms: + ss = Statement("uniforms") + for u in material.uniforms: + ss.sub.append(Statement("uniform", u.name, *u.values[:u.size])) + st.sub.append(ss) + + tech_res.statements.append(st) + else: + st = Statement("method", "") + if mat_res: + st.sub.append(tech_res.create_embed_statement("material", mat_res)) + + if material.render_mode!='CUSTOM': + if material.receive_shadows: + st.sub.append(Statement("receive_shadows", True)) + if material.image_based_lighting: + st.sub.append(Statement("image_based_lighting", True)) + tech_res.statements.append(st) + if material.shadow_method!='NONE': + st = Statement("method", "shadow") + st.sub.append(Statement("shader", "occluder.glsl.shader")) + tech_res.statements.append(st) + + st = Statement("method", "shadow_thsm") + st.sub.append(Statement("shader", "occluder_thsm.glsl.shader")) + tech_res.statements.append(st) + return tech_res class MaterialExporter: diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py index 1d6a06a0..eb4c7c24 100644 --- a/blender/io_mspgl/material.py +++ b/blender/io_mspgl/material.py @@ -1,5 +1,13 @@ import os +def compute_render_method_hash(material): + descr = "" + for m in material.render_methods: + if descr: + descr += "," + descr += "{}={}".format(m.tag, m.shader) + return hash(descr) + def check_group(node_tree, group, func): from .util import get_linked_node_and_socket @@ -93,15 +101,15 @@ class Material: self.render_mode = material.render_mode self.technique = material.technique - self.shader = material.shader + self.render_methods = material.render_methods[:] self.receive_shadows = material.receive_shadows self.cast_shadows = (material.shadow_method!='NONE') self.image_based_lighting = material.image_based_lighting if self.render_mode=='EXTERNAL' and not self.technique: raise Exception("Invalid configuration on material {}: No technique for external rendering".format(self.name)) - elif self.render_mode=='CUSTOM' and not self.shader: - raise Exception("Invalid configuration on material {}: No shader for custom rendering".format(self.name)) + elif self.render_mode=='CUSTOM' and not self.render_methods: + raise Exception("Invalid configuration on material {}: No render methods for custom rendering".format(self.name)) out_node = next((n for n in material.node_tree.nodes if n.type=='OUTPUT_MATERIAL'), None) if not out_node: @@ -164,19 +172,24 @@ class MaterialAtlas: if self.render_mode=='EXTERNAL': raise Exception("Material atlas with external render mode does not make sense") - self.shader = materials[0].shader - if self.shader: - self.name = "material_atlas_"+os.path.splitext(self.shader)[0] + if self.render_mode=='CUSTOM': + self.render_methods = materials[0].render_methods + else: + self.render_methods = None + if self.render_methods: + self.name = "material_atlas_"+os.path.splitext(self.render_methods[0].shader)[0] else: self.name = "material_atlas" self.receive_shadows = materials[0].receive_shadows self.cast_shadows = (materials[0].shadow_method!='NONE') self.materials = materials self.material_names = [m.name for m in self.materials] + self.uniforms = None + method_hash = compute_render_method_hash(self) for m in self.materials: if m.render_mode!=self.render_mode: raise Exception("Conflicting render modes in MaterialAtlas constructor") - if self.render_mode=='CUSTOM' and m.shader!=self.shader: + if self.render_mode=='CUSTOM' and compute_render_method_hash(m)!=method_hash: raise Exception("Conflicting shaders in MaterialAtlas constructor") if m.receive_shadows!=self.receive_shadows or m.shadow_method!=materials[0].shadow_method: raise Exception("Conflicting shadow settings in MaterialAtlas constructor") @@ -211,10 +224,10 @@ def create_material_atlas(context, material): if not material.material_atlas: raise Exception("Material is not part of a material atlas") - shader = material.shader + method_hash = compute_render_method_hash(material) materials = [] for m in context.blend_data.materials: - if m.material_atlas and m.shader==shader: + if m.material_atlas and compute_render_method_hash(m)==method_hash: materials.append(m) return MaterialAtlas(materials) diff --git a/blender/io_mspgl/operators.py b/blender/io_mspgl/operators.py index 8d214eac..a6fc5923 100644 --- a/blender/io_mspgl/operators.py +++ b/blender/io_mspgl/operators.py @@ -138,6 +138,30 @@ class ExportMspGLProject(bpy.types.Operator): exporter.export_to_directory(context, self.directory) return {'FINISHED'} +class AddRenderMethod(bpy.types.Operator): + bl_idname = "material.add_render_method" + bl_label = "Add Render Method" + bl_description = "Add a new render method to the material" + + def execute(self, context): + mat = context.active_object.active_material + mat.render_methods.add() + mat.active_render_method_index = len(mat.uniforms)-1 + + return {"FINISHED"} + +class RemoveRenderMethod(bpy.types.Operator): + bl_idname = "material.remove_render_method" + bl_label = "Remove Render Method" + bl_description = "Remove the selected render method from the material" + + def execute(self, context): + mat = context.active_object.active_material + mat.render_methods.remove(mat.active_render_method_index) + mat.active_render_method_index = min(mat.active_render_method_index, len(mat.render_methods)-1) + + return {"FINISHED"} + class AddUniform(bpy.types.Operator): bl_idname = "material.add_uniform" bl_label = "Add Uniform" @@ -162,7 +186,8 @@ class RemoveUniform(bpy.types.Operator): return {"FINISHED"} -classes = [ExportMspGLData, ExportMspGLAnimation, ExportMspGLScene, ExportMspGLProject, AddUniform, RemoveUniform] +classes = [ExportMspGLData, ExportMspGLAnimation, ExportMspGLScene, ExportMspGLProject, AddRenderMethod, + RemoveRenderMethod, AddUniform, RemoveUniform] def register_operators(): for c in classes: diff --git a/blender/io_mspgl/properties.py b/blender/io_mspgl/properties.py index 128b3c53..6d847012 100644 --- a/blender/io_mspgl/properties.py +++ b/blender/io_mspgl/properties.py @@ -84,9 +84,19 @@ class MspGLMaterialProperties(bpy.types.Panel): self.layout.prop(mat, "render_mode") if mat.render_mode=='CUSTOM': - self.layout.prop(mat, "shader") - if mat.shadow_method!='NONE': - self.layout.prop(mat, "shadow_shader") + self.layout.label(text="Render methods") + self.layout.template_list("MATERIAL_UL_mspgl_render_methods", "", mat, "render_methods", mat, "active_render_method_index") + row = self.layout.row() + row.operator("material.add_render_method") + row.operator("material.remove_render_method") + + if mat.active_render_method_index