Procedural night sky


[EDIT:
web demo http://hexara.com/Starfield
runnable jar with source code http://hexara.com/Starfield/StarfieldTest.jar
posted 8/10/12
Original post is below]


A while back dario3004 included a beautiful photo of a starry sky in a post. I recall asking him at the time if it was a graphic or procedural, and he said it was a NASA photo. But that got me itching to see if I could do something similar procedurally. The inspiring post is below:

So, I recently (finally!) started to figure out how to use Simplex Noise, and went back to the idea of making a procedural starry sky. Here is a screen shot:

http://hexara.com/Images/StarrySky.JPG

Was kind of proud of it and wanted to show it off, even if the use of the Simplex noise was really basic (just used the main formula and only two “octaves”). Just the little splash seems to add a lot in terms of making this look like a plausible NASA photograph.

I’m happy to make the java code available. Maybe someone wants to have some night sky? But I wanted to find out if there was any interest first, before putting a lot of time into it. Same thing with adding to the api. Right now it is really simple:

Constructor: give it the rectangular bounds, the number of stars and a seed for the random function.
There are also two methods:

void update()
void draw(Graphics2D g2)

These can go in a game loop to add animation: the stars twinkle, and occasionally there is a shooting star.

Please let me know if there is any interest.
Suggestions for gnarly nebulae creation gladly accepted!

Well done!

I have a interest in procedurally generated games / artwork… not that I have any time to do games any more :frowning:

I think my interest is because I have no artistic talent what so ever :stuck_out_tongue:

You have any plans to use it in a game?

[quote]You have any plans to use it in a game?
[/quote]
I have a puzzle game I’m working on that uses a night sky as a background element. I just replaced the old starry sky with the new version, and nothing crashed. ;D

Maybe will get a demo up and have a link in a few.

I hear you about the “artistic talent” issue. I also like to avoid copyright issues and downloading resources issues. But that doesn’t justify this case. There are lots of nice free star jpegs, and the code is about 10KB (not counting the simplex routine).

More likely I am just a dithering control freak. Nice to be able to make changes via code rather than by PhotoShopping.

Sometimes I miss the usual java2D procedural things, it’s really fun to play. Higher API like libgdx is no :frowning:

That’s really cool! I’m heavy into procedural game development and am working on making an RPG myself, which will include day and night cycles. More than wanting to copy and use your code (which nonetheless would probably be nice and easy), I’m actually interested more in how you went about it! I knew I wanted to use somewhat of a procedural system for the stars at night, but you have a few other cool things going on here, and I’m not terribly familiar with simplex noise, so more insight would be awesome! :smiley:

I’d love to try it, please? :slight_smile:

@coltonoscopy There is so much to explain! Below are links to tutorials I found most helpful. If you have more questions, I’m happy to give them my best shot.

I only used the Simplex Noise with the Galactic Cloud. The stars (made using BufferedImage) and their placement just uses the good old Java Random function (with an exponential function thrown in to inversely bias the relationship of the size of the star to the quantity generated).

This was a helpful tutorial on using Simplex Noise, and it has an important link to a paper and free Java implementation (that I use) by Stefan Gustavson. The tutorial was written by JGO member ShannonSmith.
http://www.angryoctopus.co.nz/?p=11

I’m still grappling with how best to use Simplex Noise. In this StarField, I call it to fill a BufferedImage, but the result then is of course rectangular. I’ve made a tool for tapering to alpha = 0 so as to be able to blend it into a background. Am still trying to figure out how best handle other transforms, am building a couple tools to help with that.

This next tutorial uses Simplex Noise to create a Java Paint object. (He calls it “Perlin Noise” but he uses the Java reference implementation Perlin wrote for his Simplex algorithm.) If you use that approach, you can use it as fill for shapes or letters. But I suspect it is not the best way to go if you are continually repainting a screen. Wouldn’t that require regenerating the noise with each render?

Dzzd has posted his “FastNoise” on JGO. You might want to check it out.
http://www.java-gaming.org/topics/fastest-perlin-noise-gradiant-amp-value-noise/24197/view.html
http://www.java-gaming.org/topics/fastest-perlinnoise-improved-version-bicubic-amp-bilinear-grad-amp-value-noise/23771/view.html

[quote]I’d love to try it, please? Smiley
[/quote]
Hi _Al3x - I was wondering if you were still interested in this!

I made a jar but there is a bug I want to fix, and hope to have it handled before the end of the day. In the api, one specifies the bounds of the area that the StarField is to display within. But the GalacticCloud I made is not staying within the bounds. I’m working on some code that will allow me to rotate and clip the cloud image to fit.

philfrei,

Thanks, I’ll be sure to take a look! It sounds like you’re doing a decent thing with painting to a BufferedImage, and with the applications I could use something like that for, I would most likely do the same. For the repainting issue, it sounds like that would have to unfortunately be the case. If I were going to use it in a game engine, I would probably incorporate objects into the sky (in this case, the galactic cloud) that are dynamically updated and can have certain effects applied to them (like shimmer or whatever for aurora borealis-type effects) while retaining the static starry background and night colors. Thus, they would sort of be in-game entities or at least animations that are stored or manipulated, and I’m sure they could still be generated using simplex algorithms either way. All hypothetical until I actually test it, however. :] Thank you!

Best regards,
Colton

Well, hi :slight_smile: !

I am still interested! I can´t use it right now, but I´d love to see it working and keep a sample for later deep study 8)

Keep it up, wish you the best :slight_smile:

Looks good, I could use something like this for a space strategy thing I’m working on. I imagine it would be nice to generate randomized background nebulas before starting a new game, to make each galaxy feel unique. Thanks for the links!

Alternatively I’ll just use edited NASA images, which also looks cool.

As a completely gratuitous side comment: I can’t think of any reason to use simplex noise (vs improved gradient noise) if you’re sampling function is less that 3D.

Hi Roquen -

I kind of looked at this and that and couldn’t really tell the difference, so I rolled the dice and chose Simplex rather than trying to stress it out.

My decision was mostly due to Stefan Gustavson’s paper and implementation. I was very impressed with his paper. He claims Simplex has “fewer defects” in addition to being faster at 3D & up. He also claims his updated implementation (2012!) runs a bit quicker than Perlin’s reference implementation. And, since his program emphasizes clarity, there seemed like more upside for possible optimizing if one wants to go down that rabbit hole for whatever reason.

I also just like the math behind Simplex. It seems very elegant, from my surface view.

Have you found the ImprovedNoise implementation to be superior in any regard? I have no experience really with either and am not locked in. I am all ears!

The only issue here is that if you put equal working into an improved gradient vs. simplex (for 1D & 2D) then the gradient noise version should be faster and equal in quality…that’s it. With a working simplex noise there’s zero reason to revisit improved gradient…even if performance issues pop up because you’d have to redesign all your stuff (different frequency contents between the two). So my comment was really gratuitous (or informational depending on your perspective).

That looks great. I have a few requests.

  1. Can you post more images of different skies and in a lossless image format?
  2. Source code soon please? :slight_smile:
  3. If not can you post something showing the animation?

Quickly scanned this article. It would have been easier (and cheaper) for creating the planet image to calculate the 3D coordinate of the surface and use a 3D noise function instead of 2D.

If anyone plays with any 3D simplex noise, I’m curious how my implementation compares: http://www.java-gaming.org/topics/simplex-noise-3d/23962/view.html. Also a post further down has a link to an alternate GPU version.

@Roquen - Cool! I didn’t realize you had written a version also. I might have passed over it when I saw 3D in the title, since I am still poking around with 2D, and then forgotten about it since I didn’t add it to my “favorites” list. I’m truly impressed that you are able to tackle and write such code! I’m just at the level of using the function call of an existing reference copy.

Maybe we should have a page (is there already one?) dedicated to “Perlin” based references.

@BestUsernameEver - I’ve been busy writing and testing utilities for rotating and clipping the “GalaxyCloud” image. (This was my first time writing a basic 2D Transform program.) Since I’m just beginning with generating textures, I only know how to stretch vertically and horizontally and very much wanted the cloud to be at an angle in the sky. Also, I wanted the GalaxyCloud to display correctly (not spill over the edges) if the StarField display area is set to be smaller than the GalaxyCloud image. I couldn’t figure out a way to both rotate and clip the image using Java’s AffineTransforms–so resorted to writing versions that work on images rather than on the graphical space.

So, blah, blah, I’ve only used the rotator on this cloud, still haven’t tried it on other images, so I don’t know how good the algorithm is, and assume the code has a lot of room for improvement (constructive criticism gratefully accepted). But it seems to work okay.

I forgot that I’d have to write an enclosing applet to display the animation, and I’m beat. So for the short-term, I’ll go ahead and post a jar you can download and run. It includes the source code.
http://hexara.com/Starfield/StarfieldTest.jar

Realize, the animation and randomizing do NOT include the GalaxyCloud moving around, nor Auroras or other possible cool stuff. That is yet to be built and if anyone wants to contribute or do their own, I’m happy. The animation doesn’t have as many meteorites as it could, or twinkle as much as it could. But again, these things can be altered and can become settings of a more customizable version.

If you decide to try using the jar or code in a program, the main item is the StarField object. At this point the only things that can be specified:

  1. dimensions and location of the rectangle in which it appears,
  2. number of stars in that area,
  3. it is possible to include a seed for the Random function if you want the same stars to appear every time. I’m unclear if this affects the meteorite sequence as well. Maybe it does.

Oh, I included a Timer for animation. The animation could also be done via a GameLoop.

I’ll try to make a website that displays the animation soon, but might not get to it for a few more days.

Having a local wiki page on noise would probably be a very good idea.

Well, you know what they say about suggestions… :point:

Cas :slight_smile:

Stub is already in place. :stuck_out_tongue: