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. thorgaming_42

    thorgaming_42

    Joined:
    Sep 25, 2015
    Posts:
    13
    Yeah I can confirm that this didn't work for me either I am using 3.0.9, don't lose any sleep over it tho :D its just an experiential project for me but feels like a nice feature set to sit along side Destructible 2d.
     
  2. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Yeah I'll try and figure it out. URP seems like a total mess though. If I make a new project in 2019.4.4f1 with URP, make a forward renderer asset, assign it to the Graphics settings, make an experimental 2D renderer and drop it into the previously made renderer, Unity seems to think I haven't set the 2D renderer and doesn't allow me to make any 2D lights... uhh
     
  3. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Here's an updated URP Lit shader that should work with at least URP 10.2.2 and D2D 3.0.9 and beyond!
     

    Attached Files:

  4. HoecusPocus

    HoecusPocus

    Joined:
    Oct 14, 2020
    Posts:
    7
    @Darkcoder
    I'm loving this asset! I have an issue though, related to sprites that will get packed into a Sprite Atlas...
    I made a few destructibles, and it's ace, but then I came to add the underlying sprites to a Sprite Atlas, and I get one of two issues when I do so..:

    After being added to an atlas to be packed for the build, I either cannot see the sprite at runtime, or, if I change the mesh type of the single sprite to FullRect, I get the error: "Not allowed to override geometry on sprite"

    I've scanned this forum, and it looks like 'a sprite's a sprite' regardless of being in an atlas...

    D2Dquestion.png

    Any help would be greatly appreciated!
     
  5. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    You cannot use tightly packed sprites, because D2D uses auto-resizing 'rectangle' shapes for the sprites, and these may render incorrectly. Also, destructible sprites break batching because each object has its own unique destruction state, so there isn't much benefit to atlasing anyway. If you're still experiencing issues with full rect atlased sprites then I can check it out!
     
  6. HoecusPocus

    HoecusPocus

    Joined:
    Oct 14, 2020
    Posts:
    7
    Thanks for the swift reply!!!
    Ahh right - so is this why I see (clone) in the SpriteRenderer ???
    The reason I wanted to add them to an atlas is because I thought that having, say, all one level group's sprites in one atlas would speed up rendering (especially on mobile), where I've basically got just 3 textures to send to the gpu, then Unity can batch / doesn't have to switch, when drawing...
    I could put all my destructibles in another atlas with full-rect on...? Does that sound like a good idea, or have I missed something?

    Thanks :)
     
  7. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Indeed, you should put all your level and other 'normal' sprites in an atlas (unless they're huge). If you want to put your destructible sprites in their own atlas then yes, making this separate with full-rect is a good idea, and you may still see some benefit over not doing it.
     
    HoecusPocus likes this.
  8. kometasoft

    kometasoft

    Joined:
    Nov 19, 2019
    Posts:
    1
    Hi, I am trying to create different scorch marks but no matter what I do it always leaves a complete square hole, I have tried to modify the scorch marks that come in the asset but the same thing happens.

    What steps should I follow to create my own scorch mark?
     
  9. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    The scorch mark shape/color should be stored in the RGB channels, and the hole shape should be stored in the alpha. If you do it correctly it will look like this:

    upload_2021-3-20_0-3-53.png upload_2021-3-20_0-4-0.png

    Notice the hole/alpha is smaller than the scorch/rgb.

    How you make this depends on the image editing software you use. In Photoshop for example you can create an image with 4 channels (RGBA), and paint them individually. This can then be saved correctly using the Super PNG plugin. Most png exporting tools will erase/discard the RGB values for pixels that have 0 alpha, so it can be difficult to do until you find the right plugin or settings.
     
  10. Darkcoder

    Darkcoder

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

    Version 3.1.0 of Destructible 2D is now out!
    • NOTE  This update changes a lot of features. Back up your project before updating!
    • Moved main build to Unity 2019.4.12f1.
    • Rewrote input code to support fingers, mouse, and keyboard.
    • Redesigned all demo scenes to have a simpler structure.
    • Added Procedural Setup demo scene.
    • Added ShapeBuilder tool.
    • Added D2dClickToStamp.Interval setting.
    • Added D2dClickToStamp.ScaleMin / Max settings.
    • Added D2dClickToStamp.TwistMin / Max settings.
    • Added D2dClickToStamp.Camera setting.
    • Added D2dClickToSpawn.IndicatorPrefab setting.
    • Added D2dClickToSpawn.TwistMin / Max settings.
    • Added D2dClickToRead.OnColor event.
    • Added D2dClickToRead.OnDestructibleColor event.
    • Added D2dClickToRead.OnNothing event.
    • Renamed D2dClickToStamp to D2dTapToStamp.
    • Renamed D2dClickToSpawn to D2dTapToSpawn.
    • Renamed D2dClickToFracture to D2dTapToFracture.
    • Renamed D2dClickToRead to D2dTapToRead.
    • Split sprites will now retain the original parent.
    Lots of nice changes in this version.

    In particular, I made the 'Shape Builder' tool, which allows you to make destruction shape textures with scorch marks based on two separate textures. This means it's now much easier to create these textures. To use it, simply right click in your Project view, and select "Create/Rendering/Shape Builder (Destructible 2D)", drag and drop your two textures in, and click "Generate". Keep in mind the Color texture should have the scorch mark data in the RGB channels, and the Alpha texture should have the destruction shape stored in the alpha. You can look at the example textures in the "Destructible 2D\Examples\Shapes" folder to see how this works, even if your Alpha textures are originally just normal RGB textures.

    Enjoy :)
     
    IceTrooper likes this.
  11. xiao-xxl

    xiao-xxl

    Joined:
    Nov 16, 2018
    Posts:
    48
    Do you have an Android demo? I want to test its performances before by it.
     
  12. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Try THIS.

    This is the "Trees" demo built with default settings in 2020.3.0f1 and the latest version of D2D. These objects are fairly high resolution and are set to be able to split down very small pieces, but you can always adjust this for even higher performance if required.
     
  13. Supertt18

    Supertt18

    Joined:
    Oct 29, 2018
    Posts:
    3
    Hi @Darkcoder !
    I want to know if this product support with elements in a sprite sheet with Sprite Mode Polygon?
     
  14. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    No, they must use Full Rect packing, because supporting both destruction and polygon shapes makes the code very complicated and could make the rendering slow.
     
  15. Supertt18

    Supertt18

    Joined:
    Oct 29, 2018
    Posts:
    3
    Thanks for information!
     
    Darkcoder likes this.
  16. dangnguyen113

    dangnguyen113

    Joined:
    Aug 8, 2017
    Posts:
    7
    Hi, how to compare 2 sprite, i want check match 2 object, i think use alpha count, right?
     

    Attached Files:

  17. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    I'm not sure what you mean by match. You could compare the "AlphaCount" property in both, but they could have wildly different shapes yet still have the same AlphaCount. The only way to check if they match is comparing the "AlphaData" arrays, but it's very unlikely two destructible objects will have the exact same AlphaData, even if they look very similar. The easiest way to compare them is to downsample them to a smaller texture (e.g. 16x16), and then count the +- pixel difference to give you a total difference.
     
    dangnguyen113 likes this.
  18. dangnguyen113

    dangnguyen113

    Joined:
    Aug 8, 2017
    Posts:
    7
    yes, thank you, and i have a question, how to get object when you click fracture
     
  19. Overcast

    Overcast

    Joined:
    May 16, 2013
    Posts:
    29
    Hi there,

    New to asset- so far so good - couple of questions:

    1) Is there a way to have both the default diffue shader AND acheive the Scortch Marks effect? I am using a 2d scene which requires lighting, and the other shaders that support scortch marks dont support lighting.

    2) Is there an easy way to heal a sprite with a different texture? I can think of complicated ways (like having another completely 'destroyed' sprite that heals over top), but was wondering if there's something simpler I am missing.

    EDIT:
    3) Ideally I am looking for a callback to se if any pixels are affected by a Stamp call, do you have a suggestion as to how I can do that without altering any of your code? As an example, consider your melting suns, and wanting to generate a noise if a pixel or more was melted - Ideally I want this in the 'painter' not the 'painted' (ie, watch pixels doesn't really work for this use case) as I need the context of *what* cut the pixels, and that object needs to be sure it cut at least SOME pixels when it runs Stamp()

    EDIT #2:
    4) Any gotchas you can see if I try and integrate this with PUN? Figure I can just to the Stamps with RPC?

    EDIT #3:
    @Darkcoder :)
     
    Last edited: Oct 17, 2021
  20. Darkcoder

    Darkcoder

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

    Version 3.1.2 of Destructible 2D is now out!
    • Added support for SpriteShape destructible objects.
    • Added support for URP lit shader.
    Enjoy :)



    Sorry for the late reply!

    1 - Destructible 2D comes with several shaders in the "Destructible 2D/Examples/Resources/Destructible 2D" folder. The "Diffuse" one should be a D2D compatible Diffuse shader.

    2 - The heal feature works by healing toward the specified D2dSnapshot component data you have saved. You can make a snapshot of some completely different shape, and then 'heal' toward that.

    3 - See the "46 Monitor Pixels" demo scene.

    4 - As long as the stamps are performed in the same order at the same location on sprites in the same position on all clients then it will look the same on all clients. In some kind of fast paced action game this may not be possible though, so you might want to periodically sync the AlphaData, and if you use splitting you'd need to make sure the right amount of split parts exist.
     
  21. Overcast

    Overcast

    Joined:
    May 16, 2013
    Posts:
    29
    Thanks for your reply! - re the shaders, I actually need the sprite renderers to cast shadows, in the default pipeline. I can do this by using the Unity Standard shader, set to fade - is there a way to make that compatible with d2d?

    As far as I can tell the ONLY way to use a sprite renderer, but have it cast shadows from 3D lights in the default pipeline is to use the standard shader set to fade or transparent, so I would LOVE any guidance on whether I can make that compatible :)

    EDIT: This way doesn't work, only works with 2D lights which dont acheive what I am after - Appreciate the other way to get sprite shadows is to move to URP - im just fairly far in and there's a lot of assets which would need work.

    EDIT 2: I looked at Monitor Pixels again, but had already consulted this - This still does have the interaction I am looking for - What I (think) I need is something for the Stamping Object to be ablew to tell if it cut any pixels from the sprite with that call (Stamp).
     
    Last edited: Oct 21, 2021
  22. Sesilla

    Sesilla

    Joined:
    Jan 3, 2018
    Posts:
    4
    Hi,

    after created fractured clones, is possible to export single png pieces?

    Thanks in advance
    Sesilla
     
  23. ssbbb

    ssbbb

    Joined:
    Jun 3, 2019
    Posts:
    15
    Hi Carlos,

    Is it possible to use Destructible 2D with mesh renderers instead of sprites?

    I am using Spline Terrain 2D for generating my terrain, which uses meshes instead of sprites.

    Is integrating these 2 possible out of the box?

    Thanks!
     
  24. S0ULART

    S0ULART

    Joined:
    Jun 14, 2011
    Posts:
    131

    You could use the Unity build in SpriteShapes.
    The newest version of D2D has support for it now.

    Is there a big difference for you between SpriteShapes and Spline Terrain 2D?
     
  25. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Sorry for the late replies again!

    Unfortunately the Standard shader is too complex/tedious to modify. However, the Better Shaders system makes it very easy to create shaders with various features including D2D support. If you let me know exactly which features from the Standard shader you're using I could export a shader from this system that works with lighting and D2D.

    The best way to find out what pixels a stamp took out from the underlying objects is to use a custom script that stores some state before you apply the stamp, and then the Monitor Pixels setup then passes that data to the previously stored state. Making the stamp itself solely detect what pixels were destroyed would require too many code changes.


    There's no built in feature for this, but it's easy enough from code. Each clone is the same as the parent with the D2dDestructibleSprite component, which has the AlphaTex property. This is a normal Texture2D, so you can just call yourDestructibleSprite.AlphaTex.EncodeToPNG() to get the byte[] data. You could save this to file using the System.IO.File.WriteAllBytes method, etc.


    They might be able to work together. The D2dDestructibleRenderer component is designed to work with any Renderer (e.g. MeshRenderer), so if SplineTerrain2D uses this then it may work.

    NOTE: It looks like I forgot to add a setting to this component to adjust the pixel density, which defaults to 25 pixels per unit. I'll fix this in the next version.

    The biggest challenge is that D2D requires that your destructible objects use a material that supports D2D, which requires some modifications to the shaders you use (assuming SplineTerrain2D uses a custom shader?). The documentation has information about these modifications.

    As S0ULART mentions, the latest version of D2D supports Sprite Shape which should be easier.
     
  26. ssbbb

    ssbbb

    Joined:
    Jun 3, 2019
    Posts:
    15
    That is a good point, it might work as a substitute (if I can't get the integration right). Thank you both!
     
    Darkcoder likes this.
  27. davidwa

    davidwa

    Joined:
    Jun 3, 2014
    Posts:
    8
    Hi,

    I'm using Destructible 2D for a larger destructible terrain. For this, I generate a world texture and then split it up into evenly sized sprites which are made destructible.

    This works fine, but placing the sprites next to each other unfortunately leaves a gap between them.

    gameview.png

    This happens in Scene and Game View, with texture and just these white squares, with the standard D2D Default material and my custom material (which I've built in Shader Graph using the template in the Documentation). It does not happen with Unity's Sprites-Default material, but this breaks destruction of course.

    I'm quite sure that the shader fades out the pixels towards the edge. My shader knowledge is rather limited unfortunately, so I'm not entirely sure what happens in that Shader Graph portion in the docs, but I was able to "fix" this by playing around with the values. Specifically, I changed the "Add" node right in front of the "Saturate" node to add 1 instead of 0.5.

    This stops the seams between sprites, but it produces a new problem. Explosions now create "unhittable" pixels.

    gameviewUnhittablePixels.png

    (No real way of showing this, but I promise I can't hit the three little "islands" in the middle with the Bomb prefab)

    They still do get destroyed by explosions (spawning 2 bombs right on top of it), but can't be directly hit with a single Bomb. I can't reproduce this when reverting my shader change, so I'm quite sure it's because of that.

    Any way to fix the gaps between the sprites properly?
     
  28. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    The edges appear because destructible 2D adds a 1 pixel transparent border around your sprites. This causes the original 'edge' texels to transition to transparent half way through them via bilinear filtering. Normal sprites can have solid edges, which causes the edge texels to retain their opacity, even past the half way point outside the texel center. It's a little hard to explain, but this was added to make the 'optimize' feature look better, otherwise you end up with hard edges in some areas and soft edges at others. You may also be able to fix this by scaling your sprites up by one pixel (+0.5 pixel on each edge).

    The destruction code is completely separate from the sprite rendering, so these issues are unrelated. If you're using D2dPolygonCollider or D2dEdgeCollider then you can select your destructible object(s) and see the collider outlines in the Scene view. You can also see them in the Game view if you enable Game view gizmos.

    The easiest way to prevent these small islands from appearing is to use the D2dSplitter component, which will split your sprite into one sprite per island. You can then destroy islands that fall below your minimum desired pixel count. This is how the "Dynamic Splittable" preset does, where D2dRequirements enables the destruction of the sprite if it's 10 or fewer pixels.

    Just keep in mind splitting sprites is quite an expensive operation, but since you've already split your scene up this may not be an issue. There is one alternative approach that would be faster, but requires quite a bit of custom code.
     
  29. elvin0272

    elvin0272

    Joined:
    Feb 16, 2021
    Posts:
    13
    Hi!
    I can't figure out in any way where I can set up an event when slice cutting a sprite? For example, I have a method void that should occur when cutting.
    I'm was try D2D Requirements, but it doesn't work in any way in "On Requirements Met"
     
  30. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    The D2dDestructibleSprite component has the OnModified, OnSplitStart, OnSplitEnd, and OnModifiedPixels events, which you can hook into from a C# script to perform a custom action. D2dRequirements could be used to detect damage, but it's not so flexible.
     
    elvin0272 likes this.
  31. elvin0272

    elvin0272

    Joined:
    Feb 16, 2021
    Posts:
    13
    Do you mean to change the script D2dDestructibleSprite itself? Because I added OnSplitEnd, Split End and isSplitting to my code and nothing works. I need to specify the action at the end of the cutting in my other element script. I could not call it in your script, because the heritability is different + I have a lot of these instances of elements on the scene
     
  32. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    No, you must hook your methods into the events I mentioned. You must read up on how C# events work to understand how this works.
     
  33. davidwa

    davidwa

    Joined:
    Jun 3, 2014
    Posts:
    8
    Hello again. Hope everyone is having a good holiday.

    I was able to fix my previous issue with seams between my sprites. Unfortunately I'm now having an issue with seams between my colliders. This makes raycasts against my map fail when pointing straight down (which I'm doing for placing items in the world using mouse position).

    collidergap.png

    Unfortunately this extends all the way down (and there's also horizontal gaps btw), so there's really nothing for a raycast to hit.

    collidergap2.png

    Any ideas on how to remedy this?
     
  34. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    You mentioned before that you manually split your sprite up before making it destructible. Are those gaps seen there between your split up parts only?
     
  35. davidwa

    davidwa

    Joined:
    Jun 3, 2014
    Posts:
    8
    Yes, the gaps are between the individual sprites.
     
  36. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    I see, this is probably due to the padding I mentioned before. To fix this either:

    A) Don't manually split your sprite up. Just rely on D2D to do this for you. Though performance may not be the same.
    B) Move your separate sprites together by half a pixel so the gaps disappear.
    C) Use a separate AlphaTex collision shape compared to your visual sprite that is stretched by one pixel on the edges so the collider reaches there.
     
  37. MyFunFair11

    MyFunFair11

    Joined:
    Jan 12, 2015
    Posts:
    4
    Is it possible to make sprite shape with collider and destructible? D2D Polygon Collider doesn't seem to work with sprite shape
     
  38. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Works for me. See the attached package.
     

    Attached Files:

  39. MyFunFair11

    MyFunFair11

    Joined:
    Jan 12, 2015
    Posts:
    4
  40. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    Sorry for the late reply. Could you send me an email or private message with a .unitypackage of this SpriteShape + a demo scene with it in before you make it destructible, so I can test it myself?
     
  41. elvin0272

    elvin0272

    Joined:
    Feb 16, 2021
    Posts:
    13
    Hi. I buy this asset and used it in the project. Everything worked very well, but when I started testing the save system and deleted all persistentDataPath, the error "Link already exists!" (in CwInputManager 202) constantly comes out. What should I do?
     
  42. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    What save system? Without being able to replicate the issue it's hard to say what's happening.
     
  43. elvin0272

    elvin0272

    Joined:
    Feb 16, 2021
    Posts:
    13
    Easy Save 3. I just deleted the cache and saved the file data, but this error also started coming out of Debug.Log
     
  44. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    I see. I've never used this asset, it sounds like it's saving and loading the input manager data which causes the errors. Maybe there's a way to have it not save the input manager?
     
  45. elvin0272

    elvin0272

    Joined:
    Feb 16, 2021
    Posts:
    13
    I don't know, but I'll try this. It's just strange that everything worked and after I deleted the file with the gameplay saves, an error with input started coming out
     
  46. werorojo121

    werorojo121

    Joined:
    Mar 26, 2022
    Posts:
    1
    Hello, I recently got the Asset (impressive by the way) and I have a question, I want to create a bomb that when it hits the ground has a detonation time of, for example, 5 seconds and when the time expires it explodes and destroys the objects, I have been trying and I don't know how to implement it using the Asset, could you help me? please
     
  47. chimchim

    chimchim

    Joined:
    Sep 20, 2013
    Posts:
    9
    Hello, i often see these gaps between the collision lines. Is this fixable ?
    upload_2022-3-27_23-19-47.png
     
  48. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,404
    This would require a custom script to handle the 5 second timer. The D2dImpactSpawner component has the OnImpact event, which can be used to begin your timer component. Once the timer completes, it could destroy your bomb, and then spawn an explosion prefab. Maybe I could add some kind of timer script to the next version.


    Can you send me a .unitypackage containing only a demo scene + your problem sprite configured with D2D so I can replicate the issue?
     
    werorojo121 likes this.
  49. elvin0272

    elvin0272

    Joined:
    Feb 16, 2021
    Posts:
    13
    I realized that the error is displayed in the simulator plugin Unity. Slice also doesn't work in a packaged android on phone. At the same time, everything works on the Unity game viewport and the error is not displayed. The error refers to the method public static T Create<T>(list of links<T> links, finger finger). "Link already exists!" Does this mean that the input method is saved under the mouse and therefore it cannot replace it with a finger?
     
  50. chimchim

    chimchim

    Joined:
    Sep 20, 2013
    Posts:
    9
    Can you send me a .unitypackage containing only a demo scene + your problem sprite configured with D2D so I can replicate the issue?[/QUOTE]

    I get this problem by just, downloading the package. Opening 01 Destructible sprite scene. Adding edge collider to castle sprite. Thank you for answering to fast.
    upload_2022-3-28_9-34-30.png

    Also. When you destroy a bit of the map. Do you recalculate all colliders or just the affected colliders where the pixels were removed ?
    I would like to extract the collision calculation and handle the pixel removal myself. Is this possible?
     
    Last edited: Mar 28, 2022