Batch Processing for Unity3D game scenario optimization and unity3d game scenario

Source: Internet
Author: User

Batch Processing for Unity3D game scenario optimization and unity3d game scenario

Remember my name when you like my blog:Qin YuanpeiMy blog address: http://qinyuanpei.com
Please indicate the source for reprinting. Author:Qin Yuanpei, The Source: http://blog.csdn.net/qinyuanpei/article/details/48262583

Dear friends, I amQin Yuanpei, Welcome to follow my blog, my blog address is http://qinyuanpei.com.

Zookeeper recently started to study Unity3D game scenario optimization. Every time I mention the topic of game optimization, in my mind, I will see the magic game "Legend of the legend of the fairy sword", which makes the four Titans glorious. As a fairy sword player that uses the Unity3D engine for game development, I once naively thought that the 2nd anniversary of the fairy sword with the Unity3D engine will make me look forward to the future of the series of games "Legend of the fairy sword, however, when the game is truly present in front of me, I feel the embarrassment and disappointment that I have never had before in xianjian games, what I am embarrassed about is that Unity3D, a powerful game engine, has been played by ruansoft as a ghost, i'm disappointed that this game has nothing to expect except plot and dance.

I know that there will be a bunch of xianjian players accusing me of saying that xianjian is playing the story, so if the story is good, you can forgive others. However, we all know that "Legend of the legend" is an RPG Game, it is not a new GAL animation every three years, a romance novel updated every three years, or an idol movie played every three years. Today, two years ago, I was able to pass the "Legend of the legend of the fairy sword", but this time I really can't play any more. When a game is named "legend 6: Titanium fall" due to optimization problems, as a player, I really don't want to cleanse the game any more, even though I once loved it. So let's get down to the point. As a programmer, we still do what programmers should do. So what do we say today? Let's talk about batch processing in Unity3D!

1. What is batch processing?

When drawing images we know that Unity3D is essentially called to draw a graph on the screenOpneGLOrDirectXSuch an API will produce a certain amount of performance consumption in this process. DrawCall is a volume used to describe the number of draws in OpenGL. For example, a basic OpenGL drawing process isSet color->Drawing Method->Vertex coordinates->Draw->EndIn the process of drawing, every frame repeats this process. This is a DrawCall, so when the draw process in the game becomes complex, it will bring a sharp increase in DrawCall, as a result, the performance of the game becomes an optimization problem. So what measures have you taken in Unity3D to reduce DrawCall? This is the batch processing we want to talk about today. In other words, Unity3D uses batch processing to reduce DrawCall. Batch Processing wants to achieve higher rendering efficiency by reorganizing the object mesh, imagine if we merge multiple objects into one object, we only need to draw one object at a time. Therefore, this can definitely reduce DrawCall, a more profound understanding is that this reflects the idea of resource loop calling. Friends who have been familiar with Android development must know that the ListView control can "cache" its elements to improve efficiency, because we can find that ListView is actually "reusing" the list items to some extent, which improves the efficiency. In Unity3D, this principle is also followed. One premise of batch processing in Unity3D is that objects of the same material can be merged. If these objects use different materials, then, we can merge the textures corresponding to these materials into an "Gallery", and useRenderer. sharedMaterialRatherRenderer. materialTo ensure that materials can be shared. For details about DrawCall, we can see from here that the blogger has not studied the graphics field in depth, so I am not here to make an axe, haha!

Ii. Batch Processing in Unity3D

Zookeeper has two methods in Unity3D: static batch processing and dynamic batch processing. Let's talk about these two different batch processing methods respectively!

Static Batch Processing

Zookeeper is actually known for static batch processing. Why? This is because we cultivate this habit in the process of using Unity3D, that is, to select the Static option for relatively Static objects in the scenario, which is calledStatic GameObjectsThis feature is closely related to Lightmapping, Navigation, Off-meshLinks, ReflectionProbe, Occluder and Occludee, as a result, all users know about static batch processing. The content related to scenario optimization will be covered in subsequent blogs. I hope you can keep an eye on my blog updates in a timely manner. Static batch processing allows the game engine to reduce the DrawCall produced by drawing objects of any size as much as possible. It will occupy more memory resources and less CPU resources, because it requires additional memory resources to store the merged geometric structure, if several objects share the same geometric structure before static batch processing, A ry will be created for each object, whether in the editor or running. This looks like a tough option. You need to make the most correct choice between memory performance and rendering performance. Internally, static batch processing converts static objects to World Space and creates a large vertex + index buffer for them. Then, in the same batch, a series of "cheap" painting calls, a series of "cheap", almost no state changes. So technically, it does not save "3D calls", but it can save the state changes between them (this is an expensive part ). It is very easy to use Static batch processing. Just check the Static option of the object!

Dynamic Batch Processing

Compared with static batch processing, dynamic batch processing requires stricter requirements. It requires that dynamic objects in batch processing have certain vertices, therefore, dynamic batch processing is only applicable to grids that contain less than 900 vertex attributes. If your shader uses the vertex position, normal, and single light, then you can batch process the dynamic objects of 300 vertices. If your shader uses the vertex position, normal, uv0, UV1 and tangent can only process dynamic objects of 180 vertices. Next, the most important thing is,If dynamic objects use different materials, the efficiency of dynamic batch processing is not greatly improved.If a dynamic object uses a multi-dimensional sub-material, the batch processing is invalid. If a dynamic object receives real-time light and shade, the same batch processing is invalid. The following is a script example that combines multiple objects into one object:

[MenuItem ("ModelTools/Merge multiple objects into one")] static void CombineMeshs2 () {// all objects selected in the editor [] objs = Selection. gameObjects; if (objs. length <= 0) return; // grid information array MeshFilter [] meshFilters = new MeshFilter [objs. length]; // The Renderer array MeshRenderer [] meshRenderers = new MeshRenderer [objs. length]; // merge the instance array CombineInstance [] combines = new CombineInstance [objs. length]; // Material array Material [] mats = new Material [objs. length]; for (int I = 0; I <objs. length; I ++) {// obtain the grid information meshFilters [I] = (GameObject) objs [I]). getComponent <MeshFilter> (); // get the Renderer meshRenderers [I] = (GameObject) objs [I]). getComponent <MeshRenderer> (); // obtain the material mats [I] = meshRenderers [I]. sharedMaterial; // merge instance combines [I]. mesh = meshFilters [I]. sharedMesh; combines [I]. transform = meshFilters [I]. transform. localToWorldMatrix;} // create a new object GameObject go = new GameObject (); go. name = "CombinedMesh _" + (GameObject) objs [0]). name; // set the grid information MeshFilter filter = go. transform. getComponent <MeshFilter> (); if (filter = null) filter = go. addComponent <MeshFilter> (); filter. sharedMesh = new Mesh (); filter. sharedMesh. combineMeshes (combines, false); // set the Renderer MeshRenderer render = go. transform. getComponent <MeshRenderer> (); if (render = null) render = go. addComponent <MeshRenderer> (); // sets the material render. sharedMaterials = mats ;}

The core of the zookeeper script is the CombineMeshes () method. This method has three parameters: the first parameter is the array of the merged instance, and the second parameter is whether to merge the mesh of the Child object, the third parameter is whether to share the material. If you want the object to share the material, the third parameter is true; otherwise, the third parameter is false. During my tests, I found that if the mesh of the Child object is merged, each child object cannot use a separate material, by default, the first material is used as the material of the merged object. The following shows the comparison of multiple objects before merging and one object after merging:

Iii. batch processing efficiency analysis

Why does batch processing play a role in improving game efficiency? Let's take a look at the following groups of test comparisons:

Other Objects 1. Three different objects use the same material without static batch processing or dynamic batch processing:DrawCall is 4, the number of faces is 584, and the number of vertices is 641

Other Objects 2. Use the same material for three different objects and only perform static batch processing without dynamic batch processing:DrawCall is 2, the number of faces is 584, and the number of vertices is 641

Other Objects 3. Use different materials for three different objects without static batch processing or dynamic batch processing:DrawCall is 4, the number of faces is 584, and the number of vertices is 641

2017100004. Three different objects use different materials. Only static batch processing is performed without dynamic batch processing:DrawCall is 4, the number of faces is 584, and the number of vertices is 641

Optional values 5. Use different materials for three different objects without static batch processing. Only dynamic batch processing is required:DrawCall is 4, the number of faces is 584, and the number of vertices is 641

2017100006. Three different objects use different materials for static batch processing and dynamic batch processing:DrawCall is 4, the number of faces is 584, and the number of vertices is 641

2017-12-7. Use the same material for three different objects without static batch processing. Only dynamic batch processing is required ::DrawCall is 4, the number of faces is 584, and the number of vertices is 641

Zookeeper you can note that only the DrawCall of the second group is reduced in the test results of each group. This means that only when different objects use the same material can the DrawCall be reduced to a certain extent through batch processing, that is, we mentioned at the beginning of this article to ensure material sharing as much as possible. Some objects in the game scene were dynamically batch processed yesterday afternoon, but it was found that DrawCall was very unstable during the actual test, however, in some scenarios, DrawCall can be reduced very low. If neither static or dynamic batch processing can optimize the scenario, so where should the optimization of Unity3D game scenarios start? I think this is a place where every one of us should explore with our heart. After all, the first thing we need to do is to ensure that the game can be played smoothly by players, simply emphasizing the engine and picture, however, we often ignore the subjective initiative of engine users and hope to resolve all the problems to the engine. This idea is wrong and backward, and the issue of xianjian 6 is completely out of use, I often see people in public saying that xianjian will change to "Unreal 3" in the future. In fact, according to the current state of North soft, giving them an illusory 4 is nothing but a breeze. On zhihu, I saw a high school student girl who claimed to be a game of the next age when I was 15 years old. When I made a game that could be called a DEMO, I felt that I could build an engine, even more, if you use DirectX or OpenGL to encapsulate a number of functions, you can say that you will play the game engine. Oh, are you sure your game can run on someone else's computer or mobile phone? The importance of optimization is evident.

Iv. Summary

Finally, let's sort out the following points through this article:
  1. If different objects share materials, you can directly reduce DrawCall through static batch processing.
  2. Dynamic batch processing does not reduce DrawCall, number of faces, and number of vertices (I will not tell you that I have merged models in many scenarios yesterday, and the number of result faces and vertices has not been lowered, 23333)
  3. Both static batch processing and dynamic batch processing will affect Culiing. This is also a concept involving scenario optimization. Okay, in order to reduce the DrawCall of the scenario, I may need to study a lot of Optimization content recently ......
Today's content is like this. I hope it will be helpful for you to learn about Unity3D. You are welcome to discuss these issues with me to promote each other. After all, this is the initial purpose of my blog writing, haha!

Related Links

Remember my name when you like my blog:Qin YuanpeiMy blog address: http://qinyuanpei.com
Please indicate the source for reprinting. Author:Qin Yuanpei, The Source: http://blog.csdn.net/qinyuanpei/article/details/48262583

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Reprinted, please indicate the author and source of this Article. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.