Пример #1
0
    def __init__(self):
        self.parameters = PipelineParameters()
        self.parameters.mesh['double_sided'] = Parameter(False, Type.BOOL)
        self.parameters.mesh['precomputed_tangents'] = Parameter(
            False, Type.BOOL)

        shader_dir = path.join(path.dirname(__file__), 'Shaders')
        if shader_dir not in Pipeline.SHADER_INCLUDE_PATHS:
            Pipeline.SHADER_INCLUDE_PATHS.append(shader_dir)

        self.resolution = None
        self.sample_count = 0

        self.result = None

        positions = [
            1.0,
            1.0,
            0.0,
            1.0,
            -1.0,
            0.0,
            -1.0,
            -1.0,
            0.0,
            -1.0,
            1.0,
            0.0,
        ]
        indices = [
            0,
            1,
            3,
            1,
            2,
            3,
        ]

        self.quad = Mesh(positions, indices)

        if Pipeline.BLEND_SHADER is None:
            source = '''#include "Passes/BlendTexture.glsl"'''
            Pipeline.BLEND_SHADER = self.compile_shader_from_source(source)
        self.blend_shader = Pipeline.BLEND_SHADER

        if Pipeline.COPY_SHADER is None:
            source = '''#include "Passes/CopyTextures.glsl"'''
            Pipeline.COPY_SHADER = self.compile_shader_from_source(source)
        self.copy_shader = Pipeline.COPY_SHADER

        self.default_shader = None
Пример #2
0
 def add_override(self, property_name, override_name):
     main_prop = self.get_rna()[property_name]
     new_name = property_name + ' @ ' + override_name
     property = {}
     if main_prop['type'] == Type.MATERIAL:
         property[new_name] =  MaterialParameter(main_prop['default'], self.materials[property_name].extension)
     else:
         property[new_name] = Parameter(main_prop['default'], main_prop['type'], main_prop['size'])
     self.setup(property, replace_parameters= False)
Пример #3
0
    def setup(self, parameters, replace_parameters=True):
        rna = self.get_rna()
        
        def setup_parameter(name, parameter):
            if name not in rna.keys():
                rna[name] = {}

            type_changed = 'type' not in rna[name].keys() or rna[name]['type'] != parameter.type
            size_changed = 'size' in rna[name].keys() and rna[name]['size'] != parameter.size

            def to_basic_type(value):
                try: return tuple(value)
                except: return value
            def equals(a, b):
                return to_basic_type(a) == to_basic_type(b)

            def resize():
                if parameter.size == 1:
                       self[name] = self[name][0]
                else:
                    if rna[name]['size'] > parameter.size:
                        self[name] = self[name][:parameter.size]
                    else:
                        first = self[name]
                        try: first = list(first)
                        except: first = [first]
                        second = list(parameter.default_value)
                        self[name] = first + second[rna[name]['size']:]

            if parameter.type in (Type.INT, Type.FLOAT):
                if type_changed or equals(rna[name]['default'], self[name]):
                    self[name] = parameter.default_value   
                elif size_changed:
                    resize()

            if parameter.type == Type.BOOL:
                if name not in self.bools:
                    self.bools.add().name = name
                if type_changed or equals(rna[name]['default'], self.bools[name].boolean):
                    self.bools[name].boolean = parameter.default_value
                elif size_changed:
                    resize()
            
            if parameter.type == Type.TEXTURE:
                if name not in self.textures:
                    self.textures.add().name = name

            if parameter.type == Type.GRADIENT:
                get_color_ramp(self.id_data, name)

            if parameter.type == Type.MATERIAL:
                if name not in self.materials:
                    self.materials.add().name = name
                
                self.materials[name].extension = parameter.extension
                shader_path = parameter.default_value
                if shader_path and shader_path != '':
                    if shader_path not in bpy.data.materials:
                        bpy.data.materials.new(shader_path)
                        material = bpy.data.materials[shader_path]
                        material.malt.shader_source = shader_path    
                        material.malt.update_source(bpy.context)
                
                    material = self.materials[name].material
                    if type_changed or (material and rna[name]['default'] == material.malt.shader_source):
                        self.materials[name].material = bpy.data.materials[shader_path]

            rna[name]['active'] = True
            rna[name]["default"] = parameter.default_value
            rna[name]['type'] = parameter.type
            rna[name]['size'] = parameter.size
            rna[name]['filter'] = parameter.filter
        
        #TODO: We should purge non active properties (specially textures)
        # at some point, likely on file save or load 
        # so we don't lose them immediately when changing shaders/pipelines
        if replace_parameters:
            for key, value in rna.items():
                if '@' not in key:
                    rna[key]['active'] = False
        
        for name, parameter in parameters.items():
            if name.isupper() or name.startswith('_'):
                # We treat underscored and all caps uniforms as "private"
                continue
            setup_parameter(name, parameter)

        for key, value in rna.items():
            if '@' in key and key not in parameters.keys():
                main_name = key.split(' @ ')[0]
                rna[key]['active'] = rna[main_name]['active'] and rna[key]['active']
                if rna[key]['active']:
                    parameter = Parameter(rna[main_name]['default'], rna[main_name]['type'], rna[main_name]['size'])
                    setup_parameter(key, parameter)
        
        for key, value in rna.items():
            #TODO: for now we assume we want floats as colors
            # ideally it should be opt-in in the UI,
            # so we can give them propper min/max values
            if rna[key]['type'] == Type.FLOAT and rna[key]['size'] >= 3:
                rna[key]['subtype'] = 'COLOR'
                rna[key]['use_soft_limits'] = True
                rna[key]['soft_min'] = 0.0
                rna[key]['soft_max'] = 1.0
            else:
                rna[key]['subtype'] = 'NONE'
                rna[key]['use_soft_limits'] = False

        # Force a depsgraph update. 
        # Otherwise these won't be available inside scene_eval
        self.id_data.update_tag()
        for screen in bpy.data.screens:
            for area in screen.areas:
                area.tag_redraw()