How to draw spheres using Points

Points are much faster than a intanced mesh of spheres.

But how I draw spheres using a custom shader?

My shader don’t need to calculate the shadows, neither be pixel perfect.

I have reached in a shader which draw rounded points, but it doesn’t respect the light neither the zoom:

uniform vec4 color;

void main() {
    if (length(gl_PointCoord - vec2(0.5)) > 0.5) discard;

    gl_FragColor = color;
}

If I’m not mistaken, zoom is passed along with the camera matrix, so just applying the matrices in vertex shader should solve the issue.

As for the lights - you’ll likely need to implement it manually basing on this. Three passes all lights from the scene to the fragment shader, but the calculations are done differently depending on the effect you’d like to achieve.

1 Like

This didn’t worked, zooming the scene kept the size of the spheres.

The following did work more or less:

gl_PointSize = size * length(projectionMatrix * vec4(0.01));

I also tried the following:

gl_PointSize = size * length(projectionMatrix * modelViewMatrix * vec4(0.01));

But both have one problem: when I change the size of the canvas the initial size of the sphere “change” (the size in pixels is the same, but the size compared to other objects change), but the zoom effect is correct.

Since now, thanks!