Creating a Sky from noise, shader code

I struggled a bit to find the relevant math to do this, so I’m going to post it here just in case somebody needs it.

This shader allows you to quickly create a Sky background with a noise texture that won’t be deformed at its poles. It might save you space, time, and make procedural generation easier.

shader_type sky;
uniform sampler3D noise;

void sky() {
    if (AT_CUBEMAP_PASS) {
        COLOR = vec3(0.0);
        ALPHA = 1.0;
    } else {
		vec2 norm = (SKY_COORDS * 2.0) - vec2(-1.0, -1.0);
		float long = norm.x * PI;
		float lat = norm.y * (PI/2.0);
		vec3 projected = vec3(cos(lat) * sin(long), cos(lat) * cos(long), sin(lat));
		vec3 color = texture(noise, projected).rgb;
		COLOR = color;
    }
}

Steps to implement:

  1. Create your WorldEnvironment
  2. Set ‘background’ to ‘Sky’
  3. Sky to New Sky
  4. Sky Material to New ShaderMaterial
  5. Copypaste this sahder code
  6. In Shader parameters, create a New NoiseTexture3D
  7. Set to seamless
  8. You might need to increase Seamless Blend or the resolution
5 Likes

could you paste a screenshot to see how sky would look like?

Here you go, I angled the camera down to show that it does’t produce the same wierd deformation as when you simply use 2D noise, on the sides it’s pretty much the same.

1 Like

thanks!

1 Like