Refactor depth surface and image cutout nodes for improved functionality and clarity
- Removed deprecated tests and code related to shell wall connections and coincident sheets. - Updated attribute handling in depth surface and image cutout groups, including new vector attribute functions. - Enhanced the inflation logic in the image depth cutout group to better manage shell and balloon behaviors. - Improved parameter naming and descriptions for clarity in the user interface. - Streamlined the geometry processing logic to ensure better performance and maintainability.
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
"""Face separation and connected thickness for Depth Surface assets."""
|
||||
|
||||
from .nodes import (
|
||||
boolean_node, compare_node, evaluate_field, store_float_attribute,
|
||||
store_boolean_attribute, remove_attribute_pattern, sample_field,
|
||||
boolean_node, compare_node, evaluate_field, store_boolean_attribute,
|
||||
remove_attribute_pattern, sample_field,
|
||||
)
|
||||
from .smoothing import edge_boundary_field
|
||||
|
||||
@@ -318,8 +318,7 @@ def build_surface_camera(group, original, separated, cut_vertex, strength):
|
||||
return evaluate_field(group, choose.outputs["Output"], "FLOAT_VECTOR", "POINT")
|
||||
|
||||
|
||||
def thicken_depth_surface(group, geometry, thickness, normal_smooth=None, *,
|
||||
back_mask=None, weld_zero_thickness=False):
|
||||
def thicken_depth_surface(group, geometry, thickness, normal_smooth=None):
|
||||
"""Connect caps with single quads by source vertex and cap identity."""
|
||||
nodes, links = group.nodes, group.links
|
||||
position = nodes.new("GeometryNodeInputPosition")
|
||||
@@ -378,29 +377,12 @@ def thicken_depth_surface(group, geometry, thickness, normal_smooth=None, *,
|
||||
links.new(rear, back_parts.inputs["Geometry"])
|
||||
links.new(rim, back_parts.inputs["Geometry"])
|
||||
rear = back_parts.outputs["Geometry"]
|
||||
if back_mask is not None:
|
||||
rear = store_float_attribute(group, rear, back_mask, 1.0)
|
||||
for value in (
|
||||
saved,
|
||||
rear,
|
||||
):
|
||||
links.new(value, join.inputs["Geometry"])
|
||||
construction = join.outputs["Geometry"]
|
||||
if weld_zero_thickness:
|
||||
positive = compare_node(group, "GREATER_THAN", source_thickness, 1e-12)
|
||||
flattened = nodes.new("ShaderNodeCombineXYZ")
|
||||
for axis in ("X", "Z"):
|
||||
links.new(layer_axes.outputs[axis], flattened.inputs[axis])
|
||||
collapsed = nodes.new("GeometryNodeSwitch")
|
||||
collapsed.input_type = "FLOAT"
|
||||
collapsed.inputs["False"].default_value = 0.0
|
||||
links.new(positive, collapsed.inputs["Switch"])
|
||||
links.new(layer_axes.outputs["Y"], collapsed.inputs["True"])
|
||||
links.new(collapsed.outputs[0], flattened.inputs["Y"])
|
||||
collapse = nodes.new("GeometryNodeSetPosition")
|
||||
links.new(construction, collapse.inputs["Geometry"])
|
||||
links.new(flattened.outputs[0], collapse.inputs["Position"])
|
||||
construction = collapse.outputs["Geometry"]
|
||||
merge = nodes.new("GeometryNodeMergeByDistance")
|
||||
merge.mode = "ALL"
|
||||
merge.inputs["Distance"].default_value = 0.1
|
||||
|
||||
@@ -138,6 +138,13 @@ def read_float_attribute(group, name):
|
||||
return node.outputs["Attribute"]
|
||||
|
||||
|
||||
def read_vector_attribute(group, name):
|
||||
node = group.nodes.new("GeometryNodeInputNamedAttribute")
|
||||
node.data_type = "FLOAT_VECTOR"
|
||||
node.inputs["Name"].default_value = name
|
||||
return node.outputs["Attribute"]
|
||||
|
||||
|
||||
def store_float_attribute(group, geometry, name, value, domain="POINT"):
|
||||
node = group.nodes.new("GeometryNodeStoreNamedAttribute")
|
||||
node.domain, node.data_type = domain, "FLOAT"
|
||||
@@ -150,6 +157,15 @@ def store_float_attribute(group, geometry, name, value, domain="POINT"):
|
||||
return node.outputs["Geometry"]
|
||||
|
||||
|
||||
def store_vector_attribute(group, geometry, name, value, domain="POINT"):
|
||||
node = group.nodes.new("GeometryNodeStoreNamedAttribute")
|
||||
node.domain, node.data_type = domain, "FLOAT_VECTOR"
|
||||
node.inputs["Name"].default_value = name
|
||||
group.links.new(geometry, node.inputs["Geometry"])
|
||||
group.links.new(value, node.inputs["Value"])
|
||||
return node.outputs["Geometry"]
|
||||
|
||||
|
||||
def evaluate_field(group, value, data_type, domain):
|
||||
"""Evaluate a field on an explicit domain before downstream adaptation."""
|
||||
node = group.nodes.new("GeometryNodeFieldOnDomain")
|
||||
@@ -184,26 +200,12 @@ def store_boolean_attribute(group, geometry, name, value, domain="POINT"):
|
||||
return node.outputs["Geometry"], read.outputs["Attribute"]
|
||||
|
||||
|
||||
def remove_attributes(group, geometry, *names, enabled=None):
|
||||
"""Remove only the explicitly owned temporary attributes."""
|
||||
original = geometry
|
||||
for name in names:
|
||||
node = group.nodes.new("GeometryNodeRemoveAttribute")
|
||||
node.inputs["Name"].default_value = name
|
||||
group.links.new(geometry, node.inputs["Geometry"])
|
||||
geometry = node.outputs["Geometry"]
|
||||
if enabled is not None:
|
||||
choose = group.nodes.new("GeometryNodeSwitch")
|
||||
choose.input_type = "GEOMETRY"
|
||||
group.links.new(enabled, choose.inputs["Switch"])
|
||||
group.links.new(original, choose.inputs["False"])
|
||||
group.links.new(geometry, choose.inputs["True"])
|
||||
geometry = choose.outputs["Output"]
|
||||
return geometry
|
||||
|
||||
|
||||
def remove_attribute_pattern(group, geometry, pattern):
|
||||
"""Remove every attribute matching an internal wildcard pattern."""
|
||||
"""Remove every attribute matching an internal wildcard pattern.
|
||||
|
||||
Always clean internal attributes with a pattern: naming an attribute that a
|
||||
branch never stored makes the Remove Named Attribute node warn.
|
||||
"""
|
||||
node = group.nodes.new("GeometryNodeRemoveAttribute")
|
||||
node.inputs["Name"].default_value = pattern
|
||||
node.pattern_mode = "WILDCARD"
|
||||
|
||||
@@ -17,11 +17,11 @@ from ..common.smoothing import edge_boundary_field, pinned_smooth
|
||||
|
||||
NODE_GROUP_NAME = "O Image Cutout Symmetry"
|
||||
|
||||
# 切口环与衔接带各自向内覆盖的圈数。
|
||||
# 切口环与补面衔接带各自向外覆盖的圈数。
|
||||
SMOOTH_RINGS = 4
|
||||
|
||||
# 衔接带要覆盖镜像焊好的平面环,焊完之后先把这些点记在内部属性上。
|
||||
WELD_ATTRIBUTE = '_o_anyimage_symmetry_weld'
|
||||
# 补面标记:挤出之后按面记下补面,作为补面平滑的范围。
|
||||
WELD_ATTRIBUTE = '_o_symmetry_weld'
|
||||
|
||||
|
||||
# 对称面附近按中位边长的倍数降低法线贴图强度。
|
||||
@@ -76,22 +76,22 @@ def _boundary_edge_field(group):
|
||||
return compare_node(group, 'EQUAL', neighbors.outputs['Face Count'], 1, data_type='INT')
|
||||
|
||||
|
||||
def _beyond_plane_field(group, tolerance):
|
||||
"""Report the points sitting beyond the symmetry plane."""
|
||||
def _beyond_plane_field(group, distance):
|
||||
"""Report the points sitting farther than the weld distance past the plane."""
|
||||
nodes = group.nodes
|
||||
position = nodes.new('GeometryNodeInputPosition')
|
||||
components = nodes.new('ShaderNodeSeparateXYZ')
|
||||
group.links.new(position.outputs[0], components.inputs[0])
|
||||
return compare_node(group, 'GREATER_THAN', components.outputs['Y'], tolerance)
|
||||
return compare_node(group, 'GREATER_THAN', components.outputs['Y'], distance)
|
||||
|
||||
|
||||
def _on_plane_field(group, tolerance):
|
||||
"""Report the points sitting on the symmetry plane."""
|
||||
def _on_plane_field(group, distance):
|
||||
"""Report the points sitting within the weld distance of the plane."""
|
||||
nodes = group.nodes
|
||||
position = nodes.new('GeometryNodeInputPosition')
|
||||
components = nodes.new('ShaderNodeSeparateXYZ')
|
||||
group.links.new(position.outputs[0], components.inputs[0])
|
||||
return compare_node(group, 'LESS_THAN', _math(group, 'ABSOLUTE', components.outputs['Y']), tolerance)
|
||||
return compare_node(group, 'LESS_THAN', _math(group, 'ABSOLUTE', components.outputs['Y']), distance)
|
||||
|
||||
|
||||
def _merge_points(group, geometry, selection, distance):
|
||||
@@ -166,7 +166,7 @@ def _delete_bridge_faces(group, geometry):
|
||||
return _delete_faces(group, geometry, compare_node(group, 'GREATER_THAN', _boundary_edge_count(group), 1.5))
|
||||
|
||||
|
||||
def _delete_flat_faces(group, geometry, tolerance):
|
||||
def _delete_flat_faces(group, geometry, distance):
|
||||
"""Delete the faces lying on the symmetry plane; they mirror onto themselves."""
|
||||
nodes, links = group.nodes, group.links
|
||||
position = nodes.new('GeometryNodeInputPosition')
|
||||
@@ -180,20 +180,20 @@ def _delete_flat_faces(group, geometry, tolerance):
|
||||
group, geometry,
|
||||
boolean_node(
|
||||
group, 'AND',
|
||||
compare_node(group, 'LESS_THAN', center, tolerance),
|
||||
compare_node(group, 'LESS_THAN', center, distance),
|
||||
compare_node(group, 'GREATER_THAN', _math(group, 'ABSOLUTE', normal_axes.outputs['Y']), 1 - 1e-3),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _delete_crease_faces(group, geometry, tolerance):
|
||||
def _delete_crease_faces(group, geometry, distance):
|
||||
"""Delete the faces the fold leaves sharing an interior edge on the plane."""
|
||||
nodes, links = group.nodes, group.links
|
||||
heights = _edge_abs_y(group)
|
||||
plane = nodes.new('FunctionNodeBooleanMath')
|
||||
plane.operation = 'AND'
|
||||
links.new(compare_node(group, 'LESS_THAN', heights[0], tolerance), plane.inputs[0])
|
||||
links.new(compare_node(group, 'LESS_THAN', heights[1], tolerance), plane.inputs[1])
|
||||
links.new(compare_node(group, 'LESS_THAN', heights[0], distance), plane.inputs[0])
|
||||
links.new(compare_node(group, 'LESS_THAN', heights[1], distance), plane.inputs[1])
|
||||
neighbors = nodes.new('GeometryNodeInputMeshEdgeNeighbors')
|
||||
crease = nodes.new('FunctionNodeBooleanMath')
|
||||
crease.operation = 'AND'
|
||||
@@ -209,10 +209,10 @@ def _delete_crease_faces(group, geometry, tolerance):
|
||||
return _delete_faces(group, geometry, selected)
|
||||
|
||||
|
||||
def _off_plane_edge_field(group, tolerance):
|
||||
"""Report one on edges that keep an endpoint off the symmetry plane."""
|
||||
def _off_plane_edge_field(group, distance):
|
||||
"""Report one on edges that keep an endpoint farther than the weld distance."""
|
||||
heights = _edge_abs_y(group)
|
||||
return compare_node(group, 'GREATER_THAN', _math(group, 'MAXIMUM', heights[0], heights[1]), tolerance)
|
||||
return compare_node(group, 'GREATER_THAN', _math(group, 'MAXIMUM', heights[0], heights[1]), distance)
|
||||
|
||||
|
||||
def _edge_abs_y(group):
|
||||
@@ -235,10 +235,10 @@ def _mirror(group, geometry):
|
||||
return flip.outputs[0]
|
||||
|
||||
|
||||
def _seam_field(group, tolerance):
|
||||
def _seam_field(group, distance):
|
||||
"""Report the cut ring the folded front leaves on the symmetry plane."""
|
||||
boundary = evaluate_field(group, edge_boundary_field(group.nodes, group.links), 'BOOLEAN', 'POINT')
|
||||
return boolean_node(group, 'AND', boundary, _on_plane_field(group, tolerance))
|
||||
return boolean_node(group, 'AND', boundary, _on_plane_field(group, distance))
|
||||
|
||||
|
||||
def _falloff(group, center, rings):
|
||||
@@ -266,54 +266,42 @@ def _snap_to_plane(group, geometry, selection):
|
||||
return place.outputs[0]
|
||||
|
||||
|
||||
def _relax_seam(group, geometry, seam, beyond, smooth):
|
||||
def _relax_seam(group, geometry, seam, retract, smooth):
|
||||
"""Relax the cut seam in three dimensions and keep the cut on the plane."""
|
||||
relaxed = pinned_smooth(
|
||||
group, geometry, smooth, _falloff(group, seam, SMOOTH_RINGS),
|
||||
pin_boundary=True,
|
||||
)
|
||||
return _snap_to_plane(group, relaxed, boolean_node(group, 'OR', seam, beyond))
|
||||
return _snap_to_plane(group, relaxed, boolean_node(group, 'OR', seam, retract))
|
||||
|
||||
|
||||
def _relax_fill(group, geometry, weld, smooth):
|
||||
"""Relax the junction band on the welded mesh, where the seam needs no pinning."""
|
||||
def _relax_fill(group, geometry, fill, smooth):
|
||||
"""Relax the fill faces on the welded mesh and fade outward from them."""
|
||||
return pinned_smooth(
|
||||
group, geometry, smooth, _falloff(group, weld, SMOOTH_RINGS),
|
||||
weight=0.1, pin_boundary=False,
|
||||
group, geometry, smooth, _falloff(group, fill, SMOOTH_RINGS),
|
||||
weight=0.2, pin_boundary=False,
|
||||
)
|
||||
|
||||
|
||||
def _extrude_boundary(group, geometry, selection, y_factor):
|
||||
"""Extrude selected edges to a fraction of their current height."""
|
||||
def _build_wall(group, front, boundary):
|
||||
"""Close the selected boundary edges onto the symmetry plane and mark the fill."""
|
||||
nodes, links = group.nodes, group.links
|
||||
extrude = nodes.new('GeometryNodeExtrudeMesh')
|
||||
extrude.mode = 'EDGES'
|
||||
extrude.inputs['Offset Scale'].default_value = 0
|
||||
links.new(geometry, extrude.inputs['Mesh'])
|
||||
links.new(selection, extrude.inputs['Selection'])
|
||||
position = nodes.new('GeometryNodeInputPosition')
|
||||
scale = nodes.new('ShaderNodeVectorMath')
|
||||
scale.operation = 'MULTIPLY'
|
||||
scale.inputs[1].default_value = (1, y_factor, 1)
|
||||
links.new(position.outputs[0], scale.inputs[0])
|
||||
place = nodes.new('GeometryNodeSetPosition')
|
||||
links.new(extrude.outputs['Mesh'], place.inputs['Geometry'])
|
||||
links.new(extrude.outputs['Top'], place.inputs['Selection'])
|
||||
links.new(scale.outputs[0], place.inputs['Position'])
|
||||
links.new(front, extrude.inputs['Mesh'])
|
||||
links.new(boundary, extrude.inputs['Selection'])
|
||||
geometry = _snap_to_plane(group, extrude.outputs['Mesh'], extrude.outputs['Top'])
|
||||
# 挤出之后再标记补面:范围就是补面本身连同它的边界线,挤出不会改变它。
|
||||
fill = evaluate_field(group, extrude.outputs['Side'], 'BOOLEAN', 'POINT')
|
||||
geometry, fill = store_boolean_attribute(group, geometry, WELD_ATTRIBUTE, fill, domain='FACE')
|
||||
store = nodes.new('GeometryNodeStoreNamedAttribute')
|
||||
store.data_type, store.domain = 'FLOAT', 'FACE'
|
||||
store.inputs['Name'].default_value = FACE_ATTRIBUTE
|
||||
store.inputs['Value'].default_value = 3
|
||||
links.new(place.outputs[0], store.inputs['Geometry'])
|
||||
links.new(geometry, store.inputs['Geometry'])
|
||||
links.new(extrude.outputs['Side'], store.inputs['Selection'])
|
||||
return store.outputs[0], extrude.outputs['Top']
|
||||
|
||||
|
||||
def _build_wall(group, front, boundary):
|
||||
"""Attach the curved half wall to the front and mark its faces."""
|
||||
geometry, top = _extrude_boundary(group, front, boundary, .5)
|
||||
geometry, _ = _extrude_boundary(group, geometry, top, 0)
|
||||
return geometry
|
||||
return store.outputs[0], fill
|
||||
|
||||
|
||||
def build_image_cutout_symmetry_group():
|
||||
@@ -331,7 +319,9 @@ def build_image_cutout_symmetry_group():
|
||||
bool_input(group, 'Fill Sides', True, parent=options).description = 'Connect matching front and mirrored boundaries.'
|
||||
smooth = interface_socket(group, 'Smooth', 'INPUT', 'NodeSocketInt', options)
|
||||
smooth.default_value, smooth.min_value, smooth.max_value = 4, 0, 16
|
||||
smooth.description = 'Smooth the cut ring and the junction band around the outline and the fill, each over four rings. Zero keeps both raw.'
|
||||
smooth.description = 'Smooth the cut ring and the fill band around the outline and the fill, each over four rings. Zero keeps both raw.'
|
||||
merge = float_input(group, 'Merge Distance', 0.001, minimum=0, subtype='DISTANCE', parent=options)
|
||||
merge.description = 'Weld mirrored geometry within this distance of the symmetry plane; boundaries farther away get filled.'
|
||||
interface_socket(group, 'Geometry', 'OUTPUT', 'NodeSocketGeometry')
|
||||
nodes, links = group.nodes, group.links
|
||||
inputs = group_input(nodes, set()).outputs
|
||||
@@ -345,25 +335,25 @@ def build_image_cutout_symmetry_group():
|
||||
statistics.data_type, statistics.domain = 'FLOAT', 'EDGE'
|
||||
links.new(aligned, statistics.inputs['Geometry'])
|
||||
links.new(edge_length.outputs['Value'], statistics.inputs['Attribute'])
|
||||
snap_tolerance = _math(
|
||||
group, 'MULTIPLY', _math(group, 'MAXIMUM', statistics.outputs['Median'], 1e-6), 1e-6,
|
||||
)
|
||||
beyond = _beyond_plane_field(group, snap_tolerance)
|
||||
on_plane = _on_plane_field(group, snap_tolerance)
|
||||
# 合并距离同时决定平面的贴合带和补面边界,两者一致才不会留下细小补面。
|
||||
merge_distance = _math(group, 'MAXIMUM', inputs['Merge Distance'], 1e-6)
|
||||
beyond = _beyond_plane_field(group, merge_distance)
|
||||
on_plane = _on_plane_field(group, merge_distance)
|
||||
retract = boolean_node(group, 'OR', on_plane, beyond)
|
||||
is_boundary = _boundary_edge_field(group)
|
||||
front = _delete_beyond_plane(group, aligned, beyond)
|
||||
front = _delete_bridge_faces(group, front)
|
||||
# 跨界保留的面折回对称面,切口环因此落在平面上。
|
||||
front = _snap_to_plane(group, front, beyond)
|
||||
seam_field = _seam_field(group, snap_tolerance)
|
||||
# 贴合带内和跨界保留的面都折回对称面,切口环因此落在平面上。
|
||||
front = _snap_to_plane(group, front, retract)
|
||||
seam_field = _seam_field(group, merge_distance)
|
||||
shade = nodes.new('GeometryNodeSetShadeSmooth')
|
||||
shade.domain = 'FACE'
|
||||
links.new(front, shade.inputs['Geometry'])
|
||||
front = store_float_attribute(group, shade.outputs[0], FACE_ATTRIBUTE, 1., domain='FACE')
|
||||
front = _relax_seam(group, front, seam_field, beyond, inputs['Smooth'])
|
||||
front = _relax_seam(group, front, seam_field, retract, inputs['Smooth'])
|
||||
# 松弛会把点压回平面,贴平的面和内部贴平边要在松弛之后再清一次。
|
||||
front = _delete_flat_faces(group, front, snap_tolerance)
|
||||
front = _delete_crease_faces(group, front, snap_tolerance)
|
||||
front = _delete_flat_faces(group, front, merge_distance)
|
||||
front = _delete_crease_faces(group, front, merge_distance)
|
||||
fill_iterations = nodes.new('GeometryNodeSwitch')
|
||||
fill_iterations.input_type = 'INT'
|
||||
fill_iterations.inputs['False'].default_value = 0
|
||||
@@ -373,8 +363,8 @@ def build_image_cutout_symmetry_group():
|
||||
wall = nodes.new('FunctionNodeBooleanMath')
|
||||
wall.operation = 'AND'
|
||||
links.new(is_boundary, wall.inputs[0])
|
||||
links.new(_off_plane_edge_field(group, snap_tolerance), wall.inputs[1])
|
||||
walled = _build_wall(group, front, wall.outputs[0])
|
||||
links.new(_off_plane_edge_field(group, merge_distance), wall.inputs[1])
|
||||
walled, fill = _build_wall(group, front, wall.outputs[0])
|
||||
wall_shade = nodes.new('GeometryNodeSetShadeSmooth')
|
||||
wall_shade.domain = 'FACE'
|
||||
links.new(compare_node(group, 'EQUAL', read_float_attribute(group, FACE_ATTRIBUTE), 3), wall_shade.inputs['Selection'])
|
||||
@@ -393,11 +383,10 @@ def build_image_cutout_symmetry_group():
|
||||
join = nodes.new('GeometryNodeJoinGeometry')
|
||||
for geometry in (body.outputs[0], mark_back.outputs['Geometry']):
|
||||
links.new(geometry, join.inputs[0])
|
||||
# 接缝两侧的位置本来就重合,合并距离收到容差量级,别把平面附近相邻而不同的点连起来。
|
||||
merged = _merge_points(group, join.outputs[0], on_plane, snap_tolerance)
|
||||
# 先让镜像焊住接缝,再对焊好的网格松弛衔接带。
|
||||
geometry, weld = store_boolean_attribute(group, merged, WELD_ATTRIBUTE, on_plane, domain='POINT')
|
||||
geometry = _relax_fill(group, geometry, weld, fill_iterations.outputs[0])
|
||||
# 平面附近的点已经折到平面上,接缝两侧位置重合;合并只作用在这条带内。
|
||||
merged = _merge_points(group, join.outputs[0], on_plane, merge_distance)
|
||||
# 先让镜像焊住接缝,再松弛补面和它外侧的衔接带。
|
||||
geometry = _relax_fill(group, merged, fill, fill_iterations.outputs[0])
|
||||
geometry = store_float_attribute(group, geometry, AXIS_ATTRIBUTE, 1., domain='FACE')
|
||||
orient = nodes.new('GeometryNodeTransform')
|
||||
orient.inputs['Rotation'].default_value = Matrix.Rotation(1.5707963267948966, 4, 'Z').to_euler()
|
||||
|
||||
@@ -2,18 +2,21 @@
|
||||
import bpy
|
||||
from anyimage.operators.cutout_tool.shape import NORMAL_REDUCTION_ATTRIBUTE_NAME
|
||||
from ..common.nodes import (
|
||||
interface_socket, group_input, float_input, read_float_attribute,
|
||||
store_float_attribute, prepare_node_group, sample_field, evaluate_field,
|
||||
compare_node,
|
||||
interface_socket, group_input, float_input, read_float_attribute, read_vector_attribute,
|
||||
store_float_attribute, store_vector_attribute, remove_attribute_pattern,
|
||||
prepare_node_group, evaluate_field, compare_node,
|
||||
)
|
||||
from ..common.boundary_smoothing import smooth_cut_boundary
|
||||
from ..common.cutout import _position_field, _shade_output
|
||||
from ..common.depth_surface import _math, project_depth_surface, build_surface_camera, split_depth_surface, sample_face_camera, thicken_depth_surface
|
||||
from ..common.depth_surface import _math, project_depth_surface, build_surface_camera, split_depth_surface, sample_face_camera
|
||||
from ..common.smoothing import edge_boundary_field
|
||||
from ..common.cutout_boundary import original_boundary_field, remove_boundary_triangles, taper_split_profile
|
||||
|
||||
|
||||
NODE_GROUP_NAME = "O Image Depth Cutout"
|
||||
SOLID_NORMAL_ATTRIBUTE = "_o_solid_normal"
|
||||
BALLOON_THICKNESS_SCALE = 2.0
|
||||
FRONT_INFLATION_SCALE = 0.5
|
||||
|
||||
|
||||
def build_normal_weight(group, is_shell):
|
||||
@@ -47,229 +50,85 @@ def blend_normal(group, original, smoothed, weight):
|
||||
return normalize.outputs[0]
|
||||
|
||||
|
||||
def build_volume_fields(group, rim=None):
|
||||
"""Evaluate the center and thickness on the original front surface."""
|
||||
links = group.links
|
||||
if rim is None:
|
||||
rim = evaluate_field(group, edge_boundary_field(group.nodes, links), "FLOAT", "POINT")
|
||||
profile = read_float_attribute(group, "o_balloon")
|
||||
position = group.nodes.new("GeometryNodeInputPosition")
|
||||
axes = group.nodes.new("ShaderNodeSeparateXYZ")
|
||||
links.new(position.outputs[0], axes.inputs[0])
|
||||
interior = _math(group, "SUBTRACT", 1, rim)
|
||||
depth = axes.outputs["Y"]
|
||||
depth_slow = group.nodes.new("GeometryNodeBlurAttribute")
|
||||
depth_slow.data_type = "FLOAT"
|
||||
depth_slow.inputs["Iterations"].default_value = 256
|
||||
links.new(depth, depth_slow.inputs["Value"])
|
||||
links.new(interior, depth_slow.inputs["Weight"])
|
||||
profile_slow = group.nodes.new("GeometryNodeBlurAttribute")
|
||||
profile_slow.data_type = "FLOAT"
|
||||
profile_slow.inputs["Iterations"].default_value = 256
|
||||
links.new(profile, profile_slow.inputs["Value"])
|
||||
links.new(interior, profile_slow.inputs["Weight"])
|
||||
center = _math(group, "SUBTRACT", depth_slow.outputs[0], profile_slow.outputs[0])
|
||||
residual = _math(group, "SUBTRACT", axes.outputs["Y"], center)
|
||||
full = _math(group, "ADD", residual, profile)
|
||||
minimum = _math(group, "MULTIPLY", profile, 0.05)
|
||||
positive = _math(group, "MAXIMUM", full, minimum)
|
||||
control = group_input(group.nodes, {"Thickness"})
|
||||
thickness = _math(group, "MULTIPLY", positive, control.outputs["Thickness"])
|
||||
return {
|
||||
"profile": profile, "depth": depth, "interior": interior,
|
||||
"depth_slow": depth_slow.outputs[0], "profile_slow": profile_slow.outputs[0],
|
||||
"center": center, "thickness": thickness,
|
||||
}
|
||||
|
||||
|
||||
def build_shell_fields(group, geometry, fields, is_shell, normal_weight=1):
|
||||
"""Evaluate rear directions on the center surface and select shell thickness."""
|
||||
nodes, links = group.nodes, group.links
|
||||
position = nodes.new("GeometryNodeInputPosition")
|
||||
axes = nodes.new("ShaderNodeSeparateXYZ")
|
||||
links.new(position.outputs[0], axes.inputs[0])
|
||||
center_position = nodes.new("ShaderNodeCombineXYZ")
|
||||
for axis in ("X", "Z"):
|
||||
links.new(axes.outputs[axis], center_position.inputs[axis])
|
||||
links.new(fields["center"], center_position.inputs["Y"])
|
||||
surface = nodes.new("GeometryNodeSetPosition")
|
||||
links.new(geometry, surface.inputs["Geometry"])
|
||||
links.new(center_position.outputs[0], surface.inputs["Position"])
|
||||
normal = nodes.new("GeometryNodeInputNormal")
|
||||
blur = nodes.new("GeometryNodeBlurAttribute")
|
||||
blur.data_type = "FLOAT_VECTOR"
|
||||
links.new(group_input(nodes, {"Normal Smooth"}).outputs["Normal Smooth"], blur.inputs["Iterations"])
|
||||
links.new(normal.outputs[0], blur.inputs["Value"])
|
||||
normalized = blend_normal(group, normal.outputs[0], blur.outputs[0], normal_weight)
|
||||
rear_normal = sample_field(group, surface.outputs[0], normalized, "FLOAT_VECTOR")
|
||||
control = group_input(nodes, {"Shell Thickness"})
|
||||
thickness = nodes.new("GeometryNodeSwitch")
|
||||
thickness.input_type = "FLOAT"
|
||||
links.new(is_shell, thickness.inputs["Switch"])
|
||||
links.new(fields["thickness"], thickness.inputs["False"])
|
||||
links.new(control.outputs["Shell Thickness"], thickness.inputs["True"])
|
||||
return {"thickness": thickness.outputs[0], "normal": rear_normal}
|
||||
|
||||
|
||||
def build_front_fields(group, volume, front_normal, normal_weight=1):
|
||||
"""Fit structural amplitude over neighbouring points before creating the shell."""
|
||||
links = group.links
|
||||
|
||||
def blur(value, weight=None):
|
||||
result = group.nodes.new("GeometryNodeBlurAttribute")
|
||||
result.data_type = "FLOAT"
|
||||
result.inputs["Iterations"].default_value = 256
|
||||
links.new(value, result.inputs["Value"])
|
||||
if weight is not None:
|
||||
links.new(weight, result.inputs["Weight"])
|
||||
return result.outputs[0]
|
||||
|
||||
profile, depth = volume["profile"], volume["depth"]
|
||||
depth_slow, profile_slow = volume["depth_slow"], volume["profile_slow"]
|
||||
depth_bump = _math(group, "SUBTRACT", depth, depth_slow)
|
||||
profile_bump = _math(group, "SUBTRACT", profile, profile_slow)
|
||||
numerator = _math(group, "MULTIPLY", depth_bump, profile_bump)
|
||||
denominator = _math(group, "MULTIPLY", profile_bump, profile_bump)
|
||||
numerator = blur(numerator)
|
||||
denominator = blur(denominator)
|
||||
denominator = _math(group, "MAXIMUM", denominator, 1e-10)
|
||||
structure = _math(group, "DIVIDE", numerator, denominator)
|
||||
structure = _math(group, "MAXIMUM", structure, 0)
|
||||
|
||||
normal = front_normal
|
||||
smooth = group.nodes.new("GeometryNodeBlurAttribute")
|
||||
smooth.data_type = "FLOAT_VECTOR"
|
||||
links.new(normal, smooth.inputs["Value"])
|
||||
iterations = group_input(group.nodes, {"Normal Smooth"}).outputs["Normal Smooth"]
|
||||
# Preserve the existing extra smoothing of the inflation direction at 100.
|
||||
links.new(_math(group, "MULTIPLY", iterations, 5.12), smooth.inputs["Iterations"])
|
||||
normalized = blend_normal(group, normal, smooth.outputs[0], normal_weight)
|
||||
return {"structure": structure, "inflation_normal": normalized}
|
||||
|
||||
|
||||
def build_inflation_position(group, position, distance, fields):
|
||||
"""Inflate the Balloon front while leaving the rear cap in place."""
|
||||
links = group.links
|
||||
|
||||
structure = fields["structure"]
|
||||
control = group.nodes.new("NodeGroupInput")
|
||||
a = _math(group, "MULTIPLY", control.outputs["Thickness"], control.outputs["Front Inflation"])
|
||||
# A shifted cubic blend preserves continuous growth and height order.
|
||||
shifted = _math(group, "ADD", a, 0.75)
|
||||
quadratic = _math(group, "MULTIPLY", a, shifted)
|
||||
quadratic = _math(group, "ADD", quadratic, 0.1875)
|
||||
q = _math(group, "MULTIPLY", a, quadratic)
|
||||
k = _math(group, "ADD", structure, 0.25)
|
||||
square = _math(group, "MULTIPLY", k, k)
|
||||
cube = _math(group, "MULTIPLY", square, k)
|
||||
total = _math(group, "ADD", cube, q)
|
||||
root = _math(group, "POWER", total, 1 / 3)
|
||||
root_square = _math(group, "MULTIPLY", root, root)
|
||||
product = _math(group, "MULTIPLY", root, k)
|
||||
denominator = _math(group, "ADD", root_square, product)
|
||||
denominator = _math(group, "ADD", denominator, square)
|
||||
response = _math(group, "DIVIDE", q, denominator)
|
||||
profile = group.nodes.new("GeometryNodeInputNamedAttribute")
|
||||
profile.data_type = "FLOAT"
|
||||
profile.inputs["Name"].default_value = "o_balloon"
|
||||
displacement = _math(group, "MULTIPLY", profile.outputs["Attribute"], response)
|
||||
height = fields["thickness"]
|
||||
layer = _math(group, "DIVIDE", distance, height)
|
||||
layer = _math(group, "ADD", 1, layer)
|
||||
layer = _math(group, "MAXIMUM", layer, 0)
|
||||
layer = _math(group, "MINIMUM", layer, 1)
|
||||
displacement = _math(group, "MULTIPLY", displacement, layer)
|
||||
normal = fields["inflation_normal"]
|
||||
offset = group.nodes.new("ShaderNodeVectorMath")
|
||||
offset.operation = "SCALE"
|
||||
links.new(normal, offset.inputs[0])
|
||||
links.new(displacement, offset.inputs[3])
|
||||
result = group.nodes.new("ShaderNodeVectorMath")
|
||||
result.operation = "ADD"
|
||||
links.new(position, result.inputs[0])
|
||||
links.new(offset.outputs[0], result.inputs[1])
|
||||
enabled = group.nodes.new("GeometryNodeSwitch")
|
||||
enabled.input_type = "VECTOR"
|
||||
links.new(control.outputs["Front Inflation"], enabled.inputs["Switch"])
|
||||
links.new(position, enabled.inputs["False"])
|
||||
links.new(result.outputs[0], enabled.inputs["True"])
|
||||
return enabled.outputs[0]
|
||||
|
||||
|
||||
def build_front_normal(group, normal_weight):
|
||||
"""Smooth the projected surface normals that drive the inflation."""
|
||||
links = group.links
|
||||
normal = group.nodes.new("GeometryNodeInputNormal")
|
||||
blur = group.nodes.new("GeometryNodeBlurAttribute")
|
||||
blur.data_type = "FLOAT_VECTOR"
|
||||
links.new(normal.outputs[0], blur.inputs["Value"])
|
||||
links.new(group_input(group.nodes, {"Normal Smooth"}).outputs["Normal Smooth"], blur.inputs["Iterations"])
|
||||
normalized = blend_normal(group, normal.outputs[0], blur.outputs[0], normal_weight)
|
||||
return evaluate_field(group, normalized, "FLOAT_VECTOR", "POINT")
|
||||
return blend_normal(group, normal.outputs[0], blur.outputs[0], normal_weight)
|
||||
|
||||
|
||||
def build_shell_position(group, position, front, is_shell, fields):
|
||||
links = group.links
|
||||
def build_inflation_amount(group, controls, is_shell):
|
||||
"""Balloon scales the profile, Shell moves a fixed distance."""
|
||||
nodes, links = group.nodes, group.links
|
||||
thickness = _math(
|
||||
group, "MULTIPLY", controls.outputs["Thickness"], BALLOON_THICKNESS_SCALE,
|
||||
)
|
||||
balloon = _math(
|
||||
group, "MULTIPLY", read_float_attribute(group, "o_balloon"), thickness,
|
||||
)
|
||||
amount = nodes.new("GeometryNodeSwitch")
|
||||
amount.input_type = "FLOAT"
|
||||
links.new(is_shell, amount.inputs["Switch"])
|
||||
links.new(balloon, amount.inputs["False"])
|
||||
links.new(controls.outputs["Shell Thickness"], amount.inputs["True"])
|
||||
return amount.outputs[0]
|
||||
|
||||
def connect(value, target):
|
||||
if hasattr(value, "node"):
|
||||
links.new(value, target)
|
||||
else:
|
||||
target.default_value = value
|
||||
|
||||
def vector(operation, a, b):
|
||||
result = group.nodes.new("ShaderNodeVectorMath")
|
||||
result.operation = operation
|
||||
connect(a, result.inputs[0])
|
||||
connect(b, result.inputs[3 if operation == "SCALE" else 1])
|
||||
def build_solid(group, surface, amount, normal, is_shell, controls, weld_distance):
|
||||
"""Inflate the projected surface into a front leaf, wall and rear leaf."""
|
||||
nodes, links = group.nodes, group.links
|
||||
|
||||
def offset(distance):
|
||||
result = nodes.new("ShaderNodeVectorMath")
|
||||
result.operation = "SCALE"
|
||||
links.new(normal, result.inputs[0])
|
||||
links.new(distance, result.inputs[3])
|
||||
return result.outputs[0]
|
||||
|
||||
def y(value):
|
||||
result = group.nodes.new("ShaderNodeSeparateXYZ")
|
||||
links.new(value, result.inputs[0])
|
||||
return result.outputs["Y"]
|
||||
inflation = _math(
|
||||
group, "MULTIPLY", amount,
|
||||
_math(group, "MULTIPLY", controls.outputs["Front Inflation"], FRONT_INFLATION_SCALE),
|
||||
)
|
||||
front_amount = nodes.new("GeometryNodeSwitch")
|
||||
front_amount.input_type = "FLOAT"
|
||||
links.new(is_shell, front_amount.inputs["Switch"])
|
||||
links.new(inflation, front_amount.inputs["False"])
|
||||
front_amount.inputs["True"].default_value = 0.0
|
||||
|
||||
# Shell and the thin end of Balloon share the actual front normal.
|
||||
front_y = y(front)
|
||||
raw_y = y(position)
|
||||
distance = _math(group, "SUBTRACT", raw_y, front_y)
|
||||
normal = fields["front_normal"]
|
||||
thin_offset = vector("SCALE", normal, distance)
|
||||
shell_position = vector("ADD", front, thin_offset)
|
||||
front = nodes.new("GeometryNodeSetPosition")
|
||||
links.new(surface, front.inputs["Geometry"])
|
||||
links.new(offset(front_amount.outputs[0]), front.inputs["Offset"])
|
||||
|
||||
control = group_input(group.nodes, {"Thickness"})
|
||||
clamp = group.nodes.new("ShaderNodeClamp")
|
||||
links.new(control.outputs["Thickness"], clamp.inputs["Value"])
|
||||
t = clamp.outputs[0]
|
||||
radius = _math(group, "MULTIPLY", _math(group, "SUBTRACT", front_y, fields["center"]), t)
|
||||
rear = read_float_attribute(group, NORMAL_REDUCTION_ATTRIBUTE_NAME)
|
||||
rear_radius = _math(group, "MULTIPLY", rear, _math(group, "ADD", radius, distance))
|
||||
direction = vector("SUBTRACT", fields["normal"], (0, 1, 0))
|
||||
rear_shift = vector("SCALE", direction, rear_radius)
|
||||
axis_offset = group.nodes.new("ShaderNodeCombineXYZ")
|
||||
links.new(distance, axis_offset.inputs["Y"])
|
||||
full_offset = vector("ADD", axis_offset.outputs[0], rear_shift)
|
||||
span = _math(group, "ADD", amount, front_amount.outputs[0])
|
||||
span = _math(group, "MULTIPLY", span, -1.0)
|
||||
extrude = nodes.new("GeometryNodeExtrudeMesh")
|
||||
extrude.mode = "FACES"
|
||||
extrude.inputs["Individual"].default_value = False
|
||||
links.new(front.outputs["Geometry"], extrude.inputs["Mesh"])
|
||||
links.new(offset(span), extrude.inputs["Offset"])
|
||||
|
||||
# Smoothly reach the existing full-volume shape at Thickness >= 1.
|
||||
transition = group.nodes.new("ShaderNodeMapRange")
|
||||
transition.interpolation_type = "SMOOTHERSTEP"
|
||||
links.new(t, transition.inputs["Value"])
|
||||
mix = group.nodes.new("ShaderNodeMix")
|
||||
mix.data_type = "VECTOR"
|
||||
links.new(transition.outputs[0], mix.inputs[0])
|
||||
links.new(thin_offset, mix.inputs[4])
|
||||
links.new(full_offset, mix.inputs[5])
|
||||
balloon_position = vector("ADD", front, mix.outputs["Result"])
|
||||
choose = group.nodes.new("GeometryNodeSwitch")
|
||||
choose.input_type = "VECTOR"
|
||||
links.new(is_shell, choose.inputs["Switch"])
|
||||
inflated = build_inflation_position(group, balloon_position, distance, fields)
|
||||
links.new(inflated, choose.inputs["False"])
|
||||
links.new(shell_position, choose.inputs["True"])
|
||||
return choose.outputs[0]
|
||||
# Extruding against the normal leaves the rear leaf and wall facing inward.
|
||||
flipped = nodes.new("GeometryNodeFlipFaces")
|
||||
links.new(extrude.outputs["Mesh"], flipped.inputs["Mesh"])
|
||||
rear = store_float_attribute(
|
||||
group, flipped.outputs["Mesh"], NORMAL_REDUCTION_ATTRIBUTE_NAME, 1.0,
|
||||
)
|
||||
join = nodes.new("GeometryNodeJoinGeometry")
|
||||
links.new(front.outputs["Geometry"], join.inputs["Geometry"])
|
||||
links.new(rear, join.inputs["Geometry"])
|
||||
merge = nodes.new("GeometryNodeMergeByDistance")
|
||||
merge.mode = "ALL"
|
||||
links.new(weld_distance, merge.inputs["Distance"])
|
||||
links.new(join.outputs["Geometry"], merge.inputs["Geometry"])
|
||||
return merge.outputs["Geometry"]
|
||||
|
||||
|
||||
def _build_depth_surface(group, geometry, controls):
|
||||
"""Project the mesh and sample front-surface fields while shaping the shell."""
|
||||
"""Project the depth image and inflate the projected surface into a solid."""
|
||||
links = group.links
|
||||
split = controls.outputs["Depth Split"]
|
||||
image = controls.outputs["Depth Image"]
|
||||
@@ -314,40 +173,29 @@ def _build_depth_surface(group, geometry, controls):
|
||||
mode.inputs["Balloon"].default_value = False
|
||||
mode.inputs["Shell"].default_value = True
|
||||
is_shell = mode.outputs[0]
|
||||
selected_thickness = group.nodes.new("GeometryNodeSwitch")
|
||||
selected_thickness.input_type = "FLOAT"
|
||||
links.new(is_shell, selected_thickness.inputs["Switch"])
|
||||
links.new(controls.outputs["Thickness"], selected_thickness.inputs["False"])
|
||||
links.new(controls.outputs["Shell Thickness"], selected_thickness.inputs["True"])
|
||||
has_thickness = compare_node(group, "GREATER_THAN", selected_thickness.outputs[0], 1e-6)
|
||||
amount = build_inflation_amount(group, controls, is_shell)
|
||||
statistics = group.nodes.new("GeometryNodeAttributeStatistic")
|
||||
statistics.data_type, statistics.domain = "FLOAT", "POINT"
|
||||
links.new(projection, statistics.inputs["Geometry"])
|
||||
links.new(amount, statistics.inputs["Attribute"])
|
||||
has_thickness = compare_node(group, "GREATER_THAN", statistics.outputs["Max"], 1e-6)
|
||||
zero_shell = group.nodes.new("GeometryNodeSwitch")
|
||||
zero_shell.input_type = "GEOMETRY"
|
||||
links.new(has_thickness, zero_shell.inputs["Switch"])
|
||||
links.new(projection, zero_shell.inputs["False"])
|
||||
volume = build_volume_fields(group, rim)
|
||||
normal_weight = build_normal_weight(group, is_shell)
|
||||
normal = build_front_normal(group, normal_weight)
|
||||
front = build_front_fields(group, volume, normal, normal_weight)
|
||||
shell_fields = build_shell_fields(group, projection, volume, is_shell, normal_weight)
|
||||
shell = thicken_depth_surface(
|
||||
group, projection, shell_fields["thickness"], back_mask=NORMAL_REDUCTION_ATTRIBUTE_NAME, weld_zero_thickness=True,
|
||||
normal = build_front_normal(group, build_normal_weight(group, is_shell))
|
||||
# Both leaves must inflate along one normal, so it is carried across the
|
||||
# extrusion instead of being evaluated again on the moved surface.
|
||||
projection = store_vector_attribute(
|
||||
group, projection, SOLID_NORMAL_ATTRIBUTE, normal,
|
||||
)
|
||||
fields = {
|
||||
name: sample_field(group, projection, value, data_type, index=shell["source_index"])
|
||||
for name, value, data_type in (
|
||||
("center", volume["center"], "FLOAT"), ("front_normal", normal, "FLOAT_VECTOR"),
|
||||
("structure", front["structure"], "FLOAT"), ("inflation_normal", front["inflation_normal"], "FLOAT_VECTOR"),
|
||||
)
|
||||
}
|
||||
fields.update({
|
||||
name: sample_field(group, projection, value, "FLOAT_VECTOR" if name == "normal" else "FLOAT",
|
||||
index=shell["source_index"])
|
||||
for name, value in shell_fields.items()
|
||||
})
|
||||
oriented = build_shell_position(group, shell["position"], shell["front"], is_shell, fields)
|
||||
links.new(oriented, shell["position_input"])
|
||||
links.new(shell["geometry"], zero_shell.inputs["True"])
|
||||
return zero_shell.outputs[0]
|
||||
solid = build_solid(
|
||||
group, projection, amount, read_vector_attribute(group, SOLID_NORMAL_ATTRIBUTE),
|
||||
is_shell, controls,
|
||||
_math(group, "MULTIPLY", typical.outputs["Mean"], 1e-3),
|
||||
)
|
||||
links.new(solid, zero_shell.inputs["True"])
|
||||
return remove_attribute_pattern(group, zero_shell.outputs[0], "_o_*")
|
||||
|
||||
|
||||
def build_image_depth_cutout_group():
|
||||
@@ -369,9 +217,9 @@ def build_image_depth_cutout_group():
|
||||
boundary_smooth.default_value, boundary_smooth.min_value, boundary_smooth.max_value = 4, 0, 16
|
||||
boundary_smooth.description = 'Smooth geometry near the original outline and Depth Split edges. Zero disables boundary smoothing.'
|
||||
normal_smooth = interface_socket(group, 'Normal Smooth', 'INPUT', 'NodeSocketInt', options)
|
||||
normal_smooth.default_value, normal_smooth.min_value, normal_smooth.max_value = 64, 16, 512
|
||||
normal_smooth.default_value, normal_smooth.min_value, normal_smooth.max_value = 512, 16, 512
|
||||
normal_smooth.description = 'Control smoothing of front and rear displacement normals. Higher values produce smoother directions.'
|
||||
float_input(group, 'Front Inflation', 0.2, maximum=1, subtype='FACTOR', parent=options).description = 'Control how much the front surface bulges in Balloon mode. Zero keeps the original front surface.'
|
||||
float_input(group, 'Front Inflation', 0.2, maximum=1, subtype='FACTOR', parent=options).description = 'Control how much the front surface bulges in Balloon mode, reaching half of the balloon thickness. Zero keeps the original front surface.'
|
||||
float_input(group, 'Reference Depth', 1.0, subtype='DISTANCE', parent=options)
|
||||
data = group.interface.new_panel(name='Data', default_closed=True)
|
||||
float_input(group, 'Uniform Scale', 1.0, parent=data).hide_in_modifier = True
|
||||
|
||||
Reference in New Issue
Block a user