varying float depth;
varying vec3 center;
varying vec2 coord;
varying vec3 offset;

varying float radius;

uniform mat4 pm;
uniform mat4 mvm;
             
varying vec4 position;
varying vec4 eyePosition;

uniform float offsetFade;
varying float doOffsetFade;

void main(void)
{
	// distance from sphere center to current fragment position
	float distanceSquared = dot(offset.xy, offset.xy);
	if (distanceSquared > 1.0) {
		 discard;
	 }
	
	// compute the correct depth	
	float z = sqrt(1.0 - distanceSquared);
	float depthCorrected =  depth + z * radius * (1.0 - pow(offsetFade * doOffsetFade, 0.1));
	
	vec4 depthVec = pm * vec4(0,0,depthCorrected,1);
	
	// compute depth in normal device coordinates
	float ndcDepth = depthVec.z / depthVec.w;
	
	gl_FragDepth = ((gl_DepthRange.diff * ndcDepth) + gl_DepthRange.near + gl_DepthRange.far) / 2.0;
  //gl_FragDepth = 0.9;
	
	vec3 normal_viewSpace = vec3(offset.xy, z);
	normal_viewSpace = normalize(normal_viewSpace);
	
	gl_FragData[0] = vec4(vec3(0.5 + 0.5 * normal_viewSpace), 1.0);
	gl_FragData[1] = gl_Color;
	gl_FragData[2] = vec4(ndcDepth, ndcDepth,ndcDepth, 1.0);
	gl_FragData[3] = vec4(eyePosition.xyz, gl_Color.a);
  
}