Given the Point A with the coordinates gl_FragCoord.xy, I have to find the distance to the Center of the cube face to which it belongs in screen coordinates.

Specifically what I need is the formula to convert the Center of every face of the cube in screen coordinates, in order to compare it with gl_FragCoord.

This the fragment shader:

precision mediump float;

uniform vec2 u_resolution;

varying vec2 v_uv;

void main() {
  gl_FragColor = vec4(v_uv.x, v_uv.y, 0.0, 1.0);

This the vertex shader:

precision mediump float;

attribute vec3 a_position;
attribute vec2 a_uv;

uniform mat4 u_view;
uniform mat4 u_projection;

varying vec2 v_uv;

void main() {
  v_uv = a_uv;

  gl_Position = u_projection * u_view * vec4(a_position, 1.0);

enter image description here

  • $\begingroup$ You can't calculate the center of the face in the shader, instead you have to supply it to the shader as attribute vec3 a_face_center; The good news is that you can calculate the distance to the center in the vertex shader, which will then be interpolated per-fragment. $\endgroup$
    – zoran404
    Apr 10, 2020 at 17:34


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.