As you can see, Im using the script, and Im trying to use the factor for the density, but I dont think Im using it correctly…
And as you can see aswell, the planet has a very sharp edge… Theres no fading or anything, and I want to have the volume fade outwards, with the inner volume having more density and less as you get to the surface
Also I tried using this “point density” node thing, which Ive never heard of until now…
Unfortunately, although its subtle and I kinda like its effect, its not really what I want…
Top nodes are for manual empty control, so you don’t need them.
The bottom nodes are what is important. The 3 selected nodes (Divide, Color Ramp, Emission) are the main control. Play with the values and you will get it.
Bottom Volume Scatter and Absorbtion are optional. You should also swap Emission node for Volume Scatter, as planet atmosphere is not emissive.
You’ll want scale applied, with the proper radius entered in the value field (for mathematically perfect operation, but feel free to tune to eye instead) and an object origin at the center of the sphere.
What you’re doing is creating a vector in the plane of the object and then just getting the bit of the vector pointing in the same direction as the camera. That’s the distance from the camera plane, running through the object origin, to the sample. Because this plane runs through the center of your object, the actual distance from one point of the sphere to the other is twice this number.
I should add, it’s not really correct for perspective, but that’s unlikely to be noticeable.
OP, I doubt that you can have OSL with GPU support. This feature is pretty much WIP at this point. @bandages solution is several times faster in rendering times if you need a sphere shaped atmosphere.
I just realized, it’s not quite right for volumes. Should be right for shells, but volumes have internal samples, for which the distance to the plane isn’t the distance through the sphere.
That’s fixable with a bit more math, but I’ve got some chores, I’ll show later.
For aproximating thin layer of atmosphere in OPs example sampling internal volume is not that necessary I think. But for see-through volumes you might be right. After all OSL script is doing that.
Obviously, if it’s what you want, then that’s good
To do what OP explicitly wanted, I would really do it like that-- volumetrics already have a cumulative effect from the screen space depth of the volumetric. Instead, I might, at most, do a little bit of mapping of the density based on the distance from the center of the object (which some people get with object coords -> gradient texture, but I usually get from object coords -> vector math/length.)
I tried something out to do what OP was explicitly asking for, correcting for the positions inside the sphere, but it’s starting to hurt my head, and isn’t elegant like what I offered before, so I’m going to call it a night
This doesn’t require any particular scale for the object, since it uses world space coordinates (the object coords are just for locating the center of the sphere), but it still needs to know the sphere’s world-space radius. (If you want to just use default-ish spheres with object transforms, you can build that into the mat by looking at the distance between 0,0,0 and 1,0,0 object coords in world space.)
It’s still not perspective correct, but correct for orthogonal cameras; you could do perspective correct, but it’s more math, and the difference isn’t going to be noticeable.
I’ve made an interior sphere, just as part of testing it, but it should be noted that the distance you’re getting is not the distance through just the “atmosphere” but the distance through the entire sphere.
Look closely at the Color Ramp node.
In my example there are two additional sliders at the very right of the ramp. The first missing one have balck value and second one - white.
I belive this should fix your issue.