Authoring Realistic Stars
1 April 2022
Star systems play an important role in the world of Planetguard, just as they do in the real universe. They provide homes for countless planets and are the primary source of life as we know it. In Planetgaurd, star systems play the role of what you could call "Worlds" (like the worlds in the Super Mario games), containing all of the playable levels in the game.
Given their significance both in the game and real life, I wanted to provide a depiction of stars that was based in real science. Sure, I could have taken the simple route and arbitrarily decided the stars' colors, sizes, and brightnesses, but I wanted to respect stellar astronomy to the best of my ability.
So with that goal in mind, how do I go about determining a star's color, size, and brightness in a way that matches reality? The first step is to learn about Stellar Classification and the Hertzsprung–Russell Diagram.
Stellar classification provides a lot of information about the stars in our universe and their properties, particularly Temperature and Luminosity. The Hertzsprung–Russell diagram (or HR diagram for short) further provides a neat visualization of how stars tend to follow specific patterns in the relationship between their temperature and luminosity, and is the main way we will be making sure our stars are accurate.
Luckily, a star's temperature is directly related to it's color. The coolest stars are a deep red, and as temperature increases, the color becomes orange, yellow, white, and finally blue. While there are formulas and such for these colors ( black-body radiation ), for the sake of some artistic liberties I simply made my own color ramp, which I will use later.
On the other hand, luminosity is not as straight-forward of a trait to deal with. I would really like to be able to calculate the radius of a star, so that in-game I can show the star at an accurate scale. Fortunately, we have the Stefan–Boltzmann Law. More specifically, we have formulas that we can use to interchange between a star's radius, temperature and luminosity (assuming we know the other two values).
So now we should have all of the tools we need to author our stars. We have a relationship between a star's temperature and it's color, and we have relationships between temperature, radius, and luminosity. With a bunch of Unity Editor scripting and conversion of the above formulas into code, we can make a custom editor for authoring stars.
Several properties are exposed, providing a lot of ways to author the star. The HR diagram display ensures that the chosen values are within the realm of possibility. The HR diagram can also be clicked on directly, and all of the values will change accordingly.
Now all that is needed is a shader that can take our values and display them appropriately. This is where the color ramp created earlier is used.
There is a lot of extra stuff going on to make the stars look cool, but the important properties, just like before, are the Temperature and Luminosity. A separate script is used to actually scale the star game object appropriately.
As a disclaimer, this shader is based on false-color pictures of stars that are often used to visualize them. It does not provide a necessarily accurate representation of stars as our eyes would actually see them. (Reminder to never look directly at the sun).
Just for fun, we can plug in values from real stars and see what kind of results we get, which should hopefully be fairly accurate (but note that scales are heavily adjusted).
And now we have a scientifically accruate way of authoring all of the stars in Planetguard. Use the GitHub link below if you want to play with this stuff yourself! The package contains the HR Diagram editor utility, Star Utilities for calculating the various properties of a star, and a custom shader graph for the star object (URP only).