Unity3D如何实现动态分辨率降低渲染开销
发表于:2024-10-18 作者:千家信息网编辑
千家信息网最后更新 2024年10月18日,这篇文章主要介绍Unity3D如何实现动态分辨率降低渲染开销,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!之前项目降低分辨率我们都普遍使用Screen.SetResolutio
千家信息网最后更新 2024年10月18日Unity3D如何实现动态分辨率降低渲染开销
这篇文章主要介绍Unity3D如何实现动态分辨率降低渲染开销,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
之前项目降低分辨率我们都普遍使用Screen.SetResolution,但是它有两个问题。
1.每次设置的时候屏幕会闪烁。
2.降低分辨率与摄像机无关,无法做到只降低3D摄像机的分辨率,保留UI摄像机不降低分辨率。
其实我们可以使用摄像机动态分辨率,如下图所示,给需要降低分辨率的摄像机打开allow Dynamic Resolution属性。
如下图所示,在ProjectSetting上必须勾选Enable Frame Timing Stats属性。
代码中就可以很方便设置分辨率了。
ScalableBufferManager.ResizeBuffers(m_widthScale, m_heightScale);
如下图所示在iPhone X上,频繁设置3D摄像机分辨率并不会出现闪烁的情况,而且并没有影响UI摄像机看到的文本(Text)的分辨率
需要注意的是动态分辨率安卓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("-- ")) { 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}\n动态分辨率: {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如何实现动态分辨率降低渲染开销"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
分辨率
摄像机
摄像
动态
开销
内容
属性
篇文章
频繁
两个
代码
价值
兴趣
可编
小伙
小伙伴
屏幕
情况
文本
时候
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
殡葬软件开发
金融微服务架构数据库
软件开发企业资质要求
锐捷网络技术总监李嗣明
北京华为服务器虚拟化解决方案
浙江gps双模时间服务器设置
vb获取服务器时间
网络安全公益广告海报设计
香港互联网科技etf
云服务器可能发生的故障
服务器可管理性提示联机失败
数据库中获取数据的函数
重庆软件开发
蔓利网络技术公司
tcga数据库下载cmd
烟台开发区网络技术有限公司
10台服务器并发量
网络安全专业人才培养意义
乐玛士 软件开发
我的世界015服务器
pg数据库拼接字符串sql
研究生数据库方向有前途吗
肇庆app软件开发设计
发酵罐设计软件开发
统计局网络安全工作责任制
云南天庆网络技术有限公司
网络安全入侵防御系统
菲律宾招聘网络安全
四川网络技术服务价目表
自查网络安全需要自查什么