打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
报错解决 unable to unroll loop, loop does not appear to terminate in a timely manner (994 iterations) or unrolled loop is too large, use the [unroll(n)] attribute to force an exact higher number

在 Unity 写 Shader 的时候,在一个循环里面使用了 tex2D 函数,类似与下面这样:

fixed2 center = fixed2(0.5,0.5);fixed2 uv = i.uv - center;for (fixed j = 0; j < _Strength; j++) {    c1 += tex2D(_MainTex,uv*(1 - 0.01*j) + center).rgb;}

打apk没什么问题,但是打win版本的时候有个报错

unable to unroll loop, loop does not appear to terminate in a timely manner (994 iterations) or unrolled loop is too large, use the [unroll(n)] attribute to force an exact higher numbercan't use gradient instructions in loops with break

解决方案1.

错误提示上说了,没有办法对循环进行展开,因为展开的迭代次数太多了,快到一千次了,请使用 [unroll(n)] 特性来指定一个可能的最大值。

[unroll(88)]fixed2 center = fixed2(0.5,0.5);fixed2 uv = i.uv - center;for (fixed j = 0; j < _Strength; j++) {    c1 += tex2D(_MainTex,uv*(1 - 0.01*j) + center).rgb;}

解决方案2.

由于 tex2D 函数需要确定 LOD ,即所使用的纹理层,这才必须 unroll,如果直接使用能够指定 LOD 的 tex2Dlod,那么就可以避免了。

函数签名:

  • tex2D( Sampler, IN.Texture );
  • tex2Dlod( Sampler, float4( IN.Texture, 0.0f, 0.0f ) );
fixed4 center = fixed4(.5,.5,0,0);fixed4 uv = fixed4(i.uv,0,0) - center;            for (fixed j = 0; j < _Strength; j++) {    c1 += tex2Dlod(_MainTex,uv*(1 - 0.01*j) + center).rgb;}            

解决方案3.

既然提示说不支持迭代,那么直接避免迭代就好了嘛。


参考链接

Issues with shaderProperty and for-loop[via Unity Forum]:见4楼 Dolkar 的回答,很详细了。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【浅墨Unity3D Shader编程】之六 暗黑城堡篇: 表面着色器(Surface Shader...
Shader: 获取深度贴图数据
【Unity Shader】2D动态云彩
[Unity]UGUI流光材质shader
vivado HLS硬件化指令(三)HLS增大运算吞吐量的硬件优化
C Optimisation tutorial
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服