1） About _CameraDepthTexture doubts
2） Mapping Alpha The effect of channels on image size
3）URP How to do that GrabPass The effect of
4） How to get AssetDatabase Failed to load Asset Of Instance ID
5） How to determine Bundle When the file is loaded into memory
This is the first 226 piece UWA Push of technical knowledge sharing . Today we continue to select and develop a number of 、 Optimization related issues , Suggested reading time 10 minute , If you read it carefully, you will get something .
UWA Question answering community ：answer.uwa4d.com
UWA QQ Group 2：793972859（ The original group is full ）
Q： One about _CameraDepthTexture The question of .
If open _CameraDepthTexture,Camera You need to render all the scenes with ShadowCaster Of visible objects Pass To implement depth maps .
But the objects in the scene are opening ZWrite When you write the depth into Depth Buffer It's in , Get this directly Depth Buffer Is it closer to DrawCall Doubling is more efficient ？ still Unity What are the considerations in this regard ？
in addition , Ask a more practical question ：
Our project needs to render the depth effect of the middle lake water in the scene , All opaque scene objects are associated with the same material Shader, This Shader It's with ShadowCaster Of . But only individual objects inserted into the water need to be rendered ShadowCaster Of Pass, Is there a way to not increase Shader Under the circumstances , Let objects not inserted into the water not render Shadow Caster Pass Well ？ We're going to use Built-in Rendering pipeline of .
A： The first question is , You can refer to this question Unity Official response .
There are two reasons , The first is for non full screen rendering , I wanted to get the depth of the corresponding camera rendering , however Depth Buffer It's full screen . The second reason is that many platforms do not support direct access to Depth Buffer The data of .
In addition, check FrameBufferFetch When you have related questions, you can see Unity The answer from another post on the Forum . Speaking of Unity Support FrameBufferFetch, But not supported DepthBuffer Acquisition .
The second question is , If not Shader, At present, I haven't thought of any other good methods .
If you can add Shader, You can change the original Shader A copy of , Only in ShadowCaster Add one to the part of “NeedDepth” In this way Tag, Change the shader of an underwater object to this Shader, Another one is just ShadowCaster with “NeedDepth” This Tag Of Shader, This Shader Used to do Replace operation .
Add an extra Camera, This Camera Follow the main camera , Or as a child node of the main camera , Create a RT, Let this Camera Render to this RT, stay Update It uses ReplaceShader Go and draw , So there's only one Tag Of ShadowCaster Will do deep rendering , Later, we can deal with this RT Code and so on , This RT It records the depth of underwater objects . There doesn't seem to be much extra work in the whole process , I think we can have a try （ I didn't do a test , But it works in theory ）.
thank Xuan@UWA The Q & a community provides answers
Q： stay UWA Of 《 Texture Optimization ： It's not just a picture 》 In this article , The picture contains Alpha Channels can affect memory .
Through the following test resource configuration ：
- Tga_Alpha - contain Alpha passageway
- Tga_NoAlpha - It doesn't contain Alpha passageway
- Png_Trans - With transparent pictures
- Enter into Unity Medium Format, All code is set to TextureImporterFormat.ASTC_6x6
test result ：
- Three pictures Unity All Format It's in the following format
- All three pictures show the same memory size
- Texture Importer Of Alpha Source Set to None, No effect on test results
problem ： Does the picture contain Alpha passageway , For the same Format Format , Is the memory size the same ？
A： The optimization proposal in the original text is to remove meaningless Alpha passageway （ The definition in the original text is Alpha It's all worth 1 The texture of ）, This is really helpful for memory optimization .
In the test case of the subject , Whether it's png Format or tga Format , After entering the engine , Will be converted to internal format by the engine （RGBA、ETC、ASTC etc. ）. imagine , With ETC2 For example , without Alpha passageway , When the compression quality is acceptable , You can choose RGB_Compressed_ETC2_4bits, And if you add this meaningless Alpha passageway , So when we import settings in batch , Will automatically select RGBA_Compressed_ETC2_8bits. So the memory is doubled .
And the question is about ASTC The situation of , stay Unity Editor source code in Texture Import format There is a note on the definition of ：// ASTC uses 128bit block of varying sizes (we use only square blocks). It does not distinguish RGB/RGBA, Whether or not to include Alpha Channels have nothing to do with . About ASTC Format Introduction , Here you are recommended to read Github Upper ASTC Format Overview.
And about what you mentioned in your reply upstairs Unity The problem with the new version , May have a look Unity This question on the Forum .
Some people may think that the mapping format for the project is ASTC Come on , Remove Alpha It doesn't mean much , In fact, we can't generalize , Although of the same size , But according to （https://zhuanlan.zhihu.com/p/158740249） Test in , Is there a Alpha The channel will affect the compression quality to some extent . So we should use it reasonably in the project .
Thank you, fan Jun @UWA The Q & a community provides answers
A2： When the game is running Texture Of Alpha The channel depends on the situation after import , Can't look at the source file .UWA Local resource detection for Texture Of Alpha Channel detection is the result of the import .
Format of image source file , Graphics hardware is not supported ,Unity And it doesn't take over directly . After importing the picture , According to Import Settings The settings in the , Import images into hardware supported formats （ The format in the engine ）, And the resources used in the run are also imported .
The subject will Alpha Source Set to None, Then the import is not the source file Alpha Channel to import , And the compression format is ASTC_6x6, This format contains Alpha The tunnel . In this way, all three resources will be generated by default after importing 1 Of Alpha passageway . The amount of memory used is naturally the same .
thank Prin@UWA The Q & a community provides answers
Q：URP How to achieve GrabPass The effect of ？URP The pipeline has been removed GrabPass The function of , Now there's a twist effect feature , Like thermal disturbance . although URP Can directly get the entity diagram to do distortion effect , But it's like a translucent object （ such as ： water 、 Other special effects, etc ） It also needs to be distorted, and it can't be done .
A： The subject wants to fully realize GrabPass I don't think so , But there can be alternatives ：
GrabPass It means to draw first a thing , Then draw b Objects affect a, draw c Objects affect ab. Be careful , The most important thing is b Disturbance of objects cannot affect c thing , because b The object is more than c The object was drawn first .
But if you can accept b Objects and c The disturbance of the object affects at the same time abc, So it's easy , With a first RT Save distorted information （ In fact, that is UV The offset ）, And then in Uber Shader Apply the offset to the screen . This distorts the information and disturbs everything （ Whether it's opaque or translucent ）.
I don't know URP How to achieve , But we changed it ourselves SRP, That's how it works .
Thank you Wang Shuo @UWA The Q & a community provides answers
Q： Now there is one inherited from ScriptableObject Of Asset, For whatever reason , Reference script GUID In the wrong . Then you can find this through a certain editor script Asset Of Path, But how in ProjectView Select it inside ？
After testing , Use Selection.instanceID It's optional , and Instance ID It usually needs to pass Object To get , But because the script reference is wrong , therefore AssetDatabase Can't load this Object Of . What's the way or API Can get this Instance ID Well ？
A： By looking at open source , Found a way to work ：
//assetPath : Someone whose script cannot be found Asset Instance path HierarchyProperty property = new HierarchyProperty(assetPath); Selection.activeInstanceID = property.GetInstanceIDIfImported();
This can be done in ProjectView I have correctly selected .
Thank you, Huang Cheng @UWA The Q & a community provides answers
Q： After downloading all the resources , Called in a test script LoadAssetAsync function , Here's the picture ：
stay AssetBundleProvider Here's the first puzzle ：
since Addressables All use cache mechanism to store AssetBundle package , Well, here File.Exists Not always for False Do you ？
The second question is ：
Addressables stay WebRequest After downloading , How to handle Bundle When to load into memory ？ Maybe I have a problem with my thinking , But in the following stack trace, there is no similar to LoadFromFile Such a call .
A： here File.Exists It's not checking the cache , It's about checking if the files are there StreamingAssets Inside . And all the downloaded resources are obtained through cache , Download if not in cache . and Load This part has been encapsulated in UnityWebRequestAssetBundle Inside .
Thank you Huang Cheng @UWA The Q & a community provides answers ：
The cover map comes from the Internet
Today's sharing is here . Of course , There is no end to life but to know . In the long development cycle , The problems you see may be just the tip of the iceberg , We are already in UWA The Q & a website has more technical topics waiting for you to explore and share together . You are welcome to join us , Maybe your method can solve other people's urgent needs ; And he's a mountain “ stone ”, Can also attack you “ jade ”.
Official website ：www.uwa4d.com
Official technology blog ：blog.uwa4d.com
Official Q & a community ：answer.uwa4d.com
UWA School ：edu.uwa4d.com
Official technology QQ Group ：793972859（ The original group is full ）