Search Unity

WebGL Shader problem, frag and for loops

Discussion in 'Web' started by DanWeston, Feb 26, 2017.

  1. DanWeston

    DanWeston

    Joined:
    Sep 9, 2014
    Posts:
    32
    I have been trying to perform a post-process effect which uses a for loop within the frag shader. When running a build of my test scene, the shader completely falls over and my image effect does not work (to be clear the shader works fine within the editor). Here's an example of what I've been attempting:

    Code (CSharp):
    1.  
    2. float2 coord = float2(i.uv.x, i.uv.y);
    3. int max = 9;
    4. for (int i = 0; i < max; i++)
    5. {
    6.   if (i > _loopSize)
    7.   {
    8.     break;
    9.   }
    10.   col += tex2D(_MainTex, coord  + i);
    11.   col += tex2D(_MainTex, coord  - i);
    12. }
    13.  
    If I don't use a loop and hard code the amount off texture lookups, the shader runs fine and the image effect works perfectly! Although this is great, my requirement is to enable a dynamic kernel size without having to adapt any shader code.

    I initially thought that the issue may be to do with the double texture lookup performed within the loop, however, I tested the hard-coded version of the shader by placing a simple for loop (performing addition within each iteration) before the sampling. This caused the image effect to again not work.

    As far as I am aware I am not doing anything incorrect with my formatting and use of a for loop for GLSL. Any Ideas? I am also having the same issue for Android builds as well...
     
  2. kognito1

    kognito1

    Joined:
    Apr 7, 2015
    Posts:
    331
    I don't believe dynamic loops are supported for WebGL 1. This might work for WebGL 2 though.