Search Unity

Bug SpriteAtlas variant scale, breaks spriteshape

Discussion in '2D' started by ThroughlineFums, Feb 1, 2023.

  1. ThroughlineFums

    ThroughlineFums

    Joined:
    Oct 21, 2021
    Posts:
    3
    Setting a sprite shape variant to anything other than 1, and using sprites from that sprite atlas in a SpriteShapeProfile, breaks their visuals. As in, it finds pixels outside of the rect of the sprite on the atlas and displays those, if it is even displaying anything. My reasoning for using sprite shape variant scale is to save memory on lower performance devices but otherwise have everything in the scene set up the same, but is this a misunderstanding of the Sprite Atlas Variant scale feature? My Expectations would be that the overall shape of the "rock" would be the same and size, but the resolutions of it would take a severe hit. The examples are from a new clean quickly built test project, SpriteAtlas V2 - Enabled

    SpriteShape when variant scale is 1

    SpriteShape when variant scale is 0.5

    Sprite shape when variant scale is 0.25


    And master, variant and sprite shape profile incase it's something there.


     
  2. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    SpriteShapeRenderer does not yet support variants. However we will be working on this soon and hopefully will post an update in this thread. Other 2D Renderers does support Variants. Thanks.
     
  3. ThroughlineFums

    ThroughlineFums

    Joined:
    Oct 21, 2021
    Posts:
    3
    Thanks for the response :) For anybody wanting this now, you can do something like below and add it to the Custom Geometry Modifier of the SpriteShapeController. With scale being the same as the variant scale you'd use.

    Code (CSharp):
    1. public class SpriteshapeCustomGeometryScaler : SpriteShapeGeometryModifier
    2. {
    3.     public const float scale = 0.4f;
    4.  
    5.     public struct Jobs
    6.     {
    7.         public JobHandle jobHandle;
    8.     }
    9.     public List<Jobs> jobs = new List<Jobs>();
    10.     public override JobHandle MakeModifierJob(JobHandle generator, SpriteShapeController spriteShapeController, NativeArray<ushort> indices,
    11.         NativeSlice<Vector3> positions, NativeSlice<Vector2> texCoords, NativeSlice<Vector4> tangents, NativeArray<SpriteShapeSegment> segments, NativeArray<float2> colliderData)
    12.     {
    13.         var mj = new UVScalerJob() { uvs = texCoords};
    14.    
    15.         var jh = mj.Schedule(generator);
    16.         jobs.Add(new Jobs() { jobHandle = jh });
    17.         return jh;
    18.     }
    19.     public struct UVScalerJob: IJob
    20.     {
    21.         public NativeSlice<Vector2> uvs;
    22.  
    23.         public void Execute()
    24.         {
    25.  
    26.             for (int i = 0; i < uvs.Length; i++)
    27.             {
    28.                 var uv = uvs[i];
    29.                 uv.x *= scale;
    30.                 uv.y *= scale;
    31.                 uvs[i] = uv;
    32.             }
    33.         }
    34.     }
    35. }
     
    Venkify likes this.
  4. RedArtGames

    RedArtGames

    Joined:
    Dec 16, 2021
    Posts:
    5
    Thanks a lot @ThroughlineFums ! This has been super helpful because we had the same issue as yourself.
    Unfortunately for us, it seems that it only works for Closed Shapes and not Open Shapes. Did you have the same issue on your side ?

    EDIT: forget what I said, it works for Open Shapes too, we had another issue. Thanks again for this wonderful fix.
     
    Last edited: Mar 31, 2023