This is a script based off of Michel over at blenderthings.blogspot.com . I highly recommend that site.
Anyway, I took his Starscape script and in my humble opinion, improved it a fair amount. There didn’t seem to be an easily available starscape texture for Cycles, so here’s one.
He releases his code under CC BY-SA, and I’m crediting him. If you’re reading this, and you want me to remove it, just PM me and I will.
/* Scarscape Texture for OSL and Cycles.
** Based off of the script from blenderthings.blogspo`t.com
** I've changed the scaling so that 1 produces good results.
** I've also added a random color variation to the stars,
** and my own "twinkle" effect.
*/
#include "stdosl.h"
#include "node_texture.h"
point voronoi3dp(point p, float density, output float d2)
{
int xx, yy, zz, xi, yi, zi;
xi = (int)floor(p[0]);
yi = (int)floor(p[1]);
zi = (int)floor(p[2]);
float dbest = 1e10;
point pbest = 1e10;
vector dz = vector(7,111,19);
for (xx = xi - 1; xx <= xi + 1; xx++) {
for (yy = yi - 1; yy <= yi + 1; yy++) {
for (zz = zi - 1; zz <= zi + 1; zz++) {
vector ip = vector(xx, yy, zz);
if(cellnoise(ip)<density){
point vp = ip + cellnoise(ip+dz);
vector dp = p-vp;
float d = dot(dp,dp);
if (d < dbest) {
dbest = d;
pbest = vp;
}
}
}
}
}
d2=dbest;
return pbest;
}
shader stars(
float Scale = 1
[[string help = "Scale of the starfield, smaller values give bigger stars",
float min = 0]],
float StarDensity = 1
[[string help = "Density of the stars, higher values mean more stars per area",
float min = 0]],
float StarColorVariation = 0
[[string help = "Adds random color variation to the stars.",
float min = 0, float max = 1]],
float TwinkleAmount = 1
[[string help = "Adds a random brightness to the stars",
float min = 0]],
float TwinkleTime = 0
[[string help = "Controlls the TwinkleAmount. Animate this value to get twinkling stars.",
float min = 0]],
point Pos = P,
output color Color = 0.0,
output float Fac = 0.0
)
{
float scaleint = Scale * 750;
point p = Pos * scaleint;
color starcolorint = 0.0;
float stardensityint = StarDensity/100;
float twinkle = 0.0;
float d;
voronoi3dp(p, stardensityint, d);
Fac = exp(-d * 10);
if(StarColorVariation){
starcolorint = color("hsv", noise(p/10), StarColorVariation, 1);
}
else{
starcolorint = color(0.9, 0.9, 0.9);
}
if(TwinkleAmount){
twinkle = noise("uperlin", p/10+TwinkleTime) * TwinkleAmount;
twinkle += 0.3;
twinkle = clamp(twinkle, 0, 1);
starcolorint *= twinkle;
}
starcolorint *= Fac;
Color = starcolorint;
}