在 Unity 写 Shader 的时候,在一个循环里面使用了 tex2D 函数,相似与下面这样:app
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 number can't use gradient instructions in loops with break
错误提示上说了,没有办法对循环进行展开,由于展开的迭代次数太多了,快到一千次了,请使用 [unroll(n)] 特性来指定一个可能的最大值。oop
[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; }
因为 tex2D 函数须要肯定 LOD ,即所使用的纹理层,这才必须 unroll,若是直接使用可以指定 LOD 的 tex2Dlod,那么就能够避免了。3d
函数签名:code
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; }
既然提示说不支持迭代,那么直接避免迭代就行了嘛。get
Issues with shaderProperty and for-loop[via Unity Forum]:见4楼 Dolkar 的回答,很详细了。it