예제 #1
0
    def parse(self, frag: Shader, vert: Shader) -> str:
        if self.input_type == "uniform":
            frag.add_uniform(f'{self.variable_type} {self.variable_name}',
                             link=self.variable_name)
            vert.add_uniform(f'{self.variable_type} {self.variable_name}',
                             link=self.variable_name)

            if self.variable_type == "sampler2D":
                frag.add_uniform('vec2 screenSize', link='_screenSize')
                return f'texture({self.variable_name}, gl_FragCoord.xy / screenSize).rgb'

            return self.variable_name

        else:
            if self.input_source == "frag":
                frag.add_in(f'{self.variable_type} {self.variable_name}')
                return self.variable_name

            # Reroute input from vertex shader to fragment shader (input must exist!)
            else:
                vert.add_out(f'{self.variable_type} out_{self.variable_name}')
                frag.add_in(f'{self.variable_type} out_{self.variable_name}')

                vert.write(f'out_{self.variable_name} = {self.variable_name};')
                return 'out_' + self.variable_name
예제 #2
0
def finalize(frag: Shader, vert: Shader):
    """Checks the given fragment shader for completeness and adds
    variable initializations if required.

    TODO: Merge with make_finalize?
    """
    if frag.contains('pos') and not frag.contains('vec3 pos'):
        frag.write_attrib('vec3 pos = -n;')

    if frag.contains('vVec') and not frag.contains('vec3 vVec'):
        # For worlds, the camera seems to be always at origin in
        # Blender, so we can just use the normals as the incoming vector
        frag.write_attrib('vec3 vVec = n;')

    for var in ('bposition', 'mposition', 'wposition'):
        if (frag.contains(var)
                and not frag.contains(f'vec3 {var}')) or vert.contains(var):
            frag.add_in(f'vec3 {var}')
            vert.add_out(f'vec3 {var}')
            vert.write(f'{var} = pos;')

    if frag.contains('wtangent') and not frag.contains('vec3 wtangent'):
        frag.write_attrib('vec3 wtangent = vec3(0.0);')

    if frag.contains('texCoord') and not frag.contains('vec2 texCoord'):
        frag.add_in('vec2 texCoord')
        vert.add_out('vec2 texCoord')
        # World has no UV map
        vert.write('texCoord = vec2(1.0, 1.0);')
예제 #3
0
def parse_surface(world: bpy.types.World, node_surface: bpy.types.Node,
                  frag: Shader):
    wrd = bpy.data.worlds['Arm']
    rpdat = arm.utils.get_rp()
    solid_mat = rpdat.arm_material_model == 'Solid'

    if node_surface.type in ('BACKGROUND', 'EMISSION'):
        # Append irradiance define
        if rpdat.arm_irradiance and not solid_mat:
            wrd.world_defs += '_Irr'

        # Extract environment strength
        # Todo: follow/parse strength input
        world.arm_envtex_strength = node_surface.inputs[1].default_value

        # Color
        out = cycles.parse_vector_input(node_surface.inputs[0])
        frag.write(f'fragColor.rgb = {out};')

        if not node_surface.inputs[0].is_linked:
            solid_mat = rpdat.arm_material_model == 'Solid'
            if rpdat.arm_irradiance and not solid_mat:
                world.world_defs += '_Irr'
            world.arm_envtex_color = node_surface.inputs[0].default_value
            world.arm_envtex_strength = 1.0

    else:
        log.warn(
            f'World node type {node_surface.type} must not be connected to the world output node!'
        )

    # Invalidate the parser state for subsequent executions
    cycles.state = None
예제 #4
0
    def parse(self, frag: Shader, vert: Shader) -> str:
        if self.input_type == "uniform":
            frag.add_uniform(f'{self.variable_type} {self.variable_name}',
                             link=self.variable_name)
            return self.variable_name

        else:
            if self.input_source == "frag":
                frag.add_in(f'{self.variable_type} {self.variable_name}')
                return self.variable_name

            # Reroute input from vertex shader to fragment shader (input must exist!)
            else:
                vert.add_out(f'{self.variable_type} out_{self.variable_name}')
                frag.add_in(f'{self.variable_type} out_{self.variable_name}')

                vert.write(f'out_{self.variable_name} = {self.variable_name};')
                return 'out_' + self.variable_name
예제 #5
0
def build_node_tree(world: bpy.types.World, frag: Shader, vert: Shader,
                    con: ShaderContext):
    """Generates the shader code for the given world."""
    world_name = arm.utils.safestr(world.name)
    world.world_defs = ''
    rpdat = arm.utils.get_rp()
    wrd = bpy.data.worlds['Arm']

    if callback is not None:
        callback()

    # film_transparent, do not render
    if bpy.context.scene is not None and bpy.context.scene.render.film_transparent:
        world.world_defs += '_EnvCol'
        frag.add_uniform('vec3 backgroundCol', link='_backgroundCol')
        frag.write('fragColor.rgb = backgroundCol;')
        return

    parser_state = ParserState(ParserContext.WORLD, world)
    parser_state.con = con
    parser_state.curshader = frag
    parser_state.frag = frag
    parser_state.vert = vert
    cycles.state = parser_state

    # Traverse world node tree
    is_parsed = False
    if world.node_tree is not None:
        output_node = node_utils.get_node_by_type(world.node_tree,
                                                  'OUTPUT_WORLD')
        if output_node is not None:
            is_parsed = parse_world_output(world, output_node, frag)

    # No world nodes/no output node, use background color
    if not is_parsed:
        solid_mat = rpdat.arm_material_model == 'Solid'
        if rpdat.arm_irradiance and not solid_mat:
            world.world_defs += '_Irr'
        col = world.color
        world.arm_envtex_color = [col[0], col[1], col[2], 1.0]
        world.arm_envtex_strength = 1.0
        world.world_defs += '_EnvCol'

    # Clouds enabled
    if rpdat.arm_clouds and world.arm_use_clouds:
        world.world_defs += '_EnvClouds'
        # Also set this flag globally so that the required textures are
        # included
        wrd.world_defs += '_EnvClouds'
        frag_write_clouds(world, frag)

    if '_EnvSky' in world.world_defs or '_EnvTex' in world.world_defs or '_EnvImg' in world.world_defs or '_EnvClouds' in world.world_defs:
        frag.add_uniform('float envmapStrength', link='_envmapStrength')

    # Clear background color
    if '_EnvCol' in world.world_defs:
        frag.write('fragColor.rgb = backgroundCol;')

    elif '_EnvTex' in world.world_defs and '_EnvLDR' in world.world_defs:
        frag.write('fragColor.rgb = pow(fragColor.rgb, vec3(2.2));')

    if '_EnvClouds' in world.world_defs:
        frag.write(
            'if (pos.z > 0.0) fragColor.rgb = mix(fragColor.rgb, traceClouds(fragColor.rgb, pos), clamp(pos.z * 5.0, 0, 1));'
        )

    if '_EnvLDR' in world.world_defs:
        frag.write('fragColor.rgb = pow(fragColor.rgb, vec3(1.0 / 2.2));')

    # Mark as non-opaque
    frag.write('fragColor.a = 0.0;')

    finalize(frag, vert)
예제 #6
0
def build_node_tree(world: bpy.types.World, frag: Shader, vert: Shader,
                    con: ShaderContext):
    """Generates the shader code for the given world."""
    world_name = arm.utils.safestr(world.name)
    world.world_defs = ''
    rpdat = arm.utils.get_rp()
    wrd = bpy.data.worlds['Arm']

    if callback is not None:
        callback()

    # film_transparent, do not render
    if bpy.context.scene is not None and bpy.context.scene.render.film_transparent:
        world.world_defs += '_EnvCol'
        frag.add_uniform('vec3 backgroundCol', link='_backgroundCol')
        frag.write('fragColor.rgb = backgroundCol;')
        return

    parser_state = ParserState(ParserContext.WORLD, world)
    parser_state.con = con
    parser_state.curshader = frag
    parser_state.frag = frag
    parser_state.vert = vert
    cycles.state = parser_state

    # Traverse world node tree
    is_parsed = False
    if world.node_tree is not None:
        output_node = node_utils.get_node_by_type(world.node_tree,
                                                  'OUTPUT_WORLD')
        if output_node is not None:
            is_parsed = parse_world_output(world, output_node, frag)

    # No world nodes/no output node, use background color
    if not is_parsed:
        solid_mat = rpdat.arm_material_model == 'Solid'
        if rpdat.arm_irradiance and not solid_mat:
            world.world_defs += '_Irr'
        col = world.color
        world.arm_envtex_color = [col[0], col[1], col[2], 1.0]
        world.arm_envtex_strength = 1.0

    # Irradiance/Radiance: clear to color if no texture or sky is provided
    if rpdat.arm_irradiance or rpdat.arm_irradiance:
        if '_EnvSky' not in world.world_defs and '_EnvTex' not in world.world_defs and '_EnvImg' not in world.world_defs:
            # Irradiance json file name
            world.arm_envtex_name = world_name
            world.arm_envtex_irr_name = world_name
            write_probes.write_color_irradiance(world_name,
                                                world.arm_envtex_color)

    # Clouds enabled
    if rpdat.arm_clouds and world.arm_use_clouds:
        world.world_defs += '_EnvClouds'
        # Also set this flag globally so that the required textures are
        # included
        wrd.world_defs += '_EnvClouds'
        frag_write_clouds(world, frag)

    if '_EnvSky' in world.world_defs or '_EnvTex' in world.world_defs or '_EnvImg' in world.world_defs or '_EnvClouds' in world.world_defs:
        frag.add_uniform('float envmapStrength', link='_envmapStrength')

    # Clear background color
    if '_EnvCol' in world.world_defs:
        frag.write('fragColor.rgb = backgroundCol;')

    elif '_EnvTex' in world.world_defs and '_EnvLDR' in world.world_defs:
        frag.write('fragColor.rgb = pow(fragColor.rgb, vec3(2.2));')

    if '_EnvClouds' in world.world_defs:
        frag.write(
            'if (n.z > 0.0) fragColor.rgb = mix(fragColor.rgb, traceClouds(fragColor.rgb, n), clamp(n.z * 5.0, 0, 1));'
        )

    if '_EnvLDR' in world.world_defs:
        frag.write('fragColor.rgb = pow(fragColor.rgb, vec3(1.0 / 2.2));')

    # Mark as non-opaque
    frag.write('fragColor.a = 0.0;')

    # Hack to make procedural textures work
    frag_bpos = (
        frag.contains('bposition')
        and not frag.contains('vec3 bposition')) or vert.contains('bposition')
    if frag_bpos:
        frag.add_in('vec3 bposition')
        vert.add_out('vec3 bposition')
        # Use normals for now
        vert.write('bposition = nor;')

    frag_mpos = (
        frag.contains('mposition')
        and not frag.contains('vec3 mposition')) or vert.contains('mposition')
    if frag_mpos:
        frag.add_in('vec3 mposition')
        vert.add_out('vec3 mposition')
        # Use normals for now
        vert.write('mposition = nor;')

    if frag.contains('texCoord') and not frag.contains('vec2 texCoord'):
        frag.add_in('vec2 texCoord')
        vert.add_out('vec2 texCoord')
        # World has no UV map
        vert.write('texCoord = vec2(1.0, 1.0);')
예제 #7
0
    def parse(self, vertshdr:Shader,part_con) -> str:
                
        if(self.sclX or self.sclY or self.sclZ):
            scl = parse_vector_input(self.inputs[2])
        
        if(self.sclX):
            vertshdr.write(f'spos.x *= {scl}.x;')

        if(self.sclY):
            vertshdr.write(f'spos.y *= {scl}.y;')

        if(self.sclX):
            vertshdr.write(f'spos.z *= {scl}.z;')

        if(self.billBoard):
            vertshdr.add_uniform('mat4 WV', '_worldViewMatrix')
            vertshdr.write('spos = mat4(transpose(mat3(WV))) * spos;')
        
        if(self.rotX or self.rotY or self.rotZ):
            rot = parse_vector_input(self.inputs[1])

        if(self.rotX and not self.rotY and not self.rotZ):
            vertshdr.write(f'mat3 part_rot_mat = mat3(1.0, 0.0, 0.0,') 
            vertshdr.write(f'                       0.0, cos({rot}.x), sin({rot}.x),')
            vertshdr.write(f'                       0.0, -sin({rot}.x), cos({rot}.x));')

        if(not self.rotX and self.rotY and not self.rotZ):
            vertshdr.write(f'mat3 part_rot_mat = mat3(cos({rot}.y), 0.0, -sin({rot}.y),') 
            vertshdr.write(f'                       0.0, 1.0, 0.0,')
            vertshdr.write(f'                       sin({rot}.y), 0.0, cos({rot}.y));')

        if(not self.rotX and not self.rotY and self.rotZ):
            vertshdr.write(f'mat3 part_rot_mat = mat3(cos({rot}.z), sin({rot}.z), 0.0,') 
            vertshdr.write(f'                       -sin({rot}.z), cos({rot}.z), 0.0,')
            vertshdr.write(f'                       0.0, 0.0, 1.0);')

        if(self.rotX and self.rotY and not self.rotZ):
            vertshdr.write(f'mat3 part_rot_mat = mat3(cos({rot}.y), 0.0, -sin({rot}.y),') 
            vertshdr.write(f'                         sin({rot}.y) * sin({rot}.x), cos({rot}.x), cos({rot}.y) * sin({rot}.x),')
            vertshdr.write(f'                         sin({rot}.y) * cos({rot}.x), -sin({rot}.x), cos({rot}.y) * cos({rot}.x));')
        
        if(self.rotX and not self.rotY and self.rotZ):
            vertshdr.write(f'mat3 part_rot_mat = mat3(cos({rot}.z), sin({rot}.z), 0.0,') 
            vertshdr.write(f'                         -sin({rot}.z) * cos({rot}.x), cos({rot}.z) * cos({rot}.x), sin({rot}.x),')
            vertshdr.write(f'                         sin({rot}.z) * sin({rot}.x), -cos({rot}.z) * sin({rot}.x), cos({rot}.x));')
        
        if(not self.rotX and self.rotY and self.rotZ):
            vertshdr.write(f'mat3 part_rot_mat = mat3(cos({rot}.z) * cos({rot}.y), sin({rot}.z) * cos({rot}.y), -sin({rot}.y),') 
            vertshdr.write(f'                         -sin({rot}.z) , cos({rot}.z), 0.0,')
            vertshdr.write(f'                         cos({rot}.z) * sin({rot}.y), sin({rot}.z) * sin({rot}.y), cos({rot}.y));')
        
        if(self.rotX and self.rotY and self.rotZ):
            vertshdr.write(f'mat3 part_rot_mat = mat3(cos({rot}.z) * cos({rot}.y), sin({rot}.z) * cos({rot}.y), -sin({rot}.y),') 
            vertshdr.write(f'                         -sin({rot}.z) * cos({rot}.x) + cos({rot}.z) * sin({rot}.y) * sin({rot}.x), cos({rot}.z) * cos({rot}.x) + sin({rot}.z) * sin({rot}.y) * sin({rot}.x), cos({rot}.y) * sin({rot}.x),')
            vertshdr.write(f'                         sin({rot}.z) * sin({rot}.x) + cos({rot}.z) * sin({rot}.y) * cos({rot}.x), -cos({rot}.z) * sin({rot}.x) + sin({rot}.z) * sin({rot}.y) * cos({rot}.x), cos({rot}.y) * cos({rot}.x));')
        
        if(self.rotX or self.rotY or self.rotZ):
            vertshdr.write('spos.xyz = part_rot_mat * spos.xyz;')
            if((part_con.data['name'] == 'mesh' or 'translucent') and vertshdr.contains('wnormal')):
                vertshdr.write('wnormal = transpose(inverse(part_rot_mat)) * wnormal;')
        
        if(self.posX or self.posY or self.posZ):
            pos = parse_vector_input(self.inputs[0])
        
        if(self.posX):
            vertshdr.write(f'spos.x += {pos}.x;')

        if(self.posY):
            vertshdr.write(f'spos.y += {pos}.y;')

        if(self.posZ):
            vertshdr.write(f'spos.z += {pos}.z;')
                
        if(vertshdr.contains('vec3 disp')):
            vertshdr.write('wposition = vec4(W * spos).xyz;')
예제 #8
0
def frag_write_main(world: bpy.types.World, frag: Shader):
    if '_EnvSky' in world.world_defs or '_EnvTex' in world.world_defs or '_EnvClouds' in world.world_defs:
        frag.write('vec3 n = normalize(normal);')

    if '_EnvCol' in world.world_defs:
        frag.write('fragColor.rgb = backgroundCol;')
        if '_EnvTransp' in world.world_defs:
            frag.write('return;')

    # Static background image
    elif '_EnvImg' in world.world_defs:
        # Will have to get rid of gl_FragCoord, pass texture coords from
        # vertex shader
        frag.write('vec2 texco = gl_FragCoord.xy / screenSize;')
        frag.write(
            'fragColor.rgb = texture(envmap, vec2(texco.x, 1.0 - texco.y)).rgb * envmapStrength;'
        )

    # Environment texture
    # Also check for _EnvSky to prevent case when sky radiance is enabled
    elif '_EnvTex' in world.world_defs and '_EnvSky' not in world.world_defs:
        frag.write(
            'fragColor.rgb = texture(envmap, envMapEquirect(n)).rgb * envmapStrength;'
        )

        if '_EnvLDR' in world.world_defs:
            frag.write('fragColor.rgb = pow(fragColor.rgb, vec3(2.2));')

    if '_EnvSky' in world.world_defs:
        frag.write('float cos_theta = clamp(n.z, 0.0, 1.0);')
        frag.write('float cos_gamma = dot(n, hosekSunDirection);')
        frag.write('float gamma_val = acos(cos_gamma);')
        frag.write(
            'fragColor.rgb = Z * hosekWilkie(cos_theta, gamma_val, cos_gamma) * envmapStrength;'
        )

    if '_EnvClouds' in world.world_defs:
        frag.write(
            'if (n.z > 0.0) fragColor.rgb = mix(fragColor.rgb, traceClouds(fragColor.rgb, n), clamp(n.z * 5.0, 0, 1));'
        )

    if '_EnvLDR' in world.world_defs:
        frag.write('fragColor.rgb = pow(fragColor.rgb, vec3(1.0 / 2.2));')

    # Mark as non-opaque
    frag.write('fragColor.a = 0.0;')