Yo vvvvellas!
I decided to write a little devblog here for stuff I'm adding to my packs, because as you could notice I'm not too good with keeping you guys updated about what's going on around my contribs, not anymore! so for start:
newest addition to mp.essentials is 2 rectangle packing nodes most obviously used for texture packing to an atlas with 2 different algorithms. RectPack (2d Czachurski) and RectPack (2d ChevyRay). Versions are actually carrying the name of the guys whom I ripped of.
The more advanced and most of the cases better algorithm is provided by Patryk Czachurski here: https://github.com/TeamHypersomnia/rectpack2D I ported his code to C# and you can see the result with 413 random rectangles in the above screenshot. Each rectangle has sides between 250-1250 pixels and the Czachurski node packs them into ~15080×15074 pixels sized space. This algorithm also provides multiple sheets (or bins or slices) of atlasses if the input rectangles wouldn't fit in the desired box size. Here is an example with textures using 8448×8832 pixels:
The other algorithm provided by Chevy Ray Johnston here: https://github.com/ChevyRay/RectanglePacker might be easier to use, less intelligent and you don't have to specify a box size.
However notice it is not at all as efficient as Czachurski in terms of filling the space with lots of rectangles. Same random rectangles are using 22845×24232 pixels. It does a slightly better job at mostly square shaped textures though (here 8448×8704 pixels)
you can get it here: md.ecosystem.mp through vpm. Enjoy!
Immediately after writing this post I've noticed some differences between my results and the results at Czachurski's repo and I've noticed that apparently C++ sort is the reverse of C# LINQ sort. Now the Czachurski node produces results are replaced now they are more similar to the original ones.
anonymous user login
~4d ago
~4d ago
~6d ago
~9d ago
~17d ago
~17d ago
~21d ago
~21d ago
~21d ago
~23d ago
yeah!
devblog! so very much appreciated..keep'em coming! and who's next?
Glad that you've found my algorithm useful!
That said, I must apologize because that code is an abomination. Fortunately, I don't write like that anymore. If you've managed to port it to yet another language, you must be truly a magician with an arcane capability.
@Patryk Czachurski: Nah it was totally readable and straightforward code. Here's my C# implementation: https://github.com/microdee/mp.essentials/blob/master/src/transform/CzachurskiRectanglePackerNode.cs
For my education what you consider bad in your code?
Mostly variable and function names.
Not only do they lack any convention, they are also completely devoid of meaning.
"bool fill" and "delcheck" when they should at least indicate that the function marks the nodes for repeated usage (instead of their allocating and deallocating) - I would name them "bool to_be_reused" and "mark_for_reuse_recursively"; "pnode" when I don't even recall what did the "p" stand for; "_rect2D" when it should at least indicate that it does a packing attempt; lack of const guarantees where applicable; usage of an exception-unsafe, dynamically allocated array for "rect_xywhf** ordefuncs" where an std
"fits" function shall not return a generic integer in the range of 0-4 meaning of which is briefly mentioned in a comment, but rather an enumerable type which duly describes each and every result, e.g. fitting_result
FITS, fitting_result::FITS_PERFECTLY_FLIPPED - comments lie, code does not.
That said I am sort of afraid to correct it all because... it's been working for my game for several years now, lol.
It is a funny feeling to see one's own code rewritten into another language.
Hello,
If you are still interested, I have completely rewritten the rectpack2D library to use a more efficient algorithm.
Check out the example results, they are even better!
https://github.com/TeamHypersomnia/rectpack2D#example-results
The README.md also has a detailed description of the algorithm, if you'd wish to implement it yourself in some other language.
@Patryk Czachurski: niiiice! and thanks for the reminder, I need to freshen up a bit my texture atlas handling too so yup still interested ;)