feat(selection): separate selection geometry from image content processing

- Introduced a new design for selection rasterization that decouples the selection geometry from the image content, ensuring that the SelectionMask is independent of the source image's alpha and color.
- Updated the `rasterize_selection_path` function to only accept selection paths and image dimensions, removing parameters related to source alpha and alpha thresholds.
- Implemented a new approach for handling selection paths using non-zero winding fill, ensuring overlapping areas are preserved.
- Enhanced performance by utilizing a scanline algorithm for rasterization, significantly reducing processing time for high-resolution images.
- Updated internal documentation and added comprehensive tests to validate the new behavior and performance improvements.
- Ensured backward compatibility by maintaining existing interfaces where necessary while introducing breaking changes in the selection processing logic.
This commit is contained in:
2026-09-01 22:36:20 +08:00
parent 2ff7b04f56
commit 7f6e47432e
54 changed files with 2831 additions and 634 deletions

View File

@@ -6,11 +6,12 @@ import bpy
DEPTH_PLANE_NODE_GROUP_NAME = "O Image Depth Plane"
def _interface_socket(group, name, in_out, socket_type):
def _interface_socket(group, name, in_out, socket_type, parent=None):
socket = group.interface.new_socket(
name=name,
in_out=in_out,
socket_type=socket_type,
parent=parent,
)
if in_out == "INPUT" and hasattr(socket, "structure_type"):
socket.structure_type = "SINGLE"
@@ -26,6 +27,13 @@ def _group_input(nodes, location, visible_names):
return node
def _math(nodes, operation, location):
node = nodes.new("ShaderNodeMath")
node.operation = operation
node.location = location
return node
def build_depth_plane_group(mesh_plane_group):
group = bpy.data.node_groups.get(DEPTH_PLANE_NODE_GROUP_NAME)
if group is not None:
@@ -35,156 +43,167 @@ def build_depth_plane_group(mesh_plane_group):
"GeometryNodeTree",
)
group.is_modifier = True
_interface_socket(group, "Geometry", "INPUT", "NodeSocketGeometry")
geometry = _interface_socket(group, "Geometry", "INPUT", "NodeSocketGeometry")
geometry.structure_type = "SINGLE"
subdivide = _interface_socket(group, "Subdivide", "INPUT", "NodeSocketInt")
subdivide.default_value = 7
subdivide.default_value = 6
subdivide.min_value = 0
subdivide.max_value = 10
thickness = _interface_socket(group, "Thickness", "INPUT", "NodeSocketFloat")
thickness.default_value = 0.1
thickness.min_value = 0.0
thickness.subtype = "DISTANCE"
depth_amount = _interface_socket(group, "Depth Amount", "INPUT", "NodeSocketFloat")
depth_amount.default_value = 1.0
depth_amount.min_value = 0.0
reference_depth = _interface_socket(
group, "Reference Depth", "INPUT", "NodeSocketFloat"
depth_scale = _interface_socket(group, "Depth Scale", "INPUT", "NodeSocketFloat")
depth_scale.default_value = 1.0
depth_scale.min_value = 0.0
options = group.interface.new_panel(name="Options", default_closed=True)
base_plane_depth = _interface_socket(
group,
"Base Plane Depth",
"INPUT",
"NodeSocketFloat",
parent=options,
)
reference_depth.hide_in_modifier = True
base_plane_depth.default_value = 1.0
base_plane_depth.min_value = 0.0
base_plane_depth.subtype = "DISTANCE"
data = group.interface.new_panel(name="Data", default_closed=True)
uniform_scale = _interface_socket(
group, "Uniform Scale", "INPUT", "NodeSocketFloat"
group,
"Uniform Scale",
"INPUT",
"NodeSocketFloat",
parent=data,
)
uniform_scale.default_value = 1.0
uniform_scale.min_value = 0.0
uniform_scale.hide_in_modifier = True
depth_image = _interface_socket(group, "Depth Image", "INPUT", "NodeSocketImage")
depth_image = _interface_socket(
group,
"Depth Image",
"INPUT",
"NodeSocketImage",
parent=data,
)
depth_image.hide_in_modifier = True
material = _interface_socket(group, "Material", "INPUT", "NodeSocketMaterial")
material.hide_in_modifier = True
_interface_socket(group, "Geometry", "OUTPUT", "NodeSocketGeometry")
nodes = group.nodes
links = group.links
geometry_input = _group_input(
nodes,
(-1850.0, 450.0),
{"Geometry", "Subdivide", "Material"},
(-1900.0, 500.0),
{"Geometry", "Subdivide"},
)
shape_input = _group_input(
nodes,
(-1250.0, 850.0),
{"Thickness", "Depth Amount"},
(-1550.0, 950.0),
{"Thickness", "Depth Scale", "Base Plane Depth"},
)
image_input = _group_input(
nodes,
(-1050.0, 50.0),
{"Depth Image", "Reference Depth", "Uniform Scale"},
(-1200.0, -350.0),
{"Depth Image", "Uniform Scale"},
)
cleanup_input = _group_input(nodes, (650.0, -200.0), {"Thickness"})
group_output = nodes.new("NodeGroupOutput")
group_output.location = (1850.0, 450.0)
group_output.location = (1550.0, 500.0)
mesh_plane = nodes.new("GeometryNodeGroup")
mesh_plane.node_tree = mesh_plane_group
mesh_plane.inputs["Thickness"].default_value = 0.0
mesh_plane.location = (-1550.0, 450.0)
mesh_plane.location = (-1600.0, 500.0)
links.new(geometry_input.outputs["Geometry"], mesh_plane.inputs["Geometry"])
links.new(geometry_input.outputs["Subdivide"], mesh_plane.inputs["Subdivide"])
links.new(geometry_input.outputs["Material"], mesh_plane.inputs["Material"])
links.new(shape_input.outputs["Thickness"], mesh_plane.inputs["Thickness"])
extrude = nodes.new("GeometryNodeExtrudeMesh")
extrude.mode = "FACES"
extrude.inputs["Offset"].default_value = (0.0, 0.0, -1.0)
extrude.inputs["Individual"].default_value = False
extrude.location = (-1200.0, 450.0)
links.new(mesh_plane.outputs["Geometry"], extrude.inputs["Mesh"])
links.new(shape_input.outputs["Thickness"], extrude.inputs["Offset Scale"])
geometry = extrude.outputs["Mesh"]
positive_half = _math(nodes, "MULTIPLY", (-1300.0, 300.0))
positive_half.inputs[1].default_value = 0.5
links.new(shape_input.outputs["Thickness"], positive_half.inputs[0])
translation = nodes.new("ShaderNodeCombineXYZ")
translation.location = (-1100.0, 300.0)
links.new(positive_half.outputs[0], translation.inputs["Z"])
place_block = nodes.new("GeometryNodeTransform")
place_block.location = (-900.0, 500.0)
links.new(mesh_plane.outputs["Geometry"], place_block.inputs["Geometry"])
links.new(translation.outputs["Vector"], place_block.inputs["Translation"])
uv = nodes.new("GeometryNodeInputNamedAttribute")
uv.data_type = "FLOAT_VECTOR"
uv.inputs["Name"].default_value = "UVMap"
uv.location = (-1000.0, 50.0)
uv.location = (-950.0, -350.0)
texture = nodes.new("GeometryNodeImageTexture")
texture.location = (-800.0, 50.0)
texture.interpolation = "Linear"
texture.extension = "EXTEND"
texture.location = (-700.0, -350.0)
links.new(image_input.outputs["Depth Image"], texture.inputs["Image"])
links.new(uv.outputs["Attribute"], texture.inputs["Vector"])
separate = nodes.new("ShaderNodeSeparateXYZ")
separate.location = (-600.0, 50.0)
links.new(texture.outputs["Color"], separate.inputs["Vector"])
relative_depth = nodes.new("ShaderNodeMath")
relative_depth.operation = "SUBTRACT"
relative_depth.location = (-600.0, -150.0)
links.new(image_input.outputs["Reference Depth"], relative_depth.inputs[0])
links.new(separate.outputs["X"], relative_depth.inputs[1])
local_depth = nodes.new("ShaderNodeMath")
local_depth.operation = "MULTIPLY"
local_depth.location = (-350.0, -150.0)
links.new(relative_depth.outputs[0], local_depth.inputs[0])
links.new(image_input.outputs["Uniform Scale"], local_depth.inputs[1])
raw_depth = nodes.new("ShaderNodeMath")
raw_depth.operation = "MULTIPLY"
raw_depth.location = (-100.0, 150.0)
links.new(local_depth.outputs[0], raw_depth.inputs[0])
links.new(shape_input.outputs["Depth Amount"], raw_depth.inputs[1])
inward_limit = nodes.new("ShaderNodeMath")
inward_limit.operation = "MULTIPLY"
inward_limit.inputs[1].default_value = -0.98
inward_limit.location = (-100.0, -50.0)
links.new(shape_input.outputs["Thickness"], inward_limit.inputs[0])
clamp = nodes.new("ShaderNodeMath")
clamp.operation = "MAXIMUM"
clamp.location = (150.0, 50.0)
links.new(raw_depth.outputs[0], clamp.inputs[0])
links.new(inward_limit.outputs[0], clamp.inputs[1])
has_thickness = nodes.new("ShaderNodeMath")
has_thickness.operation = "GREATER_THAN"
has_thickness.inputs[1].default_value = 1e-6
has_thickness.location = (-100.0, -350.0)
links.new(shape_input.outputs["Thickness"], has_thickness.inputs[0])
choose_depth = nodes.new("GeometryNodeSwitch")
choose_depth.input_type = "FLOAT"
choose_depth.location = (400.0, 50.0)
links.new(has_thickness.outputs[0], choose_depth.inputs["Switch"])
links.new(raw_depth.outputs[0], choose_depth.inputs["False"])
links.new(clamp.outputs[0], choose_depth.inputs["True"])
combine = nodes.new("ShaderNodeCombineXYZ")
combine.location = (600.0, 50.0)
links.new(choose_depth.outputs["Output"], combine.inputs["Z"])
front = nodes.new("FunctionNodeBooleanMath")
front.operation = "NOT"
front.location = (400.0, 300.0)
links.new(extrude.outputs["Top"], front.inputs[0])
set_position = nodes.new("GeometryNodeSetPosition")
set_position.location = (800.0, 450.0)
links.new(geometry, set_position.inputs["Geometry"])
links.new(front.outputs[0], set_position.inputs["Selection"])
links.new(combine.outputs["Vector"], set_position.inputs["Offset"])
geometry = set_position.outputs["Geometry"]
depth_components = nodes.new("ShaderNodeSeparateXYZ")
depth_components.location = (-450.0, -350.0)
links.new(texture.outputs["Color"], depth_components.inputs["Vector"])
metric_depth = _math(nodes, "MULTIPLY", (-200.0, -350.0))
links.new(depth_components.outputs["X"], metric_depth.inputs[0])
links.new(image_input.outputs["Uniform Scale"], metric_depth.inputs[1])
relative_depth = _math(nodes, "SUBTRACT", (50.0, -350.0))
links.new(shape_input.outputs["Base Plane Depth"], relative_depth.inputs[0])
links.new(metric_depth.outputs[0], relative_depth.inputs[1])
full_offset = _math(nodes, "MULTIPLY", (300.0, -350.0))
links.new(relative_depth.outputs[0], full_offset.inputs[0])
links.new(shape_input.outputs["Depth Scale"], full_offset.inputs[1])
zero_thickness = nodes.new("ShaderNodeMath")
zero_thickness.operation = "LESS_THAN"
zero_thickness.inputs[1].default_value = 1e-6
zero_thickness.location = (1000.0, -150.0)
links.new(cleanup_input.outputs["Thickness"], zero_thickness.inputs[0])
back_or_side = nodes.new("FunctionNodeBooleanMath")
back_or_side.operation = "OR"
back_or_side.location = (1000.0, 150.0)
links.new(extrude.outputs["Top"], back_or_side.inputs[0])
links.new(extrude.outputs["Side"], back_or_side.inputs[1])
delete_selection = nodes.new("FunctionNodeBooleanMath")
delete_selection.operation = "AND"
delete_selection.location = (1200.0, 50.0)
links.new(zero_thickness.outputs[0], delete_selection.inputs[0])
links.new(back_or_side.outputs[0], delete_selection.inputs[1])
delete_geometry = nodes.new("GeometryNodeDeleteGeometry")
delete_geometry.domain = "FACE"
delete_geometry.location = (1350.0, 450.0)
links.new(geometry, delete_geometry.inputs["Geometry"])
links.new(delete_selection.outputs[0], delete_geometry.inputs["Selection"])
inward_limit = _math(nodes, "MULTIPLY", (50.0, -600.0))
inward_limit.inputs[1].default_value = -0.999
links.new(shape_input.outputs["Thickness"], inward_limit.inputs[0])
limited_offset = _math(nodes, "MAXIMUM", (300.0, -600.0))
links.new(full_offset.outputs[0], limited_offset.inputs[0])
links.new(inward_limit.outputs[0], limited_offset.inputs[1])
has_thickness = _math(nodes, "GREATER_THAN", (50.0, -850.0))
has_thickness.inputs[1].default_value = 1e-6
links.new(shape_input.outputs["Thickness"], has_thickness.inputs[0])
safe_offset = nodes.new("GeometryNodeSwitch")
safe_offset.input_type = "FLOAT"
safe_offset.location = (550.0, -450.0)
links.new(has_thickness.outputs[0], safe_offset.inputs["Switch"])
links.new(full_offset.outputs[0], safe_offset.inputs["False"])
links.new(limited_offset.outputs[0], safe_offset.inputs["True"])
position = nodes.new("GeometryNodeInputPosition")
position.location = (-650.0, 50.0)
position_components = nodes.new("ShaderNodeSeparateXYZ")
position_components.location = (-400.0, 50.0)
links.new(position.outputs["Position"], position_components.inputs["Vector"])
layer_ratio = _math(nodes, "DIVIDE", (-150.0, 50.0))
links.new(position_components.outputs["Z"], layer_ratio.inputs[0])
links.new(shape_input.outputs["Thickness"], layer_ratio.inputs[1])
nonnegative_ratio = _math(nodes, "MAXIMUM", (350.0, 50.0))
nonnegative_ratio.inputs[1].default_value = 0.0
links.new(layer_ratio.outputs[0], nonnegative_ratio.inputs[0])
clamped_ratio = _math(nodes, "MINIMUM", (600.0, 50.0))
clamped_ratio.inputs[1].default_value = 1.0
links.new(nonnegative_ratio.outputs[0], clamped_ratio.inputs[0])
layer_weight = nodes.new("GeometryNodeSwitch")
layer_weight.input_type = "FLOAT"
layer_weight.location = (850.0, 50.0)
layer_weight.inputs["False"].default_value = 1.0
links.new(has_thickness.outputs[0], layer_weight.inputs["Switch"])
links.new(clamped_ratio.outputs[0], layer_weight.inputs["True"])
weighted_offset = _math(nodes, "MULTIPLY", (850.0, -350.0))
links.new(safe_offset.outputs["Output"], weighted_offset.inputs[0])
links.new(layer_weight.outputs["Output"], weighted_offset.inputs[1])
offset = nodes.new("ShaderNodeCombineXYZ")
offset.location = (1050.0, -200.0)
links.new(weighted_offset.outputs[0], offset.inputs["Z"])
set_position = nodes.new("GeometryNodeSetPosition")
set_position.location = (1050.0, 500.0)
links.new(place_block.outputs["Geometry"], set_position.inputs["Geometry"])
links.new(offset.outputs["Vector"], set_position.inputs["Offset"])
shade_smooth = nodes.new("GeometryNodeSetShadeSmooth")
shade_smooth.domain = "FACE"
shade_smooth.inputs["Shade Smooth"].default_value = True
shade_smooth.location = (1600.0, 450.0)
links.new(delete_geometry.outputs["Geometry"], shade_smooth.inputs["Geometry"])
shade_smooth.location = (1300.0, 500.0)
links.new(set_position.outputs["Geometry"], shade_smooth.inputs["Geometry"])
links.new(shade_smooth.outputs["Geometry"], group_output.inputs["Geometry"])
group.color_tag = "GEOMETRY"
group.description = (
"Build a single-sided depth terrain block with a fixed base."
)
group.use_fake_user = True
return group

View File

@@ -2,7 +2,7 @@
import bpy
from anyimage.common.object import (
from anyimage.common.material import (
DEPTH_MATERIAL_NODE_GROUP_NAME,
IMAGE_MATERIAL_NODE_GROUP_NAME,
)

View File

@@ -42,15 +42,6 @@ def build_mesh_plane_group():
thickness.min_value = 0.0
thickness.max_value = 10000.0
thickness.subtype = "DISTANCE"
data_panel = interface.new_panel(name="Data", default_closed=True)
material = new_socket(
interface,
"Material",
"INPUT",
"NodeSocketMaterial",
parent=data_panel,
)
material.hide_in_modifier = True
new_socket(interface, "Geometry", "OUTPUT", "NodeSocketGeometry")
nodes = group.nodes
@@ -58,9 +49,8 @@ def build_mesh_plane_group():
geometry_input = new_group_input(nodes, (-1800.0, 500.0), {"Geometry"})
subdivide_input = new_group_input(nodes, (-1800.0, 0.0), {"Subdivide"})
thickness_input = new_group_input(nodes, (100.0, -250.0), {"Thickness"})
material_input = new_group_input(nodes, (900.0, 0.0), {"Material"})
output = nodes.new("NodeGroupOutput")
output.location = (1350.0, 500.0)
output.location = (1750.0, 500.0)
bounds = nodes.new("GeometryNodeBoundBox")
bounds.location = (-1600.0, 500.0)
@@ -196,7 +186,7 @@ def build_mesh_plane_group():
links.new(depth_ratio.outputs[0], rounded_depth.inputs[0])
depth_segments = nodes.new("ShaderNodeMath")
depth_segments.operation = "MAXIMUM"
depth_segments.inputs[1].default_value = 1.0
depth_segments.inputs[1].default_value = 3.0
depth_segments.location = (700.0, -100.0)
links.new(rounded_depth.outputs[0], depth_segments.inputs[0])
depth_vertices = nodes.new("ShaderNodeMath")
@@ -239,11 +229,27 @@ def build_mesh_plane_group():
links.new(place_grid.outputs["Geometry"], geometry_switch.inputs["False"])
links.new(place_cube.outputs["Geometry"], geometry_switch.inputs["True"])
set_material = nodes.new("GeometryNodeSetMaterial")
set_material.location = (1050.0, 500.0)
links.new(geometry_switch.outputs["Output"], set_material.inputs["Geometry"])
links.new(material_input.outputs["Material"], set_material.inputs["Material"])
links.new(set_material.outputs["Geometry"], output.inputs["Geometry"])
capture_source = nodes.new("GeometryNodeCaptureAttribute")
capture_source.domain = "POINT"
capture_source.capture_items.clear()
capture_source.capture_items.new("BOOLEAN", "Source")
capture_source.inputs["Source"].default_value = True
capture_source.location = (750.0, 750.0)
links.new(geometry_input.outputs["Geometry"], capture_source.inputs["Geometry"])
join_material = nodes.new("GeometryNodeJoinGeometry")
join_material.location = (1000.0, 500.0)
links.new(geometry_switch.outputs["Output"], join_material.inputs["Geometry"])
links.new(capture_source.outputs["Geometry"], join_material.inputs["Geometry"])
set_material_index = nodes.new("GeometryNodeSetMaterialIndex")
set_material_index.inputs["Material Index"].default_value = 0
set_material_index.location = (1250.0, 500.0)
links.new(join_material.outputs["Geometry"], set_material_index.inputs["Geometry"])
delete_source = nodes.new("GeometryNodeDeleteGeometry")
delete_source.domain = "POINT"
delete_source.location = (1500.0, 500.0)
links.new(set_material_index.outputs["Geometry"], delete_source.inputs["Geometry"])
links.new(capture_source.outputs["Source"], delete_source.inputs["Selection"])
links.new(delete_source.outputs["Geometry"], output.inputs["Geometry"])
group.color_tag = "GEOMETRY"
group.description = (

View File

@@ -10,7 +10,7 @@ sys.path.insert(
str(next((PROJECT_ROOT / "src" / "anyimage" / "wheels").glob("blendjob-*.whl"))),
)
from anyimage.common.object import (
from anyimage.common.material import (
DEPTH_MATERIAL_NODE_GROUP_NAME,
IMAGE_MATERIAL_NODE_GROUP_NAME,
)
@@ -62,7 +62,6 @@ def main():
"Geometry",
"Subdivide",
"Thickness",
"Material",
]
assert depth_plane.bl_idname == "GeometryNodeTree"
assert depth_plane.is_modifier
@@ -70,12 +69,30 @@ def main():
"Geometry",
"Subdivide",
"Thickness",
"Depth Amount",
"Reference Depth",
"Depth Scale",
"Base Plane Depth",
"Uniform Scale",
"Depth Image",
"Material",
]
depth_panels = [
item for item in depth_plane.interface.items_tree if item.item_type == "PANEL"
]
assert [panel.name for panel in depth_panels] == ["Options", "Data"]
assert [item.name for item in depth_panels[0].interface_items] == [
"Base Plane Depth"
]
assert [item.name for item in depth_panels[1].interface_items] == [
"Uniform Scale",
"Depth Image",
]
assert all(panel.default_closed for panel in depth_panels)
assert all(item.hide_in_modifier for item in depth_panels[1].interface_items)
depth_subdivide = next(
item
for item in depth_plane.interface.items_tree
if item.item_type == "SOCKET" and item.name == "Subdivide"
)
assert depth_subdivide.default_value == 6
assert image_layer.bl_idname == "ShaderNodeTree"
assert input_names(image_layer) == [
"Color",

View File

@@ -27,21 +27,15 @@ def evaluated_mesh(obj):
return evaluated, evaluated.to_mesh()
def modifier_input_value(modifier, identifier):
inputs = getattr(getattr(modifier, "properties", None), "inputs", None)
if inputs is not None:
return getattr(inputs, identifier).value
return modifier[identifier]
def main():
bpy.ops.wm.read_factory_settings(use_empty=True)
mesh = create_mesh_plane_mesh("Mesh Plane", (-1.0, 1.0, -0.5, 0.5))
assert len(mesh.polygons) == 1
material = bpy.data.materials.new("Mesh Plane Material")
mesh.materials.append(material)
obj = bpy.data.objects.new("Mesh Plane", mesh)
bpy.context.scene.collection.objects.link(obj)
modifier = add_mesh_plane_processing(obj, material)
modifier = add_mesh_plane_processing(obj)
group = modifier.node_group
assert group.name == "O Mesh Plane"
assert group.is_modifier
@@ -50,15 +44,15 @@ def main():
for item in group.interface.items_tree
if item.item_type == "SOCKET" and item.in_out == "INPUT"
}
assert list(inputs) == ["Geometry", "Subdivide", "Thickness", "Material"]
assert list(inputs) == ["Geometry", "Subdivide", "Thickness"]
assert inputs["Subdivide"].default_value == 0
assert inputs["Subdivide"].max_value == 10
assert inputs["Thickness"].default_value == 0.0
assert inputs["Material"].hide_in_modifier
assert modifier_input_value(modifier, inputs["Material"].identifier) == material
assert obj.data.materials[0] == material
evaluated, result = evaluated_mesh(obj)
try:
assert result.materials[0].original == material
assert len(result.polygons) == 2
assert len(result.vertices) == 6
assert "UVMap" in result.attributes
@@ -75,6 +69,7 @@ def main():
(1.0, 1.0),
}
assert all(abs(vertex.co.z) < 1e-6 for vertex in result.vertices)
assert all(polygon.material_index == 0 for polygon in result.polygons)
finally:
evaluated.to_mesh_clear()
@@ -92,6 +87,7 @@ def main():
obj.update_tag(refresh={"DATA"})
evaluated, result = evaluated_mesh(obj)
try:
assert result.materials[0].original == material
assert len(result.polygons) == 184
z_values = [vertex.co.z for vertex in result.vertices]
assert abs(min(z_values) + 0.6) < 1e-6
@@ -102,6 +98,17 @@ def main():
for edge_key in polygon.edge_keys:
edge_users[edge_key] += 1
assert all(users == 2 for users in edge_users.values())
assert all(polygon.material_index == 0 for polygon in result.polygons)
finally:
evaluated.to_mesh_clear()
set_modifier_input(modifier, modifier_input_identifier(group, "Subdivide"), 0)
set_modifier_input(modifier, modifier_input_identifier(group, "Thickness"), 0.01)
obj.update_tag(refresh={"DATA"})
evaluated, result = evaluated_mesh(obj)
try:
z_values = {round(vertex.co.z, 6) for vertex in result.vertices}
assert len(z_values) >= 4
finally:
evaluated.to_mesh_clear()