3

I'm trying to make FBO-particle system by calculating positions in separate pass.
Using code from this post now http://barradeau.com/blog/?p=621. I render sphere of particles, without any movement:

The only thing i'm adding so far is a texture in simulation fragment shader:

void main() {
    vec3 pos = texture2D( texture, vUv ).xyz;
    //THIS LINE, pos is approx in -200..200 range
    float map = texture2D(texture1, abs(pos.xy/200.)).r;
    ...
    // save map value in ping-pong texture as alpha
    gl_FragColor = vec4( pos, map );

texture1 is: half black half white.

Then in render vertex shader i read this map parameter:

map = texture2D( positions, position.xy ).a;

and use it in render fragment shader to see the color:

vec3 finalColor = mix(vec3(1.,0.,0.),vec3(0.,1.,0.),map);
gl_FragColor = vec4( finalColor, .2 );

So what i hope to see is: (made by setting same texture in render shaders)

But what i really see is: (by setting texture in simulation shaders)

Colors are mixed up, though mostly you can see more red ones where they should be, but there are a lot of green particles in between.
Also tried to make my own demo with simplified texture and same idea and i got this:

Also mixed up, but you can still guess image. Same error.
I think i am missing something obvious. But i was struggling with this a couple of days now, not able to find a mistake by myself.

Would be very grateful for someone to point me in the right direction. Thank you in advance!

Demo with error: http://cssing.org.ua/examples/fbo-error/
Full code i'm referring: https://github.com/akella/fbo-test

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    Have you tried disabling texture filtering by using `GL_NEAREST`? – keaukraine Jan 10 '19 at 13:51
  • 1
    Well, it worked! i will confirm that later tonight, but so far made a commit to https://github.com/akella/fbo-test with how i understand what you mean, and worked out well! Will have a question to myself why it works by default in render pass, but not like that in simulation. But i officially owe you a beer if i confirm that tonight! – Yurii Artyukh Jan 10 '19 at 16:53
  • Confirmed! That was the problem. Could you add this to your answer so i can mark it as the correct one? Works now! http://cssing.org.ua/examples/fbo-particles/ – Yurii Artyukh Jan 10 '19 at 22:17

2 Answers2

1

You should disable texture filtering by using GL_NEAREST min/mag filters.

keaukraine
  • 5,315
  • 29
  • 54
0

My guess is that THREE.TextureLoader() loads texture with mipmaps and texture2D call in vertex shader uses the lowest-res mipmap. In vertex shaders you should use texture2DLod(texture, texCoord, 0.0) - note the 3rd param, lod, which specifies 0 mipmap level.

keaukraine
  • 5,315
  • 29
  • 54
  • 1
    Thank you for the idea! But i actually load texture in **fragment** shader in simulation. =( I tried `map = texture2DLod(texture1, abs(vec2(pos.x/100.,pos.x/100.)), 0.0);` but it didnt change this blur bug. – Yurii Artyukh Jan 10 '19 at 16:07
  • Ouch, it was cache, texture2DLod does work only in vertex shader as far as i understand, right? But i do lookup in fragment. – Yurii Artyukh Jan 10 '19 at 16:47
  • Yes, `texture2DLod` is available only in vertex shaders. You can refer to this official GLSL cheat sheet from Khronos group - https://www.khronos.org/opengles/sdk/docs/reference_cards/OpenGL-ES-2_0-Reference-card.pdf - it comes really handy. – keaukraine Jan 10 '19 at 19:27