Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Added BrushFactory.CreateFromPlanes and BrushFactory.CreateFromPoints #314

Merged
merged 2 commits into from
Mar 22, 2021

Conversation

Henry00IS
Copy link
Collaborator

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() or CenterAndSnapPlanes() fails. I am unsure of the reason why.
MergeTest.zip

mergeillustration

@LogicalError LogicalError self-requested a review March 22, 2021 08:14

// build a very large cube brush.
surfaceDefinition.EnsureSize(6);
CreateBox(ref brushMesh, new Vector3(-4096, -4096, -4096), new Vector3(4096, 4096, 4096), in surfaceDefinition);
Copy link
Member

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

Copy link
Collaborator Author

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);
Copy link
Member

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

Copy link
Collaborator Author

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.

@LogicalError
Copy link
Member

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.
@Henry00IS Henry00IS merged commit 6f8afac into RadicalCSG:master Mar 22, 2021
@Henry00IS Henry00IS deleted the ConvexHull branch March 22, 2021 17:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants