Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize dynamic Resolution and reduce rendering overhead in Unity3D

Shulou Source: shulou.com Published: 2022-06-01 04:57:04 10月04日 Update

This article mainly introduces Unity3D how to achieve dynamic resolution to reduce rendering overhead, the article is very detailed, has a certain reference value, interested friends must read it!

We have generally used Screen.SetResolution to reduce the resolution of previous projects, but it has two problems.

1. The screen flashes each time it is set.

two。 Reducing the resolution has nothing to do with the camera, so it is impossible to reduce the resolution of the 3D camera only, and retain the UI camera without reducing the resolution.

In fact, we can use the camera dynamic resolution, as shown in the following image, to turn on the allow Dynamic Resolution property for the camera that needs to reduce the resolution.

As shown in the following figure, the Enable Frame Timing Stats attribute must be checked on ProjectSetting.

It is easy to set the resolution in the code.

ScalableBufferManager.ResizeBuffers (m_widthScale, m_heightScale)

As shown in the following figure, frequently setting the 3D camera resolution on iPhone X does not cause flicker and does not affect the resolution of the text (Text) seen by the UI camera.

It is important to note that dynamic resolution Android Android (Vulkan only) or you can also use SRP programmable rendering pipeline, and finally modify RT so that both are supported.

Using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI; public class DynamicResolutionTest: MonoBehaviour {public Text screenText; FrameTiming [] frameTimings = new FrameTiming [3]; public float maxResolutionWidthScale = 1.0f; public float maxResolutionHeightScale = 1.0f; public float minResolutionWidthScale = 0.5f; public float minResolutionHeightScale = 0.5f; public float scaleWidthIncrement = 0.1f; public float scaleHeightIncrement = 0.1f; float m_widthScale = 1.0f; float m_heightScale = 1.0f / / Variables for dynamic resolution algorithm that persist across frames uint m_frameCount = 0; const uint kNumFrameTimings = 2; double massigpuFrameTime; double massively cpuFrameTime; / / Use this for initialization void Start () {int rezWidth = (int) Mathf.Ceil (ScalableBufferManager.widthScaleFactor * Screen.currentResolution.width); int rezHeight = (int) Mathf.Ceil (ScalableBufferManager.heightScaleFactor * time) ScreenText.text = string.Format ("Scale: {0:F3} x {1:F3}\ nResolution: {2} x {3}\ n", m_widthScale, m_heightScale, rezWidth, rezHeight);} private void OnGUI () {float oldWidthScale = masked widthScale; float oldHeightScale = m_heightScale / / One finger lowers the resolution if (GUILayout.Button ("- -")) {m_heightScale = Mathf.Max (minResolutionHeightScale, m_heightScale-scaleHeightIncrement); m_widthScale = Mathf.Max (minResolutionWidthScale, m_widthScale-scaleWidthIncrement) } / / Two fingers raises the resolution if (GUILayout.Button ("+ +")) {m_heightScale = Mathf.Min (maxResolutionHeightScale, m_heightScale + scaleHeightIncrement); m_widthScale = Mathf.Min (maxResolutionWidthScale, m_widthScale + scaleWidthIncrement);} if (m_widthScale! = oldWidthScale | | m_heightScale! = oldHeightScale) {ScalableBufferManager.ResizeBuffers (m_widthScale, m_heightScale) }} / / Update is called once per frame void Update () {DetermineResolution (); int rezWidth = (int) Mathf.Ceil (ScalableBufferManager.widthScaleFactor * Screen.currentResolution.width); int rezHeight = (int) Mathf.Ceil (ScalableBufferManager.heightScaleFactor * Screen.currentResolution.height) ScreenText.text = string.Format ("Scale: {0:F3} x {1:F3}\ ndynamic resolution: {2} x {3}\ nScaleFactor: {4:F3} x {5:F3}\ nGPU: {6:F3} CPU: {7:F3}", m_widthScale, m_heightScale, rezWidth, rezHeight, ScalableBufferManager.widthScaleFactor ScalableBufferManager.heightScaleFactor, m_gpuFrameTime, m_cpuFrameTime) } / / Estimate the next frame time and update the resolution scale if necessary. Private void DetermineResolution () {+ + m frames; if (m_frameCount)

Tags: Resolution camera camera motion overhead content attributes articles frequent two code value interest editable buddy partner screen situation text time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia OPPO Reno Microsoft Redmi