current position:Home>Unity Atlas Optimization Principle

Unity Atlas Optimization Principle

2022-08-06 09:32:05ThomasQxx

Graph collection batch rules

Algorithm for calculating the level number:如果有一个UI元素,within the screen area it occupies(通常是矩形),如果没有任何UIunder it,Then its level number is0(最底下);如果有一个UIunder it and shouldUIcan and itBatch,Then its level number is the same as the one below itUI层级一样;如果有一个UIunder it but not with itBatch,Then its level number is the bottomUI层级+1;如果有多个UIare below it,Then traverse and calculate all the level numbers in the first two ways,The largest of them is used as its own level number.

合并时候:If the two adjacent batches are just fineBatchwill proceedBatch.
Unity的UIThe rendering order is determined2个步骤,The first step calculates eachUIThe element's level number;The second step is to merge the same level numberBatchof elements as a batch,and sort the batches;

例如1:Blue,Green,Orange三个图片,When the atlas is not packaged.


It can be seen that three are producedDrawCall.原因:There are no merged atlases,需要CPU向GUISend three render requests.

例如2:Blue,Green,Orange三个图片,Packaged as an atlas

不相交情况
相交情况
Can see whether it intersects or not,Only one was producedDrawCall.原因:All three pictures are in the same atlas,Batched together when rendering,统一由CPU向GPUJust send a render request once

例如3:Blue,Green,Orange三个图片,Blue和GreenPacked into the same atlas,而OrangePack into another atlas,渲染顺序为Blue,Green,Orange.

The pictures do not intersect
Image intersection
Can see whether it intersects or not,都只有2个DrawCall.原因:在Hierarchy面板中,先渲染Blue,在渲染Green,最后渲染Orange.这个顺序很重要,According to the graph merging rules, we will analyze it in detail,首先渲染Blue,Calculate his hierarchy number,There are no pictures below him,So his layer number is 0.紧接着看Green,Calculate its hierarchy number,There is a picture below itBlue,并且Blue和Greenin the same collection ,UnityAfter the engine determines that it can be batched,Its level number and Blue一样为0.最后渲染Orange,Below him is the pictureGreen,由于Green和OrangeThe pictures are not in the same atlas,So it can't be batched,Its tier sequence number is in place0+1 = 1.
所以第一步:UnityThe engine calculates according toBlue和GreenThe level numbers are the same as 0,而OrangeThe level number is1.
所以第二步:UnityEngines can be merged in the same level numberBatchof elements as a batch.Blue和Orangewill be batched togetherCPU向GPUSend a render request,也就是一个DC,而Orange单独由CPU向GPUSend a render request,也就是另外一个DC.所以有2个DC.

例如4:Blue,Green,Orange三个图片,Blue和GreenPacked into the same atlas,而OrangePack into another atlas,渲染顺序为Blue,Orange,Green.

不相交
第一步:BlueThe level number is0,OrangeThe level number is0,GreenThe level number is0.
第二步:0层中Blue和GreenCan be combined into a batch,而Orange不能,所以2个DC
移动Orange覆盖Blue
操做:移动Orange覆盖Blue
第一步:BlueThe level number is0,OrangeThe level number is1,GreenThe level number is0
第二步:0in the level numberBlue和GreenThe same atlas can be merged into a batch,1层级号Orange单独一个.所以2个DC
移动Orange被Green覆盖
操做:移动Orange被Green覆盖
第一步:BlueThe level number is0,OrangeThe level number is0,GreenThe level number is1
第二步:0层级中Blue和OrangeThose who do not belong to the same atlas cannot be batched together,所以2个DC,1Only in the tier numberGreen单独一个元素,所以单独一个DC.So three in totalDC
移动Green覆盖Blue
操做:移动Green覆盖Blue
第一步:BlueThe level number is0,GreenThe level number is0,OrangeThe level number is0
第二步:0in the level numberBlue和Green可以合批,一个DC,Orange单独一个DC.所以2个DC
移动Green覆盖Orange
操做:移动Green覆盖Orange.and move aboveOrange被Greencover a truth.Here it is explained
第一步:BlueThe level number is0,GreenThe level number is1,OrangeThe level number is0
第二步:0层级中Blue和OrangeNot in the same gallery2个DC,1层级中Green单独一个DC.所以三个DC
在这里插入图片描述
操做:移动Blue被OrangeCover and moveOrange覆盖BlueOne action to do
第一步:BlueThe level number is0,OrangeThe level number is1,GreenThe level number is0
第二步:0层级中Blue和GreenBatches can be grouped together in the same atlas,1个DC.1层级中Orange一个DC.所以2个DC
在这里插入图片描述
操做:移动Blue被Green覆盖.和移动Green覆盖BlueOne action to do
第一步:BlueThe level number is0,OrangeThe level number is0,GreenThe level number is0.
第二步:0in the level numberBlue可以和Green合批.一个DC.Orange单独一个DC.所以2个DC
在这里插入图片描述
操做:移动Orange和Green如图所示
第一步:BlueThe level number is0,OrangeThe level number is1,GreenThe level number is2.
第二步:0层级中Blue一个DC,1层级中Orange一个DC,2层级中Green一个DC.总共三个DC
在这里插入图片描述
操做:Stack the three pictures as shown
第一步:BlueThe level number is0,OrangeThe level number is1,GreenThe level number is2
第二步:0层1一个DC,1level oneDC,2level oneDC.三个DC

优化:So after understanding the batch rules of graph sets,Know the direction of optimization

DrawCall:就是CPU向GPUA render command sent.
在降低DC方面,All we need to do is let as much as possibleCPUPack some more informationGPU,那么DCnaturally decreased.在UGUI里面,先不考虑Shader,UIElements with the same material and texture can be grouped together.
The so-called atlas:will be a lot2DSprite通过AtlasPack and merge into one big picture,那么为什么要这么做?
1.CPU向GPUWhen sending rendering commands,Just need a big picture,因为GPUwill be sampled in this big picture,Then generate the interface according to the rules.So here is why the same atlas is needed,If there is really only one atlas,那么一个DCYou can pass all the required information to GPU,然后GPUjust start drawing.
2.Atlas size is optional,都为2的次数幂,图片尺寸为2It will be more irregular when dealing with the power of times(Image size is not correct2 的次数幂)快,Even if the picture given by the art is not2的次数幂,But it is also after the atlas is made2的次数幂.这是Unity的设计
3.Idealization is to pack all the graphs into an atlas,There will only be oneDC了,But is it possible?If there are too many image resources,Then this picture may be very large,The resources are very large,Unity也不允许,So the maximum range is set.So at this time, we need to classify according to certain design rules.我们当然是希望DC越少,资源越少.But this is a mutually exclusive topic.Eclectic thinking has been very popular in ancient times,So it's all about finding a balance.A:For example some public buttons,public border,As a public resource, pack it into an atlas as much as possible,If not enough2-3个.B:按照功能模块进行划分;

copyright notice
author[ThomasQxx],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/218/202208060922392043.html

Random recommended