Skip to content

Commit dad25b2

Browse files
authored
Merge pull request #3 from kngwyu/partial-rendering
Partial rendering
2 parents 26f8c1f + c31a1a0 commit dad25b2

File tree

1 file changed

+57
-45
lines changed

1 file changed

+57
-45
lines changed

src/phyjax2d/moderngl_vis.py

+57-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
A simple, fast visualizer based on moderngl.
2+
A simple and fast visualizer based on moderngl.
33
Currently, only supports circles and lines.
44
"""
55

@@ -402,72 +402,76 @@ def __init__(
402402
vertex_shader=_CIRCLE_VERTEX_SHADER,
403403
fragment_shader=_CIRCLE_FRAGMENT_SHADER,
404404
)
405-
points, scales, colors = _collect_circles(
406-
space.shaped.circle,
407-
stated.circle,
408-
self._circle_scaling,
409-
)
410-
if len(points) > 0:
405+
if space.shaped.circle.is_empty():
406+
self._circles = None
407+
else:
408+
points, scales, colors = _collect_circles(
409+
space.shaped.circle,
410+
stated.circle,
411+
self._circle_scaling,
412+
)
411413
self._circles = CircleVA(
412414
ctx=context,
413415
program=circle_program,
414416
points=points,
415417
scales=scales,
416418
colors=colors,
417419
)
420+
if space.shaped.static_circle.is_empty():
421+
self._static_circles = None
418422
else:
419-
self._circles = None
420-
points, scales, _ = _collect_circles(
421-
space.shaped.static_circle,
422-
stated.static_circle,
423-
self._circle_scaling,
424-
)
425-
if len(points) > 0:
423+
points, scales, _ = _collect_circles(
424+
space.shaped.static_circle,
425+
stated.static_circle,
426+
self._circle_scaling,
427+
)
426428
self._static_circles = CircleVA(
427429
ctx=context,
428430
program=circle_program,
429431
points=points,
430432
scales=scales,
431433
colors=_get_sc_color(self._sc_color, stated.static_circle),
432434
)
435+
if space.shaped.segment.is_empty():
436+
self._static_lines = None
433437
else:
434-
self._static_circles = None
435-
static_segment_program = self._make_gl_program(
436-
vertex_shader=_LINE_VERTEX_SHADER,
437-
geometry_shader=_LINE_GEOMETRY_SHADER,
438-
fragment_shader=_LINE_FRAGMENT_SHADER,
439-
color=np.array([0.0, 0.0, 0.0, 0.4], dtype=np.float32),
440-
width=np.array([0.004], dtype=np.float32),
441-
w_rad=np.array([0.001], dtype=np.float32),
442-
l_rad=np.array([0.001], dtype=np.float32),
443-
)
444-
points = _collect_static_lines(space.shaped.segment, stated.segment)
445-
if len(points) > 0:
438+
static_segment_program = self._make_gl_program(
439+
vertex_shader=_LINE_VERTEX_SHADER,
440+
geometry_shader=_LINE_GEOMETRY_SHADER,
441+
fragment_shader=_LINE_FRAGMENT_SHADER,
442+
color=np.array([0.0, 0.0, 0.0, 0.4], dtype=np.float32),
443+
width=np.array([0.004], dtype=np.float32),
444+
w_rad=np.array([0.001], dtype=np.float32),
445+
l_rad=np.array([0.001], dtype=np.float32),
446+
)
447+
self._static_line_points = _collect_static_lines(
448+
space.shaped.segment,
449+
stated.segment,
450+
)
446451
self._static_lines = SegmentVA(
447452
ctx=context,
448453
program=static_segment_program,
449454
segments=points,
450455
)
456+
457+
if space.shaped.static_triangle.is_empty():
458+
self._triangles = None
451459
else:
452-
self._static_lines = None
453-
points, colors = _collect_triangles(
454-
space.shaped.static_triangle,
455-
stated.static_triangle,
456-
)
457-
triangle_program = self._make_gl_program(
458-
vertex_shader=_TRIANGLE_VERTEX_SHADER,
459-
geometry_shader=_TRIANGLE_GEOMETRY_SHADER,
460-
fragment_shader=_TRIANGLE_FRAGMENT_SHADER,
461-
)
462-
if len(points) > 0:
460+
points, colors = _collect_triangles(
461+
space.shaped.static_triangle,
462+
stated.static_triangle,
463+
)
464+
triangle_program = self._make_gl_program(
465+
vertex_shader=_TRIANGLE_VERTEX_SHADER,
466+
geometry_shader=_TRIANGLE_GEOMETRY_SHADER,
467+
fragment_shader=_TRIANGLE_FRAGMENT_SHADER,
468+
)
463469
self._triangles = TriangleVA(
464470
ctx=context,
465471
program=triangle_program,
466472
vertices=points,
467473
colors=colors,
468474
)
469-
else:
470-
self._triangles = None
471475

472476
if sensor_fn is not None:
473477
segment_program = self._make_gl_program(
@@ -568,15 +572,20 @@ def render(
568572
stated: StateDict,
569573
circle_colors: NDArray | None = None,
570574
sc_colors: NDArray | None = None,
575+
point_offset: NDArray | None = None,
571576
) -> None:
577+
if point_offset is None:
578+
po = np.array([[0.0, 0.0]], dtype=np.float32)
579+
else:
580+
po = point_offset.astype(np.float32).reshape(1, 2)
572581
circle_points, circle_scale, circle_colors_default = _collect_circles(
573582
self._space.shaped.circle,
574583
stated.circle,
575584
self._circle_scaling,
576585
)
577586
if self._circles is not None:
578587
circle_colors = self._get_colors(circle_colors_default, circle_colors)
579-
if self._circles.update(circle_points, circle_scale, circle_colors):
588+
if self._circles.update(circle_points + po, circle_scale, circle_colors):
580589
self._circles.render()
581590
if self._static_circles is not None:
582591
sc_points, sc_scale, _ = _collect_circles(
@@ -588,22 +597,25 @@ def render(
588597
_get_sc_color(self._sc_color, stated.static_circle),
589598
sc_colors,
590599
)
591-
if self._static_circles.update(sc_points, sc_scale, sc_colors):
600+
if self._static_circles.update(sc_points + po, sc_scale, sc_colors):
592601
self._static_circles.render()
593602
if self._triangles is not None:
594603
points, _ = _collect_triangles(
595604
self._space.shaped.static_triangle,
596605
stated.static_triangle,
597606
)
598-
if self._triangles.update(points):
607+
if self._triangles.update(points + po):
599608
self._triangles.render()
600609
if self._sensors is not None and self._collect_sensors is not None:
601-
if self._sensors.update(self._collect_sensors(stated)):
610+
if self._sensors.update(self._collect_sensors(stated) + po):
602611
self._sensors.render()
603-
if self._heads.update(_collect_heads(self._space.shaped.circle, stated.circle)):
612+
if self._heads.update(
613+
_collect_heads(self._space.shaped.circle, stated.circle) + po
614+
):
604615
self._heads.render()
605616
if self._static_lines is not None:
606-
self._static_lines.render()
617+
if self._static_lines.update(self._static_line_points + po):
618+
self._static_lines.render()
607619

608620

609621
class MglVisualizer:

0 commit comments

Comments
 (0)