This was done using a script; however, could there be a way to achieve this effect with geometry nodes?
This is my solution. I filled an icosphere with lots of small cubes. It would work the same with other objects:
I tried two different setups. One works with intersection, the other uses a Raycast node inspired by the comment of @Kuboa in a slightly different way.
This is the setup, that uses intersection:
How does it work
I will first describe the solution, that uses intersection. After that, I will describe the solution, that uses the Raycast node.
My strategy is to create a grid and scan the body from bottom to top. Doing this, I create the intersection between the grid and the volume for every position of the grid. After doing this, I simply instantiate cubes at every point of the resulting geometry.
Here we go step by step: The size for the scanning grid and its resolution are given as input parameters. I used a size of 3 with a resolution of 0.1 (0.03 for the rendered image) in every direction, while the icosphere has a radius of 1. The number of vertices of the grid are calculated by dividing the grid size by the resolution.
In order to scan from bottom to top, a mesh line with the given resolution is created:
Then I create instances of the grid along this grid line:
The resulting instances are realized and intersected with the sphere:
Finally, I create cube instances of the size defined by the resolution on every point of the intersection. In order to prevent artifacts, the geometry of the sphere is excluded by selecting only those points, that have the same position as the closest point of the generated stack of grids.
This solution starts with the same grid, as the solution before:
Then, cubes are generated on those points:
Only those points will be used for instancing, that are inside the Ico Sphere:
Let’s have a look inside the group node “Is Point in Object?”
Here we need some vector algebra, to understand what's going on. Subtracting the vector of a point P1 from the one of a point P2 results in a vector pointing from P1 to P2. This is used to create a ray starting at the point given by the group input and pointing into the direction of the closest point of the target object.
The ray will point inside the object, if the starting point is outside and vice versa. The normal of the point, that is hit on the object will always point outside the object. This means, the starting point of the ray is inside the object, if the angle between the ray and the normal of the point, that is hit on the object, is less than 90°. The dot product of two vectors is greater than 0, if their angle is less than 90°. Thus, the starting point is inside the object, if the dot product between the direction of the ray and the normal is greater than 0.