Is there a recommended way to rasterize 2D shapes (curves, polygons etc) to a texture in Unity that works in the web player? It might be possible to use the mono System.Drawing dll, but that requires libgdiplus which I’m guessing is not included? And System.Drawing is pretty crappy anyway, so a more modern and competent API would be nice.
(There are no plans from UT to allow binary extensions to the web player, right? Otherwise I might be able to get AGG 2.4 working, using the creativedocs.net wrapper)
Your only way is to use a single texture where you paint in all 2D operations.
Then you appy them and use that texture on a fullscreen guitexture object
thats the only realistic way that will not kill the whole performance.
Don’t come up with the idea of locking the backbuffer, that will not work without killing the whole performance (locked backbuffer = no 3d operation can be done at all anymore)
Thanks, but what’s the actual 2D drawing API to use, say, if I want to draw a gradient-filled ellipse (preferrable with different options for anti-aliasing)?
If you can triangulate your shapes, it might be best to do so and let the video card render your shapes as tri strips, etc.
I have done some tests using Bresenham’s Circle algorithm (Mid-point circle algorithm) coupled with his line algorithm to draw filled circles for visual feedback.
Drawing this to texture and updating the texture was fast, but not quite fast enough to really use for responsive feedback. Depends what you are drawing though.
As a note, doing this with small texture sizes at a time instead of one large texture might actually be in your favor, as you reduce the pixel copy operations, and Unity only has to update a handful of textures at a time. This likely only applies if you are only changing a few textures at a time though.
You may get some of the System.Drawing classes to work, but not likely. They also have a lot of overhead, especially if you just want basic functionality.
Check the wiki, as there is an SVG implementation there that may provide some handy algorithms.
As I understand it, it’s extremely complex to create a generalized tesselizer, but maybe there are some libraries out there I’ve missed.
I seem to remember that AGG’s Maxim Shemanarev was head-hunted to write such a library, and if they needed a guy like him to pull it off, I don’t stand a chance…
Thanks - the SVG thingy could be very interesting, although its development seems to have stopped in pre-alpha phase.
It comes as a .unityPackage - what is that? Don’t have Unity here, so I can’t open it. Will it run in the browser plugin, or could there be native code in there?
The .cs files looked like they were easy to extract from the .unityPackage file, but I decided against the “unknown format?! parse it!” path and installed the trial.
Didn’t try the tutorial yet - need to learn the IDE better first - but it seems like it’s a port of some C++ or Java lib, but with no anti-aliasing if I read the code correctly (is that right?). Also, there’s no license, and if it indeed is a port, then the original’s license is probably still valid, which is unsettling in case it’s a viral one.
It’s actually not that bad if you’re just doing 2d shapes. Google for “polygon triangulation” and you’ll find various methods with differing trade-offs and complexity. It can be done in a single function call where you pass in your polygon and get back some triangles.
Now, doing all that in script… I’m not sure how fast it would be.
If it was as simple as polygons only… Curves, ellipses, with/without gradients and textures need to be handled too. Proper anti-aliasing makes it even more complex.