You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenSCAD中如何合并循环生成的四面体?Union函数未生效

问题

我编写了如下OpenSCAD模块用于生成四面体:

module draw_tetrahedra(tetrahedron_table)
{
    for (i = [0:len(tetrahedron_table) - 1])
    {
        indices = tetrahedron_table[i];

        echo("**RESULT tetrahedron ", i, " indices: ", indices);

        p0 = edges_and_corners[indices[0]];
        p1 = edges_and_corners[indices[1]];
        p2 = edges_and_corners[indices[2]];
        p3 = edges_and_corners[indices[3]];

        color([ 1, 0, 0, 0.5 ])
            polyhedron(points = [ p0, p1, p2, p3 ], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
    }
}

调用方式如下:

tetrahedron_table = [
    [ 1, 15, 3, 19 ],
    [ 1, 14, 15, 19 ],
    [ 1, 14, 19, 18 ],
    [ 1, 18, 19, 17 ],
    [ 1, 3, 9, 17 ],
    [ 17, 19, 3, 16 ],
    [ 9, 3, 8, 17 ],
    [ 8, 17, 3, 16 ],
];

draw_tetrahedra(tetrahedron_table);

生成的四面体效果如图所示。我需要将循环生成的所有四面体进行Union(布尔合并),但尝试将整个循环包裹在union()函数中并未生效,修改后的代码如下:

module draw_tetrahedra(tetrahedron_table)
{
    union()
    {
        for (i = [0:len(tetrahedron_table) - 1])
        {
            indices = tetrahedron_table[i];

            echo("**RESULT tetrahedron ", i, " indices: ", indices);

            p0 = edges_and_corners[indices[0]];
            p1 = edges_and_corners[indices[1]];
            p2 = edges_and_corners[indices[2]];
            p3 = edges_and_corners[indices[3]];

            color([ 1, 0, 0, 0.5 ])
                polyhedron(points = [ p0, p1, p2, p3 ], faces = [ [ 0, 1, 2 ], [ 0, 2, 3 ], [ 0, 3, 1 ], [ 1, 2, 3 ] ]);
        }
    }
}

请问如何实现所有四面体的Union合并?

解决方案

问题根源是**color()指令的层级错误**:color()会将内部的几何体包装成独立分组节点,导致union()无法穿透分组去合并底层的多面体。以下是两种可行的解决方式:

方式一:先合并几何体,再整体上色

如果所有四面体不需要单独上色,直接把color()移到union()外层,让union()优先合并纯几何体,再给整体上色:

module draw_tetrahedra(tetrahedron_table)
{
    color([1, 0, 0, 0.5])
    union()
    {
        for (i = [0:len(tetrahedron_table) - 1])
        {
            indices = tetrahedron_table[i];
            echo("**RESULT tetrahedron ", i, " indices: ", indices);

            p0 = edges_and_corners[indices[0]];
            p1 = edges_and_corners[indices[1]];
            p2 = edges_and_corners[indices[2]];
            p3 = edges_and_corners[indices[3]];

            polyhedron(points = [p0, p1, p2, p3], faces = [[0,1,2], [0,2,3], [0,3,1], [1,2,3]]);
        }
    }
}

方式二:保留个体上色,同时合并几何体

如果需要每个四面体保留独立颜色,可将color()作为polyhedron()的修饰器,确保union()的直接子节点是几何体而非color()分组:

module draw_tetrahedra(tetrahedron_table)
{
    union()
    {
        for (i = [0:len(tetrahedron_table) - 1])
        {
            indices = tetrahedron_table[i];
            echo("**RESULT tetrahedron ", i, " indices: ", indices);

            p0 = edges_and_corners[indices[0]];
            p1 = edges_and_corners[indices[1]];
            p2 = edges_and_corners[indices[2]];
            p3 = edges_and_corners[indices[3]];

            polyhedron(points = [p0, p1, p2, p3], faces = [[0,1,2], [0,2,3], [0,3,1], [1,2,3]])
            color([1, 0, 0, 0.5]);
        }
    }
}

这种写法会保留各四面体的颜色,同时完成几何体合并,但如果后续需要对合并后的实体进行其他布尔操作,优先推荐方式一,避免颜色分组干扰。

内容的提问来源于stack exchange,提问作者Megidd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 03:09:58