Star Nest

This is a shader taken from shadertoy and “translated” to be used in Godot (at least 3.x versions), based on various “branches” of the ‘Star Nest’ by Pablo Andrioli (user “Kali” on Shadertoy). Credits to them.

 

I’d recommend to be used in a color rect, son of a viewport and grandson of a viewport container with stretch shrink, due to the high processing cost.

 

In the video I was playing with the variables in order to show the versatility of the shader.
(it was stretched by 5)

 

I think I’d have a “port” to the Godot 4 versions if needed.

Shader code
shader_type canvas_item;
render_mode unshaded;

uniform int iterations = 20;
uniform float formuparam = 1.00;

uniform int volsteps = 20;
uniform float stepsize = 0.1;

uniform float zoom = 0.800;
uniform float tile = 0.5;
uniform float speed = 0.001;

uniform float brightness = 0.002;
uniform float darkmatter = 0.100;
uniform float distfading = 0.650;
uniform float saturation = 0.750;

uniform vec2 iResolution = vec2(192, 192);
uniform vec2 iMouse = vec2(0,0);

float SCurve (float value) {

    if (value < 0.5)
    {
        return value * value * value * value * value * 16.0; 
    }
    
    value -= 1.0;
    
    return value * value * value * value * value * 16.0 + 1.0;
}

void fragment()
{
	//get coords and direction
	vec2 uv=FRAGCOORD.xy/iResolution.xy-.5;
	uv.y*=iResolution.y/iResolution.x;
	vec3 dir=vec3(uv*zoom,1.);
	float time=TIME*speed+.25;

	//mouse rotation
	float a1=0.5+iMouse.x/iResolution.x*2.;
	float a2=0.8+iMouse.y/iResolution.y*2.;
	mat2 rot1=mat2(vec2(cos(a1),sin(a1)),vec2(-sin(a1),cos(a1)));
	mat2 rot2=mat2(vec2(cos(a2),sin(a2)),vec2(-sin(a2),cos(a2)));
	dir.xy*=rot1;
	dir.xz*=rot2;
	vec3 from=vec3(1.0,0.5,0.5);
	from-=vec3(0.0,time,0.0);
	from.xz*=rot1;
	from.xy*=rot2;
	
	//volumetric rendering
	float s=0.1,fade=1.;
	vec3 v=vec3(0.);
	for (int r=0; r<volsteps; r++) {
		vec3 p=from+s*dir*0.5;
		p = abs(vec3(tile)-mod(p,vec3(tile*2.))); // tiling fold
		float pa,a=pa=0.;
		for (int i=0; i<iterations; i++) { 
			p=abs(p)/dot(p,p)-formuparam; // the magic formula
			a+=abs(length(p)-pa); // absolute sum of average change
			pa=length(p);
		}
		float dm=max(0.,darkmatter-a*a*.001); //dark matter
		a = pow(a, 2.3); // add contrast
		if (r>6) fade*=1.-dm; // dark matter, don't render near
		//v+=vec3(dm,dm*.5,0.);
		v+=fade;
		v+=vec3(s,s*s,s*s*s*s)*a*brightness*fade; // coloring based on distance
		fade*=distfading; // distance fading
		s+=stepsize;
	}
    
	v=mix(vec3(length(v)),v,saturation); //color adjust
    
    vec4 C = vec4(v*.01,1.);
    
     	C.r = pow(C.r, 0.35); 
 	 	C.g = pow(C.g, 0.36); 
 	 	C.b = pow(C.b, 0.38); 
 	
    vec4 L = C;   	
    
    COLOR.r = mix(L.r, SCurve(C.r), 0.7); 
    COLOR.g = mix(L.g, SCurve(C.g), 1.0); 
    COLOR.b = mix(L.b, SCurve(C.b), 0.2);     	
	
}
Live Preview
Tags
space, Starnest, Stars
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from ColorauGuiyino

Related shaders

Comments 8

  1. Would you by any chance have any ideas of fixing the very small flickering stars from this shader, I've been trying but with no luck.

    1
    1. Found this, as the best approach to resolve the flickering: http://casual-effects.blogspot.com/2013/08/starfield-shader.html but I couldn't make it work in Godot, hope you can...
      btw, this version is more "clean" and I add the colorize uniform and also the speed is now a vec3... if used with TILE and ZOOM in higher values (like 4), the flickering seems to be lesser.

      shader_type canvas_item;
      
      
      uniform int iterations : hint_range(0, 100, 1) = 17;
      uniform float formuparam : hint_range(0.0, 100.0, 0.001) = 0.53;
      
      
      uniform int volsteps : hint_range(0, 100, 1) = 20;
      uniform float stepsize : hint_range(0.0, 100.0, 0.001) = 0.1;
      
      
      uniform float zoom : hint_range(0.0, 100.0, 0.001) = 0.800;
      uniform float tile : hint_range(0.0, 100.0, 0.001) = 0.850;
      uniform vec3 speed = vec3(0.0, 3, 1);
      
      
      uniform float brightness : hint_range(0.0, 100.0, 0.0001) = 0.0015;
      uniform float darkmatter : hint_range(0.0, 100.0, 0.001) = 0.300;
      uniform float distfading : hint_range(0.0, 100.0, 0.001) = 0.730;
      uniform float saturation : hint_range(0.0, 100.0, 0.001) = 0.850;
      uniform vec4 colorize : hint_color = vec4( 0.0, 0.0, 0.0, 1.0 );
      
      
      uniform vec2 iMouse = vec2(0.0, 0.0);
      
      
      void fragment()
      {
      	//get coords and direction
      	vec2 uv=FRAGCOORD.xy/(1.0 / SCREEN_PIXEL_SIZE.xy)-.5;
      	uv.y*=(1.0 / SCREEN_PIXEL_SIZE.xy).y/(1.0 / SCREEN_PIXEL_SIZE.xy).x;
      	vec3 dir=vec3(uv*zoom,1.);
      	vec3 time=vec3(speed * TIME * 0.001);//vec3(speed.x * TIME * 00.1, speed.y * TIME * 0.1, speed.z * TIME * 0.1);
      
      
      	//mouse rotation
      	float a1=.5+iMouse.x/(1.0 / SCREEN_PIXEL_SIZE.xy).x*2.;
      	float a2=.8+iMouse.y/(1.0 / SCREEN_PIXEL_SIZE.xy).y*2.;
      	mat2 rot1=mat2(vec2(cos(a1),sin(a1)),vec2(-sin(a1),cos(a1)));
      	mat2 rot2=mat2(vec2(cos(a2),sin(a2)),vec2(-sin(a2),cos(a2)));
      	dir.xz*=rot1;
      	dir.xy*=rot2;
      	vec3 from=vec3(1.,.5,0.5);
      	from+=time;
      	from.xz*=rot1;
      	from.xy*=rot2;
      	
      	//volumetric rendering
      	float s=0.1,fade=1.;
      	vec3 v=vec3(0.);
      	
      	for (int r=0; r<volsteps; r++) {
      		vec3 p=from+s*dir*.5;
      		p = abs(vec3(tile)-mod(p,vec3(tile*2.))); // tiling fold
      		float pa,a=pa=1.0;
      		for (int i=0; i<iterations; i++) { 
      			p=abs(p)/dot(p,p)-formuparam; // the magic formula
      			a+=abs(length(p)-pa); // absolute sum of average change
      			pa=length(p);
      		}
      		float dm=max(0.,darkmatter-a*a*.001); //dark matter
      		a*=a*a; // add contrast
      		if (r>6) fade*=1.-dm; // dark matter, don't render near
      		//v+=vec3(dm,dm*.5,0.);
      		v+=fade;
      		v+=vec3(s,s*s,s*s*s*s)*a*brightness*fade; // coloring based on distance
      		fade*=distfading; // distance fading
      		s+=stepsize;
      	}
      	
      	v=mix(vec3(length(v)), v / (1.0 - colorize.rgb),saturation); //color adjust
      	COLOR = vec4(v*.01,1.);	
      	
      }
      
      0
        1. tmp_js_export.js:369 ERROR: CanvasShaderGLES2: Fragment shader compilation failed:
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369 ERROR: 0:545: 'm_x' : Loop index cannot be initialized with non-constant expression
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369 
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369  at: _display_error_with_code (drivers/gles2/shader_gles2.cpp:126) - CanvasShaderGLES2: Fragment shader compilation failed:
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369 ERROR: 0:545: 'm_x' : Loop index cannot be initialized with non-constant expression
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369 
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369 ERROR: Method failed. Returning: nullptr
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369  at: get_current_version (drivers/gles2/shader_gles2.cpp:372) - Method failed. Returning: nullptr
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369 ERROR: Condition "!version" is true. Returned: false
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369  at: bind (drivers/gles2/shader_gles2.cpp:87) - Condition "!version" is true. Returned: false
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369 ERROR: Condition "!version" is true. Returned: -1
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369  at: _get_uniform (drivers/gles2/shader_gles2.h:258) - Condition "!version" is true. Returned: -1
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369 ERROR: Condition "!version" is true. Returned: -1
          onPrintError @ tmp_js_export.js:369
          tmp_js_export.js:369  at: _get_uniform (drivers/gles2/shader_gles2.h:258) - Condition "!version" is true. Returned: -1
          Running from Chrome (Godot HTML5 Export)

          0
  2. Prakos

    I'm guessing this won't work as a sky shader for godot 4 right? I don't understand shaders very much and I wanted an easy to implement night sky with stars, but I'm not able to find anything.

    0