Polygon rasterization in Unity
I need this to modify the terrain splat map under a building when it is placed.
What is the simplest way to rasterize a polygon (convex) in Unity like shown on the image?
Answer by Bunny83 · Mar 09, 2020 at 02:57 AM
I'm not sure what exactly you're asking here. The image on the right is not just a rasterized polygon. It's already antialiased. Just rasterizing a polygon is rather simple. Performing antialiasing is a bit more complex.
The general idea is to just iterate through all pixels in the AABB of your polygon and check if the pixel is inside or outside the polygon. For antialiasing there are several techniques possible. A common one is super sampling (so you just pretend there are more pixels (double, quadruple) and you just average the result of those sub pixels). Other approaches will just sample the same polygon several times but with slight offsets in each direction.
You may want to read through Spatial anti-aliasing. Of course technically you could just use Unity's rendering engine and your GPU to render the polygon to a rendertexture and read the result back. However the tricky part would be to actually combine the result into your splat map. Also the rendering overhead and read-back would probably be worse for performance.
Thanks, I went with the super sampling approach, calculating 16 samples per each splat-map pixel.
It's still a mystery for me though what that If statement does that checks that point is inside the polygon.
Your answer
Follow this Question
Related Questions
User drawing game in unity (2d) 0 Answers
Drawing, Writing on screen. 0 Answers
How to make a drawing look better? 1 Answer
Why is drawing things in unity so difficult? Or is it me? 0 Answers
Which coordinate does GL line use? 0 Answers