Finding the color of a pixel at a specific coordinate from sampler2D using GLSL

I have a 3D object in my scene, and the fragment shader for this object gets a texture with the same screen size. I want to get the coordinates from the current fragment and find the color information in the image in the same position. Is it possible? Can someone point me in the right direction?

+7
source share
1 answer

The fragment shader has a built-in value called gl_FragCoord, which provides the pixel coordinates of the target fragment. You must divide this by the width and height of the viewport to get the texture coordinates for the search. Here is a quick example:

uniform vec2 resolution; uniform sampler2D backbuffer; void main( void ) { vec2 position = ( gl_FragCoord.xy / resolution.xy ); vec4 color = texture2D(backbuffer, position); // ... do something with it ... } 

For a complete working example, try this in a WebGL-compatible browser:

http://glslsandbox.com/e#375.15

+12
source

All Articles