I have a question that a Magma Flow guru might be able to answer.
I want to test inside an object using Magma flow only. I know you can test using the Pflow op, but I’m trying to cut PFlow out, due to its unreliable nature.
So far I have managed to test using Nearest point, and SurfData Value, but that only gives me a shell defined by distance around a target object. I would like to have everything contained within a mesh object.
Hopefully this is possible? If not I might have to resort to PFlow?
If the particles are in a PRT Loader, you can cull using its integrated culling methods - just pick a named selection set with one or more meshes in it and the particles will be deleted if inside or outside the volume.
If you want to go the MagmaFlow route, it won’t be as precise, but it can work in most cases.
Create a GeometryInput node and pick your objects.
You take the position of the particle and convert it to world space.
Then you perform a NearestPoint lookup with the Geometry and World Position
Get the Position of the Nearest Point using a SurfData operator (when using Krakatoa 1.6 and earlier).
Subtract the Position of the particle from the Position of the Nearest Point.
Normalize the result of the subtraction to get a vector with length 1.0.
Get the Face Normal of the Nearest Point with another SurfData operator.
Calculate the Dot Product of the two vectors (the normalized subtraction of positions and the Normal at the surface).
Compare the Dot Product result with 0.0 using a LessOrEqual operator.
Convert the logical output of the LessOrEqual operator ToFloat.
Output the resulting value as Selection channel.
Drop a Krakatoa Delete modifier above the KCM.
I have a demo scene, a saved flow and a BlackOp saved with the necessary nodes, but I will leave it to you to try and build it yourself. See if you can get it to work.
Btw, I used the worst mesh in the universe (a teapot which has a lot of degenerate faces and is not really closed) and it had some issues around the spout until I increased the segments to 16. Then it culled perfectly. With nice convex hulls it should work quite reliably.
Bobo your a genius!!
Works! I hadnt thought to use the normals aswell. It goes a little nuts on cubes, or anything with a 90 degree corners, but i can live with that.
Would ray intersect be more accurate? How does nearest point differ?
The Culling in PRT Loader kind of uses the ray intersect approach. It shoots a ray to the center of a random face, and then another one. If the two results agree (the point is “seeing” the two faces from the back side), the point is assumed inside. If the two disagree, a third one is shot to break the tie. There are places in concave meshes where a point could be seeing the front of a face while being inside the volume.
The Dot Product method with the nearest point is a bit faster and less precise since it uses the closest point on surface. NearestPoint returns the point that is closest to the specified position by analyzing the geometry around via the kd-tree acceleration structure. So it always returns the point on the face that is nearest. In some cases, the nearest point could be at the edge of two faces and the normal returned could be of the other face pointing away from the particle.
The Ray Intersection method seems to work better with a box - take the center of the box (using a Script Input node, e.g. $Box001.center) and use it as the target to shoot at. Subtract the Particle WS position from the Center. Shoot a ray from the particle position in the direction defined by the previous step’s subtraction and get the position and the normal at the hit point. Compare the Dot product of the Normal at the hit point with the normalized vector defined by the hit point and the particle position and do the selection like before.
This works pretty well with convex hulls like a Sphere or Box, but might fail with some more complex meshes like a Teapot.
At that point, combining the two methods with a LogicalOr can clean up the results - the ray intersection will clean up the box case, while the nearest point will work better on the Teapot.
Not sure what you mean, both Ray Intersection and Nearest Point use the same kd-tree acceleration structure to find the point on the surface, so they should perform about the same.