Atmospheric scattering (from space) - Help needed

Hi,
You may know that I am the one who shared my little Generic Planet library over to Blendswap for realistic planetary renders (Well, you probably don’t know me, but hey ^^) : http://www.blendswap.com/blends/view/70339

I accidentally found OSL support for Cycles, which is great news because OSL being a C-like language, like GLSL, I can more or less easily port one shader to another.
The problem is: I know little-to-nothing about GLSL (well I kinda understand it because of its nature), and porting a vertex+fragment shader into an OSL one is hard.

About the GLSL shader: well, there’s plenty around, mostly comes from the same source: O’Neil work. The martinsh demo is one of them, and is the one I started my work with (Blender FTW).
I only need the SkyFromSpace shader since the library isn’t supposed to be used for “inside atmosphere” shots, and the planet’s ground is already ready (though not OSL yet, if faster then maybe I’ll make a OSL shader for it).

I know the OSL requires an input for the Camera position and light direction, and those don’t work at the moment, but I have a workaround.

I have searched for a shader like this in OSL, but couldn’t find one.
Can I have some help here? Thanks! :slight_smile:

I actually ported a GLSL O’Neil-based shader set (GroundFromSpace and AtmosphereFromSpace) over to OSL a bit ago and have been working on it when I have time. I still have issues with the cloud layer, specular masking, and the scene setup is incredibly touchy (the reference shaders made a few assumptions like sphere diameter), but I’m working on eliminating some of those concerns (my physics and geometry are a bit rusty, but it’s not hard overall). I’d be happy to share what I have so far, although I’ll need to clean it up a bit first. It does work nicely, but I’m still adding some things to it. If you’d rather try porting it yourself instead, I can always help out with that too. Maybe you can think of better ways to address some of the problems I ran into and haven’t solved yet.

Bear in mind that the GLSL and HSL shaders based on O’Neil’s work require a bit of understanding from a render standpoint if you want to try porting it yourself. I’ve accounted for most of the quirks, though, and can help with overcoming them. Also, I have never been able to get getattribute() calls to work consistently to get the sun/lamp position, so I’ve had to supply the light vector in the node setup (I share quite a few “global variable” values between the shaders anyway). It’s also quite slow to render, but that comes with handling Rayleigh and Mie scattering the way that the existing work does. I may reimplement them to use precalculated/pregenerated textures, but my future plans for it may preclude that from being a good idea. Overall, I will say that porting the shaders was a nice way to learn OSL better and would recommend it if you’re inclined.

I’m preparing a new test render tonight, so I’ll post it when it’s done to give you an idea of what it can do.

Maybe we can team up together on that, as I really want to be able to make those realistic space scenes you see being made on 3DS Max. I mean, would it be cool to have this?
http://imageshack.us/a/img15/3944/kerbalspaceprogram.jpg

being made on Blender (the planet huh, not the KSP stuff :P)

If you already got a OSL shader more or less working, I won’t start over from scratch if you share, it would feel like a waste of time :wink:

I know that commons errors on the GLSL shaders are that the normals are actually reversed and that it is actually the back side of the sphere that is actually rendered, and that the light vector is actually the direction, and not the position, because the sun is so far away that the rays can be considered parallel.
For the actual vector I think we can get away with a XYZ as RGB input to the shader, with the values bound to a driver which inputs the actual rotation parameters of the sun lamp (it’s kinda hard to explain).

So, you’re going for a full OSL implementation ? I though I could get away with just the atmosphere shader, since you can easily replicate the ground shader with a Fresnel input that tints the texture towards the atmospheric color on the edge (the redshift is not that clear in real life, and can be omitted)

Sure, I’ll work on cleaning it up this weekend and add more comments.

I thought about omitting the ground shader, but you actually need it because the AtmoFromSpace shader isn’t rendered over the planet at all, but rather behind it (it’s the “halo” portion around the planet). The GroundFromSpace shader handles the colouration where ever the camera ray hits the planet. You’re right about the GLSL implementation in that way - the AtmosphereFromSpace is rendered with reversed normals to allow the planet itself (and ground shader) to “appear in front” of the atmospheric sphere. I had to re-read the rendering details a few times before understanding how that worked, so I can see how it would be confusing at first :yes: A little geometric formula made that pretty easy to handle without having to deal with normal flipping.

And yep, the light “position” is actually a direction vector for reasons you already know (why they named the variable to imply that it was a position is beyond me). Also, I did think about using drivers for the sun position, but have been using it as a test case for getattribute() crashes and moving the sun has been easy enough manually. As for the red shift, I thought about adjusting the equations to lessen it, but haven’t bothered for a few reasons I won’t get into (there are some better implementations than O’Neil’s out there, but I got lazy and just stuck to his stuff since it was easy to find and adapt the shader implementations).

Side note: I have been testing some input variations for various planets’ (and moons’) atmospheric conditions. It’s funny to see the Earth with Martian atmospheric scattering effects (or Titan, but that one’s rather difficult for a lot of reasons). Maybe I’ll do a quick render to show that when I do my test tonight.

Maybe mixing up Nodes and OSL, we can input the “Backfacing” output factor in the “Input -> Geometry” to make the shpere only show up the inside of itself:
http://www.pasteall.org/pic/show.php?id=74194

(here I put a Light Path so the emission doesn’t affect the ground sphere).

I wanna see the images ! :smiley:

This? in Blender? That shouldn’t be to hard!?! :confused:

The ship in itself, no. But the planet behind, yes. Close ups (like shots on orbit) are even harder to get right. Hopefully with the OSL shader it will be easier.

I was referring to the planet… :wink:

Waldonnis did a great job with the OSL port so far. This is a render of the actual state, all credits to him, I haven’t done any big work yet:
http://www.pasteall.org/pic/show.php?id=74261

There’s still some issues to be resolved, but so far it is looking quite good !
This was rendered on a i5 @ 3.40 GHz in 1:21 - and it’s actually inside the atmosphere, which is pretty cool :wink:

Thanks for the compliment :smiley: As SolarLiner and I have been talking about in PMs, there are still quite a few things left to do on this, and it’s not an easy setup currently (so it’s not ready for mass consumption yet). I’m sure we’ll be posting more test renders soon, though. It’s worth mentioning that there have been some solid subsequent papers on atmospheric rendering since Sean O’Neil published his work that cover some better (in my opinion) methods to accomplish this, but there were enough HSL and GLSL O’Neil shaders out there to start with those first. In time, I’ll probably start working on adapting those methods as well so we can see even better results.

Here’s my last test render. It’s using a slightly older version of the shader set and was made for testing some bump textures, but should give an idea of what kind of effect we can get out of it (ignore the cloud layer, which is still a work in progress).

http://i132.photobucket.com/albums/q25/verissi/Space%20Renders/Earth/earth-bumptest1.png

That’s my last results about Planet Rendering but it’s all with cycles node configs, a more modular OSL would be great
http://www.blenderartists.org/forum/showthread.php?346587-Planet-Rendering&highlight=Planet

Is there any more work being done on this? This is really impressive