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

[RELEASED] Sprite Slicer - 'Fruit Ninja'-style sprite cutting system

Discussion in 'Assets and Asset Store' started by mrsquare, Nov 20, 2013.

  1. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hi folks,

    I'm pleased to announce the release of Sprite Slicer on the Unity Asset Store

    Web Player Demo

    Tutorial Video

    Sprite Slicer allows you to cut any physics enabled sprite into little pieces, each of which then behaves as its own independent physics object. Its perfect for bringing your 2D game to life with destructible objects and environments, or for creating a Fruit-Ninja style cutting game.

    * Works on both Unity and 2D Toolkit sprites.
    * Simple static script functions allow you to slice sprites along a given line, or explode them into multiple parts.
    * Optimised to preserve dynamic batching and reduce draw calls.
    * Slices any sprite with a 2D box, circle or convex polygon collider
    * Full C# source code and example scene included


    The code has been designed to be as transparent as possible - there are no new gameobjects to create; you simply pass a cut start and cut end point to one of the static Sprite Slicer functions, and the code will handle the rest. Once cut, each child sprite behaves as its own separate physics body, retaining all the physics and rendering properties of the parent object, and can itself be cut into further child objects. If required, the slicing functions can return a list of all the cut objects along with their child objects and cut positions, so that the user's code can perform additional processing once the cut has taken place. Equally, you can delay destruction of the parent object in the event that you need to access variables from it before it is destroyed.

    Screenshots




    I hope this proves helpful to some people out there! Please feel free to reply in this thread or PM me if you have any questions :)
     
    Last edited: Dec 4, 2013
    jonf-io likes this.
  2. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Some guys over on Reddit were asking for some example of the interface, so this might be of interest to some folk here too:

    Code (csharp):
    1.  
    2. static void SliceAllSprites(Vector3 worldStartPoint, Vector3 worldEndPoint);
    3. static void SliceAllSprites(Vector3 worldStartPoint, Vector3 worldEndPoint, bool destroySlicedObjects, ref List<SpriteSlicer2DSliceInfo> slicedObjectInfo);
    4. static void SliceSprite(Vector3 worldStartPoint, Vector3 worldEndPoint, GameObject sprite);
    5. static void SliceSprite(Vector3 worldStartPoint, Vector3 worldEndPoint, GameObject sprite, bool destroySlicedObjects, ref List<SpriteSlicer2DSliceInfo> slicedObjectInfo);
    6.  
    So you have the option of slicing any sprite that intersects the vector or you can pass through a specific gameobject if you know exactly what it is that you want to cut. The demo is using the first option, which is why you can get multiple things getting cut if you move the mouse fast enough.

    SpriteSlicer2DSliceInfo is an optional parameter, which will get filled out with information on which slice generated which child objects from which parent object, so you can do any extra processing as required (eg. add particle effects at the cut positions, increment a score depending on what type of object the parent was)

    The explosion code is as follows:

    Code (csharp):
    1.  
    2. static void ExplodeSprite(GameObject sprite, int numCuts, float explosionForce);
    3. static void ExplodeSprite(GameObject sprite, int numCuts, float explosionForce, bool destroySlicedObjects, ref List<SpriteSlicer2DSliceInfo> slicedObjectInfo);
    4.  
    It basically slices the object multiple times at random angles through the centre, and then applies an outward force to each child object away from the centre. I've got some ideas for improving this - specifically running a triangulation algorithm over the mesh and then creating the child objects from that. Should make a more pleasing shattering type effect with more evenly distributed polys (its a bit 'chunky' at the moment).
     
  3. I am da bawss

    I am da bawss

    Joined:
    Jun 2, 2011
    Posts:
    2,574
    Fabulous! Looks absolutely fabulous! :D



    Definitely on my shopping list!

    Just one question - what's Toolkit 2D? Do you mean Unity's own 2D system or 2D TOOLKIT?



    BTW, if you want to attache screenshot, its better you just show them inside your post using [/B] tag.
    It saves the user from having to click it. ;)
     
  4. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hah - thank you so much, I'd assumed that images were disabled on the boards or something - was just caused by my profile settings! Oops :p

    Yeah, sorry, I mean 2D Toolkit - have amended the original post. The 2D Toolkit code internally calls everything TK2D, so I've got into the habit of calling it that :)
     
  5. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,422
    This is great. Going to pick this up soon!
     
  6. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Added a little tutorial video to the first post :)
     
  7. sirius_002

    sirius_002

    Joined:
    Aug 16, 2012
    Posts:
    65
    Hi mrsquare, looks great, but does this have playmaker support?
     
  8. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    I haven't added Playmaker support personally, but I was contacted by some of the Playmaker guys a while back and sent them a copy of the asset so that they could integrate the two systems. I never actually heard back from them though, so not sure what the current status is.

    Playmaker thread about it here:
    http://hutonggames.com/playmakerforum/index.php?topic=5638.0

    I'll PM jeanfabre on the Playmaker forums and see if he has an update.
     
  9. sirius_002

    sirius_002

    Joined:
    Aug 16, 2012
    Posts:
    65
    Cool, thanks for the info, I'll keep checking the thread then.
     
  10. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    I'm curious how this plugin works. I'm thinking you
    - create 2 new meshes from the existing sprite rectangle and the line intersect points
    - create a new material at runtime using some custom shader and using the texture from the original sprite
    - uv map new meshes
    - generate new game objects with new meshes
    - create polygon colliders to match the meshes

    Is that close to truth? Btw I totally understand if you want to withhold this information and make me buy it to find out :p
    All your assets look really awesome, congrats!
     
  11. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    Hi,

    my aplogies it went off my radar. I had everything! I just wanted to push one more thing, and provide a 100% playmaker swipe management for the interaction, but I guess my brain shut down that task :) so SpriteSlicer is now avaible in playmaker, and to provide the desired effect I also added an action to go trhough all the pieces generated so you can add force, delete the previous parent pieces etc etc.

    https://hutonggames.fogbugz.com/default.asp?W1177

    Sorry for the delay:)

    Bye,

    Jean
     
  12. sirius_002

    sirius_002

    Joined:
    Aug 16, 2012
    Posts:
    65
    Hi Jean, thanks for going the extra mile on this, much appreciated!
     
  13. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Yep, that's almost exactly it :) There are lots of online tutorials that go into how to slice a Box2D polygon along a given vector (Ray Wenderlich has a particularly good cocos2D one) - Sprite Slicer is essentially a C# implementation of that algorithm, and then all the extra stuff on top to work with Unity materials, gameObjects etc. But yeah - not too hard to implement yourself if you've got a bit of coding knowledge.
     
    Last edited: Mar 13, 2014
  14. TheValar

    TheValar

    Joined:
    Nov 12, 2012
    Posts:
    760
    Thanks for sharing! I guess the real challenge would be getting it to work properly with concave objects where certain slices should result in more than 2 objects. Maybe you could analyze the texture data to find isolated groups of pixels after the initial cut or something? Definitely not a trivial problem
     
  15. detour19

    detour19

    Joined:
    Dec 3, 2012
    Posts:
    9
    Hi, I'm using Sprite Slicer and love it! I have one issue which is with usage with the PolygonCollider2D. I have sprites that are generated at runtime and then add the PolygonCollider2D component to them, at which time Unity automatically traces the edges of the sprite and sets the vertices for me. The problem is, for pretty much every shape I have, some of which are simple roundish object, others more complex, Sprite Sliver complains that none of them are convex and thus won't slice them. I then tried disabling the convex check by sedating s_AllowConvexSlicing to true. This works great in the Unity editor, but once I put it on an iPad, you see some very weird textures appear after slicing, and unexpected shapes. Perhaps I should be manually setting the vertices when I add the collider? Will that even work or will it still likely be seen as not convex?
     
  16. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hey,

    Yeah, Unity generates a *lot* of vertices (far more than it really needs to) when its automatically tracing a sprite. If you zoom in loads you'll probably see a couple of rogue vertices that are causing it to be a concave shape even it doesn't appear to be one visually.

    I don't really recommend using the s_AllowConvexSlicing flag (oops - that should be s_AllowConcaveSlicing shouldn't it!) - it might work acceptably on a very few concave objects, but in general its going to cause very weird issues as you've seen.

    If you can add the vertices yourself rather than letting Unity do it, that would probably be the ideal solution, although if you have a look in the Sprite Slicer code, there's a static 'MakeConvex()' function which attempts to repeatedly trim off vertices from the polygon collider until it becomes convex. Currently this is designed for use in the editor (it's attached to Tools->Sprite Slicer->Make Convex button), but it shouldn't be hard to make it work at runtime too.
     
  17. detour19

    detour19

    Joined:
    Dec 3, 2012
    Posts:
    9
    Yes, I just noticed your editor script. That is super handy! I just brought each sprite into a scene, ran the MakeConvex function, did a little hand tweaking and then exported the vertices to a data file I load at runtime and use to set the collider vertices upon creation. Thanks for making that available.
     
  18. RyuK

    RyuK

    Joined:
    Feb 12, 2014
    Posts:
    55
    Hi, I'm playing with Sprite Slicer and I added CircleCollider2D to an object (Fixed Angle is checked in Rigidbody2D) and tried to use SpriteSlicer2D.SliceAllSprites, but it couldn't be sliced saying
    But sometimes it works and the sprite is destroyed (not sliced, just destroyed. Objects might be too small? The radius of the circle collider is 0.4)
    Explosion always works though.
    SliceManager.cs is my test code that was just copied from the video tutorial. Is there any workaround?

    EDIT: Setting a BoxCollider2D worked, but Polygon Collider couldn't be cut either.
     
    Last edited: Apr 25, 2014
  19. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hi - the fact that ExplodeSprite works and SliceAllSprites doesn't suggests that the problem isn't actually with the object you're trying to cut, but that another invalid object is getting included in the slicing operation. Are there some other concave physics bodies nearby in the scene that might be getting intersected by the slice vector?

    You could try using SliceSprite() instead, if you're only wanting to slice that one particular sprite.
     
  20. detour19

    detour19

    Joined:
    Dec 3, 2012
    Posts:
    9
    I'm seeing an issue where certain sprites I have can't be sliced at certain angles. The sprites get instantiated and a polygon collider attached with points data that I've tweaked by hand. The process is, I add a sprite in the scene, add the collider, select it, run the MakeConvex script. Inevitably I have to adjust some points to make it match the shape, and then I run MakeConvex again. I then export the points to a text file, and then load them in later at runtime. Most of my objects slice just fine, but a few won't slice at certain angles, I get the warning that vertices were rejected. Any way I can tell whether a sprite will be sliceable from all angles of approach?
     
  21. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hey,

    Hmm, that is quite odd - I wonder if something weird has happened to the vertices due to running the MakeConvex script. Its hard to know without being able to look at it myself - I don't suppose you'd be able to send me either a copy of your project or a little test scene that demonstrates the problem?
     
  22. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    on this

    GameObject objv = GameObject.Find ("Barrel");
    SpriteSlicer2D.ExplodeSprite(objv, 5, 8000.0f, true, ref m_SlicedSpriteInfo);

    and nothing happen
     
  23. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Really? Where are you calling that from?

    In the demo scene, with no other changes, if I just replace the contents of SpriteSlicer2DDemoManager.cs with:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. public class SpriteSlicer2DDemoManager : MonoBehaviour
    6. {
    7.     List<SpriteSlicer2DSliceInfo> m_SlicedSpriteInfo = new List<SpriteSlicer2DSliceInfo>();
    8.  
    9.     void Update ()
    10.     {
    11.         if(Input.GetMouseButtonDown(0))
    12.         {
    13.             GameObject objv = GameObject.Find ("Barrel");
    14.             SpriteSlicer2D.ExplodeSprite(objv, 5, 8000.0f, true, ref m_SlicedSpriteInfo);
    15.         }
    16.     }
    17. }
    18.  
    Then when I left click, the barrel explodes.
     
  24. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    thank's i think's it's because rigidbody is set in kinematic it's working now
     
  25. alvinphu

    alvinphu

    Joined:
    May 24, 2013
    Posts:
    2
    So I've been having a problem getting this to work on iOS. It looks great on PC though. Everytime I try to slice a Sprite it just straight hangs, whether I'm setting a destroy time, or not, or not even destroying it at all.

    *edit*
    Even trying the demo scene completely hangs whenever I try to slice. Is there some kind of project setting that's required to have this work? Update maybe?

    http://i.imgur.com/yaj7gYG.jpg

    There seems to be a problem with

    public void InitFromUnitySprite(SpriteRenderer unitySprite, PolygonCollider2D polygon)
    for(int loop = 0; loop < s_MaterialList.Count; loop++)
    {
    if(s_MaterialList[loop].mainTexture.GetInstanceID() == unitySprite.sprite.texture.GetInstanceID())
    {
    material = s_MaterialList[loop];
    }
    }

    Specifically:
    if(s_MaterialList[loop].mainTexture.GetInstanceID() == unitySprite.sprite.texture.GetInstanceID())

    where s_MaterialList[loop].mainTexture is null, it's being added to the material list correctly but being destroyed prematurely

    I also forgot again, I'm on the latest Unity3D Pro, trying it on an iPhone5, and trying to slice normal SpriteRenderer Sprites, although your demo isn't much different. Also tried turning off Dynamic Batching, Static Batching, and Optimize Mesh Data to no avail.
     
    Last edited: Jul 1, 2014
  26. detour19

    detour19

    Joined:
    Dec 3, 2012
    Posts:
    9
    I am having this exact same problem, crashing on an iPad when built from Unity 4.5.2 Pro. The s_MaterialList[loop].mainTexture is giving a null reference exception. It runs fine in the editor however.
     
  27. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Heya - if you take a look at the InitFromUnitySprite function in SpriteSlicer.cs, could you try changing:

    Code (csharp):
    1. material.mainTexture = unitySprite.sprite.texture;
    to

    Code (csharp):
    1. material.SetTexture("_MainTex", unitySprite.sprite.texture);
    And see if that helps?
     
  28. detour19

    detour19

    Joined:
    Dec 3, 2012
    Posts:
    9
    My apologies! Apparently I updated to the 1.146 version a while back, but failed to actually bring it into the project. It is working properly now on devices in 4.5.2. Thanks!
     
  29. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Phew! :) Cool - yep, this was a known issue, but should be fixed in the latest version. Thanks for letting me know!
     
  30. seansteezy

    seansteezy

    Joined:
    Nov 28, 2013
    Posts:
    122
    Hey @mrsquare! I'm just getting started with your toolkit for sprite slicing and I am mainly interested in the exploding part of it. SO far I am having fun :)

    My question/issue is that I want to destroy a 2d building using your sprite slicer, but I only want to explode chunks out of the entire sprite at a time. So basically, instead of clicking, I use a cannonball object, it has velocity and mass, etc, the point of collision is where the explosion will trigger, but here is where I am clueless... I just want to explode some of the sprite around the point of impact... it's a big castle, and I want to blow a chuck out of it instead of exploding the entire sprite.

    I see you were saying your sprites/explosions originated in the center of the sprite, so would there be a way to 1) move where the center of the explosion takes place and 2) limit the radius of the explosion so that some/most of the sprite is still intact?

    Any help or direction you could give me would be much appreciated!

    Thanks

    -ss
     
  31. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Heya,

    One of the limitations of the algorithm that I'm using is that it has to slice the object into two pieces each time you make a cut - very good for doing Fruit Ninja type stuff but unfortunately not so good if you just want to slice a small chunk out of something.

    The explosion code that Sprite Slicer provides has the same limitation, so it always has to explode the whole sprite, it can't just destroy part of it. Essentially it just cuts the sprite multiple times at random angles at random offsets from the centre, so you end up with lots of little random shards. To do what you're describing, you'd basically need to work out where you wanted the explosion to be, then somehow create two new sprites - one representing the area to be exploded and the other representing the rest of your sprite. You could then run the Sprite Slicer explode code on the bit to be exploded.
     
  32. Recon03

    Recon03

    Joined:
    Aug 5, 2013
    Posts:
    845
    This is what I been looking for for ever, never seen this one, most don't support 2d tool kit...so I never buy them... Thanks, I will pick up later today or tomorrow.
     
  33. khamosh132

    khamosh132

    Joined:
    Jan 15, 2016
    Posts:
    1
    I am trying to make it work for Concave slicing. How do you propose I can achieve that?
     
  34. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hi - this isn't currently supported, but will hopefully be added very very soon :)

    I've found a nice concave slicing algorithm that seems to be producing good results. I still need to test it out a bit more and tidy up my implementation, but it's looking promising! If all goes well, I hope to have a new version uploaded by the weekend at the latest.

    Give me a PM if you need it urgently and I can try to sort you out with a work-in-progress version.


    *EDIT* Aaaaand submitted. v.1.3 should be out in a few days, with support for concave polygon slicing :)
     
    Last edited: Feb 24, 2016
  35. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    Hi, there is a straightfoward bug on line 438

    Code (CSharp):
    1. childSprite.InitFromTK2DSprite(
    2.       parenttk2dSprite, ref childCollider, ref childSpriteVertices
    3.       );
    looks like it takes FOUR arguments, not THREE. I can't figure out what the fourth bool should be in this case, true false or something else.

    I have emailed a couple times, perhaps you are not getting the email.

    (Actually, I've been trying to get it to work with tk2dSpriteFromTexture, after the above problem is resolved.)

    Hope you guys get this message .. it's an absolutely fantastic asset and it's a shame I can't get past this very minor woe!!
     
  36. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hey - apologies, don't seem to have been getting your emails apart from the most recent one.

    Sorry - my fault for not testing with TK2D_SLICING_ENABLED enabled. The line should just be:

    Code (csharp):
    1. childSprite.InitFromTK2DSprite(parenttk2dSprite, ref childCollider, ref childSpriteVertices, false);
    Thanks for the heads up - I'll get an update pushed out to the Asset Store asap.
     
  37. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    Code (csharp):
    1. childSprite.InitFromTK2DSprite(parenttk2dSprite, ref childCollider, ref childSpriteVertices, false);
    Got it , false --

    Say, have you ever tried with specifically tk2dSpriteFromTexture

    it's a bit different from a normal tk2D sprite .......... but incredibly useful
     
  38. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    I just tried slicing a tk2dSpriteFromTexture and it was being sliced correctly but the sliced objects were purple (indicating a missing material) - was that the problem you were seeing?

    If so, this seems to fix it:

    In InitFromTK2DSprite, change line 1740 to this:

    Code (csharp):
    1. InitSprite(sprite.gameObject, sprite.GetComponent<MeshRenderer>(), refpolygon, refpolygonPoints, spriteDefinition.uvs[0], spriteDefinition.uvs[spriteDefinition.uvs.Length - 1], spriteDefinition.GetBounds(), sprite.GetComponent<MeshRenderer>().material, isRotated, isHFlipped, isVFlipped, Vector2.zero, pivotVector, isConcave);
    so I've just switched
    Code (csharp):
    1. spriteDefinition.material
    to
    Code (csharp):
    1. sprite.GetComponent<MeshRenderer>().material
    Hope that helps :)
     
  39. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    Hi,

    1) the change at line 438, for `tk2dSpriteFromTexture`, ("false") works perfectly

    2) the change at line 1740, for `tk2dSpriteFromTexture`, (via MeshRenderer) works perfectly

    thanks!!!!!!!!!!!!

    3) FYI I can't get the fade-out to work on TK sprites: I bet because of the alpha, I'll have a look

    4) I wonder if it would be worth slightly modifying it to, simply, look at the Component and decide if it is a Unity sprite, TK sprite or TK-from-texture sprite, and have it thus work automatically on all those types. (Rather than commenting out that first line, you know.)

    5) FYI anyone reading I like the outwards-from-center explosive effect (seen in Shatter) so I modifed around line 300 like this to get that ...

    Code (CSharp):
    1.  //Vector2 here = childRigidBody.transform.position; - doesn't work of course,
    2. //chard centers remain unchanged from parent center
    3. Vector2 here = childRigidBody.GetComponent<Renderer>().bounds.center;
    4. Vector2 forceDirection = here - parentCentre;
    5. forceDirection.Normalize();            
    6.                          
    7. //childRigidBody.AddForceAtPosition(new Vector2(0.0f, 1.0f) * explosionForce, centre);
    8. childRigidBody.AddForceAtPosition( forceDirection * explosionForce, centre);
    6) I was thinking surely in many cases one would not want nor need a collider attached to the end shards: I'll modify that myself but perhaps it would be a useful option? (Maybe a bool inspector variable would be good, "Don't bother with colliders on resulting shards" .. you know.)
     
  40. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    say @mrsquare .. a new version just arrived in the asset store 1.31

    in fact did these changes make it ..
    1) the change at line 438, for `tk2dSpriteFromTexture`, ("false") works perfectly
    2) the change at line 1740, for `tk2dSpriteFromTexture`, (via MeshRenderer) works perfectly​

    not a problem, just checking - cheers!

    PS reminder, I believe the "fade out" does not work with TK sprites (either type of TK sprite)
     
  41. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hey,

    First one did - second one will be in the next update.

    You're using the provided 'Fade and Destroy' script, right? Check your output error message - you're probably seeing this error:

    Material doesn't have a color property '_Color'
    UnityEngine.Material:get_color()

    The problem is your shader - you either need to be using one with an exposed '_Color' property, or modify the shader to add it yourself. Here is a version of the tk2D BlendVertexColor shader with a _Color property added, if you need an example of how to do that. Changes are:

    Line 9: Add an exposed _Color variable.
    Line 28: Declare the _Color variable for use by the fragment shader.
    Line 55: Multiply the output color by _Color.

    Code (csharp):
    1. // unlit, vertex colour, alpha blended
    2. // cull off
    3.  
    4. Shader "tk2d/BlendVertexColor"
    5. {
    6.     Properties
    7.     {
    8.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    9.         _Color ("Main Color", Color) = (1,1,1,1)
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    15.         ZWrite Off Lighting Off Cull Off Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha
    16.         LOD 110
    17.      
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert_vct
    22.             #pragma fragment frag_mult
    23.             #pragma fragmentoption ARB_precision_hint_fastest
    24.             #include "UnityCG.cginc"
    25.  
    26.             sampler2D _MainTex;
    27.             float4 _MainTex_ST;
    28.             fixed4 _Color;
    29.  
    30.             struct vin_vct
    31.             {
    32.                 float4 vertex : POSITION;
    33.                 float4 color : COLOR;
    34.                 float2 texcoord : TEXCOORD0;
    35.             };
    36.  
    37.             struct v2f_vct
    38.             {
    39.                 float4 vertex : POSITION;
    40.                 fixed4 color : COLOR;
    41.                 float2 texcoord : TEXCOORD0;
    42.             };
    43.  
    44.             v2f_vct vert_vct(vin_vct v)
    45.             {
    46.                 v2f_vct o;
    47.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    48.                 o.color = v.color;
    49.                 o.texcoord = v.texcoord;
    50.                 return o;
    51.             }
    52.  
    53.             fixed4 frag_mult(v2f_vct i) : COLOR
    54.             {
    55.                 fixed4 col = tex2D(_MainTex, i.texcoord) * i.color * _Color;
    56.                 return col;
    57.             }
    58.          
    59.             ENDCG
    60.         }
    61.     }
    62.  
    63.     SubShader
    64.     {
    65.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    66.         ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off Fog { Mode Off }
    67.         LOD 100
    68.  
    69.         BindChannels
    70.         {
    71.             Bind "Vertex", vertex
    72.             Bind "TexCoord", texcoord
    73.             Bind "Color", color
    74.         }
    75.  
    76.         Pass
    77.         {
    78.             Lighting Off
    79.             SetTexture [_MainTex] { combine texture * primary }
    80.         }
    81.     }
    82. }
    83.  
    84.  
     
  42. ayushsingla991

    ayushsingla991

    Joined:
    Oct 1, 2016
    Posts:
    14
    How can i slice curves through the sprite. I can see only straight lines. Is there any way?
     
  43. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hi - sorry, curved slices aren't supported. The algorithm used requires each slice vector to have two points (start + end), both of which must lie outside of the object to be sliced - it then bisects the object along that line and divides it into two pieces.
     
  44. sfilo

    sfilo

    Joined:
    Oct 23, 2012
    Posts:
    30
    Hi,
    Is there an easy way not to destroy the exploded sprite and just to stop rendering it in order to show it again later. Also, is there a way not to add colliders to exploded sprite shards.

    Thank you.
     
  45. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hey - yep, absolutely. When calling the slicing functions, there is a 'destroySlicedObjects' argument that you can pass down to tell the function to tell the code whether to destroy the objects that get sliced or not. It defaults to 'true', IIRC, but if you pass down 'false' then the sliced objects will be deactivated but not deleted.

    Using the demo code as an example, you just need to do this:

    Code (csharp):
    1. SpriteSlicer2D.SliceAllSprites(m_MousePositions[loop].m_WorldPosition, m_MousePositions[m_MousePositions.Count - 1].m_WorldPosition, false, ref m_SlicedSpriteInfo);

    As for not having colliders on the exploded objects - the easiest approach is probably just to iterate over the fragments and disable the colliders on each. Again, taking the demo scene code as an example:

    Code (csharp):
    1. SpriteSlicer2D.ExplodeSprite(rayCastResult.rigidbody.gameObject, 16, 100.0f, true, ref m_SlicedSpriteInfo);              
    2.  
    3. foreach(SpriteSlicer2DSliceInfo sliceInfo in m_SlicedSpriteInfo)
    4. {                    
    5.     // Iterate over all the fragments and turn off the collider
    6.     foreach(GameObject childObject in sliceInfo.ChildObjects)
    7.     {
    8.         childObject.GetComponent<Collider2D>().enabled = false;
    9.     }
    10. }
     
  46. Didjridoo

    Didjridoo

    Joined:
    Jun 30, 2017
    Posts:
    2
    Hi. Tell me please, is it possible to adapt your module to create a game in the style of iSlash?

     
  47. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hey - yeah, that looks like it should be easily do-able with little to no changes required. I've not played the game, but from your video, the play area appears to just be a flat polygon that the player can linearly slice in two with each move, which is exactly what sprite slicer is designed to do.
     
  48. jonf-io

    jonf-io

    Joined:
    Feb 26, 2016
    Posts:
    2
    Hello!
    Great module! Been using it extensively in my latest project. But I want to slice objects with a specified width. Is this possible?
     
  49. mrsquare

    mrsquare

    Joined:
    Oct 25, 2013
    Posts:
    281
    Hi - could you expand on that a bit please, I don't quite understand what you're meaning.
     
  50. jonf-io

    jonf-io

    Joined:
    Feb 26, 2016
    Posts:
    2
    Sorry for the late reply!

    By default the cut is pixelperfect and divides perfectly. But is it possible for the cut to be 3 pixel in width? I made an image with example cuts with varying widths to demonstrate what I mean. I hope this cleared it up! :)

    http://i.imgur.com/sejvHxz.jpg