Shaders getting a pixels location in a shader

G

Gun_Goose

Guest
Hi, I am trying to write a shader in which I pass it a point and a radius, and the shader fades objects to black the farther you get from the point. I am having a little trouble understanding how to compare this location to every pixel on the screen. (i am working in 3d, so I would like to try to grab the z value of each pixel as well.) My code looks something like this: (pos is the position I am passing in and 300 is just a radius value I am testing with.)
//vertex
vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0); v_vWorld = (gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos).xyz;

// fragment
vec4 base_col = v_vColour * texture2D(gm_BaseTexture, v_vTexcoord); gl_FragColor = vec4(base_col.rgb, base_col.a*(distance(pos,v_vWorld)/300.0));

Does anyone know how I could make my distance comparison to every pixel on the screen?
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
You want to add a "varying" that's shared between vertex/fragment, like
Code:
attribute vec3 in_Position;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec3 v_vPosition; // new!
//
void main() {
   v_vPosition = in_Position;
   gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION]
       * vec4(in_Position.x, in_Position.y, in_Position.z, 1.0);
   v_vColour = in_Colour;
   v_vTexcoord = in_TextureCoord;
}
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
varying vec3 v_vPosition; // new!
// .. uniforms
void main() {
   gl_FragColor = ...;
}
Larger post on clipping: https://yal.cc/gamemaker-draw-clip/
 
You are doing 3d, so it is possible you may be dealing with points that are transformed with the world matrix. In which case, you'll want to multiply vec4(in_Position,1.0) (on the right side) with the world matrix (on left).
 
Last edited:
G

Gun_Goose

Guest
Thanks flyingsaucerinvation! That seemed to fix my problem. My passed point is offset from where I want it, but I believe that should not be a hard fix. Thanks!
 
Top