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
    Split Pixels Min currently does nothing because I wasn't happy with its previous implementation, and doing what I want is a little tricky with the new feathering algorithm, so I plan to add it in a bit later.

    In the mean time I sent you a private message with the old implementation.
     
  2. jarbleswestlington_unity

    jarbleswestlington_unity

    Joined:
    Dec 6, 2017
    Posts:
    32
    Hey there, quick question about "auto split":

    When an object has a rigidbody and auto split enabled, it seperates the object into several smaller objects. When there is no rigidbody it stays a single object no matter how deformed it gets. When auto split is disabled the objects collision box doesn't change at all.


    In short:
    Is there a way to have a rigidbody enabled, the collision box change, and to not have the object split into tinier bits?
     
  3. jpjosey

    jpjosey

    Joined:
    Apr 14, 2018
    Posts:
    1
    I just purchased this (first asset I've purchased). When using the Replace feature to replace a sprite on an object with another, my computer froze up all night.

    Also when I make a sprite destructible, it gets a collider child with like a bajillion colliders.

    Anyone else getting this? Was my sprite too big? Also what's the official way to raise this with the developer?
     
  4. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Having a rigidbody or not has no impact on the way splitting works. If you don't want an object to split then just disable Auto Split. If you want the collision shape to update with destruction then you must use one of the custom colliders that comes with D2D (D2dAutoCollider, D2dPolygonCollider, D2dEdgeCollider). Normal colliders like BoxCollider2D won't update.


    Does the Breakable Eggs demo scene work? If so, please describe the exact steps required for me to replicate this issue with sprite replacement, or attach a .unity scene file using only the D2D example media that demonstrates this issue. Also, what's the exact version of Unity you're using?


    The amount of colliders created depends on the collider Cell Size setting, so a 1024x1024 sprite with a 128 Cell Size is 8x8 (64) colliders. To reduce this number I recommend you call 'Optimize Alpha' from the D2dDestructible component's context menu (gear icon).


    Posting here is fine, or PMing me, or e-mail (in the Read Me.pdf).
     
  5. jarbleswestlington_unity

    jarbleswestlington_unity

    Joined:
    Dec 6, 2017
    Posts:
    32
    Darkcoder I made a video showing the differences between auto-split on/off.



    I added a D2d polygon collider script, but it only seems to record actual damage once in a blue moon when auto split is disabled
     
  6. jarbleswestlington_unity

    jarbleswestlington_unity

    Joined:
    Dec 6, 2017
    Posts:
    32
    as an addition to my previous comment: I looked through example projects, and they seem to have the same quirk. The "car damage" example has a d2dPolygonCollider and yet the actual collider doesn't seem to be damaged, just the alpha.
     
  7. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Interesting, I've noticed this issue before and thought I fixed it. I'll take a look in depth tomorrow and see if I can replicate it.
     
  8. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    I did some testing and was unable to replicate this issue. Are you using the latest version of D2D (see Change Log.pdf)? There was a bug like this fixed in 2.1.6. Also, what is the exact version of Unity you're using?
     
  9. drorriov

    drorriov

    Joined:
    Jun 7, 2014
    Posts:
    43
    Hey,

    Can it do effect (Destructible 2D) to UI elements? like Text?
     
  10. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    No, only 2D Unity sprites.
     
  11. Anto1882

    Anto1882

    Joined:
    Feb 4, 2018
    Posts:
    3
    Hi,

    I noticed something weird... When adding a sprite (drag and drop in the scene) and setting it to "make destructible - static".

    when exiting the scene (or reloading it) the texture is kept in memory
    print("Textures " + Resources.FindObjectsOfTypeAll(typeof(Texture)).Length);

    count gives +1 each time... (I noticed the same thing in the "Huge Map" sample scene)

    I tried to clean the memory manually without any success.... Any idea on what can cause that?
    Please note than when I drag/drop only the sprite in the scene it is working well... memory is cleaned properly.. it comes from the destructible item...

    Antonin
     
  12. Anto1882

    Anto1882

    Joined:
    Feb 4, 2018
    Posts:
    3
    OK I think I found a solution:
    In the file "D2dDestructible.cs" I added the following function that I am calling before destroying the destructible gameobject:

    public void ClearOtherText() {
    MainTex = D2dHelper.Destroy(MainTex);
    DensityTex = D2dHelper.Destroy(DensityTex);
    alphaTex = D2dHelper.Destroy(alphaTex);

    }
     
  13. mnar786

    mnar786

    Joined:
    Jan 11, 2017
    Posts:
    9
    hey there.
    how are you ? first of all my english is little weak. so please pardon if any spelling mistake etc.
    my question is very simple.
    when i cut image from bottom with mouse dragging, the upper part falls down, as due to gravity apply on clone.
    now i just want to know how to keep upper part remain stay and the lower part must be fall? let me know where to change or add lines in script.
    thanks.
     
  14. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Thanks, I'll take a look.


    If I understand correctly, this feature is demonstrated already in the 'Break Off' demo scene. Where the Rigidbody2D stays still using Rigidbody2D.isKinematic = true, and if the clone gets disconnected from the 'Fixture' child GameObject, then the D2dFixtureGroup component will make Rigidbody2D.isKinetmatic = false.

    In this demo scene the fixture is at the bottom of the tree, so in your scenario you need to move it to the top, and it should work as you describe.
     
  15. mnar786

    mnar786

    Joined:
    Jan 11, 2017
    Posts:
    9
    wow..it's working. thanks a lot.
     
    Darkcoder likes this.
  16. BigBoiii_Jones

    BigBoiii_Jones

    Joined:
    Aug 3, 2014
    Posts:
    40
    Would this work well with a DOOM like game? So for example its a 3D world with 2D environments and everything billboarded. I want to use this for a gore system to make characters have destructible bodies and gib. I'm using this asset for the characters to project in a 3D world: https://assetstore.unity.com/packages/tools/sprite-management/spriteman-3d-47367 just wondering if it would work right if I just made it so every animation and view had a destructible layer underneath.
     
  17. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    It's possible, but if you want gibs to fall down in a 3D scene then it won't work since D2D only generates 2D colliders. You could skip the colliders and make them fall yourself though. Also, the current damage scripts are designed to work in a 2D scene where all the sprites are laid across the X/Y plane, and not freely in 3D. If you're really interested in doing this then I could update these damage scripts and make a demo scene for this?
     
  18. BigBoiii_Jones

    BigBoiii_Jones

    Joined:
    Aug 3, 2014
    Posts:
    40
    That would be great if you could update the script and somehow have it work in a 3D world and environments and have things work on a Z axis so we could have a gibbing system that works like old school shooters that'd be great. I already have the blood spilling system set up its mostly getting the dismemberment with proper gibbing.
     
  19. mnar786

    mnar786

    Joined:
    Jan 11, 2017
    Posts:
    9
    hi there,
    i have p3d paintable on a image. and i want to replace with Destructible 2D. when i remove paintable script from the image and apply Destructible 2d script. the image disappear. i want to know why image is getting disappear?
     
  20. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    By default, Destructible 2D works by reading SpriteRenderer data, and converts it into a destructible object it renders with a normal MeshFilter+MeshRenderer. Paint in 3D however only works with MeshFilter+MeshRenderer or SkinnedMeshRenderer objects, so they're not compatible by default. If you want to make a painted object destructible then you must add the D2dDestructible component, and call the ReplaceWith(...) method on it, and pass the Texture2D you were painting with. Keep in mind the object you were painting has to have been a quad, as a normal 3D mesh will not work.
     
  21. jarbleswestlington_unity

    jarbleswestlington_unity

    Joined:
    Dec 6, 2017
    Posts:
    32
    I don't think I thanked you for helping me last time, my version said up-to-date in the unity store--and I had just downloaded--but it turned out I WAS one update behind. Good to see you're still improving this awesome plugin!

    I was wondering if you would be able to suggest where to start on interacting with your code slightly. My game has a character lifting (and swinging from) destructible blocks. Short gif for context:


    In the clip you can see that my hand seems to glitch out whenever a bullet collides with a held object. This is actually an improvement from the previous issue where my hand would suddenly not be attached to a real collider, or be attached to a smaller piece of the collider far away from the target point. My solution was to "wait till end of frame" before my hand destroys itself and respawns in the same location. This seems to cause some slowdown and is not entirely accurate. I was wondering if you could tell me where exactly in your code I can trigger an event right after a new object is formed, or if you have a better idea for how to maintain a grip on your objects?
     
    Chizel likes this.
  22. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    It looks like your grapple code depends on a specific Collider2D being unmodified for it to remain connected, which stops working because D2D is updating it whenever a pixel in that particular collider cell is modified. To fix this, I recommend you modify your grapple code to instead depend on the GameObject the collider belongs to, and to store a local position for the grapple point. Your grapple update code can then keep checking to see if it's still on top of a collider that belongs to the grappled GameObject. This should maintain a lock up until the pixel it's grappled to gets destroyed and the collider no longer covers it.
     
  23. jarbleswestlington_unity

    jarbleswestlington_unity

    Joined:
    Dec 6, 2017
    Posts:
    32
    Hmm that's a good idea, I'll see what I can do
     
  24. jarbleswestlington_unity

    jarbleswestlington_unity

    Joined:
    Dec 6, 2017
    Posts:
    32
    So I tried coming up with an alternate system of manually updating the grapple, but I started realizing how useful accessing a specific collider is for me. Is it possible you could point me in the direction of the moment the new collider is finished? I can create a trigger that checks for a grapple being attached and then re-spawns the grapple physics connection
     
  25. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    The colliders are rebuilt from the D2dDestructible.OnAlphaDataModified event, which triggers D2dPolygonCollider.OnAlphaDataModified, which calls D2dPolygonCollider.RebuildCell for each cell.
     
  26. SpiffyStache

    SpiffyStache

    Joined:
    Apr 14, 2014
    Posts:
    2
    Hi,

    I've been trying to figure out why D2dExplosion.Start() is causing me massive performance spikes but can't seem to find a real cause. The spikes vary in size greatly. The performance hit ranges from perfectly reasonable to freezing the game for seconds at a time on an iPhone. No matter the device (PC, Android, iOS), the performance hit can easily and noticeably reduce the frame-rate. I've observed that the issue only occurs if "stamp" is turned on for the D2dExplosion object (Might slightly narrow it down).

    Related Profiler Images:
    https://imgur.com/a/sLvlGFZ

    Any ideas? I'm happy to provide more information or screenshots as needed.
     
  27. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Can you post a screenshot of the inspector for the object you're trying to destroy? On PC you can enable deep profiling so you can narrow it down.
     
  28. SpiffyStache

    SpiffyStache

    Joined:
    Apr 14, 2014
    Posts:
    2
    Looks like it's having a hard time calculating new islands.
     

    Attached Files:

  29. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Seems pretty slow for a destructible with only 6700 solid pixels. In the next version I will improve collider regeneration speed and allow for pooling which should speed this up a bit.

    I'm currently on holiday though so I can't work on it yet.
     
  30. mnar786

    mnar786

    Joined:
    Jan 11, 2017
    Posts:
    9
    hi there,
    i hope you are fine. i am again here, sorry to take your important time.
    i want to ask to thing.
    1. i have problem with instantiate game object position. (take a look attached image).
    2. how to apply undo operation, on Destructible 2D image?
    thanks in advance?
     

    Attached Files:

  31. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    1 - You need to modify the CloneForSplit method in D2dDestructible.cs to order the children how you want. For example, you can add this after the first instantiate line: clone.transform.SetSiblingIndex(transform.GetSiblingIndex());

    2 - You can call yourSnapshot = yourDestructible.GetSnapshot() on your destructible before you damage it, then call yourDestructible.ReplaceAlphaWith(yourSnapshot); after. This will only undo alpha damage though, if you need to undo more things you can either clone and disable a previous state, or extend the snapshot code to include more data.
     
  32. DPhaze

    DPhaze

    Joined:
    Jun 10, 2016
    Posts:
    3
    Hey there,

    I'm having some issues with the polygon collider generation. I'm using the actual points of the polygon for targeting and they are not always there, so I have bits and large chunks of sprite leftover, still showing about 10% remaining alpha but with no collider to actually hit/lock onto.

    I noticed when I look at it in the editor(in pause) that not all the collider grid it shows in editor mode is always there in run mode. Is there some way to make it so the colliders are always on/generated?
     
  33. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Colliders are only generated for "alpha tex" pixels that are solid, which means they have over 0.5 opacity (128). Pixels that are just below this are still visible though, which allows for the destroyed object edges to look smooth. If you want to increase the sharpness of this cutoff, then you can increase the "Sharpness" setting in your D2dDestructible inspector.

    I'm not sure what you mean by the grid not showing in paused run mode, as long as you select the destructible that has colliders they should show up. Can you upload a screenshot showing the issue?
     
  34. DPhaze

    DPhaze

    Joined:
    Jun 10, 2016
    Posts:
    3
    here they are, sorry for the late reply:
    These are the base colliders, it shows them all there here, and I have an asteroid from the example scenes for D2D that generates the same and just works.
    Base colliders.png
    After the laser starts damaging the image, only some colliders near the damage are there, and then some random ones near the top right edge.
    Colliders damage.png
    When it goes past the 'main' body, it seems like the colliders don't generate and there's nothing for the laser to lock on to.
    Colliders finished.png
    Colliders missing.png
    I looked at it step by step while running, and it looks like the colliders are only generating/showing up when an explosion stamp is near them/hitting them. I'm guessing you might be storing them somewhere but hiding them from the simulation for efficiency, is there any way for me to retrieve them from your scripts?
     
  35. Ktulian

    Ktulian

    Joined:
    Apr 6, 2014
    Posts:
    8
    I've gotten this to work over the network but I'm having problems with autosplit not spawning the new objects from the network manager. Is there a solution to this built in somewhere I'm missing or is that bit not compatible with unet out of the box?
     
  36. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Sorry for the late reply, I missed your message. Could you send me the image you're using via email or private message?


    There is no networking support built in. You would probably have to modify the instantiation code in the split method to handle the networking.
     
  37. ryanspr1vates95

    ryanspr1vates95

    Joined:
    Jul 30, 2018
    Posts:
    5
    I purchased this recently, and am excited to begin using it! I think the click to stamp script is really cool, but I am wondering how I can "instantiate" (in a way) a stamp at a gameObject's location instead of the mouse's? Like if I were to shoot a projectile?
     
  38. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    The D2dExplosion component can be used to stamp, damage, and add force to nearby objects. If you make a prefab with this component and set it up, then you can instantiate it from your code when you need to make an explosion there. This same approach is used in many of the demo scenes via D2dClickToSpawn, D2dBullet, etc.
     
  39. ryanspr1vates95

    ryanspr1vates95

    Joined:
    Jul 30, 2018
    Posts:
    5
    Yes, this helped! Appreciate it! Also, is there some sort of collision detection? So like, I have a player, and need to know when he “isGrounded”. But with your script’s edge collider, it doesn’t actually have a collider box. If there is no way to detect it, then what possible workarounds might you suggest?
     
  40. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    That's up to your character controller code. The D2dEdgeCollider component just generates Unity's built-in EdgeCollider2D, which can be interested with like any other 2D collider (e.g. raycast). To detect the floor you would probably want to detect the normal/direction/angle of the hit surface and make a decision based on that.
     
  41. mnar786

    mnar786

    Joined:
    Jan 11, 2017
    Posts:
    9
    point No.1 is working fine :
    but point No.2 the undo, could you please let me explain in little details..i couldn't understand proper.
    here what to do in Attached code.
    thanks in advance.
     

    Attached Files:

    • code.jpg
      code.jpg
      File size:
      198.3 KB
      Views:
      787
  42. mnar786

    mnar786

    Joined:
    Jan 11, 2017
    Posts:
    9
    are you available?
    waiting for my reply.
    thanks.
     
  43. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Sorry for forgetting to reply.

    If you call: var snapshot = yourDestructible.GetSnapshot();

    Then 'snapshot' will store the current destruction state of the D2dDestructible. You can store this in a field in your custom script and then later call: yourDestructible.ReplaceAlphaWith(snapshot);

    This will restore the destruction state. This is similar to how the 'Heal Damage' demo scene works.

    Keep in mind this only stores the state of the destruction (alpha tex), and won't restore splits, or other complex changes. For that you may want to just clone your object and deactivate it for later use.
     
  44. mnar786

    mnar786

    Joined:
    Jan 11, 2017
    Posts:
    9
    yes it's valuable for me. but since it's not restoring split, could you please let me know how to reset whole alphaTex on One Click.
     
  45. mnar786

    mnar786

    Joined:
    Jan 11, 2017
    Posts:
    9
    hoping for the answer today.
     
  46. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Sorry for the late replies, I'm very busy this week.

    To restore a split you must duplicate (instantiate) the GameObject, and 'store' it somewhere by deactivating it. This requires writing a custom script if you want it work by just clicking one button.

    In the next version I will improve the snapshot system to include more information so you can restore the D2dDestructible itself from a split, but if your split does more complex things (e.g. uses fixtures), then you will still need to duplicate the object and restore it manually.
     
  47. Naeja

    Naeja

    Joined:
    Jul 10, 2014
    Posts:
    17
    Hello, is there a straightforward way to cull all "islands" in an effected are that are below a minimum size?
     
  48. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Without custom code you can enable splitting, use the D2dRequirements with your desired AlphaCountMax setting, and have the OnRequirementMet event trigger D2dDestroyer to destroy the island.

    I'm currently working on a big update that will drastically improve the way islands and splitting works, and will include this kind of feature.
     
    Naeja likes this.
  49. Naeja

    Naeja

    Joined:
    Jul 10, 2014
    Posts:
    17
    Thanks for your reply! This would require using the Auto Split feature correct? Will the update improve the performance of splitting? Or will we be able to only enable splitting within a radius of the effected area?
     
  50. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Yes, splitting is required. The new version should improve splitting performance, I still need to do more work before I can benchmark it.