溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Unity3D如何實(shí)現(xiàn)動(dòng)態(tài)分辨率降低渲染開(kāi)銷(xiāo)

發(fā)布時(shí)間:2022-01-05 18:09:02 來(lái)源:億速云 閱讀:2001 作者:小新 欄目:大數(shù)據(jù)

這篇文章主要介紹Unity3D如何實(shí)現(xiàn)動(dòng)態(tài)分辨率降低渲染開(kāi)銷(xiāo),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

之前項(xiàng)目降低分辨率我們都普遍使用Screen.SetResolution,但是它有兩個(gè)問(wèn)題。

1.每次設(shè)置的時(shí)候屏幕會(huì)閃爍。

2.降低分辨率與攝像機(jī)無(wú)關(guān),無(wú)法做到只降低3D攝像機(jī)的分辨率,保留UI攝像機(jī)不降低分辨率。

其實(shí)我們可以使用攝像機(jī)動(dòng)態(tài)分辨率,如下圖所示,給需要降低分辨率的攝像機(jī)打開(kāi)allow Dynamic Resolution屬性。

Unity3D如何實(shí)現(xiàn)動(dòng)態(tài)分辨率降低渲染開(kāi)銷(xiāo)

如下圖所示,在ProjectSetting上必須勾選Enable Frame Timing Stats屬性。

Unity3D如何實(shí)現(xiàn)動(dòng)態(tài)分辨率降低渲染開(kāi)銷(xiāo)

代碼中就可以很方便設(shè)置分辨率了。

    ScalableBufferManager.ResizeBuffers(m_widthScale, m_heightScale);

如下圖所示在iPhone X上,頻繁設(shè)置3D攝像機(jī)分辨率并不會(huì)出現(xiàn)閃爍的情況,而且并沒(méi)有影響UI攝像機(jī)看到的文本(Text)的分辨率

Unity3D如何實(shí)現(xiàn)動(dòng)態(tài)分辨率降低渲染開(kāi)銷(xiāo)

需要注意的是動(dòng)態(tài)分辨率安卓Android(僅適用于Vulkan) 或者也可以用SRP可編程渲染管線,最后在修改RT這樣就都支持了。

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 m_gpuFrameTime;    double m_cpuFrameTime;     // Use this for initialization    void Start()    {        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}\nResolution: {2}x{3}\n",            m_widthScale,            m_heightScale,            rezWidth,            rezHeight);    }      private void OnGUI()    {        float oldWidthScale = m_widthScale;        float oldHeightScale = m_heightScale;         // One finger lowers the resolution        if (GUILayout.Button("<size=100>--</size>"))        {            m_heightScale = Mathf.Max(minResolutionHeightScale, m_heightScale - scaleHeightIncrement);            m_widthScale = Mathf.Max(minResolutionWidthScale, m_widthScale - scaleWidthIncrement);        }         // Two fingers raises the resolution        if (GUILayout.Button("<size=100>++</size>"))        {            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}\n動(dòng)態(tài)分辨率: {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_frameCount;        if (m_frameCount <= kNumFrameTimings)        {            return;        }        FrameTimingManager.CaptureFrameTimings();        FrameTimingManager.GetLatestTimings(kNumFrameTimings, frameTimings);        if (frameTimings.Length < kNumFrameTimings)        {            Debug.LogFormat("Skipping frame {0}, didn't get enough frame timings.",                m_frameCount);             return;        }         m_gpuFrameTime = (double)frameTimings[0].gpuFrameTime;        m_cpuFrameTime = (double)frameTimings[0].cpuFrameTime;    }}

以上是“Unity3D如何實(shí)現(xiàn)動(dòng)態(tài)分辨率降低渲染開(kāi)銷(xiāo)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI