Unity 3D – Shaders scripts – part 001.

This is the first tutorial in a series of shaders tutorials in Unity 3D.
Open your Unity 3D project and using right click mouse add a Unlit shader to your project, see the image:

Rename your shader file and open this shader with right click mouse.
You will see the content of the default shader into your Visual Studio IDE.
Let’s see my shader example named NewUnlitShader :

When you write your first shader then you need to know some rules:

  • take a look at this link to see the most common variables and HLSL type and rules;
  • example: vec2 types with float2;
  • example: mat2 with float2x2;
  • example: vec3(1)  is float3(1,1,1);
  • example: mainImage(out vec4 fragColor, in vec2 fragCoord) is float4 mainImage(float2 fragCoord : SV_POSITION) : SV_Target;
  • example:
  • Shader “Unlit/NewUnlitShader” – the shader will add into your material from : Unlit – NewUnlitShader;
  • you can change the name NewUnlitShader , but you need to change the name of the file shader;
  • the body of shader start with word CGPROGRAM and end with word ENDCG;
  • Shader Level of Detail (LOD) is a number (see doc);
  • the Properties word is used for: inputs like a texture (see and _MainTex (“Texture”, 2D) = “white” {} );
  • the SubShader word is used to start define your settings for shader and the content of shader;
  • the words like: #pragma, #include , struct  are reserved words and is used to set and make the body of shader;
  • any shader come with : vertex shader body and fragment shader body;

One simple example of settings for vertex and fragment body shader but also you can have pixel shader body:

The next step is to add this to your shader:

Now, about vertex shader this can manipulate the attributes of vertices.
The fragment shader is the same as pixel shader.
The fragment/pixel shader is part of the rasterization steps.
You can see in my first example I used fragment shader not pixel shader:  #pragma fragment frag
The result is this function :

This is first part of this tutorials about shaders and you will need to learn all of this and make connections with my examples.

Posted in All, Programming, Unity 3D. Tagged with , , , , .