Invisiblity Reveal Shader

It is easy to underestimate! GPUs will spin thousands of threads, even 10 year old gpus will have at least 500 ready to go.

This reduced script is very simple too, no branches (if the ternary is compiled down to a step function) and the hardest function to crunch is up to 64 lengths.

Alpha is still scary so be sure to profile, I’d watch out for the alpha sorting step since this effect is applied to many if not all meshes. If some part of alpha becomes too taxing you may be able to discard instead of assigning ALPHA, this doesn’t help much with GPU performance but gets the material out of the alpha pipeline so it may fix visual issues and again dodge the sorting cost.

void fragment() {
	float alpha = 0.;
	
	for (int idx = 0; idx < sphere_data_length; idx++) {
		vec4 sphere_datum = sphere_data[idx];
		vec4 sphere_attribute_datum = sphere_attributes_data[idx];
		
		float dist = length(sphere_datum.xyz - world_vertex);
		
		alpha += dist <= sphere_datum.w ? 1. : 0.;
	}

	// sampling some noise instead of 0.5 may look better
	if (alpha < 0.5) {
		discard;
	}
	
	ALBEDO = vec3(1., 1., 1.);
}