- Notifications
You must be signed in to change notification settings - Fork47
Unity DOTS Sprite Rendering Package
License
Antoshidza/NSprites
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This framework provides sprite rendering system compatible with Entities package (unity ECS).Changelog
Basically it sync whatever entity component you want with GPU data to perform instanced rendering. As a result all entities with same Material can be rendered with single drawcall.
- Using power of 💥DOTS💥 + instancing to render numerous of sprites
- Using any public to you per-entity blittable component as shader instanced property
- Data update strategies to avoid unnecessary CPU load
- Edit-time rendering (subscene only)
For more detailed information please readproject's wiki 📘
// registrate components as properties at assembly level anywhere in project[assembly:InstancedPropertyComponent(typeof(WorldPosition2D),"_pos2D")][assembly:InstancedPropertyComponent(typeof(SpriteColor),"_color")]
// registrate render with ID, Material, capacity data and set of propertiesif(!SystemAPI.ManagedAPI.TryGetSingleton<RenderArchetypeStorage>(outvarrenderArchetypeStorage))return;// don't registrate same renderIDrenderArchetypeStorage.RegisterRender(renderID,material,// material with [Enable GPU Instancing] enabled and shader supporting instancingbounds// bounds in which sprites will be visible. For example new Bounds(Vector3.zero, Vector3.one * float.MaxValue)null,// override for MaterialPropertyBlock if needed128,// initial ComputeBuffers capacity128,// minimal capacity step for ComputeBuffers"_pos2D",// world 2D position property"_color"// color property);
// initialize sprite entity with all needed components for renderingentityManager.AddSpriteRenderComponents(spriteEntity,renderID);// WorldPosition2D and SpriteColor are example client's componentsentityManager.AddComponentData(spriteEntity,newWorldPosition2D{Value=/*your value here*/});entityManager.AddComponentData(spriteEntity,newSpriteColor{Value=Color.White});// or from bakerprivateclassBaker:Baker<SpriteAuthoring>{publicoverridevoidBake(SpriteAuthoringauthoring){AddComponent(newWorldPosition2D{Value=newfloat2(authoring.transform.position.x,authoring.transform.position.y)});AddComponent(newSpriteColor{Value=Color.White});this.AddSpriteComponents(authoring.RenderID);// Render ID is client defined unique per-render archetype int. You can define it manually or for example use Material's instance ID or whatever else.}}
Also shader you're using should be compatible with instancing. Check myexample shader gist. The main idea is to useStructuredBuffer<T> _propertyName
. Though it is possible to use instanced properties with ShaderGraph, so you may try your option. For local example shader main part can look like:
// ...#ifdefined(UNITY_PROCEDURAL_INSTANCING_ENABLED)StructuredBuffer<int> _propertyPointers;StructuredBuffer<float4> _color;#endif// ...VaryingsUnlitVertex(Attributes attributes,uint instanceID :SV_InstanceID){// ...#ifdefined(UNITY_PROCEDURAL_INSTANCING_ENABLED)int propPointer = _propertyPointers[instanceID];// this is internal package property to point right data during component syncfloat4 color = _color[propPointer];#else//fallback if somehow instancing failedfloat4 color =float4(1,1,1,1);#endif// ...}
SpriteRenderingSystem
sync registered entity components withComputeBuffers to send data to GPU and then renders entities withGraphics.DrawMeshInstancedProcedural
. System also controls how ComputeBuffers reallocates if capacity exceeds. Sprites are simple entities with no limits of what components you use.
CheckFoundation
NSprites doesn't provide anything except rendering and managing data for it. Though you can implement anything you want on top of it. Also I want to share some foundation project where you can find examples and maybe even useful tools to work with this package. Foundation provides such things as sorting / culling / animation / 2D transforms / basic data authoring and registration.
Check sample project -Age of Sprites
This sample project covers basics of rendering with NSprites. Use it to get a main idea of how stuff can be implemented but not as production-ready solutions.


- Unity 2022.2+
- Entities v1.0.0-pre.65+
There is few things we should care when talking about using NSprites in real projects. Since this package usesGPU instancingandComputeBuffer
on CPU side withStructuredBuffer<T>
on GPU side (in shader) to send sprites data, we need platform and Graphics API supportthis two.
Graphics API | Description |
---|---|
Direct3D11 | |
Direct3D12 | ✅ supported |
Vulkan | ✅ supported |
OpenGLCore | |
OpenGLES3 |
Platform | Description |
---|---|
PC | ✅ supported |
WebGL | ⛔ unsupported |
Android | ❔ not fully checked |
iOS | ❓ not checked |
- Window -> Package Manager -> + button -> Add package from git url
- Paste
https://github.com/Antoshidza/NSprites.git
cd
to your project's/Packages
folder- git submodulehttps://github.com/Antoshidza/NSprites.git
I wish this project will be helpful for any ECS early adopters! So feel free to send bug reports / pull requests, start discussions / critique, those all arehighly appreciated!You can contact with mydiscord account / joindiscord server!Also there is athread on unity dots forum!
Preferred way to send support:BOOSTY
About
Unity DOTS Sprite Rendering Package