Unity Shader Chapter 1 (Beginning)

        I haven't written a blog for a long time, and I have been delayed by various things (actually lazy). Well, not much nonsense, this time a new pit has been opened, and it is expected to be completed within 100 years. This time I write Shader mainly to forget some knowledge points in the future, (I am very stupid, I forgot what I did when I saw it at all) I will write this Shader tutorial as a novice (you are a Is it good for a novice).

        When I learn shaders, I always teach very loosely. The front is a novice teaching, and the back is a super complicated example. It will not be good at all. Many Shader tutorials in China are either scattered in content, or the span is very large. Very unfriendly to a newbie like me. Is it a foreign tutorial... I don't understand it at all.

        That's right, if you're a shader, you can go to the right, reading what I wrote is a waste of life.

The principle of Shader

        I'm not going to say anything about what the GPU passes to the CPU, I'm not going to say any of that. If you want to know, search for yourself, there are many.

Some basics of Shader  
       Unity shadr is simply divided into two categories,
  • Surface Shader -----unity does most of the work for you, and you can achieve a lot of good effects with simple writing. But the functionality that can be achieved is not as much as the vertex fragment shader.
  • Fragment Shader - Can do more things, but is relatively difficult to write.
Basic structure of Shader program

        Although this is an introductory article, I won't cover surface shaders, so I'll come up with fixed-point fragment shaders.

Let's first look at the basic structure of the grammar

Shader "shun/basic structure"{//The name of Shader is defined here, you can start it at will
	Properties{
		//Write properties here, you can

	}


	//When SubShader can write many graphics card running effects, starting from the first SunShader, if the effects in the first SubShader can be run, then use the first SubShader, if there are some effects in the SunShader of the graphics card If it can't be achieved, it will automatically run the next SubShader
	SubShader{
		//At least one Pass block
		Pass{
			//Write Shader code here HLSLPROGRAM

			CGPROGRAM
			//Use CG language to write Shader code

			ENDCG
		}
	}

	Fallback "Diffuse" // Enables the Fallback shader when none of the SubShader graphics cards above support it. This is an alternate solution, you can

}
My first Shader
    
Shader "Shun/My first Shader"{

	Properties{
	
	}

	SubShader{
		Pass{
			
			CGPROGRAM

			//The vertex function is just declared here, the function name of the fixed-point function
			//The basic function is to complete the conversion of fixed-point coordinates from model space to clipping space (from the game environment to the field of view camera screen)
			#pragma vertex vert

			//The fragment function is only declared here, the function name of the fragment function
			//The basic function returns the color value of each pixel on the screen corresponding to the model
			#pragma fragment frag

			float4 vert(float4 v : POSITION) : SV_POSITION{//Tell the system through semantics what this parameter is for, for example, POSITION tells the system that I need fixed point coordinates
			//SV_POSITION This semantic is used to explain the return value, which means that the return value is the vertex coordinates in the clipping space
			return UnityObjectToClipPos(v);

			}


			fixed4 frag() : SV_Target{
				return fixed4(0.54,0.45,0.98,1); //This returns a color value RGBA, you can change the parameter to change the color, the parameter range is 0 to 1
			}

			ENDCG
		}
	}


	Fallback "Diffuse"
}

The "Properties{ }" written above can be deleted. It is not used yet, and will be used in the future.

I'm tired. Next time I'm writing. If I have time, I'll talk about it in detail on the top of the pair.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324819848&siteId=291194637