Unity3D研究院之ECS渲染Sprite
发布日期:2021-05-10 00:50:34 浏览次数:13 分类:精选文章

本文共 4198 字,大约阅读时间需要 13 分钟。

Unity ECS Sprite������������������

SpriteAtlas���������������������

���Unity������������SpriteAtlas���������������������������������������������������������������������������_sprite���������������Batch���������������������DrawCall���������������DrawCall������������������������������������

Sprite������������

1. SpriteAtlas������

���������������SpriteAtlas������������������������������_sprite��������������������������������������������������� Sprite ���������������������������������DrawCall���������

2. ������������

��������������� GSprite������������������������������������ BatchRendererGroup ��������������������������������������������� Sprite���

3. ������������

������������������ BatchRendererGroup.AddBatch ���������������������������������

  • ������������ Sprite ��������������������� ��������� BatchRendererGroup.AddBatch ���������������������
  • ������������������ ������Job������������ Sprite ���������
  • ��������������������������� ������ RefreshBlock() ������������MaterialPropertyBlock���

������������������

1. ������������

������ ECS ���������������������

  • position: ������������
  • scale: ������������
  • pivot: ������������

2. ������������

������ NativeList<ECS> ������ Sprite ��������� BatchRendererGroup ���������������������

3. Refresh������

  • RefreshElement��� ���������������Batch���������MaterialPropertyBlock���
  • RefreshPosition��� ������ Sprite ���������������Job���������������
  • RefreshBlock��� ������������Sprite���������������

������������������������������

// ������ Sprite ���������������
void AddSprite(Sprite sprite, Vector2 localPos, Vector2 localScale, float slider) {
// ������ Sprite rect ���������������������������������
Vector4 rect = GetSpriteRect(sprite);
// ������ slider ���������������������������
// ������ ECS ������������
ECS obj = new ECS
{
position = localPos,
scale = CalculateScale(sprite, localScale, slider),
pivot = CalculatePivot(sprite, localScale)
};
m_SpriteData.Add(obj);
m_SpriteDataOffset.Add(rect);
}
// ������������������
void Refresh() {
RefreshElement();
RefreshPosition();
RefreshBlock();
}
// ���������������������
void RefreshElement() {
if (m_BatchRendererGroup == null) {
m_BatchRendererGroup = new BatchRendererGroup(OnPerformCulling);
} else {
m_BatchRendererGroup.RemoveBatch(m_BatchIndex);
}
// ������ offset ������
CreateMaterialPropertyBlock();
m_BatchIndex = m_BatchRendererGroup.AddBatch(m_Mesh, m_Material, m_SpriteData);
}
//Scheduled Job ������������������
public class UpdateMatrixJob : IJobParallelFor {
public NativeArray
Matrices;
[ReadOnly] public NativeList
Objects;
public void Execute(int index) {
Matrices[index] = CalculateMatrix(Objects[index], outs);
}
}
// Shader ������
Shader "ECSSprite" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_Offset("_Offset", Vector) = (1, 1, 0, 0)
}
SubShader {
// ������������������
Tag {
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGProgram
#pragma target 3.0
#include "UnityCG.cginc"
sampler2D _MainTex;
fixed4 _Offset;
v2f vert(appdata v) {
v2f o;
UnityObjectToClipPos(v.vertex);
fixed4 l = _Offset;
o.uv = v.uv.xy * l.xy + l.zw;
return o;
}
fixed4 frag(v2f i) {
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
}
}
}

������������������

���������������������������Shader��������������������������������������� `UI������������������������������Chosenify������������������������������������������

������������

��������������������������� Sprite ���������������������������������������������������������������������DrawCall������������������������������������������������

��������������������������������������������������������������������������������������������������� Sprite ���������������

上一篇:Unity3d 设置小球为刚体控制滚动
下一篇:Unity3D研究院之动态分辨率降低渲染开销

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年04月27日 15时09分57秒