Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Other [4.0.1] NSprites - sprite rendering package

Discussion in 'Entity Component System' started by Tony_Max, Nov 29, 2022.

  1. Sirius64

    Sirius64

    Joined:
    Jan 15, 2020
    Posts:
    7
    Ah, no, now I get it. You must be right, my apologies - I assumed that unity doesn't create prefabs at runtime itself.
    But it definitely does. And since registration also runs on prefabs you already have prefabs that already registered, so if I had slowdown previously it was something else.. Huh.
     
    Tony_Max likes this.
  2. showwho

    showwho

    Joined:
    Jun 18, 2013
    Posts:
    19
    Hi, In the Age-of-Sprites sample project have some errors
    1. AndroidTargetFramerateController missing script
    2. bulid android il2cpp, have a error
    upload_2023-3-4_16-43-33.png

    unity version:2022.2.8f1
    entities version:1.0.0-pre.44
     
    Last edited: Mar 4, 2023
  3. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    Hi! Thank you for pointing me to this. I was testing framerate, because of problems with flickering units of some user and forgot to remove those changes. Will fix this as soon as I can.
     
  4. showwho

    showwho

    Joined:
    Jun 18, 2013
    Posts:
    19
    thanks for your reply!
    when i build android, but nothing happened, just show background.
    upload_2023-3-4_16-59-8.png
     
  5. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    Fixed that - update Foundation package

    After all changes can't reproduce this issue on android. Can you check with all updates? Update NSprites / Foundation / Age-of-Sprites

    Also want to share that in new NSprites v3.0.0 you can now use any blittable component as shader properties. Previously only 1-field component can be used.
     
  6. floppygames

    floppygames

    Joined:
    Feb 4, 2015
    Posts:
    2
    Hi I downloaded your age-of-sprites from github. And I built and ran it with Android, but I only get a blank screen that doesn't render anything(just camera background color). Have you checked your Android build recently?

    The package version was

    NSprites 3.0.4
    NSprite Foundation 1.0.0
    Entities 1.0.0-pre.47
     
  7. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    378
    What is your device? Does it support Vulkan? Or OpenGL ES 3.2? The shader used in this project requires the device to properly support StructuredBuffer.

    You can also open this
    RegularNSprites_Shader.shader
    (inside NSprites Foundation package) and remove this line

    Code (CSharp):
    1. #pragma exclude_renderers gles gles3 glcore
    then build again and see if your device can properly show the sprites. You would know immediately if it does not.
     
  8. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    Seems it is time to bring some compatibility table to readme
     
  9. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    Yes, before any version I build Android / Windows and test. It is not first time I see such issues but can't reproduce it on my device, so I'm very interested to dig inside it problem.
     
  10. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    378
    I've run Age of Sprites on my Android 9 device and it only shows a complete mess of color blocks with colors flying everywhere. Haha.
     
  11. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    378
    On the journey to find an alternative solution, I've found this yesterday on a thread about particle system (?). Unfortunately I can't find the thread again, but here is the original repo:
    https://github.com/vadersb/Unity_SpriteBatcherTest

    I've run some tests on Android 9 emulators (with LDPlayer is the requirement) and the result is very promising. No issue with shader because this solution uses the built-in Sprite shader.

    Another thing I've just found out today is Mesh Data, which might help more, but I don't have time to experiment with it now.
    https://docs.unity3d.com/2022.3/Documentation/ScriptReference/Mesh.MeshData.html
     
  12. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    This is (as I understand) completely different solution which just does the same as unity - batching sprites in same mesh and then render it within 1 drawcall.
    MeshData
    can help to construct this complex mesh in more performant way, again as I understand.

    I doubt such solution can be more performant then instancing, but it requires metrics to know. The good thing it works without special shader and solves your problem with Android 9.

    Also I've read again our discussion and it contains not answered questions which might solve the problem of using instancing with Android below 10 version.
     
    Last edited: Jun 4, 2023
  13. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    378
    Yes, for mobile platform, the wider range of devices to be supported the better. Also this solution stays consistently around 58fps on LDPlayer 9 for rendering 2000 sprites, which is good enough for my project.
     
  14. Marvin0328

    Marvin0328

    Joined:
    Jun 8, 2023
    Posts:
    3
    Is this compatible with Entities 1.0.10? Since the latest version of ECS new entities graphics package replaces hybrid renderer package, things need to be updated I guess.
     
  15. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    This package doesn't use entities graphics and calls
    Graphics
    API itself. Also it does compatible with entities 1.0.10 all the time, except NSprites-Foundation package and Age-of-Sprites sample project which was require some little changes, though all is compatible now after a bunch of commits.

    If you want to stick with entities graphics you can implement simple quad mesh and render texture on it to work with 2D sprites.
     
  16. Marvin0328

    Marvin0328

    Joined:
    Jun 8, 2023
    Posts:
    3
    Yeah just found out it is :oops:
    Thanks for great asset.
     
    Tony_Max likes this.
  17. Marvin0328

    Marvin0328

    Joined:
    Jun 8, 2023
    Posts:
    3
    Can I apply rotation to sprite?
    I'd love to apply rotation but I can't find how...
     
  18. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    There is a pull request in NSprites-Foundation repo from great guy which implement 2D rotation (basically the same as Unity.Transform did but with
    LocalTransform2D
    ), I still trying to fix broken culling system in this PR, have not so much time.

    However there was no really difference with unity built-in implementation. So I recommend, if you want it right now, to just register
    LocalToWorld
    as instanced property in sprite and just receive it in shader through
    StructurredBuffer<float4x4>
    . To better understand what I sad you can read NSprites wiki on github.

    Also there was plans to implement pure 2D transformations with optimized matrices so instead of 16 floats it would use only 6, but again it takes time.
     
    Last edited: Jun 14, 2023
    Marvin0328 likes this.
  19. mgi388

    mgi388

    Joined:
    Jun 3, 2023
    Posts:
    4
    Is it fair to say that it's not possible to add a separate game object with a traditional sprite renderer and have its sorting layer play nicely with the NSprite renderer authoring sorting layer? I tried adding a simple sprite to the age-of-sprites demo, and fiddled with the sorting layers to see if I could get the NSprite sprites and the traditional sprite sorted to different layers, but the traditional sprite is always on top. Is this because the NSprite material/shader uses a completely different sorting technique to traditional sprite renderer material/shader? Are the NSprite authoring fields for "Sorting layer" just reusing the project's sorting layers, but actually incompatible with sprite renderer's "Sorting layer" field?

    I was experimenting with trying to get a "hybrid" project working that used traditional tilemap renderers, but then rendering entities in the game on top of the tiles using NSprite.
     
  20. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    All true

    This is caused because shader implements sorting in a bit cheating way. The more natural way would be shifting Z position relatively to sorting value knowing camera clipping planes (to define min and max Z values). This way all meshes no matter what shader it was should be sorted by depth.

    I've refused shifting Z because in scene view it looks crazy like with 10k entities it is a huge curved plane made of sprites like a sea wave or something, so I decide to make it look less scary.

    So you can change shader a bit and again instead of cheating with SV_POSITION just shift Z value in matrix construction depending on sorting value. Though even this change wont make sorting system itself account gameobject sprites because it only knows about entities (except you will make them hybrid entities to take place in sorting process).
     
    mgi388 likes this.
  21. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    NSprites-Foundation project now includes 2D transforms and allow you to apply rotation to sprites
     
    Laicasaane likes this.
  22. filod

    filod

    Joined:
    Oct 3, 2015
    Posts:
    224
    upload_2023-7-1_0-49-23.png
    it constanly glitch when play the scene (Age-of-Sprites)
     
  23. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    Can't reproduce. Can you share details? What unity / entities / NSprites / NSprites-Foundation you use ?
     
  24. filod

    filod

    Joined:
    Oct 3, 2015
    Posts:
    224
    seems you use file package path in age-of-sprites's packages-lock.json, and it auto resolve to git version on my machine (may outdated?)
     
  25. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    Seems outdated yes, but how it can be if you say that package auto resolve to git version which is latest (I have no difference localy). I need to have package like that to provide them separately but at the same time I need real life case to test changes, so all is inside one project.

    btw: you've added me to discord, but it seems you don't see my messages
     
  26. yokobe0012

    yokobe0012

    Joined:
    Nov 2, 2021
    Posts:
    16
    I also get this rendering error in player mode like this, but it doesn't appear on Andriod build.
     
  27. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    filod have found out that enabling VSync resolves whole issue
     
  28. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    filod likes this.
  29. Ksanone

    Ksanone

    Joined:
    Feb 7, 2015
    Posts:
    41
    With Age of Sprite sample and Unity 2022.3.3f1 I have two issues
    - box around all sprites, e.g red box around tents
    - animation set error - no error if I change walk animation set sprite to idle sprite

    Code (CSharp):
    1. NSpritesException: SpriteAnimationSet Legionnaire_AnimationSet: all _animations's SpriteSheet must have same texture as passed "legion_idle", but animation "walk" have SpriteSheet's texture "legion_walk"
     

    Attached Files:

  30. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    Can you see this in build. I have same issues with certain cases but not with tents. Seems like little error in calculations in shader.

    Have you changed anything or this is happening with default project?
     
  31. Ksanone

    Ksanone

    Joined:
    Feb 7, 2015
    Posts:
    41
    Wow, everything works when just using the plain project. For some reason I imported AOS, Nsprites, and Foundation into a new URP project, and that is when I had the error. False report, sorry!

    Also, is there still a discord?
     
  32. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    You can join our humble community for now consisting only of me and one another dev:D
    https://discord.gg/rvxrHEFx8n
     
    Last edited: Aug 13, 2023
  33. rayden2909

    rayden2909

    Joined:
    Feb 2, 2014
    Posts:
    7
    Is there a step by step documentation of what you need to do to install and use the package to just show a sprite on screen ?
     
  34. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    You can read main readme on github page. Also there is more detailed documentation on repo's project.
     
  35. rayden2909

    rayden2909

    Joined:
    Feb 2, 2014
    Posts:
    7
    Thanks, I was hoping for something more ... beginner friendly , I am just starting out with ECS and I am having some issues figuring out what I need to do to setup a simple sprite to render with animation. Don't get me wrong , that documentation is really good , seems to go into a lot of details.
     
  36. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    https://discord.gg/J4Z8ke9Z there is discord server where we can answer any your NSprites related questions as well as dots related questions (at least we can try). In discord I can provide step by step instructions while understanding what point in Entities or in NSprites is not clear for you.
     
    rayden2909 likes this.
  37. rayden2909

    rayden2909

    Joined:
    Feb 2, 2014
    Posts:
    7
    Thanks, can you update the invite to discord ? I says it is invalid.
     
  38. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
  39. uncoilking

    uncoilking

    Joined:
    Sep 17, 2020
    Posts:
    8
    Repeatedly entering game levels,FPS is getting lower and lower.
    I found that, in "SpriteRenderingSystem".
    Entering game levels for the first time "renderArchetypeStorage.RenderArchetypes.Count = 126",
    Entering game levels for the second time "renderArchetypeStorage.RenderArchetypes.Count = 252",
    Entering game levels for the third time "renderArchetypeStorage.RenderArchetypes.Count = 378",
    more and more
     
  40. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    My guess is: you use foundation and entering game level means for you that baked entities with
    SpriteRenderDataToRegister
    component is again exist for
    RegisterRenderersSystem
    , and this system doesn't check if storage already have particular archetype.
    I will add checking to avoid this kind of registration, but I also warn you that if you want to use foundation in production then you need to know that exactly happens, because I was developing this package is an example and tools for prototyping.

    Also registration system isn't something even good enough to be in production project, it's just a simple and dumb way to get NSprites to work, so I would also completely rewrite this part if I use it in production context.

    UPD: I've updated both repos. You can try now. Also it is not ok to have that many render archetypes, you probably should place your sprites on the sprite atlas, because 126 render archetypes means you have at least 126 drawcalls.
     
    Last edited: Jan 9, 2024
  41. uncoilking

    uncoilking

    Joined:
    Sep 17, 2020
    Posts:
    8
    Thanks
     
  42. uncoilking

    uncoilking

    Joined:
    Sep 17, 2020
    Posts:
    8
    Call "renderArchetypeStorage.Clear()" when leaving the game level.
     
  43. uncoilking

    uncoilking

    Joined:
    Sep 17, 2020
    Posts:
    8
    I have discovered another bug.
    Coordinates exceeding 512, all sprites will not be displayed on the screen.
     

    Attached Files:

  44. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    That is because
    RegisterRenderersSystem
    uses
    RenderArchetypeStorage.RegisterRender()
    overload with default bounds which is
    new Bounds(Vector3.zero, Vector3.one * 1000f)
    . This wasn't a great idea to have overload without
    Bounds
    parameter, because there is no way to have infinite bounds or ignore bounds mode. So all I can do is make it 100 times bigger by default, but it is better for you to just pass bounds of what size you need.

    UPD: I've updated NSprites API, so now
    RegisterRenderersSystem
    registers render with
    new Bounds(Vector3.zero, Vector3.one * float.MaxvValue)
    .
     
    Last edited: Jan 11, 2024
  45. uncoilking

    uncoilking

    Joined:
    Sep 17, 2020
    Posts:
    8
    Very Good
     
    Tony_Max likes this.
  46. unity_b-dC9vekAEcKqQ

    unity_b-dC9vekAEcKqQ

    Joined:
    Dec 3, 2018
    Posts:
    3
    How to flip an image with nsprite in runtime I am unable to access this field?. Also how to change the color of the Sprite ? Also if I want don't want to loop the animation then ?

    Please help I am new with ecs and nsprite
     
  47. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    If you use NSprites-Foundation and RegularSpriteShader from it then:
    * To flip sprite you need to change values of
    Flip
    component, which has
    float2
    values representing x and y flip directions. You just need to set any negative number to flip image or positive to revert flip. You should be able to access fields because both component and it's fields are public.
    * To change color you need to extend RegularSpriteShader. Please read wiki on NSprites github page to learn how to register components. Then in shader you need to obtain per-instance color value (which is also described in wiki) and then just do what you want with this color. I guess you want use it as a tint, then you can just multiply sampled texture color value by your color tint value.
    * If you need one shot animation then you need to rewrite animation system. Not looped animation feature isn't implemented right now. NSprites-Foundation is just a example of how things could be done, but not a complete production ready solution.
     
    unity_b-dC9vekAEcKqQ likes this.
  48. unity_b-dC9vekAEcKqQ

    unity_b-dC9vekAEcKqQ

    Joined:
    Dec 3, 2018
    Posts:
    3
    Ok but suppose I have an animation of enemy die and I want to disable or destroy the entity after that how to do that any idea



     
  49. Tony_Max

    Tony_Max

    Joined:
    Feb 7, 2017
    Posts:
    354
    If you want to react on animation ends you can
    * rewrite animation system in a way that it will throw an event in some way (like component or entity, or any other appoach)
    * or (which is better in my opinion) just know how long your die animation is and just have a timer to react
     
  50. sauvik4redapple

    sauvik4redapple

    Joined:
    Oct 10, 2022
    Posts:
    3
    Hello i am getting a red box in the animation is there any settings i am missing ?
    Also in low end device i saw some flickering issues