【问题标题】:Unity - shader works differently on Android (OpenGL) and Editor (DX11)Unity - 着色器在 Android (OpenGL) 和 Editor (DX11) 上的工作方式不同
【发布时间】:2015-05-04 12:34:31
【问题描述】:

我编写了圆形切口精灵着色器,它在编辑器和独立版本中运行良好,但在 Android 设备上表现不正确。

本质上是简单的像素着色器:

fixed4 frag(pixelData IN) : SV_Target
{
 const float PI = 3.1415;
 float angle = -atan2(IN.texcoord.x - 0.5, -IN.texcoord.y + 0.5) + PI;

 if (angle < _Percent * 2 * PI) //_Percent is in range from 0 to 1        
     return tex2D(_MainTex, IN.texcoord) * IN.color;            
 else
     return float4(1,1,1,0);            
}

在编辑器中渲染(DX9 GPU 上的 DX11)

Android 屏幕截图(OpenGL - Nexus 4)

正如你所看到的,在中间有一些像素应该是红色的

我正在使用 Unity 5.0.0f4。附加压缩测试项目:ShaderTest.zip (30kB)

【问题讨论】:

    标签: android opengl-es unity3d directx shader


    【解决方案1】:

    atan2 中,随着y 参数从上方接近零(例如-IN.texcoord.x - 0.5 ~= +0),您将开始获取PI 附近的值,因为您将获得arctan(y/x) + PI,其中y ~= 0arctan(0) = 0 (http://en.wikipedia.org/wiki/Atan2)。 Android 版本可能使用了较低精度的浮点运算,因此这在此处更为明显。如果您在桌面版本上放大到足够大,您可能会看到不符合条件的像素。

    【讨论】:

    • 从它在 wiki 上所说的 atan2 仅当 x == 0 &amp;&amp; y == 0 两者都未定义时,所以情况并非如此
    • 更新的答案...答案的本质是正确的,但情况并不完全正确。
    • “在 atan2 中,当 y 参数从上方接近零时(例如 - IN.texcoord.y + 0.5 ~= +0)”你能澄清一下你的意思是哪个参数吗? atan2 被定义为 atan2(y,x) 但您与 -IN.texcoord.y + 0.5 相关,我将其用于 x(第二个)参数。
    • 再次更新答案以更正参数值...相同的参数成立。
    【解决方案2】:

    更新到较新版本的 Unity 后问题自行解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多