# How can I aggregate instance properties with geometry nodes?

I'm trying to use ray-casting to generate some geometry. I have this working by generating a grid of points, emitting a ray into some target geometry (Suzanne in this example), and offsetting the position of the grid points to their hit location:

This basically produces the following algorithm:

for each vertex v in grid_geometry:
origin = v.position + vec3(0,0,-100)
is_hit, hit_position = RayCast(target_geometry, origin, vec3(0,0,100));
if (is_hit):
v.position = hit_position
else:
delete v


Which in turn produces the following output:

To produce a smoother result, I'd like to emit multiple rays per grid point, and take the minimum hit distance of the set of rays for each instance. I've created a smaller set of points to cast rays from, have made this sample pattern an instance on the original grid's points:

The algorithm I'd like to run is basically the following:

for each vertex v in grid_geometry:
min_hit = vec3(0,0,9999);
any_hit = false
for each vertex vv in sample_geometry:
origin = v.position + vv.position + vec3(0,0,-100)
is_hit, hit_position = RayCast(target_geometry, origin, vec3(0,0,100));
if (is_hit):
min_hit = min(min_hit, hit_position)
any_hit = true
if (any_hit):
v.position = min_hit
else:
delete v


Unfortunately, I can't figure out how to encode this algorithm in geometry nodes. I've tried the following and a variety of similar graphs, but all seem to produce zeros for the output field equivalent to v.position + vv.position.

$$\hspace{15pt}$$If I understood correctly, you want to do multiple raycasts per vertex using vertices of another geometry as offsets to the raycast origin, and then get the result of the one with the smallest distance.

$$\hspace{15pt}$$To do the above you can convert the offsets geometry into a single spline and instantiate it in every point of the source geometry. After Realizing the spline instances, raycast for every point and then, for every spline, sort its points by hit distance and get the final result from the point with the first sorted index. Now that every spline has a final result, just index sample it to the grid using the index of the vertices.

Conversion of offsets mesh to spline:

Points with hit:

Splines with at least one point with hit:

Single raycast:

Smallest distance of multi raycast:

• The raycast node has its Source Position input disconnected. Does this mean it’s tracing rays from the points of Suzanne and not the sample grid? Oct 23, 2023 at 18:55
• @MooseBoys The Raycast node's source position input uses by default the position of the current context in the current domain, which is the points of the instanced splines, as you can see by the Capture Attribute node chain which is operating in the point domain of the geometry containing the splines. Oct 23, 2023 at 21:20
• What is it about "curves" that makes them work as instances while "mesh" doesn't work? They both have "points" domains. Oct 26, 2023 at 0:36
• @MooseBoys with curves you can use the Points of Curve node to sort its points by raycast hit distance, thus being able to get the point which had the closest hit for each curve. Oct 26, 2023 at 2:14