Effect: Black Hole Background

Published July 12, 2018 by Villem Otte, posted by Vilem Otte
Do you see issues with this article? Let us know.
Advertisement

For one of the upcoming projects (which will follow in some of the following posts), and as it had to fit the lore of the game, a black hole was necessary. Before going forward - let me add an image of what result I want to achieve:

original.jpg.2a289921b41b9786fbc3ba36eef0f0e4.jpg

Artist conception of black hole - NASA/JPL-Caltech

While the image is similar to the final effect I wanted to achieve, I did some changes on the effect to be more colorful and bright - but the original idea was from this image.

The effect is actually separated in 3 major parts - Disk, Streaks and Post processing.Of course there is also the core of the black hole (which is just a small sphere, with black color).

Disk

The disk around black hole is actually just matter that rotates around the core. Depending on the distance from the core the average density will increase near the event horizon and decrease further from it. Near the core the density can be so high that it may eventually have temperature close to star - therefore there might be high emissive energy - and therefore light.

Also, due to the time dilation (and therefore light having hard time escaping near the event horizon), the emissivity is getting lower very close near the event horizon. Anything beyond event horizon is invisible from the outside, because gravity there is so strong, not even photons can escape.

At least that is what I understand from the topic from physics point of view.

This actually explained what can be seen on the image and what is going on graphically, that is:

  1. The disk rotates around the core
  2. The density of disk decreases further from the core
  3. The emissive light decreases further from the core, and therefore some (outer) parts of the disk will be lit by inner part ... although inner part around the core has to be somehow darker

Which can be solved with simple texturing and some basic lighting of the result. Using whirl-like texture as a basis proved to be a good start for me. I started off by creating a whirl-like texture that would define density in various parts of the disk, which resulted in this:

accertion_base.png.521ffed0062547b64d04cbaf3791e8b7.png

Generating a normal map for lighting from this is actually quite straight forward (and easy in Substance Designer F.e.) - and after short time, I also had a normal map:

acceration_disk_normal.png.7ee61162a0662e8da855d2f574dbb0aa.png

Putting just these together with basic diffuse lighting (standard N.L) from the center (slightly above the plane) gives us some basic results:

disk_1.jpg.d40bf082089e478392a29b74148f57db.jpg

Next thing is defining emissivity. This is done simply by using 1D gradient texture for which the coordinate will be distance from the center. The gradient I came up with is:

acceration_disk_basecolor.thumb.png.9c316310aad81e4bfb25b9125c426261.png

Notice the left part - which is near the event horizon.will give us similar feeling to the image as we're not jumping straight to bright value. Applying emissive value (as both - multiplier for the color, and as emission) gives us this look:

disk_2.jpg.d2b0a1887f3ce304be9ebabeece14e0b.jpg

Which looks good enough already - I personally played a bit with values (mainly playing with contrast and other multiplication factors - F.e. for alpha channel/transparency), and ended up with this result:

disk_3.jpg.8ad7f678d57204b9c7d149c0f9a07393.jpg

Resulting pixel shader is as simple as:


fixed4 frag (v2f i) : SV_Target
{
	// Calculate texture coordinate for gradient
	float2 centric = i.uv * 2.0f - 1.0f;
	float dist = min(sqrt(centric.x * centric.x + centric.y * centric.y), 1.0f);

	// Lookup gradient
	float3 gradient = tex2D(_GradientTex, float2(dist, 0.0f)).xyz;

	// Light direction (hack - simulates light approx. in the middle, slightly pushed up)
	float3 lightDir = normalize(float3(centric.x, -centric.y, -0.5f));

	// Use normals from normal map
	float3 normals = normalize(tex2D(_NormalsTex, i.uv).xyz * 2.0f - 1.0f);

	// Simple N.L is enough for lighting
	float bump = max(dot(-lightDir, normals), 0.0f);

	// Alpha texture
	float alpha = tex2D(_AlphaTex, i.uv).x;
	
	// Mix colors (note. contrast increase required for both - lighting and alpha)
	return fixed4((gradient * bump * bump * bump + gradient) * 0.75f, min(alpha * alpha * 6.0f, 1.0f));
}

Streaks

There are 2 streaks, directing upwards and downwards from the core. My intention was to make them bright compared to the core and blue-ish - to keep the background more colorful in the end.

Each streak is composed from 2 objects, a very bright white sphere (which will take advantage of used post processing effects to feel bright), and a geometry for the streaks (instead of using particles). The geometry is quite simple - looks a bit like rotated and cut hyperbole, notice the UV map on the left (it is important for understanding the next part):

geometry.thumb.jpg.508c618d2d287505304a0edaf5e6ae67.jpg

This geometry is there 4 times for each direction of the streak, rotated around the origin by 90, 180 and 270 degrees.

The actual idea for streaks was simple - have a simple geometry of cut surface, and roll a texture over it. Multiplying with correct color and distance from the beginning of the streak adds color effect that nicely fades into the background. To create a particles-like texture that varies in intensity I used Substance Designer again and come up with:

particles.thumb.jpg.c11beaf8a9d2169cdf276c6913367e06.jpg

By simply applying this texture as alpha, and moving the X-texture coordinate the streak is animated, like:

particles_1.jpg.c964a123d87725408cc479f08c8d8d93.jpg

Multiplying by wanted color gives us:

particles_2.jpg.de06f30622aaa3ea2290e410bd0a08b8.jpg

And multiplying by factor given by distance from the origin of the streak results in:

particles_3.jpg.080f74fc0fbd7aec4508430038e09743.jpg

Which is actually quite acceptable for me.

For the sake of completeness, here is the full pixel shader:


fixed4 frag (v2f i) : SV_Target
{
	// Texture coordinates, offset based on external value (animates streaks)
	float2 uv = i.uv.xy + float2(_Offset, 0.0f);

	// Alpha texture for streaks
	fixed alpha = tex2D(_AlphaTex, uv);

	// Distance from origin factor (calculated from texture coordinates of streaks)
	float factor = pow(1.0f - i.uv.x, 4.0f);

	// Multiplication factor (to 'overbright' the effect - so that it 'blooms properly' when applying post-process)
	float exposure = 6.0f;

	// Apply resulting color
	return fixed4(exposure * 51.0 / 255.0, exposure * 110.0 / 255.0, exposure * 150.0 / 255.0, alpha * factor);
}

Putting the effects together ends up in:

effect.jpg.9238c0165b750c44336228baf1ff00dc.jpg

Post Processing

By using simple bloom effect, we can achieve the resulting final effect as shown in video, which improves this kind of effect a lot. I've added lens dirt texture to bloom. We need to be careful with the actual core - as that needs to stay black (I intentionally let it stay black even through the bloom). You can do this either by using floating-point render target before the bloom and write some low value instead of black (careful with tone mapping though - yet you might want to go even for negative numbers), or just render the core after the bloom effect.

The resulting effect looks like:

result.jpg.b123d0e8af256f01efe310f42dfd50fb.jpg

And as promised - a video showing the effect:

Cancel Save
13 Likes 4 Comments

Comments

nihiven

Nice work. Looks great!

August 03, 2018 10:21 PM
DerekB

Really nice work. Now I just want you to put the black sphere in (or make it bigger if that's already there?), the little black dot in the middle is heavily aliased and letting your effect down imo.

August 09, 2018 01:41 PM
yueyang liu

Anyone who can create a three js demo for this ? 

August 09, 2018 01:55 PM
Vilem Otte

@DerekB It is up technically up to think off something with 'event horizon', Physically taken, with the amount of debris you wouldn't most likely see anything (it would just be too small). Using perfect sphere with ray tracer could be a way around (as it's quite easy to do nice antialiasing on it).

@yueyang liu I could build you one with Unity almost instantly. If you really wish for ThreeJS one, if I'm able to find some spare time it may be possible

August 10, 2018 12:51 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement