Pages

luni, 10 iulie 2023

Graphics programming - Unity 3D shaders - 001.

If you have studied shader theory and computational graphics in the past, then the only impediment would be the Unity 3D interface.
For a shader that uses the CG programming language, then you can use this type of shader in Unity 3D software.
You can create with right-click on Unity 3D interface.
This will create a shader file, you can open and see the default shader.
Let's make a CG shader with one color.
Remove all content from this file and add this source code:
Shader "Cg basic shader" { // this is the name of the shader 
   SubShader { // Unity chooses this like an subshader and works with your GPU specifications
      Pass { // passes in higher-level shader/material systems GLSL/Cg/HLSL is a way of setting up states necessary for multi-pass rendering, in this case is one pass
         CGPROGRAM // start programming with Unity's Cg

         #pragma vertex vert 
            // this specifies the vert function as the vertex shader 
         #pragma fragment frag
            // this specifies the frag function as the fragment shader
		// define vert for vertex shader 	
         float4 vert(float4 vertexPos : POSITION) : SV_POSITION 
            // vertex shader 
         {
            return UnityObjectToClipPos(vertexPos);
              // this line transforms the vertex input parameter 
              // and returns it as a nameless vertex output parameter 
              // (with semantic SV_POSITION)
         }
		 // define frag for fragment shader
         float4 frag(void) : COLOR // fragment shader
         {
            return float4(0.0, 1.9, 7.6, 1.0); 
               // this fragment shader returns a nameless fragment
               // output parameter (with semantic COLOR) that is set to
               // opaque red (red = 1, green = 0, blue = 0, alpha = 1)
         }

         ENDCG // ends the part in  programming with Unity's Cg
      }
   }
}
This is a standard shader and if you search on the web you can find more simple shaders.
Add an object to your scene, create a new material, and add to this object, drag the file shader to the material, and will fill with this RGBA color: 0.0, 1.9, 7.6, 1.0.