Files
AnyImage/docs/internals/operators/cutout-tool.md
nan 08309f719d feat: Implement cutout tool and depth generation features
- Added a new cutout tool for selecting regions in images, allowing users to choose mesh shapes based on selected areas.
- Introduced depth generation capabilities for rasterized images, including normal data extraction.
- Created jobs for generating depth planes and region data, enhancing the AI data generation workflow.
- Developed a selection refinement process for cutout regions, improving accuracy in generated outputs.
- Established a new region module for handling rasterized image regions and their associated AI generation tasks.
- Enhanced node graph builders for asset generation, facilitating smoother integration of new features.
2026-08-30 23:09:13 +08:00

4.2 KiB
Raw Blame History

Cutout Tool

Cutout 由两个核心 Operator 组成:

  • SelectCutoutRegion(select_region.py)把 Viewport 手势转换为图片空间 Region,并弹出 Shape 菜单。
  • CutoutRegionToShape(region_to_shape.py)准备所需数据,再构建并创建 Shape。

路径约定:Blender 侧文件相对 src/anyimage/operators/cutout_tool/,Server 侧文件相对 src/anyimage/server/。

执行路径

flowchart TD
    Select["圈定 Cutout Region<br/>选择 Shape"]
    Raster["栅格化 Region"]
    Route{"需要 AI 数据?"}
    Local["使用本地 Region 数据"]
    AI["生成 Refine / Depth / Normal"]
    Ready["汇总 Shape 输入"]
    Build["构建基础 Shape"]
    Create["创建 Blender 对象"]

    Select --> Raster
    Raster --> Route
    Route -- "否" --> Local --> Ready
    Route -- "是" --> AI --> Ready
    Ready --> Build --> Create

节点说明

圈定 Cutout Region

SelectCutoutRegion 将 Lasso 路径投射到图片像素坐标,形成 RegionPath,然后把 Region 和用户选择的 Shape 交给 CutoutRegionToShape。双击图片时,Region 使用完整图片范围。

RegionPath 定义在 operators/common/image_region.py,包含图片空间路径点、反选状态和填充规则。它既供 Cutout 使用,也供 Image Tool 使用。

栅格化 Region

CutoutRegionToShape 把 RegionPath 栅格化为 RegionMask。RegionMask 只包含局部像素权重 values 和它在源图中的 bounds,后续几何、颜色裁切和 AI 输入都以它为范围。

Surface 或 Balloon 在未启用 Refine 与 Normal 时直接使用本地 Region 数据;选择 Depth Shape,或启用 Refine、Normal 时进入 AI 路径。两条路径最终都调用 region_to_shape()。

生成 AI 数据

generate-region-data Job(jobs/generate_region_data.py)根据请求生成所需数据:

  • generate_selection()(region/generate_selection.py)使用 BEN2 收紧 Region;
  • generate_depth(..., normal_mode=...)(geometry/generate_depth.py)使用 MoGe-2 生成 Depth,并可从同一次 Prediction 派生 Tangent 或 Object Normal。

Blender 传入的已是带 Region Alpha 的局部 RGBA。Depth 与 Normal 同时启用时只运行一次 MoGe-2,颜色贴图仍使用 Region 的原始裁切分辨率。

构建基础 Shape

region_to_shape() 汇总 Region、Depth 和 Normal 输入,再依次执行几何构建与 Blender 数据创建。

build_base_shape() 从 RegionMask 构建纯内存 BaseShape:先由 region_contours.py 提取包含外轮廓与孔洞的 RegionContours,再由 triangulation.py 按 Point Spacing 采样并约束三角化,最后使用 balloon.py 计算 o_balloon。depth_calibration.py 负责把 AI Depth 标定为 Blender 中使用的深度基准与方向。四种 Shape 共用这张单面基础网格。

创建 Blender 对象

create_shape_object() 创建裁切后的 Color Image、Material、Mesh Object 和统一的 O Image Cutout Modifier。Shape 的差异由 Modifier 中对应的几何分支表达:

Shape 形成方式 Shape 自身所需 AI 数据
Surface 保持基础 Shape 的平面轮廓,并可增加真实厚度 无
Balloon 使用 o_balloon 从轮廓向内部平滑鼓起 无
Depth Balloon 在 Balloon 上叠加相对基准面的图片深度,并沿 Depth Direction 形成浮雕 MoGe-2 Depth
Depth Surface 使用完整相机点 XYZ 重建带透视位移的表面,并可增加厚度 MoGe-2 Camera Points

四种 Shape 共用 Region、基础三角面、UV、材质、Cleanup 和 Smooth。Cleanup 的 UI 数值放大 100 倍,节点内部乘以 0.01 还原,因此显示尺度不会改变实际清理阈值。

Depth 与 Normal

Depth Shape 根据最终 Region Alpha 选择基准:透明像素比例达到 5% 时从有效内容自适应计算 Base Plane Depth;不足 5% 时使用固定基准。Depth Balloon 还会在自适应路径中拟合 Depth Direction。

Normal 是材质层的浮雕细节,与四种 Shape 都可组合。None 使用几何法线;Tangent 以完整图片平面为切线基准;Object 直接编码到 Cutout 对象局部轴。Geometry Nodes 负责大轮廓和深度,Normal 负责表面细节。