Unity3d problem solving based on physical rendering

Source: Internet
Author: User

In the last 1 months, there have been some procedural support for unity's sub-generation development, which is also based on physical rendering, mainly the use of Skyshop Marmoset, which is one of the best options for unity4.x version of PBR.
But in the use and performance, or some of the pits and deficiencies, here are some of their own experience it, I hope that other interested in this program friends to help.

First, encountered a decrease in FPS bug

Before the national day of the old version of the project and the latest version of the project to run without any difference, but the new version on the real machine running efficiency problems, only 7.5fps
Development and operational environments,
XCode6.1 IOS8.1
IPad Air Resolution 2048x1536
Graphic Level GLES 3.0



Figure 1 The old version of the operation, in order to make reference, I here to copy the new version of the scene file to the old project


The new version of the same scene is less than 8FPS

Then the two versions were analyzed with Xcode, the old version fps analyze


New version fps analyze, the same shader consumption of MS is 5 times times the old version

Try to locate the bug after you find it
1 initially considered a marmoset version issue, but with the old version completely covered, FPS does not improve
2 Delete all resources related to the scene resource, after the rebuild, FPS is restored, but this workaround should be nothing more than accept

At present, the temporary solution, the old works of the projectsettings in the document replaced well, the specific reasons to continue to troubleshoot.

There are also some versions of Skyshop's mobile shader that do not have gles3.0 support and need to add keywords to the shader of marmoset used in all scenarios
#pragma surface marmosetsurf marmosetdirect vertex:marmosetvert exclude_path:prepass noforwardadd Approxview
#pragma only_renderers d3d9 OpenGL gles d3d11 d3d11_9x gles3
Good early Marmoset is the design of ubershader, so just change a few just fine.


For unity to support es3.0, you need to set the player Settings. Select automatic or force gles3.0 in the graphics level.

If you use es2.0 only, because Unity uses texcube or Texcubebias to replace Texcubelod, without LOD support, the ability to use cubemap mipmap to achieve roughness is affected

The roughness of the material may not be visible in the final render screen.

If this bug is resolved, then the performance evaluation should be the same as the 10.1 report, the IPad Air can withstand the full screen, 100Draw call supporting the drawing of the Ibl material object, and the shader of the IBL is also the bottleneck

Should be on the pixel shader, if 1, 2 IBL material fills the entire 2048x1536 screen, the same will have the potential bottleneck problem to produce.

Second, the problem of another scene to solve

Draw call can be viewed first in editor or online porfile, and some devices do not support the GPU profile of Unity Editor, so use Xcode

Without any optimizations, draw call is 592, and the two most occupied shader,9.8ms and 7.94ms in the GPU are terrain-occupied (terrain and he and shadow maps).

Also turn off the shadow after the 289 Drawcall, close the terrain after 289->219 (terrain 70DC), close the water surface 289dc->246dc (surface reflection)

The first is the terrain, whether it is shader or DC are a bit more, in Xcode frame view, terrain mapping unreasonable place, from the scene, this size of the terrain needs 70 draw call number is exaggerated, and the other is shader on , if it is a static baking shadow, it should be able to be combined into a shader to draw. It is advisable to start by optimizing the terrain, lowering the DC, merging the shader, if the terrain of u3d is not optimized, instead of making the grid directly in Max.

The surface of the water can optimize the realization of the reflective part, the reflection of the static scene can be pre-baked to a map, and can reflect the part, it is recommended to add to a layer alone. Reduce the number of DCs.

The visual range of the camera, the angle, and the settings of the shader Lod, in Xcode's analysis, some of the far-off mountains are still drawn, and the same high-quality shader as the nearby characters, which should be resolved with unity's internal settings. In addition, the angle of the demo camera is too low, causing objects in the distance to be rendered, if the camera angle is properly modified, such as the traditional 45 angle of view, you should be able to crop a part of the scene object. To reduce the DC and PS fill rates.

Iii. comments on the improvement of Marmoset shader

1. If you do not use the dynamic function of the skyshop Sky box, the Skymanager update can be turned off

PublicvoidLateupdate () {
If(Firstframe) {
If(_globalsky) {
Firstframe =False;
_globalsky.apply (0);
_globalsky.apply (1);
If(_skyboxmaterial) {
_globalsky.apply (_skyboxmaterial,0);
_globalsky.apply (_skyboxmaterial,1);
}
}
}
#if Unity_editor
if(! Application. isplaying) return;
#endif

directly in #if  unity_editor front return so you can save skymanager update cpu Consumption and GC

2 Glossymap used before in order to resolve 7FPS A solution to the problem when trying to Anylaze directly modified in Texturelod The Parameters

tmpvar_40 = Texturelod (_speccubeibl, lookup_38.xyz, LOOKUP_38.W);

For example

tmpvar_40 = texturelod (_speccubeibl, lookup_38.xyz, 1); ,

This efficiency can improve 1 times more than

Analysis can be seen



HIGHP float glosslod_36;
  glosslod_36 = tmpvar_27;
  mediump vec4 spec_37;
  mediump vec4 lookup_38;
  HIGHP vec4 tmpvar_39;
  TMPVAR_39.XYZ = ((V_33.XYZ * tmpvar_32.x) + ((V_34.XYZ * tmpvar_32.y) + (V_35.XYZ * tmpvar_32.z));
  TMPVAR_39.W = glosslod_36;
  lookup_38 = tmpvar_39;
  lowp vec4 tmpvar_40;
  tmpvar_40 = texturelod (_speccubeibl, lookup_38.xyz, LOOKUP_38.W);

It 's not made any changes. Shader , Lod , namely LOOKUP_38.W or according to the Glossy Map the value, in Shader calculated in



lowp  vec4   tmpvar_40;
  tmpvar_40 = texturelod   (_SPECCUBEIBL, LOOKUP_38.XYZ,  1.0 );

in the Shader in a direct pass. Const value of the method, hot update can be seen after Shader consumption of Ms and before there is a certain reduction.

But restarting the game, MS has a reduction of around 40%, which is due to the optimizations made by iOS, and if Lod is determined before shader runs, it will directly pre-fetch the texture, and it will not need to re-perform the sampling in each pix shader.
Therefore, glossy map is recommended to use in moderation, some materials that do not require detail can be replaced directly with a constant roughness parameter.


There is the optimization of the PBR shader algorithm, marmoset own internal MARMO_HQ keyword, through the switch can achieve a certain degree of optimization, first two quality comparison

Marmo_lq


MARMO_HQ differences in the accuracy of shader, as well as the brightness of the shot light

MARMO_HQ the use of vector normalization, similar to the role of energy conservation, The FRESENL function of the algorithm selection, so that the total amount of the output of the rendering is more in line with the physical effect
Self-shadowing Blinn
#ifdef Marmo_diffuse_direct
Spec *=Saturate(10.0*DP);
#else
Spec *=saturate10.0*dot (n,l));
#endif
#ifdef  marmo_diffuse_direct
saturate (10.0*DP);
#else
    spec *=  saturate10.0< Span style= "color: #444444;" >*dot (n,l));
#endif
#ifdef MARMO_HQ
Localn = normalize(LOCALN);
#endif
The above is a sample of some of the normalized settings in the shader code.

There are some other shader that can be optimized.


For example specular intensity,sharpenss,fresnel strength, is not the standard parameter of PBR material, generally only need roughness or glossy map to choose one of the roughness parameters can be
While the other parameters can be removed to reduce the amount of shader calculation

The above mentioned Glossymap and roughness switch, need shader support, the editor must make certain changes.

Fresenl reflection Equation, Marmoset provides two methods of Splinefresnel and Fastfresnel, but the actual amount of computation is still relatively much, according to GDC2014 unity5 scheme, can be replaced by a simple POW 4 times Square form. Determine the direction of the Texturecubelod and the LOD value of the algorithm also optimizes the space.

Ubershader design is an advantage, in the development of the time can reduce a lot of work, the actual operation, the creation and compilation of shader number is very small, easy to analyze and locate shader problems, and in Xcode optimized debugging.

Another is the skymanager part of Marmoset, or needs to be modified, his original design is based on the Sky box to generate the cubemap of IBL, and the real IBL lighting is based on the surrounding environment to generate, so, Skyshop this entire scene unifies a CubeMap method, in the authenticity and the effect is still very worthy of consideration, its IBL generation and the management interface to do the game also is not very convenient, the advantage is that it provides the editor and the Cumbemap generation part of all the code, Whether it's an extension or a bug fix, it's doable.

unity4.x version, in the mobile side is unable to support the delay rendering method, so the light source in the scene will be more stringent restrictions, a light is the limit of the direction of the scene, the use of the point light source, can only use Lightmap and lightmap of the specific algorithm, but also to users to achieve their own , Marmoset realized directional lightmap lighting in Marmosetdirect.cginc, Lightingmarmosetdirect_dirlightmap This naming is to be named according to the Unity Custom shader specification, can be automatically recognized in the Unity rendering pipeline, if you want to optimize, or to support other types of liaghtmap such as dual lightmap, it is best to provide a method of optimization. We hope that our own projects will provide TBDR support in the future to achieve a large number of light source support.

The final consideration or to UE4 mobile products to do a competitive comparison:

UE4 's Sum Temple project is a good reference, gles2.0 graphics specifications, can also achieve good results

UE4 Mobile's graphical specifications, simply speaking, is a directional light+distance shadow to generate light and shadows, other scene shading and the brightness of various color lights by lightmap to achieve,

For IBL, you can generate IBL for objects within the development area by setting refelctcapture, and you can use multiple CUBEMAP implementations in the same scenario (PPT says he uses a unified cubemap on the mobile side, This needs to be followed directly to its engineering to do a real machine analysis). There are also post-processing effects such as Bloom+aa+light shaft+dof, but due to the limitations of the graphics specifications, the ES2.0 version is not supported by true HDR.

Next I'll prepare an analysis of UE4 rendering and optimization methods.

Four, summary

Through this u3d PBR experiment, on the ipad Air and K1 hardware-level machines, the production of PBR game is not a problem, the first worry about the retina screen filling rate problem, in the actual test, still feasible, but need the entire development team has a certain degree of optimization awareness, In order to ensure a good operational efficiency throughout, such as support for IBL distribution, and game production, but also to consider what type of game, in order to play the advantages of PBR rendering, especially the indirect lighting on the quality of the game scene (the consumption of the highest Iblshader support is the high light portion of indirect lighting). There is more use of Unity's batch function, to minimize DC and about shader state switching, and so on, in addition, unfortunately, because of the problem of 7fps bug, this time did not have the post effect part of unity implementation, Personal considerations can be ue4 this part of the implementation of the transplant, UE4 to U3d can play a very good role in the future of the PBR effect and efficiency of testing and optimization, a certain comparative analysis and reference, is also very helpful.

Unity3d problem solving based on physical rendering

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.