Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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,404
    1 - Yes, 400k AlphaCount is too high for a fully splittable sprite. Do you have a picture of the sprite in question? If it's something like a level sprite then you could use 'Local' splitting, instead of 'Whole' splitting, which makes splitting small parts off sprites very fast, but doesn't work for such large areas. You can also create your object from lots of smaller parts and glue them together, and optimize each part individually.

    2 - Fracturing only works on the whole sprite, but if you use 'Local' splitting mentioned above, then you can fracture the locally split part to get both fast splitting and fast fracturing of just the split part.


    Yes, just remember that if the sprite changes shape then it may not work so well. For example if you have a character walking animation where the arms move, destroying the arm will not cause the arm to be destroyed in the rest of the animation frames, because there's no way to know what or where the arm is in the other frames. As long as your animation just does minor movements, or changes things that don't matter (e.g. color) then it should work fine.
     
  2. Emphy

    Emphy

    Joined:
    Feb 7, 2014
    Posts:
    33
    Is there some kind of 'best practice' regarding sprite resolution and alpha count for (fully splittable) sprites? I was messing around with a 640x640 circle sprite and one 2048x2048 circle sprite. So inherently they have a lot of transparent pixels in the corners. I've brought down the alpha count to 15k-ish for both (via Optimize Alpha), but that makes the stamping pretty 'inaccurate'. By that I mean that the star stamp doesn't look much like a star anymore with a low alpha count. Between using a 'low-res' and 'high-res' sprite I couldn't detect much difference in performance. But when a lot is going on, with a quad fracturer fracturing multiple times quickly after each other, it tends to lag (a lot) with anything that has an alpha count of > 10k-ish. But with a low alpha count the edges look very jagged and not very pretty.

    In the Explode-example all the sprites like the fish are around 128x128 which is maybe a bit too low for my taste. But keeping the stamping figure intact, the alpha count needs to be reasonably high... I'm not sure what may cause the lag when stamping, is it rebuilding the collider? I can imagine detecting islands etc is expensive, but it's also when I've disabled splitting, in which case it would only be rebuilding the collider? Typically I set it to size 64 so that should work ok right?

    You also mentioned glueing parts together, which I understand. But.. When I make a Destructible game object and add a child that is also Destructible (like a building standing on a piece of terrain), when the terrain Destructible is split (either locally or as a whole) it copies the game child object as well. So when the terrain breaks off, the part that breaks off also gets a building of its own :p So, curious how you would compose such a scene. My end goal is a planet circle sprite with trees etc placed on the surface which can all be stamped/fractured. And in the end upon enough damage the planet should fracture/explode as well, not in 100 chunks but perhaps 20 or so and maybe break a bit when pieces hit eachother.

    Sorry for the long post, hope you have some valuable addition to these problems/questions :)
     
  3. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    While optimising the alpha does reduce the resolution of your destruction, keep in mind that it acts as a 'signed distance field', so with carefully designed media the destruction can still be quite high quality. Or to put it another way, you should try and avoid using stamps that have a sharp contrast between white and black.

    To check performance you can disable colliders, or look at the profiler. But yes, rebuilding colliders is what takes most of the time, alongside splitting.

    You can glue a terrain together by making an empty GO and adding a bunch of child destructibles. Since they're children, any splits won't cause the others to get modified, though splitting wouldn't work on the boundaries.

    If you have a medium sized planet I recommend you make the planet core solid, but add destructible terrain segments and objects on top. If it's taken enough damage then you could split the core using some other kind of animation that runs fast. Trying to make the whole thing destructible is probably going to be impractical to optimise to say the least.
     
  4. TrickShinobi

    TrickShinobi

    Joined:
    Aug 31, 2016
    Posts:
    17
    Hi there,
    I am using the Destructible2D fragment feature, where an object is being dissected. In the editor and on Android devices it all works out fine. But once I run it on a iOS device, something is wrong. The Debugger doesn't spit any error out. I have the feeling it is the shader Keep-Alpha I am using. But I am not sure what to change.
     
  5. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Strange, I'll send you some alternative shaders in a few days.
     
    TrickShinobi likes this.
  6. TrickShinobi

    TrickShinobi

    Joined:
    Aug 31, 2016
    Posts:
    17
    Do you have any recommendations for the iOS Player Settings in Unity? Like Static Batching, Dynamic Batching, GPU Skinning, Auto Graphics API (GL2, GL3, Metal), etc.
     
  7. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Destructible objects can't be batched (because they require unique AlphaTex data), so you can set those to whatever you like. The built-in shaders are designed to work with Shader Model 2 and up, so you should be able to use any graphics API you want.
     
  8. TrickShinobi

    TrickShinobi

    Joined:
    Aug 31, 2016
    Posts:
    17
    Ok, got it!

    I can elaborate a little bit on the iOS problem I still have:

    I fractured one sprite into 3 pieces. And it only shows one of them correctly. The others are just transparent although they should be visible. It works on all Android devices so far (S5, S2), on my Windows laptop in the Unity editor and on my Macbook in the Unity editor. But once I deploy it on an iOS device (iPad 2 for now), it won't show 2 of the 3 fractured pieces. Any idea? I tried changing to a different Destructible2D Material and Shader. But I went through all of them without any progress.
     
  9. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    I sent you an updated Keep Alpha shader that removes some old code.

    However, if none of the other materials/shaders work then I'm not really sure what the issue is, as the shader itself is very simple. It's possible using Metal (if you are) has some kind of issue with the way I update the destructible materials, but I'm not sure yet. I'll try testing on my iOS devices tomorrow.
     
  10. TrickShinobi

    TrickShinobi

    Joined:
    Aug 31, 2016
    Posts:
    17
    Hi there, thanks for the Shader. On iOS devices it now shows the whole object correctly at first, but once it splts into fragments it just splits into exact clones of the whole object.


    In the Unity editor it fragments correctly, but looks like this though:
    Bildschirmfoto 2016-09-22 um 12.09.29.png
    There are some spikes, on the corners which shouldn't be there.

    Also: When I create fragmentable objects that are round (sphere), they appear oval or only a piece of them is being shown. Tested all shaders and materials. (Only iOS devices again, Unity editor and Android works fine)
    Hope you can help! Thank you :)
     
    Last edited: Sep 22, 2016
  11. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Sorry for the delay, I updated my iOS dev license earlier and I'll be testing it shortly!
     
  12. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    I tested the latest version of D2D on my iPhone 6 and iPad Air 2 and didn't notice any issues with the features you described. Can you give me more specific details about your project settings, etc?
     
  13. TrickShinobi

    TrickShinobi

    Joined:
    Aug 31, 2016
    Posts:
    17
    Hey thanks for testing. I figured it out! :)
    I reinstalled the entire package and it suddenly worked.
    Thank you!
     
    Last edited: Sep 23, 2016
    Darkcoder likes this.
  14. TrickShinobi

    TrickShinobi

    Joined:
    Aug 31, 2016
    Posts:
    17
    Hi Darkcoder,

    I have another small question: I am trying to make a Destructible Object change its color (Mesh Color). But if I change it with an Animator, it remains the same color and won't change. I figured the Mesh Color ist just being set to the same value (1,1,1,1) back again. How do I manipulate it? I tried changing it in D2dDestructible.Color, but that wouldn't work either in runtime. Thank you! :)
     
  15. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    You must call the 'UpdateMesh' method if you update the color field directly. From code you edit the Color property, which sets the color field and calls this method, but you can't modify properties from animations.
     
    TrickShinobi likes this.
  16. sheepjeffery

    sheepjeffery

    Joined:
    Nov 3, 2016
    Posts:
    3
    not good at English


    can u tell me how to fix this?

    version 2.0.7 destructible 2d
     

    Attached Files:

  17. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Try setting Rigidbody2D -> Collision Detection -> Continuous
     

    Attached Files:

    • ccd.png
      ccd.png
      File size:
      51.2 KB
      Views:
      847
  18. kurismakku

    kurismakku

    Joined:
    Sep 18, 2013
    Posts:
    66
    Can this work fine without using Unity physics and colliders?
     
  19. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Yes, all the main features work without colliders or rigidbodies.
     
  20. CyberLobster

    CyberLobster

    Joined:
    Nov 13, 2016
    Posts:
    1
    Hi!
    Thank you for great asset! Im really enjoing this purchase :)

    Can it work with playmaker?
    Or how can i change color of an object (in D 2d Destructable (script)) on every collision (would be great to filter by tag) but not the first split?

    Playmaker can "Call method", but it needs "Method name" and "Parameters".
    Sorry i cant code yet :)

    Thank you in advance and i apologize for my language.
     
  21. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Hey,

    There's no method to change the color, but there is a property in the D2dDestructible script called 'Color' that allows you to change it. Does PlayMaker not have the ability to call properties? If not, I can add a method to do this.

    To change the color of your object each time it gets hit, you first need to write a script (or PlayMaker FSM) that has this feature. You can then use the D2dCollisionHandler's 'OnImpact' event to call your color change method (or activate the FSM). To make the color only change after the first split, you could disable the D2dCollisionHandler component, and enable it from the 'OnStartSplit' event.

    Let me know if you have any issues with implementing this.
     
  22. stnp

    stnp

    Joined:
    Nov 22, 2016
    Posts:
    2
    Hi!

    I am thinking about buying your extension but I am unsure if it will work in my use case, could you give your opinion?

    I am planning a side-view game in which you can mine the ground (think Dungeon Keeper in side-view). Here is an example mock up: http://i.imgur.com/xlof8IP.jpg
    Is your extension performant enough to support mining like that on a map approx twice as big as a typical Worms map with about 20 AI agents + several NPC characters? What should I keep in mind when tuning for performance?

    Thanks for your input!

    Dietmar
     
  23. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Hey,

    In your mockup, it looks like the level is made from blocks with rounded edges. In this scenario it is best to use a tilemap system, because that gives you very fast digging speed, rendering fidelity, and AI performance, but of course it limits you to blocks. Destructible 2D is more useful for games where you can freely dig or destroy scenes/objects. You can also combine the two, for example the tree in your mockup would look bad if it was cut down using blocks, but it would look good with Destructible 2D, so you could simply place this on top of your grid tilemap terrain.
     
  24. stnp

    stnp

    Joined:
    Nov 22, 2016
    Posts:
    2
    Thanks for the quick reply! Here is another example: http://i.imgur.com/6ZOGBHB.jpg
    I am guessing this one would be too elaborate for the Destructible 2D extension? I did not plan for a tilemap system because I would like to support diagonal ramps and a more natural looking environment.
    EDIT
    Last question :) Is Destructible 2D compatible with Ferr2d? Could I design the level and use shaders from Ferr2d and let it be destructible with your extension?
     
    Last edited: Nov 22, 2016
  25. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Elaborate shapes like this aren't an issue for Destructible 2D (after all it is pixel based destruction), but doing it on this scale with such high detail may not be possible on most machines, not to mention the pathfinding code would have to be done by you from scratch. A scene very much like that could probably be done with a good tilemap though, it's not like each tile must be on or off like in minecraft.

    No, it's not compatible with Ferr2D.
     
  26. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    614
    Can i make the slicing super thin?
     
  27. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    You can slice down to 1 pixel, but if your slice is rotated then 2 pixels would probably give you better results. Just keep in mind the actual size of 1-2 pixels depends on the resolution of your textures relative to their scale.
     
  28. a-tauzzi

    a-tauzzi

    Joined:
    Dec 20, 2015
    Posts:
    33
    Can someone give an hint how to create an alphatex? Adobe Illustrator is fine for this?

    I need to create an AlphaTex that is a combination of the AlphaTex of two different images, just like it is done in the Animation scene (Bee and Bee Flap are combined).
     
  29. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    The bee AlphaTex was created by loading the first frame, then loading the second frame on top of the first on a separate layer, then saving them together as a single png with transparency. I also stripped the RGB channels, but this isn't required, or you can do this via the Unity texture importer by making the image single channel/Alpha 8, then just drop it into the Replace -> Alpha With.
     
  30. a-tauzzi

    a-tauzzi

    Joined:
    Dec 20, 2015
    Posts:
    33
    Using an alpha tex that is a combination of two frames (alternating in the animation) would I get rid of the bad effect I see in this video ?


    My animation is done in the Update method using the

    dsr.ReplaceTextureWith(animalSprites [index]);
     
  31. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    I forgot to mention that the Bee example uses the 'Keep Alpha' shader/material. This shader combines the alpha of the AlphaTex and the current MainTex, which should avoid the issue you see there. Simply select the MeshRenderer for your snake and change the Material -> Element 0 to 'Keep Alpha' and it should work.
     
  32. a-tauzzi

    a-tauzzi

    Joined:
    Dec 20, 2015
    Posts:
    33
    Definitely better, even if the snake tail seems cut in one of the frames.



    P.S: How should I setup the alphatex using the code? If I try to call function ReplaceAlphaWith, it expects a strange type named D2dSnapShot and if I declare a public D2dSnapShot member I cannot drop my AlphaText on it.
     
    Last edited: Jan 12, 2017
  33. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Are your snake sprites different sizes? If so, one will stretch/shrink when it gets changed, causing the issue you see there. Make sure the source images are the same size, and the sprite rectangle itself is exact the same, like the bee ones.


    ReplaceAlphaWith has five overloads, the snapshot one is just the first. You probably want: ReplaceAlphaWith(Sprite sprite)
     
  34. coshea

    coshea

    Joined:
    Dec 20, 2012
    Posts:
    319
    Sorry if this has been asked already, but does this work with 2dtoolkit sprites?

    Thanks :)
     
  35. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    No, only Unity sprites.
     
  36. SoBiT

    SoBiT

    Joined:
    Dec 5, 2012
    Posts:
    44
    Last edited: Feb 5, 2017
  37. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Same answer as above, unless you can bake the terrain into a sprite or texture. Trees are no problem though if they're normal sprites or textures.
     
  38. SoBiT

    SoBiT

    Joined:
    Dec 5, 2012
    Posts:
    44
    I decided to buy your asset :) I like it so far.

    However, I recognized that when I have multiple sprites overlapping each other, and if I set the stamp size to x=3,y=3 it takes about a second to perform the action. I tried to scale the stamp to x=5,y=5 and it became even worse.

    Is the performance issue because of the scaling (say if I would use a larger texture for stamping and scale it to x=1,y=1 would it increase performance)? Or is it because of the amount of pixels being stamped? Or is it because there are multiple colliders being stamped?

    How can I increase performance best?

    • Make 1 sprite out of the complete level (terrain)?
    • use a larger stamp texture?
    • or a combination of both?
     
  39. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Last time I profiled the code, I found that damaging overlapping sprites was slow simply because of the amount of colliders that need updating, so to optimize it you'd have to reduce the AlphaTex resolution, simplify the colliders, or prevent them from overlapping as much. I can check the code again though.

    Changing the scale for the same visual result won't change anything, as you'll still be modifying the same amount of pixels.
     
  40. matthewbrand

    matthewbrand

    Joined:
    Jan 9, 2017
    Posts:
    25
    I just bought the Destructible 2d plugin. I am creating a sprite with this tutorial:


    But the version that I have looks a little different, and in the D 2d Destructible script I don't see a collider type. How do I make the sprite a collider without this option?
     
  41. matthewbrand

    matthewbrand

    Joined:
    Jan 9, 2017
    Posts:
    25
    Oh I see, you use Preset: Static!
     
    Darkcoder likes this.
  42. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Yeah, the video is a bit outdated. I'll upload a new one when I get around to making more videos!
     
  43. matthewbrand

    matthewbrand

    Joined:
    Jan 9, 2017
    Posts:
    25
    Great, thank you! Great plugin!!!
     
    Darkcoder likes this.
  44. kikk95

    kikk95

    Joined:
    Apr 27, 2016
    Posts:
    1
    Good afternoon. I recently bought your plugin. Very slows down on android.
    I use D2dDestructible.StampAll on Gameobject.
    Tell me how to improve performance for android devices?
    I use Unity 5.6.0f3.
    Thank you.
     

    Attached Files:

  45. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    The performance of StampAll depends on how many destructible objects are in the stamp area, the AlphaTex pixel count of each of those objects, and the features those destructible objects have (e.g. collider, local/whole splitting).

    The easiest way to improve performance is to just select the 'Optimize Alpha' setting in the D2dDestructible context menu (as noted in the FAQ.pdf) one or more times. You can also use lower resolution colliders, design your game so that destructible objects are less likely to be so close together, or design your game so the stamping isn't done on such a large scale.
     
  46. matthewbrand

    matthewbrand

    Joined:
    Jan 9, 2017
    Posts:
    25
    I am encountering a bit of a delay when I use StampAll.

    My setup: I have a bullet sprite that hits a terrain sprite, and detects the hit with OnCollisionEnter2D here:
    Code (CSharp):
    1.  void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         if (collision.collider.tag == "Ground" && !_hittingGround)
    4.         {
    5.             _hittingGround = true;
    6.             _groundHits++;
    7.  
    8.             if (_bounce <= 0.0f)
    9.             {
    10.                 D2dDestructible.StampAll(transform.position, _size, _angle, StampTexture, _hardness, _layers);
    11.                 GameManager.Instance.MakeStamp(_index, transform.position, _size, _angle, StampTexture, _hardness, _layers);
    12.                 GameManager.Instance.RemoveBullet(this);
    13.                 Destroy(this.gameObject);
    14.             }
    15.         }
    16.     }
    When the bullet hits the ground, there is a slight delay where the bullet just hangs in the same location, and then the terrain gets destroyed. The terrain object uses D 2d Polygon Collider.

    Any ideas why the delay occurs?
     
  47. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Your code looks fine, and I can't see any reason why it would hang in the air. How long does it hang there?

    Maybe you can pause the game just before impact and then step through each frame to see exactly how long the delay is for.

    StampAll is designed to write the new pixel data instantly, which will then be shown during the next frame. As far as I know OnCollision__ and other physics events all occur before rendering, so there shouldn't be any visual delay.
     
  48. e140games

    e140games

    Joined:
    Aug 2, 2012
    Posts:
    4
    Hi, I create a prefab with D2DExplosion. How can I know the size of the stamp in the coordinates of the scene?
     

    Attached Files:

    • pic.JPG
      pic.JPG
      File size:
      30.2 KB
      Views:
      736
  49. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    The Stamp Size setting is the size in world/scene coordinates. I'll update the tooltip to clarify this in future versions.
     
  50. Ktulian

    Ktulian

    Joined:
    Apr 6, 2014
    Posts:
    8
    I hope I'm putting this question in the right place... Do you have some written instructions for each feature including the newly added features like say crumble for example? I've also been wondering about pooling to save on processing. I did see a couple posts about pooling but no detailed information as to why that may be bad. I've also wondered if small fractions of the object couldn't be instantiated as some generic form of debris that I make myself that I have pooled and referenced to cut down on system stress. If there is no written information on this maybe you could point me in the right direction with a few tips?