mjholtzem / Unity-2D-Destruction

MIT License
622 stars 98 forks source link

Issues with parents, scaling, and performance. Details inside. #12

Open clonzeh opened 5 years ago

clonzeh commented 5 years ago

I replaced a bunch of GetComponent calls to cache them, so it does less work during runtime.

Aside from that, I like this PoC, thanks for sharing it.

For those wanting to make use of it during runtime: -you must remove the line for removing unused assets. Instead, you can call it once every so often rather than once per call. to clean up memory leaks. -you must object pool the creation of the "pieces" so it doesn't call several new GameObjects() and AddComponents<>() in a single frame.

some flaws I've experienced: -If a parent object has a different scale than the object holding the graphics it all goes terribly wrong. -If a parent object is controlling the sprite's flip with a negative scale, it also goes wrong.

I've fixed these issues by using lossyscale in various areas.

clonzeh commented 5 years ago

in case anyone else stumbles across it heres some fixes that I have found:

(Same applies to Triangle methods) inside GenerateVoronoiPieces 1) store the originalscale local scale in a separate vector3, set the origScale = the source.lossyscale at the bottom of the same method, set the source localscale to 1) inside GenerateVoronoiPieces set the piece localscale to the source lossyscale inside calcUV

change the textHeight and texWidth to divide by losscale instead of localscale.

Lastly, inside: the getRect(GameObject source) method, set the return to: (unless you're not using spriterenderer)

private static Rect getRect(GameObject source)
    {
        SpriteRenderer sr = source.GetComponent<SpriteRenderer>();//.sprite.bounds;

        //return new Rect(source.transform.localPosition - bounds.extents, bounds.size);
        return new Rect(sr.bounds.extents.x * -1, sr.bounds.extents.y * -1, sr.bounds.size.x * 2, sr.bounds.size.y * 2);
    }
ColorTwist commented 5 years ago

Thanks for the info. My major issue with Unity-2D-Destruction is that it cannot be inside a prefab as prefab does not save the meshes. I thought to use the runtime option. Currently, with the original version of Unity-2D-Destruction, there is a slight delay and it is not smooth enough. With your changes, does it work properly?

clonzeh commented 5 years ago

I made quite a few changes but I did not intend to push changes from the beginning, only to get it working. So my code is spaghetti'd in to my project :(

But yes, I got it working perfectly. The main performance hit during runtime is the creation of gameobjects and components. So if you create a new class that handles object pooling, slowly add in like 30-40 Gameobjects with all the necessary components and use those for the "shards" that split off.

There's also a case where you need to do Mesh.Clear() I forgot, but I just remember that being necessary.

ColorTwist commented 5 years ago

Thanks, appreciate your feedback.

martin-braun commented 5 years ago

I really want to use this package by myself, too. But I want to use it on objects that are pooled, so it needs a way to return the object and its pieces back to original state instead of creating any garbage. This and all your changes would be perfect for me, @ClonzEh. Performance is a big concern to me, honestly.