1 Star 0 Fork 0

明天的明天 / Age-of-Sprites

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Age of Sprites - Sample project for NSprites package

RomeGIf

What this project solves

  • Registering render for each sprite
  • Registering properties-components for NSprites rendering
  • Rendering of all entities-sprites within 1 drawcall using texture atlas and passing tiling-and-offset values for each entity
  • Culling sprites outside of camera bounds
  • Screen sorting sprites with layers and dynamic / static sorting
  • Animating sprites using tiling-and-offset values change
  • Implementing 2D transforms to avoid unecessary data
  • +Poor example of producing units and building squads for strategy games

Requirements

How register render

For this project I've used adding managed component to sprite entity to be able to register unique renders at runtime. Such solution doesn't scale well with growing count of existing sprites in scene. Though there are few possibilities to solve problem of managed components such as using components with fixed string GUID and then load managed data with AssetDatabase.

How register properties-components

There is nothing to say more then in docs. All registering happens in PropertiesManifest.cs.

How rendering goes within 1 drawcall for different sprites

First thing is to use sprite atlasing. Then we come up with question how to render instances with same texture. Answer is simple - we just need pas texture's Tiling&Offset as property. I've used Reactive property type, because animation system writes to this component, so we need to sync updated data, even if some sprites never change theirs Tiling&Offset property. Then in shader we use simple function to locate our sprite on atlas

float2 TilingAndOffset(float2 UV, float2 Tiling, float2 Offset)
{
    return UV * Tiling + Offset;
}
// ...
varyings.uv = TilingAndOffset(attributes.uv, mainTexST.xy, mainTexST.zw);

You can easily obtain Tiling&Offset values using NSpritesUtils.GetTextureST(Sprite).

How culling happens

Culling system is simple as grab main camera, obtain it's view bounds and then check each sprite in parallel. If culled sprite is inside boudns then it should remove cull tag. If not-culled sprite is outside of bounds then it should get cull tag. To not render culled sprites we register [assembly: DisableRenderingComponent(typeof(CullSpriteTag))] in PropertiesManifest.cs. Animation and sorting systems also perform only not-culled sprites.

Tip: you can navigate to NSprites -> Toggle frustum culling system to enable / disable culling system if you want to see how it works.

How screen sorting happens

Shader makes a little trick described in this thread. It recieves some sorting value which is a [0,1] float and then just varyings.positionCS.z = sortingValue;. positionCS is SV_POSITION.

Sorting system lets us use any number of constant defined layers. In this project I've defined 8 layers, 2 of each I actually use. 0 layer all is for herbs and rocks, 1 layer is for soldiers, tents and flags. Sprites on 0 layer use static sorting which means that sorting happens only when new static-sorting entities was created / destroyed (chunk order version changed). Sprites on 1 layer uses regular dynamic sorting which happens every frame. Sorting system handle only sprites without cull tag.

How animation happens

Sprite per-frame animation is just changing sprite we render, so animation system just changes Tiling&Offset values over time. There is banch of ScriptableObjects and authorings to prepare animation data. It just contains frame count / distribution / duration / etc. At the end all data goes to blob assets. The best solution would be use HashMap in blob, but there is no built-in solution and community solutions unsupported, so every time I need switch animation I perform searching in blob array, thougn it can be improved.

How 2D transforms implemented

It is just a copy of Unity.Transforms package but for 2D needs. Though project doesn't use 90% of this functionality, because there is no parent-child relationship. Also I would not recommend bring such relationship to 2D rendering, because it complicate sorting a lot.

What happens in this example

There is few tents which are factories for units. They produce units over time. Free units search for squad which need units and then recieves it's position in squad. After all squads was filled with units new squad spawns. That is all.

Tip: you can navigate to NSprites -> Toggle draw squads for View window to see where squads exists, but it is expansive part.

空文件

简介

https://github.com/Antoshidza/Age-of-Sprites 展开 收起
C# 等 2 种语言
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/mtdmt/Age-of-Sprites.git
git@gitee.com:mtdmt/Age-of-Sprites.git
mtdmt
Age-of-Sprites
Age-of-Sprites
main

搜索帮助