Exemplo n.º 1
0
def generate(parent=None, texture=None, foreground=False):
    bgnp = p3d.NodePath(p3d.CardMaker('bgimg').generate())
    bgnp.set_shader(p3d.Shader.make(p3d.Shader.SL_GLSL, _BG_VERT, _BG_FRAG))
    bgnp.set_shader_input('exposure_inv', 1.0 / base.render_pipeline.exposure)
    bgnp.set_bin('fixed' if foreground else 'background', 0)
    bgnp.set_depth_test(False)
    bgnp.set_depth_write(False)
    bgnp.node().set_bounds(p3d.OmniBoundingVolume())
    bgnp.node().set_final(True)

    if parent is not None:
        bgnp.reparent_to(parent)

    if texture is not None:
        suffix = 'fg' if foreground else 'bg'
        tex = base.loader.load_texture(f'backgrounds/{texture}{suffix}.png')
        if tex.num_components == 4:
            bgnp.set_transparency(p3d.TransparencyAttrib.M_alpha)
            tex.set_format(p3d.Texture.F_srgb_alpha)
        else:
            tex.set_format(p3d.Texture.F_srgb)
    else:
        tex = p3d.Texture()
    bgnp.set_shader_input('tex', tex)

    return bgnp
Exemplo n.º 2
0
    def __init__(self, room):
        self.root = core.NodePath('Room')
        self.room_model = room
        self.room_model.reparent_to(self.root)
        self.name = room.name

        self.doors = [i for i in self.room_model.find_all_matches('**/door*')]
        self.portals = [Portal(self, i) for i in self.doors]

        for i in self.doors:
            i.set_transparency(core.TransparencyAttrib.M_alpha)
            i.set_alpha_scale(0.0)
            i.set_color(core.Vec4(0))
            for np in i.find_all_matches('**/+GeomNode'):
                geom_node = np.node()
                process_geom_node(geom_node)

        portal_vert_str = load_shader_str('portal.vert')
        portal_frag_str = load_shader_str('portal_screenspace.frag')
        portalshader = core.Shader.make(
            core.Shader.SL_GLSL,
            vertex=portal_vert_str,
            fragment=portal_frag_str,
        )
        for i in self.doors:
            i.set_shader(portalshader)
        self._active = False
        self.room_model.node().set_bounds(core.OmniBoundingVolume())
        self.room_model.node().set_final(True)

        for i in self.room_model.find_all_matches('**/Sun*/Sun*'):
            try:
                self.root.set_light(i)
            except AssertionError:
                pass
Exemplo n.º 3
0
    def _make_fullscreen_quad(self):
        tris = p3d.GeomTristrips(p3d.GeomEnums.UH_static)
        tris.add_next_vertices(4)
        vdata = p3d.GeomVertexData('abc', p3d.GeomVertexFormat.get_empty(),
                                   p3d.GeomEnums.UH_static)

        geom = p3d.Geom(vdata)
        geom.add_primitive(tris)
        geom.set_bounds(p3d.OmniBoundingVolume())

        node = p3d.GeomNode(f'{self.name}_fullscreen_quad')
        node.add_geom(geom)

        return p3d.NodePath(node)
Exemplo n.º 4
0
def render_depth_pixel(region, distance, near, far, clear=None, write=True, state=None):
    """Renders a fragment at the specified distance using the specified render
    settings, and returns the resulting depth value."""

    # Set up the scene with a blank card rendering at specified distance.
    scene = core.NodePath("root")
    scene.set_attrib(core.DepthTestAttrib.make(core.RenderAttrib.M_always))
    scene.set_depth_write(write)

    if state:
        scene.set_state(scene.get_state().compose(state))

    camera = scene.attach_new_node(core.Camera("camera"))
    camera.node().get_lens(0).set_near_far(near, far)
    camera.node().set_cull_bounds(core.OmniBoundingVolume())

    if distance is not None:
        cm = core.CardMaker("card")
        cm.set_frame(-1, 1, -1, 1)
        card = scene.attach_new_node(cm.generate())
        card.set_pos(0, distance, 0)
        card.set_scale(60)

    region.active = True
    region.camera = camera

    if clear is not None:
        region.set_clear_depth_active(True)
        region.set_clear_depth(clear)

    depth_texture = core.Texture("depth")
    region.window.add_render_texture(depth_texture,
                                     core.GraphicsOutput.RTM_copy_ram,
                                     core.GraphicsOutput.RTP_depth)

    region.window.engine.render_frame()
    region.window.clear_render_textures()

    col = core.LColor()
    depth_texture.peek().lookup(col, 0.5, 0.5)
    return col[0]
Exemplo n.º 5
0
        def cb_update_draw_calls(cbdata):
            for np in self.temp_nps:
                np.remove_node()
            self.temp_nps = []

            tex = self.draw_manager._indirect_buffer
            gsg = self.win.get_gsg()
            if self.graphics_engine.extract_texture_data(tex, gsg):
                tex_view = memoryview(tex.get_ram_image()).cast('i')
                for call_idx in range(tex.get_x_size()):
                    i = call_idx * 4
                    primcount = tex_view[i + 1]
                    first = tex_view[i + 2]

                    prims = p3d.GeomPoints(p3d.GeomEnums.UH_dynamic)
                    prims.add_next_vertices(primcount)
                    geom = p3d.Geom(vdata)
                    geom.add_primitive(prims)
                    geom.set_bounds(p3d.OmniBoundingVolume())
                    node = p3d.GeomNode('Draw Call {}'.format(call_idx))
                    node.add_geom(geom)
                    path = p3d.NodePath(node)
                    path.set_bin('fixed', 50)
                    path.set_depth_test(False)
                    path.set_depth_write(False)
                    path.set_shader(resolve_shader)
                    path.set_shader_input('first', first)
                    window_size = p3d.LVector2i(win_width, win_height)
                    path.set_shader_input('window_size', window_size)
                    path.set_shader_input('vertex_cache',
                                          self.mesh_cache.vert_texture)
                    path.set_shader_input('index_cache',
                                          self.mesh_cache.index_texture)
                    path.set_shader_input('intersections', self.int_texture)
                    self.material_cache.bind(path)
                    path.reparent_to(self.render)
                    self.temp_nps.append(path)
            cbdata.upcall()
Exemplo n.º 6
0
def render_color_pixel(region, state, vertex_color=None):
    """Renders a fragment using the specified render settings, and returns the
    resulting color value."""

    # Skip auto-shader tests if we don't support Cg shaders.
    if not region.window.gsg.supports_basic_shaders:
        sattr = state.get_attrib(core.ShaderAttrib)
        if sattr and sattr.auto_shader():
            pytest.skip("Cannot test auto-shader without Cg shader support")

    # Set up the scene with a blank card rendering at specified distance.
    scene = core.NodePath("root")
    scene.set_attrib(core.DepthTestAttrib.make(core.RenderAttrib.M_always))

    camera = scene.attach_new_node(core.Camera("camera"))
    camera.node().get_lens(0).set_near_far(1, 3)
    camera.node().set_cull_bounds(core.OmniBoundingVolume())

    if vertex_color is not None:
        format = core.GeomVertexFormat.get_v3cp()
    else:
        format = core.GeomVertexFormat.get_v3()

    vdata = core.GeomVertexData("card", format, core.Geom.UH_static)
    vdata.unclean_set_num_rows(4)

    vertex = core.GeomVertexWriter(vdata, "vertex")
    vertex.set_data3(core.Vec3.rfu(-1, 0, 1))
    vertex.set_data3(core.Vec3.rfu(-1, 0, -1))
    vertex.set_data3(core.Vec3.rfu(1, 0, 1))
    vertex.set_data3(core.Vec3.rfu(1, 0, -1))

    if vertex_color is not None:
        color = core.GeomVertexWriter(vdata, "color")
        color.set_data4(vertex_color)
        color.set_data4(vertex_color)
        color.set_data4(vertex_color)
        color.set_data4(vertex_color)

    strip = core.GeomTristrips(core.Geom.UH_static)
    strip.set_shade_model(core.Geom.SM_uniform)
    strip.add_next_vertices(4)
    strip.close_primitive()

    geom = core.Geom(vdata)
    geom.add_primitive(strip)

    gnode = core.GeomNode("card")
    gnode.add_geom(geom, state)
    card = scene.attach_new_node(gnode)
    card.set_pos(0, 2, 0)
    card.set_scale(60)

    region.active = True
    region.camera = camera

    color_texture = core.Texture("color")
    region.window.add_render_texture(color_texture,
                                     core.GraphicsOutput.RTM_copy_ram,
                                     core.GraphicsOutput.RTP_color)

    region.window.engine.render_frame()
    region.window.clear_render_textures()

    col = core.LColor()
    color_texture.peek().lookup(col, 0.5, 0.5)
    return col