-
Notifications
You must be signed in to change notification settings - Fork 33
Added BrushFactory.CreateFromPlanes and BrushFactory.CreateFromPoints #314
Conversation
… with the quick hull algorithm.
|
||
// build a very large cube brush. | ||
surfaceDefinition.EnsureSize(6); | ||
CreateBox(ref brushMesh, new Vector3(-4096, -4096, -4096), new Vector3(4096, 4096, 4096), in surfaceDefinition); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
until we find a good way to find the bounds of the plane intersections, if we have a version of this method that takes a bounds, then we can just use the bounds of all the brushes that are being merged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added a bounds parameter. This completely replaced the hard-coded range limit and improved the accuracy.
|
||
// calculate a convex hull out of the points. | ||
ConvexHullCalculator convexHullCalculator = new ConvexHullCalculator(); | ||
convexHullCalculator.GenerateHull(points, ref planes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... or just take the bounds of all the points used in the convex hull
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the ConvexHullCalculator creates planes describing a convex hull, I made it efficiently calculate and output the resulting bounds. This is then passed into the CreateFromPlanes() function.
At some point we might want to convert all this to burst compilable code, so we can use it in the generators, which will also all become burst compilable eventually. But we don't need to worry about that in this PR |
…arameter. While the ConvexHullCalculator creates planes describing a convex hull, it also efficiently calculates the resulting bounds. CreateFromPoints() passes on the precomputed bounds to CreateFromPlanes(), so it just works without an additional parameter. The comments have been changed to provide some guidance on working with planes of unknown bounds.
BrushFactory.CreateFromPlanes
An official function to create a convex polyhedron out of a set of planes. Realized with the technique of cutting up a huge box using the given planes. The current algorithm only works within a limited range of +-4096 world units from the world center. If possible, operations should be performed near the center of the world for optimal accuracy. In the future it may be possible to cut up a box with infinite dimensions to get around the size constraint or replace this algorithm entirely. But it would be good to at least have an official method available in the API.
BrushFactory.CreateFromPoints
This function forms the basis for the ability to merge selected brushes when they form a convex shape, as shown in #313, by generating a convex hull using the quick hull algorithm. The clipping planes are derived from the convex hull and a brush is created using the method described above. Other use-cases include generating a brush between one or more selected faces or randomized rocks out of a random set of points (also useful for nodes).
The quick hull algorithm
ConvexHullCalculator
was taken from https://github.com/OskarSigvardsson/unity-quickhull with the MIT license left intact in the source code accordingly. Many changes have been made to remove unnecessary calculations and streamline variables to data types that Chisel uses.Here is a small example script that you can add to the Assets directory. You can select two brushes and merge them using the Henry->MergeTest menu. There are several edge cases on complex brushes, where either
BrushMesh.Cut()
orCenterAndSnapPlanes()
fails. I am unsure of the reason why.MergeTest.zip