Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add RegularPolygon and Circle meshes #3730

Closed
wants to merge 25 commits into from

Conversation

rparrett
Copy link
Contributor

@rparrett rparrett commented Jan 20, 2022

Objective

Bevy users often want to create circles and other simple shapes.

All the machinery is in place to accomplish this, and there are external crates that help. But when writing code for e.g. a new bevy example, it's not really possible to draw a circle without bringing in a new asset, writing a bunch of scary looking mesh code, or adding a dependency.

In particular, this PR was inspired by this interaction in another PR: #3721 (comment)

Solution

This PR adds shape::RegularPolygon and shape::Circle (which is just a RegularPolygon that defaults to a large number of sides)

Discussion

There's a lot of ongoing discussion about shapes in bevyengine/rfcs#12 and at least one other lingering shape PR (although it seems incomplete).

That RFC currently includes RegularPolygon and Circle shapes, so I don't think that having working mesh generation code in the engine for those shapes would add much burden to an author of an implementation.

But if we'd prefer not to add additional shapes until after that's sorted out, I'm happy to close this for now.

Alternatives for users

For any users stumbling on this issue, here are some plugins that will help if you need more shapes.

https://github.com/Nilirad/bevy_prototype_lyon
https://github.com/johanhelsing/bevy_smud
https://github.com/Weasy666/bevy_svg
https://github.com/redpandamonium/bevy_more_shapes
https://github.com/ForesightMiningSoftwareCorporation/bevy_polyline

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Jan 20, 2022
@alice-i-cecile alice-i-cecile added A-Math Fundamental domain-agnostic mathematical operations C-Enhancement A new feature and removed S-Needs-Triage This issue needs to be labelled labels Jan 20, 2022
@rparrett
Copy link
Contributor Author

rparrett commented Jan 20, 2022

Another thing to note is that this is generating meshes like the pentagon on the right side: (edit: fixed this)

regularpolymesh

Doing it like what is shown on the left would result in fewer triangles, but I'm not sure if there are any graphical implications with "tiny thin triangles" when using this to draw circles or with UVs or how much we should be concerned about that otherwise.

@rparrett
Copy link
Contributor Author

Now generating the "optimized" mesh like the left side in the diagram above.

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small nits, but otherwise this looks great. I'd missed this one!

examples/README.md Outdated Show resolved Hide resolved
@TheRawMeatball
Copy link
Member

Personally I'd prefer if the choice of which triangulation method to wasn't hard coded, but could be selected at runtime

@alice-i-cecile
Copy link
Member

@bevyengine/rendering-team; do y'all have opinions on the triangulation strategy?

@StarArawn
Copy link
Contributor

StarArawn commented Feb 4, 2022

@bevyengine/rendering-team; do y'all have opinions on the triangulation strategy?

I personally prefer using something like SDF's for circles as they can scale much better and you don't have to be concerned with sides, but otherwise this seems fine to me.

@StarArawn
Copy link
Contributor

Personally I'd prefer if the choice of which triangulation method to wasn't hard coded, but could be selected at runtime

I think if someone wants a different triangulation method they can just build their own mesh?

@alice-i-cecile
Copy link
Member

I personally prefer using something like SDF's for circles as they can scale much better and you don't have to be concerned with sides, but otherwise this seems fine to me.

Agreed, but this is a solid stop-gap.

@HackerFoo
Copy link
Contributor

I use recursive triangulation, like this:

Screen Shot 2022-02-04 at 12 43 19

It can produce narrow triangles, but they'll be at the edges anyway. It's a pretty simple algorithm, recursively splitting the list of vertices into thirds. It's only valid for convex polygons, though. I can submit a PR if desired.

@rparrett
Copy link
Contributor Author

rparrett commented Feb 4, 2022

@HackerFoo

I only had a vague notion of "long triangle thin triangle bad" to go on, and I couldn't really dig up any concrete information and the (limited) testing I did wasn't revealing. So I just went with fewest triangles / fastest mesh generation.

I'm assuming you had a pretty good reason to implement that recursive strategy in your own app? Seems worthwhile to me if it actually solves some sort of problem.

@alice-i-cecile
Copy link
Member

I'm assuming you had a pretty good reason to implement that recursive strategy in your own app? Seems worthwhile to me if it actually solves some sort of problem.

The recursive algorithm is much easier to extend to arbitrary polygons. That's probably a minor bit of useful future-proofing here, but not essential. I'm unsure if there are other benefits though.

@HackerFoo
Copy link
Contributor

I did this while I was working on UV mapping. I can't remember if I saw artifacts, but I was using the fan method (on the left of the diagram above) and circles were ugly when looking at them in Blender, so I fixed it. I like the recursive approach because I love recursion, but also because if you draw the triangles from largest to smallest with this method, you get an increasingly accurate approximation of the polygon. So the lowest quality triangles contribute the least to the shape.

It's probably best to stick with the simple fan until it's at least an annoyance, though.

@superdump superdump added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Mar 4, 2022
sides: 6,
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a hexagon the default? Does it make sense to provide a default?

uvs.push([0.5 * (c + 1.0), 1.0 - 0.5 * (s + 1.0)]);
}

let mut indices = Vec::with_capacity((sides - 1) * 3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will crash for 0 sides.

Copy link
Contributor Author

@rparrett rparrett Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think is the most appropriate way to handle that situation?

if sides < 3 {
    panic!(/* ... */)
}

Or clamping the value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now protected by a debug_assert. Still a panic, but it doesn't really make any sense to try to build a polygon with fewer than 3 sides, and now there's a friendly error message.

crates/bevy_render/src/mesh/shape/regular_polygon.rs Outdated Show resolved Hide resolved
@adsick
Copy link
Contributor

adsick commented Mar 27, 2022

"Bevy users often want to create circles and other simple shapes." who would have thought?))) I mean the engine is like 1.5 years old and still doesn't have a beginner-approachable drawing API. I love Bevy but can't use it because of that...

@Nilirad
Copy link
Contributor

Nilirad commented Apr 12, 2022

@adsick You can use bevy_prototype_lyon until Bevy gets a proper drawing API.

@adsick
Copy link
Contributor

adsick commented Apr 13, 2022

@adsick You can use bevy_prototype_lyon until Bevy gets a proper drawing API.

I know that, looked at the examples, but it seems to me that immediate mode API is more intuitive.

@superdump
Copy link
Contributor

bors r+

bors bot pushed a commit that referenced this pull request May 5, 2022
# Objective

Bevy users often want to create circles and other simple shapes.

All the machinery is in place to accomplish this, and there are external crates that help. But when writing code for e.g. a new bevy example, it's not really possible to draw a circle without bringing in a new asset, writing a bunch of scary looking mesh code, or adding a dependency.

In particular, this PR was inspired by this interaction in another PR: #3721 (comment)

## Solution

This PR adds `shape::RegularPolygon` and `shape::Circle` (which is just a `RegularPolygon` that defaults to a large number of sides)

## Discussion

There's a lot of ongoing discussion about shapes in <bevyengine/rfcs#12> and at least one other lingering shape PR (although it seems incomplete).

That RFC currently includes `RegularPolygon` and `Circle` shapes, so I don't think that having working mesh generation code in the engine for those shapes would add much burden to an author of an implementation.

But if we'd prefer not to add additional shapes until after that's sorted out, I'm happy to close this for now.

## Alternatives for users

For any users stumbling on this issue, here are some plugins that will help if you need more shapes.

https://github.com/Nilirad/bevy_prototype_lyon
https://github.com/johanhelsing/bevy_smud
https://github.com/Weasy666/bevy_svg
https://github.com/redpandamonium/bevy_more_shapes
https://github.com/ForesightMiningSoftwareCorporation/bevy_polyline
@bors bors bot changed the title Add RegularPolygon and Circle meshes [Merged by Bors] - Add RegularPolygon and Circle meshes May 5, 2022
@bors bors bot closed this May 5, 2022
robtfm pushed a commit to robtfm/bevy that referenced this pull request May 10, 2022
# Objective

Bevy users often want to create circles and other simple shapes.

All the machinery is in place to accomplish this, and there are external crates that help. But when writing code for e.g. a new bevy example, it's not really possible to draw a circle without bringing in a new asset, writing a bunch of scary looking mesh code, or adding a dependency.

In particular, this PR was inspired by this interaction in another PR: bevyengine#3721 (comment)

## Solution

This PR adds `shape::RegularPolygon` and `shape::Circle` (which is just a `RegularPolygon` that defaults to a large number of sides)

## Discussion

There's a lot of ongoing discussion about shapes in <bevyengine/rfcs#12> and at least one other lingering shape PR (although it seems incomplete).

That RFC currently includes `RegularPolygon` and `Circle` shapes, so I don't think that having working mesh generation code in the engine for those shapes would add much burden to an author of an implementation.

But if we'd prefer not to add additional shapes until after that's sorted out, I'm happy to close this for now.

## Alternatives for users

For any users stumbling on this issue, here are some plugins that will help if you need more shapes.

https://github.com/Nilirad/bevy_prototype_lyon
https://github.com/johanhelsing/bevy_smud
https://github.com/Weasy666/bevy_svg
https://github.com/redpandamonium/bevy_more_shapes
https://github.com/ForesightMiningSoftwareCorporation/bevy_polyline
exjam pushed a commit to exjam/bevy that referenced this pull request May 22, 2022
# Objective

Bevy users often want to create circles and other simple shapes.

All the machinery is in place to accomplish this, and there are external crates that help. But when writing code for e.g. a new bevy example, it's not really possible to draw a circle without bringing in a new asset, writing a bunch of scary looking mesh code, or adding a dependency.

In particular, this PR was inspired by this interaction in another PR: bevyengine#3721 (comment)

## Solution

This PR adds `shape::RegularPolygon` and `shape::Circle` (which is just a `RegularPolygon` that defaults to a large number of sides)

## Discussion

There's a lot of ongoing discussion about shapes in <bevyengine/rfcs#12> and at least one other lingering shape PR (although it seems incomplete).

That RFC currently includes `RegularPolygon` and `Circle` shapes, so I don't think that having working mesh generation code in the engine for those shapes would add much burden to an author of an implementation.

But if we'd prefer not to add additional shapes until after that's sorted out, I'm happy to close this for now.

## Alternatives for users

For any users stumbling on this issue, here are some plugins that will help if you need more shapes.

https://github.com/Nilirad/bevy_prototype_lyon
https://github.com/johanhelsing/bevy_smud
https://github.com/Weasy666/bevy_svg
https://github.com/redpandamonium/bevy_more_shapes
https://github.com/ForesightMiningSoftwareCorporation/bevy_polyline
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
# Objective

Bevy users often want to create circles and other simple shapes.

All the machinery is in place to accomplish this, and there are external crates that help. But when writing code for e.g. a new bevy example, it's not really possible to draw a circle without bringing in a new asset, writing a bunch of scary looking mesh code, or adding a dependency.

In particular, this PR was inspired by this interaction in another PR: bevyengine#3721 (comment)

## Solution

This PR adds `shape::RegularPolygon` and `shape::Circle` (which is just a `RegularPolygon` that defaults to a large number of sides)

## Discussion

There's a lot of ongoing discussion about shapes in <bevyengine/rfcs#12> and at least one other lingering shape PR (although it seems incomplete).

That RFC currently includes `RegularPolygon` and `Circle` shapes, so I don't think that having working mesh generation code in the engine for those shapes would add much burden to an author of an implementation.

But if we'd prefer not to add additional shapes until after that's sorted out, I'm happy to close this for now.

## Alternatives for users

For any users stumbling on this issue, here are some plugins that will help if you need more shapes.

https://github.com/Nilirad/bevy_prototype_lyon
https://github.com/johanhelsing/bevy_smud
https://github.com/Weasy666/bevy_svg
https://github.com/redpandamonium/bevy_more_shapes
https://github.com/ForesightMiningSoftwareCorporation/bevy_polyline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Math Fundamental domain-agnostic mathematical operations A-Rendering Drawing game state to the screen C-Enhancement A new feature S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants