Search Unity

Destructible 2D ☄️ Dynamic Sprite Destruction

Discussion in 'Assets and Asset Store' started by Darkcoder, May 29, 2014.

  1. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414

    Besides the component documentation (D2dQuadFracturer) there's no specific documentation about this feature. For now you can check the demo scenes that use it though, and examine how they work.

    There is no pooling implemented for the destructible objects because they can all have very different component setups that can change over time, and managing this would be difficult.

    Handling small destruction using generic debris is a good idea, the code right now would need to be modified to handle this though (e.g. D2dDestructible.cs:TryLocalSplit & TrySplit). I'll see if I can add it to the next version.
     
  2. barjed

    barjed

    Joined:
    May 9, 2013
    Posts:
    70
    Hi,

    I am on Unity 5.5.7 and it seems that the examples attached to the project do not work. For example, the Glass example looks like this:



    This is true for all the examples attached to the project. Any ideas about what's wrong? Thanks!
     
  3. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Last time I saw this bug it was caused by the project's serialization setting causing some sort of conflict. You can change this in Edit/Project Settings/Editor -> Asset Serialization. Try changing to Force Text before freshly importing Destructible 2D.
     
  4. barjed

    barjed

    Joined:
    May 9, 2013
    Posts:
    70
    It is set to 'Force Text'. It was set as such when the asset was first imported. I tried the usual 'delete the Library' and see if it helps but no cigar. Still broken :(
     
  5. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    I'll try and replicate the issue. What do you mean by Unity 5.5.7 though? I see no such version in the download archive.
     
  6. barjed

    barjed

    Joined:
    May 9, 2013
    Posts:
    70
    Sorry, my bad. That was a mistype. I am on Unity 5.5.3p4 (64-bit).
     
  7. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    I tested it and there is indeed an issue. However, I was only able to reproduce the issue with 'Force Text' enabled before importing Destructible 2D. You can avoid this by changing to 'Mixed' or 'Force Binary' before importing, then changing back after if you so desire.

    The Destructible 2D asset itself is developed and uploaded using 'Force Text', so I'm not really sure if there's anything I can do to fix this.
     
  8. qqqbbb

    qqqbbb

    Joined:
    Jan 13, 2016
    Posts:
    113
    Bug in "Breakable Glass" scene: the sound of breaking glass plays even if you disable D2dSpawner script component. And the volume of the sound is too loud.
     
  9. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    The D2dSpawner component isn't what triggers the sound, so enabling or disabling it doesn't matter. It's triggered by the D2dDestructible -> OnStartSplit (inside Show Events). I guess it would make sense to check if it's disabled inside the Spawn method though, thanks.
     
  10. morvi

    morvi

    Joined:
    Feb 8, 2014
    Posts:
    10
    Hello,
    first of all I want to say sorry, if I am asking something that was already asked before (my problem seems to be so general that I actually cannot imagine I am the first), but I really didn't find anything about.

    OK, so I am trying to build up a simple map consisting of 2 different sprites: 1 pure dirt/sand sprite and 1 with grass on top - thanks to Kenney by the way :)

    I want the sprites with grass on top to be dynamically splittable, while the others are static. But when I put it all together, I notice a gap in the grass sprites when I set them to dynamically splittable. If I set them to static, they fit seamlessly with the other tiles.

    I played around and around (with blurring, etc), searched through the example screens, the FAQs ... I didn't find an example like that with my map.

    What am I doing wrong? To make it more clear, I add screenshots of scene and game view with 3 grass tiles - the 2 upper tiles are dynamic (splittable and solid) and the 1 below is static. As you can see, the dynamic ones show gaps, the static one doesn't.

    Thank you so much in advance! Scene-View_Sprites fit perfectly.JPG Game_View_Gaps.JPG
     
  11. morvi

    morvi

    Joined:
    Feb 8, 2014
    Posts:
    10
    OK,, so far I understood that the Rigidbody2D component, which is necessary for dynamic sprites. causes a slight movement of the sprites. So it's no issue of Destructible2D. Had anyone a similar setup and got it solved somehow?

    Thanks a lot!
    Morvi
     
  12. Niels-Paladin

    Niels-Paladin

    Joined:
    Nov 26, 2012
    Posts:
    6
    Hey there!

    I just bought the tool and I imported the 2.1.2 version into Unity 2017.2.0f3. Opened the Glacier example scene. And when running it seems to create new edge colliders for every damage and does not remove any. Probably not intended behaviour.

    Screen Shot 2017-10-20 at 11.11.50.png

    Can you help me with this?

    Thanks
    Niels
     
    peetron likes this.
  13. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    The gaps can be caused by a number of things. If I make a sprite like this:



    Where the whole texture is just one tile, and the width/height are a square number like 256 and the 'Pixels per Unit' neatly divides into it like 128, then I don't notice any gaps when placing the tiles in increments of 2 units. Even when I make them static or dynamic splittable.

    You can easily get gaps if these numbers don't neatly align though, or if the sprite is part of an atlas with transparent padding, or if the tiles are very far away from the origin.


    I emailed you a quick fix. There's another issue I want to fix before I submit a new version though, so I'll do that next week as soon as possible.
     
  14. bkg23

    bkg23

    Joined:
    Jul 8, 2017
    Posts:
    2
    Hello there,
    Does there happen to be any possible way for a sprite to be fully destroyed on start? I would like to be able to heal a sprite from nothing.
    I would greatly appreciate your help on this. Thank you.
     
  15. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Sure, just set your destructible object up normally, and set the Heal Tex to the same as the Main Tex. Then duplicate the Main Tex asset itself, open it up this duplicate in your favourite image editing program, delete all the pixels so it's just all transparent, save, then drag and drop this new clear (but same resolution) texture into your destructible's Replace -> Alpha With sprite or texture slot, and it should now start fully destroyed, and be able to be healed to the normal state.
     
  16. bkg23

    bkg23

    Joined:
    Jul 8, 2017
    Posts:
    2
    Awesome man, Thank you so much.
    One more question. Is there any reason why, when I replace the alpha, unity's performance lowers significantly. Is this a Optimization problem or anything that you know of?
     
  17. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    If you previously called 'Optimize Alpha', then replacing the alpha will undo this and revert back to its full resolution state. To fix this you can call 'Optimize Alpha' again as many times as you like to get back to the previous resolution, or resize the sprite/texture you're using to replace to your ideal size.
     
  18. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    Greetings,

    First off, I must say I am very impressed with things that this plugin could achieve! Explored the plugin for 2 weeks and it has so many potential use cases.

    I encountered an issue for my project though, that is:

    If I use a hair sprite and set it to Destructible, then play the game, then when I make a first click on the sprite, the pixels on the border of sprite are suddenly changed into something that looks more solid, but my expectation is that the pixels on the border shouldn’t be changed.

    You can view this video (link: https://www.dropbox.com/s/g6h8i8oujni1o1d/d2d-issue.mov?dl=0) to understand my issue further.

    I tried to reset alpha, or set sharpness to 1, or change shader to Destructible 2D Keep Alpha / Diffuse / Default, the result is still the same.

    If I set the Auto Split option to None, the issue will not happen and stamping will still work, but then the image won’t be split into parts.

    Though, if I change to the image with solid pixels on image’s border, like the tree image or duck image in your demo, or square photo, the issue will not happen.

    Is there a solution for this issue? Kindly let me know if you need more info, my sincere thanks in advance!
     
  19. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    This behaviour is a result of how the splitting algorithm works. The algorithm converts all pixels into a 1 or 0 based on if the opacity is above 127 (50%), and then it floodfills each pixel with a 1 until there are none left. If more than one floodfill was performed to complete this, then the destructible object contains multiple 'islands' of solid pixels that aren't connected. The original destructible object is then duplicated many times and each clone given the data from each of these islands. The reason why your alpha data changes like this is because the edges of your hair texture are very smooth, and this smoothness extends across many many pixels, and is far outside of any island borders using this 1/0 conversion. The splitting algorithm does take into account the underlying alpha data, because you may notice the example scenes don't produce jagged binary split edges, but this underlying alpha usage only extends to one pixel beyond the island's border, which works well for mostly solid with slightly smooth edged textures, but clearly not for overly smooth textures like yours.

    The way to fix this is to not cut the islands off when they reach the point below 127 opacity, but to instead make them go all the way to the border between that island and the next one, or at least to an opacity of 0. Another way to think of it is to 'grow' each island out until they can no longer grow. The reason why I didn't implement the algorithm like this is because I couldn't think of a fast way to implement this. When I have more free time I could try implementing it though, as I think a naive approach could be quite easy to implement.
     
    alvaris924 and bkg23 like this.
  20. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    Truly thanks for your such detailed explanation! It's really great to know how underlying algorithm works, and potential solutions for the issue, such an eye opener!

    For possible fixes you mentioned, I wonder if these fixes will influence the cutting precision, like the cutting will not work like what’s shown in demos, with perfect precision?

    Also, I am thinking if you're free to work on the mentioned solution at the moment, or the fixes will be included in further updates?

    Besides, where will be the code that does the splitting algorithm you mentioned, so I can try on the solutions too.

    I also noticed that you're the developer of Paint in 3D plugin, which I also bought to use in my project. I'm such a huge fan of your plugins :D

    All in all what I planned to do is to use Paint in 3D plugin’s feature to erase part of a texture, then if the texture looks like 2 different pieces that are not connected to each other (like image shown in this link: https://www.dropbox.com/s/xl3ok6l8plttx2f/erased-texture.jpg?dl=0), I will need to split the texture into 2 gameobjects. Will that be possible with Paint in 3D alone (maybe with some plugins or Unity features that I missed)?

    My sincere thanks in advance for your further thoughts!
     
  21. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    Hi, I am thinking what will be your thoughts on my previous posts, since I hope to solve the issue we discussed.

    Truly sorry for disturbing you if you're in busy period, and my heartfelt thanks in advance for your reply!
     
  22. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Sorry, I forgot to reply.

    There will be no precision issues with the proposed algorithm, in fact the result will at worst just be the same. Performance would be an issue though, but it depends on how optimized I can make it. The floodfill algorithm is very optimized using some neat tricks, and these may be applicable here too.


    I want to add it as soon as possible, but I have a large backlog of things to do. I'll send you a build as soon as I can.


    Thanks! What you describe here is exactly what D2D was designed for. While calculating a split like that in P3D would more or less be the same code, the big difference is that D2D only works with flat sprites, whereas P3D can work with any arbitrary mesh, so an algorithm that works with that would be incredibly complex.
     
  23. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    Truly thanks for your further detailed reply and thoughts!

    That will be great, so I will be looking forward to the new build with potential fixes and algorithms, let’s hope the result will be positive and well, and my sincere thanks in advance for your hard work!

    I am actually using P3D for 2D textures, particularly for erasing texture, like using Eraser tool in Photoshop. So I somehow thought if there is a way for me to use P3D to erase part of texture, and use certain feature to check if erased texture looks like 2 different pieces that are not connected to each other, then split the texture into 2 game objects, guess I will need to figure out this part lol.
     
  24. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    As a small update I did spend some time last week extending the floodfill algorithm to include this island expansion code, and while it works for some scenarios, there are many scenarios where it doesn't work well, so I reverted the code. I have a new idea for how to make it work though, but I'm currently in the middle of updating 2 other assets, so I'll attempt this once those are done.
     
  25. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    My sincere thanks for your works and trials indeed, truly appreciated!

    And sure, I'm certainly looking forward to the potential fixes, meanwhile if you think the method could work for my scenario but not all scenarios, kindly send the scripts/build for me to test, since I'm totally fine that if the particular solution can solve my particular issue, thank you and happy holidays!
     
  26. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    Hi Darkcoder,

    Hope you had a great new year celebration! :)

    I’m writing to ask about the progress of the issue we discussed before, how’s the progress so far? It’s fine that if I could have the build with any potential solution that could solve the issue, even if the solution has performance issue, truly thanks! :D
     
  27. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    I haven't had time to work on this asset again yet. This holiday season has been very busy for me! I'll update you as soon as I have news.
     
  28. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    Many thanks for your response Darkcoder. Sure, truly hope that there could be a solution soon, as it's almost 50 days from this issue is reported, my sincere appreciation in advance!
     
  29. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    I've implemented most of the new splitting code. Here's a preview of it:



    left = original smooth destructible
    middle = current behaviour (one pixel border)
    right = new variable pixel border (set to 5)

    I still need to make it work with the local splitting, and fix some small bugs, but it seems to work well.
     
  30. connerfritz

    connerfritz

    Joined:
    Feb 9, 2014
    Posts:
    4
    I was wondering if the asset is capable of applying a custom effect / outline to only the cut edges? I noticed in a lot of the asset screenshots there's a sort of glow effect on the cut edges (the lightbulbs, the joint blocks) but don't see it mentioned specifically in the feature lists.
     
  31. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Those highlights were added in photoshop to illustrate what happened more clearly, and aren't actually a feature of the asset. Using the asset as-is, you can kind of create this effect by using the provided outline shader with a smooth brush, but the results won't be the same. To get the same effect there would have to be some additional data written when slicing the objects that would then be rendered by the shader. I plan to add this in a future version, as it allows for some really interesting things, but I can't promise it will be in too soon.
     
  32. connerfritz

    connerfritz

    Joined:
    Feb 9, 2014
    Posts:
    4
    Good to know, thanks!
     
  33. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    That's truly great to see Darkcoder, the image looks very promising and hopeful that I'm so glad to see it :)

    Sure, I will be looking forward to the update then, I am a bit curious that will the fixes be done in a week?

    Again, my sincere thanks for your hard work and dedication, I truly bought the right asset, supported by right developer. :D
     
  34. hieu9102002

    hieu9102002

    Joined:
    Dec 13, 2017
    Posts:
    2
    Don't know you've answered this before, but how can you make your own explosion a destroyer? Right now I'm using the default Red Explosion but want to use other explosion prefabs. Thanks
     
  35. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Hopefully, just have to fix some weird bugs.


    You can just add the D2dExplosion component to your prefabs and it will work. You can also duplicate the 'Red Explosion' and modify its settings for your custom prefab.
     
  36. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    Hey Darkcoder,

    I tried the latest version, and previous issue we encountered together were indeed solved (with Split Feather value between 15 and 20), my sincere thanks to you on this! :D:D

    I am facing a weird issue though, that is if I put up my previous hair image, then applied ‘Reset Alpha’, then applied ‘Optimize Alpha’ for once, then upon clicking on the hair, some tiny hairs around the outer hair will be split automatically.

    You can observe this issue by refer to this from 00:05 mark to 00:07 in this video: https://www.dropbox.com/s/n08a8mr7vl93gno/tiny-hair-issue.mov?dl=0.

    Oddly, if I performed ‘Optimize Alpha’ again, the issue won’t happen, but then the hair image resolution will be too low for me, and the stamped border will look too blurred.

    As a side note, the more I apply ‘Optimize Alpha’, the less tiny hairs around the outer hair will be split.

    I tried to change the Split Expand value from -1 to 1~300, and Split Pixels Min from 1~200, but the result is still the same.

    I also tried the Destructible2D’s Default, Diffuse and KeepAlpha shader, but no luck so far.

    Here is the hair image (link: https://www.dropbox.com/s/6v7g7cmnf8ys9nc/white-male_front_hair@2x.png?dl=0) that you can download and test with.

    I look forward to your thought on this, thank you very much!
     
  37. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Interesting, this looks like a bug somewhere.

    I'll have a look mid next week when I have free time. Thanks for pointing it out!
     
  38. Anto1882

    Anto1882

    Joined:
    Feb 4, 2018
    Posts:
    3
    Hi Darkoder,

    I bought your package and it is great! thanks a lot for your work.

    I faced one problem: when one object is splitable, the children also become splitable. Maybe it would be great to define how the children should behave (behavior might be different from parent).

    for example:

    The first object is destructible and splitable. When splitted, the children inherit these properties. But then when those children are splitted, their children become indestructible and non splitable. Maybe we even want to destroy them after 3 seconds by enabling the right script (which already exists).

    The first object has the property "canbesplitted" = 3
    each time the object is splitted, their chidren inherit the same parameter minus 1. When it reaches 0, the object can't be splitted anymore.

    All the best
     
  39. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    D2dDestructible originally had this setting, but in practice it doesn't make sense when it's so simple. For example, if you have a large boulder that you want to be split in half just once, then you could set 'canbesplitted = 3', but if there's a tiny explosion or slice right at the edge that takes off just a few pixels this would be technically splitting the object and cause it to now become invincible, despite visually looking like a bug in your game.

    The new approach to do this in a more clever way is the D2dRequirements component. Here you can specify the 'Alpha Count Max = 100', and link the 'On Requirements Met' event with the D2dDestructible's enabled = off setting. This will disable the destruction of any piece that goes below 100 solid pixels. The asteroids in the 'Spaceship' demo scene show something similar.

    Now that I try it I notice you can't set things like 'Indestructible' from these events, so I'll add that to the next version.
     
  40. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    Thank you very much! Looking forward to see what we can do with this. :)
     
  41. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    Hi Darkcoder, hope you had a great weekend! have you been looking into issue? Thank you! :)
     
  42. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    I sent you a private message with a new build using a new feather algorithm, and it seems to work with your test image.

    Keep in mind your image begins with hundreds of tiny pixel islands around the edges, so it may not produce split results you expect. You can see these islands by editing your image in Photoshop or similar and thresholding it above 127 alpha. I recommend you first remove these islands by tweaking the image so it starts as one big island.
     
  43. Luis_m

    Luis_m

    Joined:
    Jan 7, 2014
    Posts:
    2
    Hi Darkoder,

    i´ve look at the documentation of the plugin, and i can´t figure out how to acces to the "cloned" or "splited" objects.

    What i want is to set the opacity of the sprite (and its splited pieces) and then destroy them. I can do it succefully with the "main" sprite calling the "On End Split Event". But i dont know how to acces to the splited pieces of the sprite.

    Regards.
     
  44. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    The OnEndSplit event is called once on the original destructible object. This event passes the List<D2dDestructible> type, which stores a reference to the current object and all split/cloned children. So to modify the opacity of the children you must listen for this event and loop through all children and modify their alpha. The last item in the list will be the original destructible.
     
  45. alvaris924

    alvaris924

    Joined:
    Aug 3, 2011
    Posts:
    35
    Hi Darkcoder, I am truly sorry for my late reply as I was handling some emergency issues, sincerely thanks for sending me a private build for test!

    I imported my previous issue scene into the latest build, though it seems like the issue displayed in video I showed still persists.

    I am curious about this:

    Though I am a Photoshop user myself, I am not very sure abiyt the steps to edit my image and thresholding it above 127, can I know the steps to do so? This is so I can remove those islands myself in Photoshop, my further thanks!
     
  46. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    1 - Open your example image.
    2 - Ctrl + click the base layer in the Layers tab.
    3 - Go to the Channels tab, and click the 'Save Selection as Channel' button at the bottom.
    4 - Click on the new alpha channel to select it.
    5 - Go to Select/Desect.
    6 - Go to Image/Adjustments/Threshold, and select 128 -> OK.

    If you zoom in and look at your newly thresholded alpha channel, you can see there are tiny islands everywhere, especially around the edges. Having islands at the edges in an image like this is a big issue because all that smooth feathering will then be grouped into the tiny islands, because those islands are closer to the feathering than the main hair/island parts.
     
  47. furneauxhugo

    furneauxhugo

    Joined:
    Mar 3, 2017
    Posts:
    2
    Hi, was wondering if you could help. I've bought the asset and we are trying to slice a square. We first of all fire the squares (so they are moving), then when we slice the square we want the remaining pieces to jump away from each other - like they are breaking apart.

    However, we can't seem to get this right especially when the square is rotating. Any help would be really appreciated. Thanks a lot
     
  48. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    The easiest way to achieve this is after slicing, you raycast left and right along the slice line at set intervals, and apply a force to anything hit. This should work in most scenarios, but if you slice next to an object (but not slicing it) then it will still apply a force to it, which may look wrong. To fix this you would have to hook into the slice events and only apply forces to the split parts, this makes the code a bit complex though. If the first solution is fine then I could send an updated D2dDragToSlice component soon?
     
  49. furneauxhugo

    furneauxhugo

    Joined:
    Mar 3, 2017
    Posts:
    2
    Thanks we'll try that and let you know how it goes
     
  50. Exonto

    Exonto

    Joined:
    Nov 22, 2017
    Posts:
    16
    Hello,

    I recently purchased Destructible 2D and it's working great so far, except I can't figure out how to prevent tiny pieces from being split off a large object. I tried increasing the "Split Pixels Min" to larger and larger values (eventually reaching 500) and there is no difference. I still end up with super tiny pieces being chopped off. My split expand is -1 and split feather is 1 in case that matters.

    Thank you.