This is a pixelshader that draws spheres on pointsprites. This allows the spheres to be perfectly round, and to have a lot of them in view in realtime.
They intersect as though they are spheres (and not like the planes which they are drawn on). This is done by rendering the sprites to the depth-buffer as though they were spheres.
Here’s the pixel-shader code :
-first render point-sprites, and let the vertex-shader pass their depth to the pixel-shader.
void PS_ShowDepth(VS_OUTPUT input, out float4 color: COLOR0,out float depth : DEPTH)
float dist = length (input.uv - float2 (0.5f, 0.5f)); //get the distance form the center of the point-sprite
float alpha = saturate(sign (0.5f - dist));
sphereDepth = cos (dist * 3.14159) * sphereThickness * particleSize; //calculate how thick the sphere should be; sphereThickness is a variable.
depth = saturate (sphereDepth + input.color.w); //input.color.w represents the depth value of the pixel on the point-sprite
color = float4 (depth.xxx ,alpha ); //or anything else you might need in future passes