Академический Документы
Профессиональный Документы
Культура Документы
I will most likely make more of these kind of documents about more advanced shaders and
compute shaders.
The doc will go over how to setup a shader that renders a texture on an object, and being able to
assign a color to it.
Once that has all been explained with code and all we will go over some very simple effects you can
add on the object.
Every object should only use one renderer but that renderer can however have
as much materials as it pleases. A material is basically a wrapper for shaders.
*If you already know how to create a shader file in unity skip this page*
/Go into the project tab and in the asset folder press your Right mouse button goto create and
create a folder name whatever you want your shader folder to be called/
Creating your first shader:
After youve created your shader file, Open it and delete all the code from it.
We are going to start from scratch so we understand what happends piece by piece..
Shader "Custom/myFirstShader"
{
What we do here is tell unity that this is a shader and also show it where it's going to be displayed in
unity.
Shader "Custom/myFirstShader"
{
Properties
{
}
}
_Color = The variable name; Color = Inspector name; Color = variable type; (1,1,1,1) = default value
_MainTex("TextureMap",2D) = "white" {}
Shader "Custom/myFirstShader"
{
Properties
{
_Color("Color", Color) = ( 1,1,1,1)
_MainTex("Texture",2D) = "white" {}
}
}
Lets now add the subshader and passes in. You can have as many of these as youd like.
Shader "Custom/myFirstShader"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Texture",2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAMM //Lets the cg know the shader starts here
}
}
}
Everything outside of the pass {} part is part of the shaderlab which sends the cg data over to unity
Everything inside Pass {} is part of the cg and will be where you put in the shader code.
In the pass shader we are first going to assign the what the vertex and fragment function are going
to be named as:
ertex vert
#pragma v
#pragma f ragment frag
You also have
eometry name
#pragma g
#pragma h ull name
#pragma d omain name //But we are not going to fuck with these
Creating your first shader:
Now let's give the shader a helper file which is called UnityCG.cginc. This will include some built in
helper functions that are used in most shaders.
#include "UnityCG.cginc"
So lets start with a predefined struct made from the UnityCG.cginc all still in the pass {} section:
struct appdata
{
Float4 vertex : POSITION;
Float2 uv : TEXCOORD0
};
Now we have another struct we want to make. The vertex to fragment struct.
struct v2f
{
float4 position : SV_POSITION;
//SV_ makes it so it also works on ps4 and dx9
float2 uv : TEXCOORD0;
};
Next we are going to Build the object:
v2f vert(appdata INPUT)
{
V2f OUT;
OUT.position = mul(UNITY_MATRIX_MVP, INPUT.vertex);
OUT.uv = INPUT.uv;
Return OUT;
}
mul() is basically what youd think it would do it multiplies.
UNITY_MATRIX_MVP = the current modelview projection, I wouldn't worry too much about this
Creating your first shader:
Now we make the last part of the cg part of the shader which Ill explain more about later.
First we need to redefine our properties since the cg part of the shader has no clue about our
properties.
So first let's create our color:
float4 _Color; //A color is a float4 because its a rbga
And our texture:
sampler2D _Texture; //sampler2D is a 2D texture coming from glsl
So you see the 2 parts Shaderlab and CG.
The shaderlab part sends the data from the cg part over to unity.
And the cg part is the actual shader.
Click on it and go to menu item Custom, Under custom press the myFirstShader.
Click on it and change the color.
Now put the material on a object and there it is your self written shader.