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,406
    I have no idea, I don't use this other asset. If you find errors in my assets using only official Unity features then I can help you.

    Thanks, I can replicate the issue. I'll see if I can fix it soon.

    When a sprite is damaged the D2dDestructibleSprite component's OnModified(D2dRect) event is invoked. The collider components hook into this, and only update the colliders within range of the modified area. Both the colliders and visual damage are based on the D2dDestructibleSprite component's AlphaData array, which gets written to the AlphaTex for the visual damage.
     
    chimchim likes this.
  2. elvin0272

    elvin0272

    Joined:
    Feb 16, 2021
    Posts:
    13
    So the error was not in the work of another asset. I checked this project on another PC where there were no saves at all and the same error still appears. I.e. Slice works on the desktop version, but it does not work on the simulator of the mobile version and with a compiled project for android on the phone, too. It is the Input script for creating Finger that does not work correctly.
     
  3. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    Hey everyone,

    Version 4.0.2 of Destructible 2D is now out!

    • Added D2dTimer component.
    • Fixed D2dEdgeCollider inspector changes not applying instantly.
    • Fixed D2dEdgeCollider inspector warning.
    • Fixed D2PolygonCollider inspector changes not applying instantly.
    • Fixed D2PolygonCollider inspector warning.
    • Added D2PolygonCollider.Density setting.
    • Added D2PolygonCollider.UsedByEffector setting.
    • Added D2PolygonCollider.UsedByComposite setting.
    • Added D2dEdgeCollider.Density setting.
    • Added D2dEdgeCollider.UsedByEffector setting.
    • Added D2dEdgeCollider.UsedByComposite setting.
    • Fixed D2dEdgeCollider potential gaps.
    • Improved D2dEdgeCollider collider quality.


    This has now been fixed.


    I was referring to the console error. In the latest version I updated the input code which should fix the mobile touch issue.

    Unity's simulator gives bogus values for the mouse so it doesn't work:

    upload_2022-3-31_12-57-31.png
     
    elvin0272 and chimchim like this.
  4. chimchim

    chimchim

    Joined:
    Sep 20, 2013
    Posts:
    9
    Thank you for correcting this so quick. unfortunatly the gaps still occur. Add a edge collider two the castle and there are gaps to the left of the left tower. I would be very grateful if you could look at it again.
    upload_2022-3-31_18-12-38.png
     
  5. chimchim

    chimchim

    Joined:
    Sep 20, 2013
    Posts:
    9
    I tried drawing the edgecolliders points and nothing seems wrong here. Maybe its not an issue with your code.
    upload_2022-3-31_23-53-22.png
     
  6. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    Oh right, the castle in the 01 demo scene hasn't been trimmed, so the pixels on the left side go 'outside' of the sprite and so the collider disappears there. If you click the D2dDestrictibleSprite component's Trim button then it will add transparent pixels to the outside, which fixes this issue.

    Before:

    upload_2022-4-1_10-12-9.png

    After (trimmed):

    upload_2022-4-1_10-12-32.png

    When trimmed the sprite should smoothly fade out to 0 opacity, but that doesn't appear to happen for some reason. I'll add that on my to-do list to check.

    Also, in the first screenshot you can see at the bottom right there is another gap that isn't at the edge. I think this is the same as the issue in your second screenshot. This issue is due to Unity automatically ignoring collider vertices when they're too close together. For example, if you increase the sprite scale then they appear again:

    upload_2022-4-1_10-16-10.png

    To avoid this you can increase the D2dEdgeCollider component's Optimize setting, or increase the scale of your object. I don't think there's any setting to disable this.
     
  7. chimchim

    chimchim

    Joined:
    Sep 20, 2013
    Posts:
    9
    Is it possible to add pixels to a map and regenerate the collision cells at that location? If i understand correctly the heal function only restores states that existed but it is possible to add entirely new pixels without recaculating the whole map?
     
  8. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    The heal feature of Destructible 2D requires you to first make a snapshot/state to heal to, because otherwise you could easily heal areas 'outside' of your original sprite, which likely has unusable RGB data and would look bad. To overcome this you can either:

    a) Write a new paint mode based on Heal, but it removes the CanHeal check and removes the parts referring to the 'healData'.

    b) Manually adjust your destructible sprite's AlphaData array, and then call the NotifyModified method with the specified pixel rect you modified.

    c) Ask me to add option A in the next version.
     
  9. MyFunFair11

    MyFunFair11

    Joined:
    Jan 12, 2015
    Posts:
    4
    Does terrain destruction work on a headless build? I'm using This plugin with photon to build a simple shooting game like gunbound/worms, after a few tries, it seems to me that the terrain destruction doesn't work on a headless build.
     
  10. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    When a destructible sprite is built for the first time or rebuilt, a RenderTexture is used to grab the current texture state and that's then converted into the AlphaData array. If graphics are disabled then this wouldn't be possible to do. To fix this you would have to build your destructible sprites beforehand and not rebuild them. You should debug the AlphaData and other properties to see if this is the case, or if it's something else.
     
  11. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    Hello.

    Great asset. I am looking for the following cenario: When a egg hit the ground it shatters. Did not find a specific demo for that, could you please tell me which script should I use for that case? Thanks.
     
  12. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    The "16 Fracture" demo scene is configured to do this. For eggs you will want to reduce the D2dImpactDamage component's Threshold setting so it can more easily take damage, and probably reduce the D2dFracturer component's Damage / Damage Required setting.
     
    khrysller likes this.
  13. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    Hey, thank you for the reply. It is working now.

    Another question. now I have the icecube falling and spliting but it is spliting on cascade, like the splited part got splited and so on.

    Is there an option to split only once on the impact? This is the image of the components the gameobject has.

    upload_2022-4-22_11-2-16.png

    Thank you again for your valuable information.
     
  14. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    There's no setting for this because a fracture can create both big and small pieces, and in my opinion it looks wrong if there is a large 'split' piece that is no longer able to be fractured. That said, you can get a similar result by changing the D2dFracturer component's Damage / Damage Multiplier to a very high number like 10000.
     
    khrysller likes this.
  15. helloroy

    helloroy

    Joined:
    Jun 23, 2016
    Posts:
    42
    Hello @Darkcoder Destructible 2D can make a dig effect like this?
    thanks.
     
  16. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    Destructible2D allows you to dig holes in terrain similar to this. However, there is no feature to add the ground texture to the parts of the dug ground that face up, so it would look different.
     
  17. Seanchaoz

    Seanchaoz

    Joined:
    Jun 19, 2019
    Posts:
    18
    Edit: The issue is a false error, I've been having Visual Studio issues and this has proven to be one of them. Everything works so this is not an issue related to the asset, but I'll leave the post anyway.

    Spiffy asset, loving it!

    --------------------------------------------------

    Pardon my ignorance, I'm still learning Unity and coding, but I've been trying for hours now to implement this asset into existing code and I cannot for the life of me figure out how to access the scripts from outside of the pre-packaged stuff in the "Plugins/CW/" folder.

    I have a player character with existing code and scripts, and I would like to access the Destructible2D.D2DSlice.All() function as part of the player's attack, so that when I trigger the attack logic on the player script, his attack will slice things apart in the game. But no matter what I do, I cannot reference any of the D2D scripts from any of my existing code. The namespaces do not exist, even the using CW.Common definition does not exist.

    The only way I figured out to get it working was to move my PlayerScript into the Plugins/CW folder (I assume because then the PlayerScript becomes part of the D2D namespace?) - then everything works like a charm - but ehh, I'd rather not have to shuffle all of my existing game scripts into new folders and namespaces just to get this working.

    In fact, if I move any script into the Plugins/CW folder, those scripts become invisible to the rest of my game and can now only be used by other scripts inside of that folder structure.

    Surely I'm missing something here?
     
    Last edited: May 11, 2022
    Darkcoder likes this.
  18. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    Glad you found a solution.

    Inside the root Destructible2D is the Destructible2D.asmdef (assembly definition, see Unity docs for more info). This causes all scripts within here to be compiled into this assembly. Assuming your code isn't inside an asmdef, it's my understanding that Unity will put it in the default assembly which should have access to all the others automatically. Things only get a bit tricky if your code is inside its own asmdef, then you would have to manually modify your asmdef to reference the D2D one.
     
  19. k3ndro

    k3ndro

    Joined:
    Dec 8, 2016
    Posts:
    28
    Hi @Darkcoder , is it possible to work with a Trigger bullet? The only examples I found using physical colliders. Thanks
     
  20. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    The D2DBullet component uses a 2D raycast which hits colliders as well as triggers if you have this enabled:

    upload_2022-6-21_14-4-55.png

    If you're talking about the D2dImpactSpawner then no, that only activates with collisions. You would have to modify or make a new component based on this that hooks into cachedCollisionHandler.OnOverlap and spawns something. Trigger overlaps have no 'hit point' though, so it's not clear where the impact prefab should spawn.
     
  21. gooses2

    gooses2

    Joined:
    Feb 2, 2018
    Posts:
    6
    I'm having the exact same problem as @MyFunFair11 with the spriteshapes, It works using the demo scene you provided but doesnt work in my own scenes.

    If I copy and paste the destructable sprite shapes from your demo scene to my scene they stop working once pasted.
     
  22. gooses2

    gooses2

    Joined:
    Feb 2, 2018
    Posts:
    6
    Hi @Darkcoder,

    I've been investigating the issue by comparing line by line the scene files of your demo scene and my scene and the issue seems to be releated to skybox materials. I can replicate the issue in your "DestructibleRenderer" demo scene by assaigning a skybox material in the lighting settings and likewise I can fix the issue in my scene by removing the skybox material.

    Hope that helps.
     
    Last edited: Jun 25, 2022
  23. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    Thanks for tracking the issue down, I think I've fixed it. I'll send you a test build soon.
     
  24. boypipz321

    boypipz321

    Joined:
    Jan 27, 2018
    Posts:
    7
    Hi! I've been using your asset and its been very helpful on my project Thanks!

    just a quick question tho, is there a way to add a Scorch mark effect using Shader D2D Sprite-Lit-Default
    as I just added some lights to my scene and D2D Default + Color are not affected by lights,

    Apologies in advance if its some kind of a dumb question a noob here alert hehe.
     
  25. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    Sorry for the late reply.

    I've now rewritten the shader code so that all of the features (Keep Alpha, Color, Outline) are available in all versions of the shaders. I'll do some testing and submit this in 2 days or so.
     
  26. chimchim

    chimchim

    Joined:
    Sep 20, 2013
    Posts:
    9
    Im working on multiplayer-platformer game with a large map that i want completely destructable. These islands need to be removed but it causes huge lag spikes using d2splitter when working on a 4096x4096 map. How would i go about this issue without splitting up my map?
    Im not afraid to try and write custom code
    And thank you for your work, D2 is great. Im trying to disect your code and apply it in my enviroment. As using monobehaviors is not preferred for me, but its abit hard to follow when everything is static.

     
  27. chimchim

    chimchim

    Joined:
    Sep 20, 2013
    Posts:
    9
    Im also having an issue when i create new gameobjects from generated textures in runtime. The optimize is working fine Collision-wise. But the visual state is unaffected
    Code (CSharp):
    1. var sd = fractureObj.AddComponent<D2dDestructibleSprite>();
    2. fractureObj.AddComponent<D2dPolygonCollider>();
    3. sd.Rebuild(2);
     
    Last edited: Sep 4, 2022
  28. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    Hey everyone,

    Version 4.0.4 of Destructible 2D is now out!
    • Merged "Default + Color" shader into "Default" shader.
    • Merged "Keep Alpha + Color" shader into "Default" shader.
    • Merged "Keep Alpha" shader into "Default" shader.
    • Merged "Outline" shader into "Default" shader.
    • Simplified custom shader integration.
    • Updated URP shaders.
    Enjoy :)
     
  29. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    If you cannot split up your map then you must write your own splitter code that is optimized for your scenario. The D2dSplitter component checks every pixel in the sprite and groups them into each pixel 'island', and then splits them into separate GameObjects. As you can imagine, this would be slow for such a large map. In your scenario, you could have similar results by just checking a limited area around each explosion. So for example if the explosion is at XY50,50 with a radius of 20 pixels, then you could check at that location with a radius of 30 pixels or whatever works, and then remove any islands within there. One edge-case is that you would be able to gradually cut away a large chunk and the island of this chunk wouldn't be detected, but perhaps this isn't an issue in your project.

    You can check the D2dSplitter code to see how it hooks into the D2dDestructibleSprite component's OnModified event to know which part was damaged, and then read and modify the AlphaData array, and finally call NotifyModified.


    Your sprite is still using the original sprite material, which doesn't show the destruction data. You can call the D2dDestructibleSprite component's ChangeMaterial method to change it, or do this manually.
     
  30. chimchim

    chimchim

    Joined:
    Sep 20, 2013
    Posts:
    9
    Thanks for replying so fast

    Couldt i do something with the collider? As im using edgecollider, an island would apear as a small polygon where every point is connected. calculate the length of these or something like that? The collisionstate would need more logic/data i guess
    upload_2022-9-5_22-1-34.png
     
    Last edited: Sep 5, 2022
  31. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    You could do. Keep in mind the colliders are split up into cells, so these islands may be part of more than one collider.
     
  32. chimchim

    chimchim

    Joined:
    Sep 20, 2013
    Posts:
    9
    Also i think i found a bug in this code here. Maybe this is happening when trying to optizime very small sprites? Divide By zero
    upload_2022-9-6_22-52-5.png

    Howeever this is a fix:
    upload_2022-9-6_22-55-38.png
     
  33. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    Thanks for pointing this out, this will be fixed in the next version!
     
  34. boypipz321

    boypipz321

    Joined:
    Jan 27, 2018
    Posts:
    7
    Thanks for the 4.0.4 Update Dark!

    Upon Re-Downloading and Importing the new 4.0.4, it works perfectly!

    For anyone who also missed the scorched marks on the URP Shader Material, you'll need to enable
    "Combine Destructible RGB" on the .mat for it to work ;)

    upload_2022-9-29_10-11-10.png

    Once again Thank you Dark!
     
    Darkcoder likes this.
  35. znagler

    znagler

    Joined:
    Sep 25, 2022
    Posts:
    9
    Hi,

    I just emailed Carlos directly before discovering this thread but now I figure I will ask here for better visibility. I seem to have a fundamental issue with my D2D that others are not having. In full disclosure, I am new to Unity so maybe I am doing something wrong or I broke my asset (I have tried re-downloading/re-importing several times).

    The issue is - for most sprites that I try, as soon as I hit "Make Destructible" on the Sprite Renderer, usually the Sprite becomes completely deformed with visual artifacts/glitches/colors separating. In the example screenshots below I am showing the before and after of clicking "Make Destructible - Preset Static" (the same issue happens with dynamic etc.) on a Sprite. The expected behavior is that the visual should not change at all from simply making it destructible. This trophy sprite comes from a Pixel Arts 2D package commonly used in tutorials. Weirdly, all of the example Sprites that came with the D2D package work fine, but most sprites from elsewhere break in similar manners to the example.

    I have tried tinkering with the Sprite 2D import settings/color channels etc. in every way imaginable and I have also tried using these Sprites in totally new projects and the issue still occurs with most sprites (which is very odd because others don't seem to have the issue).

    Has anyone seen this or have ideas? Thank you
     

    Attached Files:

  36. znagler

    znagler

    Joined:
    Sep 25, 2022
    Posts:
    9

    Hi, I am still trying to fix my mysterious bug. I created a totally new 2D (non URP) project and only imported D2D and the art and the issue remains. I noticed that for any given sprite, the glitch is deterministic. I also see that the issue is probably caused by the "D2D Scale" and "D2D Offset" becoming seemingly random numbers when I think they should be 1 and 0 etc. (see screenshot). I have no idea where these numbers are coming from. Does anyone know enough about Shaders or D2D to further debug this? Thank you.

    Screen Shot 2022-10-18 at 7.21.02 PM.png
     
  37. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    Sorry for the late reply.

    Yeah as you say it looks like the offset and scale values are wrong.

    Is this sprite in a sprite atlas by any chance? If so, this might be cause since all of the D2D example sprites aren't in one.

    Could you send me a link to this "Pixel Arts 2D" package so I can test out this sprite on my end?
     
  38. znagler

    znagler

    Joined:
    Sep 25, 2022
    Posts:
    9
    @Darkcoder No worries and thanks for the reply. These sprites from the screenshot (cherry and trophy) are not from a sprite atlas. They come from this free asset. But the same issue happens with many other sprites as well, both from other assets and ones I have created. I think somehow my shaders run different than normal, but I don't know what setting/configuration is doing it or how to restore the default. It's strange because the issue still occurs on fresh projects. I can't really use D2D because of this bug sadly.
     
  39. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    I just made a new project in Unity 2022.1.20f1 with Destructible 2D 4.0.4 and Pixel Adventure 1. If I drag and drop the "End (Idle)" sprite into the scene and use the "Make Destructible" preset, then it gets made into a destructible sprite as expected and there are no visual issues.

    upload_2022-10-24_21-40-10.png
     
  40. znagler

    znagler

    Joined:
    Sep 25, 2022
    Posts:
    9
    Thank you for checking. There must be something wrong with my set-up but I can't figure out what it could be. I'm using Apple silicon and I'm on Unity 2021.3.10f1 (and I tried the 2022 Unity version and had the same issue). There must be some shader-related setting that I need to restore to default or something. Even when I start a totally new project and download/install the D2D package fresh, I get this issue. You can see in my last screenshot that something is making the D2D Alpha "zoomed in" and offset relative to the Sprite texture itself, causing the bug. I think this is caused by the D2D Scale and D2D Offset values getting computed incorrectly. The same thing happens for most sprites (see the cherry to the left in the same screenshot). I tried looking at the default.shader file in the asset (which is generating the D2D alpha) but I don't see anything that stands out (with my limited understanding). So if you have any ideas to help debug those values based on your knowledge of the shader, that would be great.
     
  41. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    Thanks, it looks like the issue only occurs with the Metal graphics API.

    Could you open Plugins/CW/Destructible2D/Required/Scripts/D2dCommon.cs and change line 111 from this:

    Code (csharp):
    1. y = texture.height - y - h;
    To this:

    Code (csharp):
    1. if (SystemInfo.graphicsUVStartsAtTop == true && SystemInfo.graphicsDeviceType != UnityEngine.Rendering.GraphicsDeviceType.Metal) y = texture.height - y - h;
    That should fix the issue.
     
  42. znagler

    znagler

    Joined:
    Sep 25, 2022
    Posts:
    9
    That seemed to fix it :) Thank you so much. I will continue to test and let you know if I encounter any issues.
     
    Darkcoder likes this.
  43. ScratchBattless

    ScratchBattless

    Joined:
    Oct 23, 2022
    Posts:
    6
    Is there a way I can have an object destructible and destroy? Like in the demo scene where the angry suns melt into the castle could you also have them melt each other? when I tried it the object just destroyed itself.
     
  44. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    The angry sun uses the D2dRepeatStamp component to apply damage on top of itself every 0.125 seconds. If you make the sun destructible then it will damage itself too. Therefore, to make it not do this, you would need to put each sun on a separate layer and exclude itself using the D2dRepeatStamp's Layers setting. I could add a setting to D2dRepeatStamp that allows you to exclude a specific D2dDestructibleSprite to make this easier though.
     
  45. ScratchBattless

    ScratchBattless

    Joined:
    Oct 23, 2022
    Posts:
    6

    Thanks for the reply. It's working now!
     
  46. ScratchBattless

    ScratchBattless

    Joined:
    Oct 23, 2022
    Posts:
    6
    When I put the object on a separate layer and made it destructible, I realized it would update its collider just fine but would not update its destroyer shape so when I had destroyed most of it it still damaged my player the same as though I had done nothing to it. Is there a way to make it update its damage shape to match the sprites shape like the collider?
     
  47. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    Each destructible sprite has the AlphaTex property, which stores the current destruction texture. You could make a little script that sets that into the stamp shape texture so it applies damage with that shape.
     
  48. ScratchBattless

    ScratchBattless

    Joined:
    Oct 23, 2022
    Posts:
    6
    I tried using the impact spawner instead of repeat stamp for a similar effect so I wouldn't need a new layer for every game object in my scene, but it only spawns in the prefab at the first contact point. is there a way I can tell it to spawn at each contact point and keep spawning until contact ceases?
     
  49. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,406
    I'll send you a private message with an early copy of the next version which has the Exclude setting.
     
  50. ScratchBattless

    ScratchBattless

    Joined:
    Oct 23, 2022
    Posts:
    6
    Thank you so much!!!
     
    Darkcoder likes this.