コード例 #1
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)
コード例 #2
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);')