Red channel of the rendered image is swapping with the blue channel

It is not entirely clear what goals you are pursuing. However, at the moment you are accessing the disk every frame to write a file, which slows down rendering accordingly, if it is possible to save all files to RAM. Then, using another thread, extract them to disk in parallel.

Also, for such simple tasks, there is no need to use the ShowBase. Something like this template is enough:

from panda3d.core import GraphicsEngine, GraphicsPipeSelection, FrameBufferProperties, WindowProperties, GraphicsPipe, GraphicsOutput
from panda3d.core import LColor, NodePath, Texture, Camera, Loader, Filename, PNMImage, PerspectiveLens

engine = GraphicsEngine.get_global_ptr()
pipe = GraphicsPipeSelection.get_global_ptr().make_module_pipe("pandagl")
loader = Loader.get_global_ptr()

fb_prop = FrameBufferProperties()
fb_prop.rgb_color = 1
fb_prop.color_bits = 24
fb_prop.depth_bits = 24

win_prop = WindowProperties()
win_prop.size = (960, 540)

render = NodePath("render")

lens = PerspectiveLens()
lens.set_fov(30)
lens.set_near_far(0.5, 100000)

cam = Camera("camera")
cam.set_lens(lens)
camera = NodePath(cam)
camera.set_pos(0, -30, 5)
camera.set_sx(win_prop.size[0]/win_prop.size[1])
camera.reparent_to(render)

texture = Texture("texture")

buffer = engine.make_output(pipe, name = "buffer", sort = 0, fb_prop = fb_prop, win_prop = win_prop, flags = GraphicsPipe.BFRefuseWindow)
buffer.set_clear_color_active(True)
buffer.set_clear_color(LColor(0, 0, 0, 0))
buffer.set_clear_depth_active(True)
buffer.set_clear_depth(1.0)
buffer.add_render_texture(texture, GraphicsOutput.RTM_copy_ram)

display_region = buffer.make_display_region()
display_region.camera = camera

model = NodePath(loader.load_sync("panda"))
model.reparent_to(render)

for i in range(2):
    engine.render_frame()

    frame = PNMImage()
    texture.store(frame)
    frame.write(Filename(f'rendered{i}.png'))
2 Likes