How to create skybox using shader graph in Unity

Hello everyone,Welcome back. Today we will look into how to create skybox shader in unity.

Before we get started, I am using Unity version 2022.2.10f1, but it can be applicable for all Unity LTS verstions. I have a project setup in URP.

First of all, what is a skybox? Skyboxes are a wrapper around your entire scene that shows what the world looks like beyond your geometry.

Now without further ado, let's get started.

We can set any unlit material as our skybox, so let's create the shader graph, go to shader graph > built in > unlit shader graph.

Skybox will typically has different colors over the Y axis or vertically top to bottom so let's create a Position node, we want world position so let's keep Space at World, now we need to normalize it, so feed the output into Normalize node. It will convert our position vector's length or magnitude to 1 unit. That way we will get value between 1 and -1.

We need Y axis so let's create a Split node. Feed normalized output into split input, G will be our Y axis.

Let's deal with the sky part first, take the Y output and hook it into Clamp node and clamp the values between 0 and 1. so the bottom half no longer contains any negative values.

It will look something like this.



Then we want to control the gradient blend, so feed the output into Power node.

Now we want to control the blend from the inspector so let's create a float property, call it ZenithBlend, Give a Default value of like 1, and  set it's Mode to Slider, Min 0.01 and Max depends on preference let's try 50.

Drag it in and hook it into Power node. Now we need a color for our sky so let's create another property, Color, call it SkyColor, give a default value of your liking and drag that in and multiply it with our Power node's output.



Time to do the same for the ground. We need bottom part of our Y axis, and the bottom part is 0 to -1 so let's Clamp it, Min -1 and Max 0.

Now, we need to convert negative values into positive values, so to convert them we will use Negate node.

Now we have ground part. We will follow the same for blend and color part as we did with sky.

Create float property, call it NadirBlend. Drag it in, feed it into Power node with our output of Negate.Create another property for Color, call it GroundColor, give default and multiply with our Power node's output.



Time to deal with the horizon part.

We will just add Clamp output from the sky with Negate node's output. And we will get a black horizon. We need to invert this we will use One minus node.

We also want to control the blend of the horizon so, you guessed it let's create another float property, call it HorizonBlend, drag it in. Pass the output of the One minus into another Power node and feed the HorizonBlend. and give a default of something like 30 so we can see the effect.

And same thing for color so, create a Color property, call it HorizonColor and multiply.



Only thing that remains is, to add all these multiply outputs, we can add in any order it does not matter. Add Sky part with Horizon part and the output of these two's addition with our Ground part and feed it into our Master node's base color.



Save our graph, create material, go to our Lighting window's Environment tab, Window > Rendering > Lighting and assign our newly created material in Skybox material slot.

There we have it, we can customize it from the inspector and we have our procedural skybox.

Trouble following along? Check out this video.


Thank you so much for reading.

Comments