robolib.systems.fpv_render_system

fpv_render_system.py - System operating on FPV entities (e.g. uavs, controllable robots etc.)

 1"""fpv_render_system.py - System operating on FPV entities (e.g. uavs, controllable robots etc.)"""
 2from microecs import World, EntityId
 3import raylib as rl
 4
 5from robolib.camera import Camera
 6from robolib.components import HasPose, HasModel
 7from robolib.constants import DEFAULT_BACKGROUND
 8from robolib.traits import Drawable
 9from robolib.utils import FPVData
10
11class FPVRenderSystem:
12    """FPVRenderSystem - renders all the connected eids to custom textures that are streamed to the network clients"""
13    def __call__(self, world: World, connected_eids: list[EntityId], extra_drawables: list[Drawable] | None = None):
14        for eid in connected_eids:
15            entity = world.get_entity(eid)
16            fpv_texture: "rl.RenderTexture" = entity.fpv_texture.item()
17            fpv_data: FPVData = entity.fpv_data.item()
18            camera: Camera = entity.fpv_camera.item()
19
20            rl.BeginTextureMode(fpv_texture)
21            rl.ClearBackground(DEFAULT_BACKGROUND)
22            rl.BeginMode3D(camera.camera[0])
23
24            qr = world.query(HasModel, HasPose)
25            for i, (model, pose, scale) in enumerate(zip(qr.model, qr.pose, qr.scale)):
26                if qr.entity_ids[i] == eid: # don't draw yourself in FPV
27                    continue
28                rl.DrawModel(model.item().model, pose[0:3, 3].tolist(), scale.item(), rl.WHITE)
29
30            for drawable in (extra_drawables or []): # e.g. plugins
31                drawable.draw(world, entity_id=eid)
32
33            rl.EndMode3D()
34            rl.EndTextureMode()
35
36            fpv_img = rl.LoadImageFromTexture(fpv_texture.texture)
37            fpv_img_ptr = rl.ffi.new("Image *", fpv_img)
38            rl.ImageFlipVertical(fpv_img_ptr)
39            with fpv_data.lock:
40                fpv_data.frame = bytes(rl.ffi.buffer(fpv_img_ptr.data, len(fpv_data.frame)))
41                fpv_data.frame_id += 1
42                fpv_data._frame_compressed = None # pylint: disable=protected-access
43            rl.UnloadImage(fpv_img_ptr[0])
class FPVRenderSystem:
12class FPVRenderSystem:
13    """FPVRenderSystem - renders all the connected eids to custom textures that are streamed to the network clients"""
14    def __call__(self, world: World, connected_eids: list[EntityId], extra_drawables: list[Drawable] | None = None):
15        for eid in connected_eids:
16            entity = world.get_entity(eid)
17            fpv_texture: "rl.RenderTexture" = entity.fpv_texture.item()
18            fpv_data: FPVData = entity.fpv_data.item()
19            camera: Camera = entity.fpv_camera.item()
20
21            rl.BeginTextureMode(fpv_texture)
22            rl.ClearBackground(DEFAULT_BACKGROUND)
23            rl.BeginMode3D(camera.camera[0])
24
25            qr = world.query(HasModel, HasPose)
26            for i, (model, pose, scale) in enumerate(zip(qr.model, qr.pose, qr.scale)):
27                if qr.entity_ids[i] == eid: # don't draw yourself in FPV
28                    continue
29                rl.DrawModel(model.item().model, pose[0:3, 3].tolist(), scale.item(), rl.WHITE)
30
31            for drawable in (extra_drawables or []): # e.g. plugins
32                drawable.draw(world, entity_id=eid)
33
34            rl.EndMode3D()
35            rl.EndTextureMode()
36
37            fpv_img = rl.LoadImageFromTexture(fpv_texture.texture)
38            fpv_img_ptr = rl.ffi.new("Image *", fpv_img)
39            rl.ImageFlipVertical(fpv_img_ptr)
40            with fpv_data.lock:
41                fpv_data.frame = bytes(rl.ffi.buffer(fpv_img_ptr.data, len(fpv_data.frame)))
42                fpv_data.frame_id += 1
43                fpv_data._frame_compressed = None # pylint: disable=protected-access
44            rl.UnloadImage(fpv_img_ptr[0])

FPVRenderSystem - renders all the connected eids to custom textures that are streamed to the network clients