如果应用程序的帧缓冲区方向与显示器的原生方向(对于大多数设备为纵向)不匹配,则每次渲染帧出现在屏幕上时,Android 都需要应用额外的旋转。这种额外的旋转可能会产生性能成本,具体取决于设备的硬件功能。
大多数情况下,可以在渲染期间应用此旋转,而开销很小。为此,请导航到 Android Player Settings(另请参阅: PlayerSettings.vulkanEnablePreTransform
)并启用 Apply display rotation during rendering。
注意:此旋转仅适用于 Unity 直接渲染到后备缓冲区时。它在渲染到渲染纹理时不起作用。
为了应用旋转,Unity 修改了投影矩阵(UNITY_MATRIX_MVP、UNITY_MATRIX_P)。这意味着旋转本身是在顶点着色器中应用的。
preTransform
设置不会影响 Unity 的 C# API 的行为。例如,Screen.width
的值不会因为 preTransform
设置而改变。这同样适用于视口和剪辑矩形。Unity 根据需要调整这些设置,还会处理来自后备缓冲区的回读操作,例如 Grab Pass、ReadPixels 或 Screenshot。
Unity 提供实用程序宏来处理着色器中的特殊情况(有关更多信息,请参阅下面的已知限制部分)。
仅当以下所有条件都为真时才定义宏 UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION(否则,未定义):
preTransform
在播放器设置中启用UNITY_DISPLAY_ORIENTATION_PRETRANSFORM 是一个常量,设置为当前 preTransform
旋转。它的值是以下之一:
如果 UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION 未定义,或者当渲染到渲染纹理时,UNITY_DISPLAY_ORIENTATION_PRETRANSFORM 的值是 UNITY_DISPLAY_ORIENTATION_0。
UNITY_DISPLAY_ORIENTATION_PRETRANSFORM 被转换为 Vulkan 特化常量,这样它在 if
或 switch
语句中的使用会很高效。
在以下情况下,如果启用 preTransform,则 Unity 项目可能需要额外修改,然后才能使用:
SV_Position
)这些情况仅适用于直接渲染到后备缓冲区时。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.