diff options
| Commit | c2f3a63ed8da8490071972c8fd9d34be35d34b48 (patch) | |
|---|---|---|
| Author | 2023-12-24 21:09:30 +0200 | |
| Committer | 2023-12-24 21:09:30 +0200 | |
| Files | 350b7a38d358dd1163044938eade428fb639e398 /src/blog | |
| syndamia.com-c2f3a63ed8da8490071972c8fd9d34be35d34b48.tar syndamia.com-c2f3a63ed8da8490071972c8fd9d34be35d34b48.tar.gz syndamia.com-c2f3a63ed8da8490071972c8fd9d34be35d34b48.zip | ||
| Parent | 71db9a7a0a240a6d4ba81dd0c4a4941385419339 (diff) | |
(blog/bhs) Added contents, pretty major rewording and restructuring, put LaTeX source in appendix
1 files changed, 143 insertions, 92 deletions
| -rw-r--r-- | src/blog/difficulties-of-making-a-black-hole-shader/index.md | 235 |
diff --git a/src/blog/difficulties-of-making-a-black-hole-shader/index.md b/src/blog/difficulties-of-making-a-black-hole-shader/index.md index b142f99..17304c6 100644 --- a/src/blog/difficulties-of-making-a-black-hole-shader/index.md +++ b/src/blog/difficulties-of-making-a-black-hole-shader/index.md @@ -2,12 +2,26 @@ ## Contents +1. [Preface](#preface) + 1. [Brief introduction to computer graphics](#brief-introduction-to-computer-graphics) + 2. [Brief introduction to WebGL](#brief-introduction-to-webgl) +2. [Theoretical and algorithmic background](#theoretical-and-algorithmic-background) + 1. [What is a black hole](#what-is-a-black-hole) + 2. [Geodesics, gravity and gravitational lensing](#geodesics-gravity-and-gravitational-lensing) + 3. [Finding a formula](#finding-a-formula) + 4. [Applying the formula to "bend" light's path](#applying-the-formula-to-bend-lights-path) + 5. [Properties of a black hole](#properties-of-a-black-hole) + 6. [Putting everything together](#putting-everything-together) +3. [WebGL preparation](#webgl-preparation) +4. [Shader](#shader) +5. [Appendix](#appendix) + Back in August I had to submit a project for an elective WebGL programming course in university. I chose to create an interractive and animated black hole demo. The primary idea was to show how the gravitational pull of black holes "bend" light and how the result looks from different points of view. During development I found a lot of hurdles, primarily with the actual mathematics behind it. -Information online is difficult to parse and understand if you don't have a physics background, so I'll try my best to lay out everything I know. +Information online is difficult to find and understand if you don't have a physics background, so I'll try my best to lay out everything I know. I'll assume you don't know much about physics, but are familiar with mathematics and at the very least vectors. @@ -16,10 +30,10 @@ I'll assume you don't know much about physics, but are familiar with mathematics ### Brief introduction to computer graphics Image and video files store visual information in a "fixed" way: you get a huge array of colors, each connected to a particular pixel on your screen. -The objects in those mediums cannot be modified easily, and additions or subtractions are even harder to do, usually requiring an expert using a professional tools for hours, days, weeks or months on end. +The objects in those mediums cannot be modified easily, and additions or subtractions are even harder to do, usually requiring an expert using professional tools for hours, days or weeks on end. Computer graphics aims to offload a lot of the creator's workload by "generating" the images on the fly using a set of descriptions for our objects. -The idea is to define objects with some simpler geometric figures, then give those to the computer, which will in turn have to calculate what the precise color of every pixel in the resultant image should be. +The idea is to define objects with some the primary properties of simpler geometric figures, like the points on a triangle, then give those to the computer, which will in turn have to calculate what the precise color of every pixel in the resultant image should be. For example, we can represent a tin can (without a label) as a simple cyllinder. Changing it's height or width is trivial: just change the corresponding property you pass to the computer, and it will handle the rest. @@ -32,9 +46,9 @@ Expanding on out tin can example, if you want an image with a tin can up close,  *Image by Sun Ladder on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=5746428)* -Such an image contains way way too much detail the average computer to handle in reasonable time (some small fraction of a second). +Such an image contains way way too much detail for the average computer to handle in reasonable time (some small fraction of a second). However, computer generated graphics are still a very useful tools. -Thats why modern machines come with a processor, handling most tasks like running programs, managing internet traffic, etc. and a graphics card handling *only* graphical tasks. +Thats why modern machines come with a graphics card handling *only* graphical tasks (alongside a processor, handling all other tasks like running programs, managing internet traffic, etc.). A graphics card is a combination of a special processor (GPU), designed to be able to *only* compute stuff related to graphics, with the upside of doing so much much faster than a general purpose processor (CPU) could, and some memory for use exclusively by that GPU. @@ -54,21 +68,34 @@ You would get a DOM object in which the graphics will be rendered, and a "contex ## Theoretical and algorithmic background -Using [Einstein's field equations](TODO), or to simplify our life, [Schwarzschild's metric](TODO), we can precisely calculate the curvatures in space(time) at every point around a massive stationary object, like a non-rotating black hole. -Of course, the computation would still be too laborious, so making an approximation using [celestial mechanics](TODO) and representing light as a particle (corpiscule), we can tame the physics into a simple algorithm. +Using [Einstein's field equations](TODO), or to simplify our life, [Schwarzschild's metric](TODO), we can precisely calculate the curvatures in space(time) at every point around a Schwarzschild black hole. +Unfortunetly, the computation would still be too laborious, so we'll need to make an approximation using [celestial mechanics](TODO). -Of course, there are a lot more details, so let's back up a bit. +That is the overall gist of the underlying physics, but I promised an explanation, so let's back up a bit. ### What is a black hole -A black hole is an object in space, which "collapsed" down to an infinitely small point. -In other words, if you take something with mass (although it's incorrect, you may think of mass as the amount of atoms comprising the object) and compress it into a small enough size, then the forces between the atoms and even subatomic particles will not be strong enough to keep themselves intact and separate. +First, imagine atoms like marbles on a table. + + +*Image from By Okkisafire on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=30338734)* + +Now, take those marbles and start pushing on them from all sides. +In the beginning, when they are all faraways apart, it won't be very hard, since you only have to move them. +After they are touching each other, you'll find that your push doesn't do anything, the glass will keep the marbles together and just "deal" with your force, until a certain threshold. +When reached, the glass will break, turning the marbles into shards. + +A similar occurence can happen with atoms. +Ignoring what happens to the orbiting electrons, by pushing atoms together, you reach a certain point where all of the nuclei are "touching". +Then, the [nuclear force](https://en.wikipedia.org/wiki/Nuclear_force) will try and keep those nuclei together, but if you push hard enough, then you can overcome it, and "shatter" the nucleus into it's subatomic particles. -You can think of atoms like magnetic marbles, all marbles are separated with empty space (due to the magnet force), so if you push on them, you can compress them so they get closer together. -However, you can continue pushing them together, after enough forces, they'll all start deforming into a small really dense block of metal. +But in the same way you can continue compressing the shards until they break and turn into glass granules, you can continue compressing those subatomic particles. +At a certain point, what breaks, is physics. +After that certain threshold all atomic and "subatomic" "forces" will be overcame and nothing will be able to hold matter "together"[^matter-together]. -Outside analogies, keep in mind that this infinitessimal point has the same initial mass as the object which collapsed. -This has great consequences on gravity, but what is gravity anyway? +The result is a "collapse", all of that matter just "deflates" into a single, infinetly small point. +That's called a **black hole** (keep in mind that this infinitessimal point has the same initial mass as the object which collapsed). +It doesn't affect many stuff in the universe, but the effect on gravity is the most special one. ### Geodesics, gravity and gravitational lensing @@ -79,16 +106,16 @@ If you then extend that segment, so it has an infinite length, it becomes a line However, what if our surface isn't flat? Imagine two points on a sphere, what is the shortest distance between them? -You might be inclined to say that's also a "straight" line, but it really isn't: it's an arc of a circle! +You might be inclined to say that's also a "straight" line, but it really isn't: it's an arc of a circle.  -[*Image by CheCheDaWaff on Wikipedia*](https://commons.wikimedia.org/w/index.php?curid=48187293) +*Image by CheCheDaWaff on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=48187293)* This circle is called a [great circle](https://en.wikipedia.org/wiki/Great_circle). The main problem with calling this arc a "spherical line segment" (and the great circle a "spherical line" or something similar) is that both objects have different properties. Looking at all possible great circles, we find that their center (and radius) matches with the center (and radius) of the sphere. -From this we can find the bigest difference: lines can be parallel, but great circles cannot. +From this we can extrapolate that lines can be parallel, but great circles cannot. A line is an extended segment, so if you extend two different arcs (which are the shortest distance between two pairs of points on the sphere) and get the great circles, those will always intersect[^parallels-of-latitude]. Therefore, the general term for the shortest path between two points on a non-flat surface is (kind of) called a geodesic[^geodesics-are-local] (the arc on the sphere is a geodesic). @@ -98,10 +125,13 @@ Going back to physics, what the General theory of relativity tells us is that ob As a result, the geodesics too are curved, so even though you may be always moving forwards at the same speed, to an outside observer it will look like you turn and change speed. The amount by which and the size of the area which is affected depend on that object's mass and size. -Generally, if two objects have the same mass, the bigger one will bend less a larger portion of space, compared to the smaller one, which will bend a lot the smaller portion of space. +Generally, if two objects have the same mass, the bigger one will affect a larger portion of space, but will bend it less, compared to the smaller one, which will affect a smaller portion, which will be bend a lot. -Since a black hole is a single infinetly small point, then all of it's mass is concentrated on that point, making a really really **really** strong bend in a small (compared to a non-blackhole object with the same mass) area. -Strong enough in fact, that if you get too close, you will not be able to overwhelm gravity and "merge" into that infinitessimal point. +Since a black hole is a single infinetly small point, then all of it's mass is concentrated on that point, making it bend a relatively small are of space **a lot**. +Strong enough in fact, that if you get too close, you will not be able to overwhelm gravity and "merge" into that infinitessimal point (the white lines are our "geodesics"): + + +*Image by OpenStax University Physics on [Wikipedia](https://commons.wikimedia.org/w/index.php?curid=64307901)* An interesting effect from gravity is that light (which always travells "forwards", so along a geodesic) also gets "bent", allowing an observer to view otherwise obstructed objects. We call that [graviational lensing](TODO). @@ -110,17 +140,22 @@ Though not unique to black holes, thats when the effect is strongest:  *(notice how the gray arrows show the path light travels from a galaxy that is behind (obstructed by) the black hole)* -The distinction that gravity doesn't change the direction or speed of an object "directly" (preserving the speed for light rays) is at upmost importance for physics. +I'll abstrain from explaining light, but you may think of it as a small particle, "similar" to an electron, travelling through space. +In fact, Newton understood light to be just that, and called the underlying particle a "corpiscule". +For our purpopses, this approximation will serve us well. + +The distinction that gravity doesn't change the direction or speed of an object "directly" is at upmost importance for physics, since this way we preserve two very important properties of light: it's constant speed and straight travelling path. However, in our simulation, it would be significantly simpler to assume the opposite, since to an outside observer (like the "player") the difference is indistinguishable. -Therefore, we would need to make some modifications to the proper physics equations. + +Therefore, in our simulation, we'll have light rays, but when they approach a black hole, their speed and direction would change. +This will be determined by some physics equations. ### Finding a formula Now, how do we calculate the exact curvature a black hole produces? General theory of relativity provides us with the (really complicated) [Einstein field equations](https://en.wikipedia.org/wiki/Einstein_field_equations) for this exact problem. [Schwartzschild](TODO) solved these equations, assuming that the body with mass doesn't spin[^schwm-assumptions]. -This solution is called the [Schwarzschild metric](https://en.wikipedia.org/wiki/Schwarzschild_metric). -A black hole for which this (these) assumption is true is called a "Shwartzschild black hole". +This solution is called the [Schwarzschild metric](https://en.wikipedia.org/wiki/Schwarzschild_metric), and a black hole for which this assumption is true is called a "Shwartzschild black hole". The Shwarzschild black hole is the symplest of it's type, so using the Schwarzschild metric should give us the least trouble. Now, here is the metric in Schwartzschild coordinates: @@ -138,15 +173,15 @@ Thankfully [Riccardo Antonelli](http://rantonels.github.io/starless/) was able t  -Question now is: how do we use it? +I will not be trying to explain why or how the Schwarzschild metric and this formula work, that's way way beyond my elementary understanding and the scope of this post. + +Question now is: what do all of those letters represent? This turned out to be a much harder problem than it should have, I actually had to refer [Ricardo Antonelli's](https://github.com/rantonels/starless/blob/7a0c3c4efdc0666f53dcfa48d310fb2164be2d1a/tracer.py) and [Dmitry Brant's](https://dmitrybrant.com/2018/12/11/ray-tracing-black-holes) implementation in their projects, alongside copus articles on wikipedia to fully grasp how to put it to work. ### Applying the formula to "bend" light's path -First, how are we going to represent light? -The simplest way is to think of light as a small particle (which Newton called "corpiscule") travelling through space. -We'll move our particle by a tiny amount in a certain direction, then update the direction, accounting for gravity, and then repeat. -The direction, alongside the amount by which the particle moves can be represented with a single vector, which we'll call light's "velocity". +Our plan for simulating gravitational lensing is to move the corpiscule by a tiny amount in a certain direction by a certain amount, update the direction and amount according to gravity, rince and repeat. +The direction, alongside the amount by which the particle moves, can be represented with a single vector, which we'll call light's "velocity". Referring to the formula again: @@ -156,9 +191,9 @@ The <code>F<sup>→</sup></code> returns a force vector (`F` for "force" and Imagine you are pushing a box: you are applying some force to it in a "forward" direction. If you pull the box, you'll apply some force in a "backward" direction. -This force vector specifies how much and in what direction exactly the black hole is pulling on the corpiscule. +This force vector conveys how much and in what direction exactly the black hole is "pulling" on the corpiscule. -Although the function takes in only one argument, `r`, that doesn't mean that `h` and <code>r<sup>\^</sup></code> aren't also arguments: +Although the function takes in only one argument, `r`, that doesn't mean that `h` or <code>r<sup>\^</sup></code> aren't also arguments: - `r` is the distance from the center of the black hole (the singularity) to the **current** position of the corpiscule. @@ -172,7 +207,7 @@ Although the function takes in only one argument, `r`, that doesn't mean that `h Since the train is moving at a really great speed, this barrier will have to be much much heavier (with much more mass) than the train itself. Alternatively, you can "launch" something against the train, meaning you can put a second train, moving in the opposite direction of the first train, so they hit head-on. - Since the second train would also be moving, if it's travelling at the same speed and has the same mass as the first train, when they hit they would both stop. + Since the second train would also be moving, if it's travelling at the same speed and has the same mass as the first train, upon collision they would both stop[^hitting-trains]. If the second travells at twice the speed, then it can have half of the mass of the first to produce the same effect. The property with which we can "prove" why both a larger mass at no speed, and a smaller mass at a faster speed, can stop the train, is the momentum. @@ -200,62 +235,41 @@ Although the function takes in only one argument, `r`, that doesn't mean that `h Finally, putting all needed variables together in one proper function, we get: -<!-- -\begin{align*} -& \overrightarrow{F}(r_l,h) = -\frac{3}{2} \; h^2 \; \frac{\hat{r_l}}{\|r_l\|^5} \quad \text{, where}\\ -& h = \|r_l \cross v_l\|\\ -& \hat{r_l} = \frac{r_l}{\|r_l\|}\\ -& \|x\| = \text{magnitude of }x\\ -& r_l = \text{vector from black hole center to light "particle"}\\ -& v_l = \text{velocity of light "particle"} -\end{align*} ---> - + As you may notice, this can be simplified, if we substitude <code>r<sup>^</sup></code>: -<!-- -\begin{align*} -& \overrightarrow{F}(r_l,h) = -\frac{3}{2} \; h^2 \; \frac{r_l}{\|r_l\|^6} \quad \text{, where}\\ -& h = \|r_l \cross v_l\| \text{ (calculated right before being affected by black hole)}\\ -& \|x\| = \text{magnitude of }x\\ -& r_l = \text{vector from black hole center to light "particle"}\\ -& v_l = \text{velocity of light "particle"} -\end{align*} ---> - + In effect, we pass the distance from the black hole to the corpiscule and the function returns how light's velocity changes. Finally, we know what to give the formula, and what it will return. The task at hand is making an algorithm which utilises it. -For that we'll need to get familiar with black holes. +For that we'll need to get even more familiar with black holes. ### Properties of a black hole -We'll be working with non-rotating stationary black holes, since those are the simplest and provide perfectly useable results. - -From the [no-hair conjecture](https://en.wikipedia.org/wiki/No-hair_theorem) we know that a stationary black hole +As stated, we'll be working with non-rotating stationary black holes, Schwarzschild black holes. +From the [no-hair conjecture](https://en.wikipedia.org/wiki/No-hair_theorem) we know that such a black hole has only three distinct properties: mass, electric charge and angular momentum. +The last one doesn't apply for our case, and the second is irrelevant. -Physics regards black holes as an infinitessimal (infinitely small) point with some huge amount of mass. -From the mass we can derive three radii, which will be the backbone of our black hole: the event horizon, the photon sphere and [ISCO](https://en.wikipedia.org/wiki/Innermost_stable_circular_orbit). +Meaning, from the mass alone, we can derive the gravitational "forces", alongside three radii, which will be the backbone of our black hole: the event horizon, the photon sphere and [ISCO](https://en.wikipedia.org/wiki/Innermost_stable_circular_orbit). -The event horizon, also called the Schwarzschild radius, is the distance from the center of the black hole (singularity), which once passed, cannot be escaped. -In other words, once something gets too close, and passes this distance, then it cannot ever escape, even if it's light. +The event horizon, also called the Schwarzschild radius, is the distance from the center of the black hole (singularity), which once passed, cannot be passed again (you cannot escape the black hole). The formula for it is: - + where `G` is the [gravitational constant](https://en.wikipedia.org/wiki/Gravitational_constant) and `M` is the mass of the black hole (in kg). The photon sphere is the distance from the singularity, in which photons start orbiting the black hole (and can either escape or pass the event horizon). Formula: - + where `G` and `M` are the same as before, `c` is the speed of light in vacuum and <code>r<sub>s</sub></code> is the afroementioned Schwarzschild radius. -Finally, the innermost stable circular orbit (ISCO), officially it's the distance, before which you can orbit the black hole, but once passed, you'll get sucked in and either get shot out or forever lost past the event horizon. +Finally, the innermost stable circular orbit (ISCO), formally it's the distance, before which you can orbit the black hole, but once passed, you'll get sucked in and either get shot out or be forever lost past the event horizon. For our purposes, it's fine to approximate it as the maximum radius after which spacetime isn't bent[^isco] and objects aren't affected. Formula: @@ -284,39 +298,76 @@ The simplest way to "track" this is just to limit how many times we can move the An algorithm doing this would look a bit like this (in pseudocode, inspired by Python): -<!-- + + +as a reminder, here is the definiton of <code>F<sup>→</sup></code>: + + + +## WebGL preparation + +## Shader + +## Appendix + +##### LaTeX source for equations + +The images for my custom LaTeX equations were created on [latexeditor.lagrida.com](https://latexeditor.lagrida.com/), by using Firefox's built-in ability to screenshot HTML nodes, in particular the [MathJax](https://www.mathjax.org/) node with the equation. + +###### Antonelli's formula - explained + +```latex \begin{align*} -& p_{cbh} = \text{position of center of black hole}\\ -& r_s = \text{Schwarzchild distance from } p_{cbn} \\ -& r_{ps} = \frac{2}{3}r_s \quad \text{(photon sphere)}\\ -& r_{ISCO} = 3 r_s \\ -\\ -& p_l = \text{position of light "particle"}\\ -& v_l = \text{velocity of light "particle"}\\ -\\ -& r_l = \| p_l-p_{cbh} \| \quad \text{(distance between black hole and light)}\\ -& \text{If } r_l \leq r_{ISCO} : \\ -& \quad h = \| r_l \cross v_l \|\\ -& \quad \text{LightMovesLimit} = \infty \\ -& \quad \text{Repeat maximum LightMovesLimit times}:\\ -& \qquad \text{If } r_l \leq r_s : \\ -& \qquad \quad \text{return "Light never escapes"} \\ -& \qquad \text{If } r_l \leq r_{ps} : \\ -& \qquad \quad \text{LightMovesLimit} = 100 \\ -& \qquad \text{If } r_l > r_{ISCO} : \\ -& \qquad \quad \text{return "Light escaped black hole"} \\ -& \qquad p_l = p_l + v_l \quad \text{(move light)}\\ -& \qquad r_l = \| p_l-p_{cbh} \| \quad \text{(update distance to $p_{cbh}$)}\\ -& \qquad v_l = v_l + \overrightarrow{F}(r_l,h) \quad \text{(update light velocity)} \\ -& \quad \text{return "Light stuck in photon sphere, never escapes"} + & \overrightarrow{F}(r_l,h) = -\frac{3}{2} \; h^2 \; \frac{\hat{r_l}}{\|r_l\|^5} \quad \text{, where}\\ + & h = \|r_l \cross v_l\|\\ + & \hat{r_l} = \frac{r_l}{\|r_l\|}\\ + & \|x\| = \text{magnitude of }x\\ + & r_l = \text{vector from black hole center to light "particle"}\\ + & v_l = \text{velocity of light "particle"} \end{align*} ---> +``` - +###### Antonelli's formula - simplified -as a reminder, here is the definiton of <code>F<sup>→</sup></code>: +```latex +\begin{align*} + & \overrightarrow{F}(r_l,h) = -\frac{3}{2} \; h^2 \; \frac{r_l}{\|r_l\|^6} \quad \text{, where}\\ + & h = \|r_l \cross v_l\| \text{ (calculated right before being affected by black hole)}\\ + & \|x\| = \text{magnitude of }x\\ + & r_l = \text{vector from black hole center to light "particle"}\\ + & v_l = \text{velocity of light "particle"} +\end{align*} +``` - +###### Algorithm + +```latex +\begin{align*} + & p_{cbh} = \text{position of center of black hole}\\ + & r_s = \text{Schwarzchild distance from } p_{cbn} \\ + & r_{ps} = \frac{2}{3}r_s \quad \text{(photon sphere)}\\ + & r_{ISCO} = 3 r_s \\ + \\ + & p_l = \text{position of light "particle"}\\ + & v_l = \text{velocity of light "particle"}\\ + \\ + & r_l = \| p_l-p_{cbh} \| \quad \text{(distance between black hole and light)}\\ + & \text{If } r_l \leq r_{ISCO} : \\ + & \quad h = \| r_l \cross v_l \|\\ + & \quad \text{LightMovesLimit} = \infty \\ + & \quad \text{Repeat maximum LightMovesLimit times}:\\ + & \qquad \text{If } r_l \leq r_s : \\ + & \qquad \quad \text{return "Light never escapes"} \\ + & \qquad \text{If } r_l \leq r_{ps} : \\ + & \qquad \quad \text{LightMovesLimit} = 100 \\ + & \qquad \text{If } r_l > r_{ISCO} : \\ + & \qquad \quad \text{return "Light escaped black hole"} \\ + & \qquad p_l = p_l + v_l \quad \text{(move light)}\\ + & \qquad r_l = \| p_l-p_{cbh} \| \quad \text{(update distance to $p_{cbh}$)}\\ + & \qquad v_l = v_l + \overrightarrow{F}(r_l,h) \quad \text{(update light velocity)} \\ + & \quad \text{return "Light stuck in photon sphere, never escapes"} +\end{align*} +``` [^parallels-of-latitude]: You might think about the latituginal lines on a globe, they are indeed parallel (equidistant), but if you take any two points on the same such line, the distance between them won't be the smallest. [^geodesics-are-local]: This actually isn't completely true, a geodesic is the "locally shortest distance", I've skipped the details since they aren't really that important for our purpopses. More info can be found on [wikipedia](https://en.wikipedia.org/wiki/Geodesic#Introduction). |