Unity performance optimization solution

Unity performance optimization solution

1.Unity 性能调试工具及使用方法
2.垃圾回收(Garbage Collection)
3.CPU相关优化
4.GPU相关优化
5.使用UPR(Unity Performance Reporting)云服务进行性能测试

what needs to be done every frame

  • The time required to render a frame > the time required for CPU calculation + the time required for GPU calculation
  • (For example, if the physical calculations and scripts on the CPU take a lot of time to run, then no matter how well the Shader is optimized, it will not increase the frame rate; conversely, if the pressure on the GPU is high, then optimizing the physical system and scripts is also beneficial. Improving the frame rate doesn't help much. So we analyze to find the weak link in the whole system, which is the lowest plate on the leaky bucket.)

What CPU and GPU are responsible for

  1. CPU 相关:Skinning,Batching(Static Batching,Dynamic
  2. Batching, etc.), physics-related, user scripts, particle effects, etc. GPU-related: Shader, Drawcalls, Image
  3. Effects (post-processing) But the CPU and GPU are not isolated:
  4. Draw Call is too high -> use CPU to do Batching to reduce Draw Call -> performance improvement
  5. Draw Call is not high -> use CPU to do Batching to reduce Draw Call -> waste CPU processing power, resulting in decreased culling efficiency -> performance degradation

The environment used when optimizing

Optimize the Project Settings

  • Open the project settings interface through Edit/Project Settings on the top menu of the editor. Find the Other Settings interface under Player.
  • Make sure Static Batching, Dynamic Batching, GPU Skinning, Graphics Jobs are enabled.
  • (If Oculus Quest) Disable ARMv7, enable ARM64. Select IL2CPP in the Scripting Backend drop-down box, so that all C# codes will be converted into C++ codes to improve the running speed.
  • (For Rift and Rift S) Select IL2CPP in the Scripting Backend drop-down box (but Debug will become inconvenient), so you can use Mono during the development stage, and change to IL2CPP when the package is officially released.
  • Open the XR Plugin Management setting interface, install the Oculus XR Plugin, and ensure that Initialize on Startup is checked.
  • Open the Oculus settings interface, make sure that Shared Depth Buffer and Dash Support are checked, and then set Stereo Rendering Mode to Single Pass Instanced.
  • In the Quality setting interface, make sure that only the Medium option is checked under the Android platform, and other options must be unchecked; other platforms can be checked all.
  • Set Pixel Light Count to 1 or 0 (if lighting information is provided through the Vertex shader).
  • Set Anti Aliasing to 4x Multi Sampling, which can also be used on Oculus Quest, because the pressure on the GPU is not great, but the picture quality can be improved a lot.
  • Set Shadows to Disable Shadows or Hard Shadows Only.
  • Set Blend Weights to 2 Bones.
  • Open the Graphics settings interface and make sure the Android platform is set to Low or Medium (Oculus Quest).

code related

  • You can use Invoke Repeating or Coroutine instead of Update method

  • The Debug.Log() method consumes a lot of performance

  • Pay attention to the Component (component) that is often needed for caching

CPU-related optimizations

  • light baking
  • Using Occlusion Culling
  • Improve Mesh Resource Performance with Mesh Baker Free
  • GPU Instancing
  • Texture Atlasing
  • When getting the material from the script, use Renderer.sharedMaterial instead of Renderer.Material, because the latter will generate a new copy. If the original material is batched, the batch will be invalid

Using Occlusion Culling

General method to reduce drawcall

  • Find the Main Camera in the Hierarchy window, and use the rotation tool to rotate the camera in the Scene window. We can see that no matter which direction the camera is facing, all GameObjects in the scene will be rendered when viewed from the top in the Scene window.
  • Check the Occlusion Culling option on the Main Camera.
  • In the Occlusion interface, click the Bake button in the lower right corner, and wait for a while to complete the baking.

Improve Mesh Resource Performance with Mesh Baker Free Plugin

This plugin can merge grids

A possible side effect of grid merging is that Occlusion Culling does not work because the merged grid is too large.

GPU Instancing

  • Multiple Game Objects using the same mesh (Mesh) can be rendered with fewer Draw Calls to improve rendering performance.
  • Suitable for rendering buildings, forests, grass, or any model that can be reused in the scene.
  • Even if these GameObjects have different colors, sizes, etc., as long as they use the same grid, GPU Instancing can be used.
  • You can also use the GPU Instancing API in scripts to implement related functions:
    Graphics.DrawMeshInstanced and Graphics.DrawMeshInstancedIndirect

Unity's Batching priority:

  • Static Batching > GPU Instancing > Dynamic Batching
    • If Static Batching in the Project Settings > Player interface is checked, the GameObject is also set to Batching Static and the GPU Instancing of the material is turned on: a warning will be displayed in the Inspector, indicating that GPU Instancing will be disabled.

GPU related optimization

  • Three aspects of GPU optimization:
    • Fill Rate:
      • Pixel Fill Rate: The number of pixels that the GPU can draw on the screen per second
      • Texture Fill Rate: The number of textures that the GPU can view per second
  • GPU Memory Bandwidth:
    • The amount of data (in bytes) that the GPU can transfer to the video memory (VRAM) per second
  • Vertex Processing (vertex processing):
    • number of vertices on the mesh
    • The number of operations to do for each vertex

Inspection Method

  1. View the time the GPU spends on rendering in the Profiler
  2. Reduce resolution in Player Settings
  3. Run again to check the rendering time of the GPU
  4. If the performance improves, it means that the GPU is too late to render and there is a problem that the Fill Rate is not enough

Specific optimization method:

  • Reduce the complexity of the Fragment Shader in the shader, because the Fragment shader is responsible for telling the GPU how to draw pixels on the screen.
  • Use a more optimized Shader, such as Mobile Shader (Mobile Shader can also be used in places other than mobile platforms)
  • If you are using Standard Shader, you can reduce the number of textures used, so that Unity will automatically optimize when compiling.
  • It is necessary to analyze the impact of the Image Effect used on the overall Fill Rate. If the impact is really great, some Image Effects should be disabled.

GPU Memory Bandwidth

  • View the time the GPU spends on rendering in the Profiler
  • Lower the Texture Quality setting in Quality Settings
  • Run again to check the rendering time of the GPU
  • If the performance is improved, it means that the texture is too large and the video memory is not enough

Specific optimization method:

  1. Texture Compression
  2. Mipmap 和 Mipmap Streaming

Texture Compression

  • Texture Compression optimization:
    • For example: Build After the construction is completed, a build log will be generated in the Editor Log, open the Window > Console window, and click the Open Editor Log menu in the three small dot icons in the upper right corner of the Console window. Search in the opened text: Used Assets and files from the Resources folder, sorted by uncompressed size,
      where you can see all the textures used in the project but not compressed.
    • There are several corresponding compression methods for changing the compression attribute of an image

Mipmap 和 Mipmap Streaming

  • Mipmap in Unity is equivalent to Texture's Lod technology, in order to prevent stripes from appearing due to insufficient sampling rate of the texture when the object is far away from the camera. The method of generating Lod in Unity is very simple, check Generate Mip Maps in the Texture panel, and then click Apply. After the generation is complete, you can view the pictures corresponding to each level below. The principle should be the mean sampling method.
  • Mipmap is a typical method of exchanging storage space for memory usage (if the Z direction of the resource does not change, then using Mipmap is a waste:)
  • Texture Steaming technology solves the problem of loading the entire Texture Mipmap pyramid when Shader renders. The optimization effect is very good when the model is far away from the camera. In other words, the optimization effect of the big world game is very good. When the camera sees the picture, it will be calculated. A reasonable mipmap level, and then clip all mipmaps of other levels. Do not load, also set in MaxLevelReduction. When the game is running, there will be a process of first blurring and then clearing.

Vertex Processing

  1. Minimize the number of vertices on the mesh (simplifies the model)
  2. Use Normal Map to express model details
  3. If the Normal Map is not used on the model, the Vertex Tangents option of this model can be disabled in the Model Import Setting interface.
  4. Use LODs
  5. Reduce the complexity of the Vertex shader in the custom Shader (Vertex shader is the code block in the shader that tells the GPU how to draw each vertex)
  6. If you are using the Standard shader, try to use a simple shader such as Mobile Shader.
  7. If the Normal Map is not used on the model, you can set Normals to None in the Model Import Setting interface
  8. If the grid is not dynamically generated, or the data of the grid needs to be copied at runtime, the Read/Write Enabled option should not be turned on.
  9. If read and write is enabled, the grid information will be uploaded to both GPU and CPU; otherwise, it will only be uploaded to GPU. Therefore, it is usually not to open the read and write option, which can save memory.
  10. You can reduce the number of model vertices by using texture and normal maps, rationally layout UVs to avoid seams and allow textures to be evenly distributed on the model surface, reuse models and use Instancing and Static Batching to merge models to reduce the number of Draw Calls.
  11. Players will pick up objects and observe them very closely, so the VR layout needs to be very uniform, because they can be observed from any angle in VR.
  12. Also pay attention to details like texture seams, as the player will be watching carefully.
  13. Limiting the number of materials used can reduce the Draw Call and reduce the burden on the GPU.
  14. Models designed and produced in a modular manner can be reused as much as possible.

Asset size scaling recommendations:

  1. It is very important to ensure that the objects in the scene have a consistent size. If the objects in the scene are not uniform in size, such as a door shorter than a person, or a car as big as a mountain, it will make people feel unreal.
  2. In addition, the method of dynamically changing the FOV commonly used in ordinary games is not applicable in VR. In VR, it is recommended to keep the FOV constant.
  3. When scaling the model in the scene, in order to ensure the uniform physical effect, the size of the collision body should also be scaled accordingly. Otherwise, the effect of physical simulation will be unreal.
  4. When people experience VR, they usually dig their heads into the models in the scene, wanting to see places they shouldn't see. We can design that when it is detected that the player turns his head into the model, the entire screen will be blacked out in a gradual manner. To do this, we need to set the correct collider size for all relevant models.

Guess you like

Origin blog.csdn.net/bellainvan/article/details/127002843