Unity - 着色器在Android(OpenGL)和编辑器(DX11)上的工作方式不同

时间:2021-10-11 03:56:21

I wrote circular cutout sprite shader which works ok in editor & standalone version but behaves incorrectly on Android devices.

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

In essence it's simple pixel shader:

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

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);            
}

Rendering in editor (DX11 on DX9 GPU)

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

Unity  - 着色器在Android(OpenGL)和编辑器(DX11)上的工作方式不同

Screenshot from Android (OpenGL - Nexus 4)

Android屏幕截图(OpenGL - Nexus 4)

As you can see exactly in the middle there are pixels which should be red

正如您可以在中间看到的那样,有些像素应该是红色的

Unity  - 着色器在Android(OpenGL)和编辑器(DX11)上的工作方式不同

I'm using Unity 5.0.0f4. Attaching zipped test project: ShaderTest.zip (30kB)

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

2 个解决方案

#1


In atan2, as the y parameter approaches zero from above (eg - IN.texcoord.x - 0.5 ~= +0), you're going to start getting values near PI, because you'll get arctan(y/x) + PI, where y ~= 0, and arctan(0) = 0 (http://en.wikipedia.org/wiki/Atan2). The Android version is presumably using lower precision floating point operations, and thus this is much more noticeable there. Likely if you zoomed in enough on the desktop version, you'd see pixels that fail your conditional as well.

在atan2中,当y参数从上面接近零时(例如-IN.texcoord.x - 0.5~ = + 0),你将开始在PI附近获得值,因为你将获得arctan(y / x)+ PI,其中y~ = 0,arctan(0)= 0(http://en.wikipedia.org/wiki/Atan2)。 Android版本可能使用较低精度的浮点运算,因此在那里更加引人注目。可能如果你在桌面版本上放大了,你会看到像素也会失败你的条件。

#2


After updating to newer version of Unity problem resolved itself.

更新到更新版本的Unity后,问题解决了。

#1


In atan2, as the y parameter approaches zero from above (eg - IN.texcoord.x - 0.5 ~= +0), you're going to start getting values near PI, because you'll get arctan(y/x) + PI, where y ~= 0, and arctan(0) = 0 (http://en.wikipedia.org/wiki/Atan2). The Android version is presumably using lower precision floating point operations, and thus this is much more noticeable there. Likely if you zoomed in enough on the desktop version, you'd see pixels that fail your conditional as well.

在atan2中,当y参数从上面接近零时(例如-IN.texcoord.x - 0.5~ = + 0),你将开始在PI附近获得值,因为你将获得arctan(y / x)+ PI,其中y~ = 0,arctan(0)= 0(http://en.wikipedia.org/wiki/Atan2)。 Android版本可能使用较低精度的浮点运算,因此在那里更加引人注目。可能如果你在桌面版本上放大了,你会看到像素也会失败你的条件。

#2


After updating to newer version of Unity problem resolved itself.

更新到更新版本的Unity后,问题解决了。