From 3175817b63d591e7d5c2eb182c5ee91b8bcfada5 Mon Sep 17 00:00:00 2001 From: alcoholicgirl <2680813175@qq.com> Date: Mon, 27 Apr 2026 22:49:19 +0800 Subject: [PATCH] bool addition, few fixes --- konabot/plugins/marchtoy/gl_render.py | 6 ++++-- konabot/plugins/marchtoy/obj.py | 21 +++++++++++++++++++++ konabot/plugins/marchtoy/scene.py | 7 +++---- konabot/plugins/marchtoy/shaders/frag.glsl | 3 ++- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/konabot/plugins/marchtoy/gl_render.py b/konabot/plugins/marchtoy/gl_render.py index e54468f..784c9e6 100644 --- a/konabot/plugins/marchtoy/gl_render.py +++ b/konabot/plugins/marchtoy/gl_render.py @@ -14,8 +14,10 @@ with (PATH / "vert.glsl").open(encoding='utf-8') as f: async def render(command: str, res: tuple[int, int]): fs = Scene(command).compile() - - ctx = moderngl.create_context(standalone=True, backend="egl") + try: + ctx = moderngl.create_context(standalone=True) + except: + ctx = moderngl.create_context(standalone=True, backend="egl") ctx.gc_mode = "auto" try: program = ctx.program( diff --git a/konabot/plugins/marchtoy/obj.py b/konabot/plugins/marchtoy/obj.py index e70a279..019bb3a 100644 --- a/konabot/plugins/marchtoy/obj.py +++ b/konabot/plugins/marchtoy/obj.py @@ -244,6 +244,27 @@ class BoolSubstract(Object): def sdf_block_glsl(self): return f"max({self.block_a}, -{self.block_b})" +class BoolAddition(Object): + def __init__(self): + super().__init__() + + def parse_args(self, args: list[str]): + from konabot.plugins.marchtoy.scene import Scene + try: + if not len(args) >= 2: + raise Exception("expecting at least 2 args") + scene_a = Scene(args[0]) + scene_b = Scene(args[1]) + self.block_a = scene_a.canvas_objs[0][1] + self.block_b = scene_b.canvas_objs[0][1] + except Exception as e: + raise Exception(f"cannot build bool object over {args}: {e}") + + def sdf_block_glsl(self): + return f"min({self.block_a}, {self.block_b})" + + + @make_obj("camera", "cam") class Camera(Object): def __init__(self, _focus: float = 2.0) -> None: diff --git a/konabot/plugins/marchtoy/scene.py b/konabot/plugins/marchtoy/scene.py index 2162872..9a0029c 100644 --- a/konabot/plugins/marchtoy/scene.py +++ b/konabot/plugins/marchtoy/scene.py @@ -69,11 +69,10 @@ class Scene: index = 0 for canvas_item in self.canvas_objs: obj, sdf_expr = canvas_item - sdf_block += f"float sd{index} = {sdf_expr};" - sdf_block += f"if(sd{index} < qry.value)" - # 1e-8 最好换成某个 epsilon round_corner = f"- {obj.round_corner}" if obj.round_corner > 1e-8 else "" - sdf_block += "{" + f"qry.value = sd{index} {round_corner}; qry.obj_id = {index}; " + "}\n" + sdf_block += f"float sd{index} = {sdf_expr}{round_corner};" + sdf_block += f"if(sd{index} < qry.value)" + sdf_block += "{" + f"qry.value = sd{index}; qry.obj_id = {index}; " + "}\n" color = obj.texture.color color_block += ( f"if(obj_id == {index}) return vec4(" diff --git a/konabot/plugins/marchtoy/shaders/frag.glsl b/konabot/plugins/marchtoy/shaders/frag.glsl index 550d768..3aca0f7 100644 --- a/konabot/plugins/marchtoy/shaders/frag.glsl +++ b/konabot/plugins/marchtoy/shaders/frag.glsl @@ -1,6 +1,7 @@ #version 330 const float EPS = 0.001; const int MAX_ITER = 128; +const float INF = 1e10; uniform vec2 u_resolution; out vec4 fragColor; @@ -43,7 +44,7 @@ float smin( float a, float b, float k ) } sdQuery sd(vec3 p) { sdQuery qry; - qry.value = 100000000.0; + qry.value = INF; qry.obj_id = -1; return qry;