Procedurally generate a night skybox

I started to experiment on a fully procedurally generated game. Along with the models and textures, I decided to procedurally create the skybox textures to have more flexibility and a minimize theĀ game build size. Here is a simple example of the generated skybox :

Procedurally generated skybox with stars

Procedurally generated skybox with stars

The code is pretty simply and self-explanatory. The header initialize a couple of variables. Then, 6 textures are created by looping trough each pixel individually and setting their color with the Texture2D.SetPixel() function. The code set some single pixel as grey-white and some 2-by-2 pixels as a yellowish color with their texture varying too. A random number is evaluated on each pixel to decide if its a black,yellow or grey pixel . Once all texture are created, they are assigned to the skybox material and the material is defined as the skybox to use in the RenderSettings.

Texture2D texture = new Texture2D(1024, 1024, TextureFormat.ARGB32, false); // Create a new 1024x1024 texture ARGB32 (32 bit with alpha) and no mipmaps
Material skyboxMaterial = new Material (Shader.Find("Mobile/Skybox")); // Create a new material for the skybox.
string[] skyboxTextures = new string[] {"_FrontTex", "_BackTex", "_LeftTex", "_RightTex", "_UpTex", "_DownTex"}; // The skybox textures names
Color color; // Color that will be set for each pixel

// Create 6 different textures to use as skybox
for (int i = 0;i < 6; i++)
{
	// Loop horizontally on all pixel
 	for(int x = 0; x < texture.width; x++)
 	{
		// Loop vertically on all pixel
		for(int y = 0; y < texture.height; y++)
 		{
 			float randomValue = Random.Range (0.0f,1.0f); // Random value between 0 and 1
 			if(randomValue < 0.001f) // 0.1% Of the pixel are set to a grey/white pixel with varying transparency
 			{
 				float colorValue = Random.Range (0.6f,1.0f); //Random value to use as RGB value for color
 				color = new Color(colorValue,colorValue,colorValue,Random.Range (0.5f,0.9f)); // Set the pixel to a white/grey color with variying transparency
 				texture.SetPixel(x, y, color); // Set the pixel at (x,y) coordinate as the Color in color
			}
 			else if(randomValue < 0.0015f && x > 0 && y > 0) // 0.05% Of the pixels that aren't on the first row/column
 			{
 				float colorValue = Random.Range (0.85f,1.0f); //Random value to use as RGB value for color
				color = new Color(colorValue,colorValue,0.0f,Random.Range (0.6f,0.9f)); // Set the pixel to a yellowish color with variying transparency

				// Set a square of 4 pixels to the current color. The 3 other pixel have been stepped on earlier so their color won't be modified afterward
 				texture.SetPixel(x, y, color);
 				texture.SetPixel(x-1, y, color);
 				texture.SetPixel(x, y-1, color);
 				texture.SetPixel(x-1, y-1, color);
 			}
 			else
 			{
 				texture.SetPixel(x, y, Color.black); // Set the pixel as black for the default background color
 			}
 		}
	}

// Apply all SetPixel calls
texture.Apply();

// Set the current texture on one side of the skybox
skyboxMaterial.SetTexture(skyboxTextures[i],texture);
}

// Set the RenderSettings skybox to the created material
RenderSettings.skybox = skyboxMaterial;

Once you have this going, you can try more complex patterns such as using perlin noise for pixel color or more complex object such as galaxy or star clusters!

For the interested, here is a first screenshot of the game I’m working on. It shows the terrain, the ground of a sci-fi base and a wood table. All models and texture are procedurally created. The ground texture come from perlin noise, the table use Voronoi noise and the sci-fi base ground are sinuses to make the lines that crosses.

First screenshot of new procedural game

First screenshot of new procedural game