# Loops in shader

**URL:** <https://discourse.processing.org/t/loops-in-shader/8176>\
**Category:** Project Guidance\
**Created:** [February 6, 2019, 8:35pm UTC](https://discourse.processing.org/t/loops-in-shader/8176 "2019-02-06T20:35:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vjjv](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@vjjv](https://discourse.processing.org/u/vjjv)\
**Post date:** [February 6, 2019, 8:35pm UTC](https://discourse.processing.org/t/loops-in-shader/8176/1 "2019-02-06T20:35:47Z")

</div>

Hi !m trying to fix this shader. the effects is a radial blur around a point position, passing from the cpu in a array. The calculations works fine for each point and generates de effect, but as you can see in this picture, for each loop the shader keep generate samples, and i dont know how to avoid. i only want the blur for each point in the array

```auto
#version 150

in vec2 varyingtexcoord;
uniform sampler2DRect tex0;

uniform int size;

 float exposure = 0.79;
 float decay = 0.9;
 float density = .9;
 float weight = .1;
 int samples = 25;

out vec4 fragColor;
const int MAX_SAMPLES = 25;
const int N = 3;
uniform vec2 ligthPos [N];

int a = 1;

vec4 halo(vec2 pos){

    float illuminationDecay = 1.2;
    vec2 texCoord = varyingtexcoord;
    vec2 current = pos.xy;
    vec2 deltaTextCoord = texCoord - current;

    deltaTextCoord *= 1.0 / float(samples) * density;
    vec4 color = texture(tex0, texCoord);

    for(int i=0; i < MAX_SAMPLES; i++){

        texCoord -= deltaTextCoord;

        vec4 sample = texture(tex0, texCoord);
        sample *= illuminationDecay * weight;
        color += sample;
        illuminationDecay *= decay;

    }
    return color;

}

void main(){

        vec4 accum = vec4(0.0);

        for(int e = 0; e < N;e++){

            vec2 current =ligthPos[e];
            accum += halo(current);

        }

        fragColor = (accum) * exposure;

}

```

this is what happens:

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/aef7c4c83ce9cb3ad5fc9147bffd675667ab94de.jpeg)

and this is what i want:

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3af05ccf98ffa8615a21668f939ed314eccf0fd0.png)

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [February 7, 2019, 6:19am UTC](https://discourse.processing.org/t/loops-in-shader/8176/2 "2019-02-07T06:19:11Z")

</div>

Hi! Could you share the Processing code?

---

<div class="post-metadata">

**Author:** ![vjjv](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@vjjv](https://discourse.processing.org/u/vjjv)\
**Post date:** [February 7, 2019, 7:03am UTC](https://discourse.processing.org/t/loops-in-shader/8176/3 "2019-02-07T07:03:11Z")

</div>

im not actually doing this in processing, because i need to send the positions in screen space and i found a function in openframeworks that do that work.  
but its super simple, something like that:

```auto
bufferA.begin();
drawGeometry();
bufferA.end();

bufferB.begin();
shader();
bufferA.draw();
bufferB.end();

```

---

<div class="post-metadata">

**Author:** ![vjjv](https://avatars.discourse-cdn.com/v4/letter/v/df705f/32.png) [@vjjv](https://discourse.processing.org/u/vjjv)\
**Post date:** [February 7, 2019, 7:05am UTC](https://discourse.processing.org/t/loops-in-shader/8176/4 "2019-02-07T07:05:00Z")

</div>

i can see what is the problem of the shader, but i dont know how to fix it. For each loop its calculate the positions of the points, plus the others positions, since the fragment affect the all screen. i need some way to avoid that,and only calculate the necesarry points, and discard the others.
