Unity learning shader notes [5] texture mapping

Keywords: Unity Fragment

Here is a brief explanation of the type of unit imported image material

Default: default map type for materials
NormalMap: normal map. There is a kind of map specially used for normal map to display the details of the model.
Editor: a picture of the UI such as the buttons used to customize components in the unit editor
Sprite: picture for UGUI production
Cursor: cursor picture
Cookie s: for shadow making
LightMap: LightMap, i.e. the map with high lighting rendering added, sometimes can replace the shadow map with real-time rendering

WrapMode represents what the material will do when the tiling of texture map is greater than 1, that is, when it is about to shrink.
Repeat means to use the original image to fill in the blank area caused by reduction
Clamp represents the use of repeated image edge pixels to fill the blank area caused by reduction.
Mirror means to use the mirror image of the original image (equivalent to the original image flipping) to fill the blank area caused by the reduction.
MirrorOnce has the same effect as Clamp. For details, please refer to the official document.
PerAxis is equivalent to the u-direction (horizontal) and v-direction (vertical) of the picture. The above options can be carried out without affecting each other.

FilterMode refers to the filtering mode, which can be used to control the sharpness of the image when zooming in
Point mode has the lowest definition when zooming in. It's a little pixelated. It can be specially used for pixel games. Of course, its performance is the best.
Bliner mode is bilinear bilinear, slightly better than Point mode, with poor performance
Trilinear is trilinear imaging is the thinnest and most performance consuming

The code for texture mapping is as follows

// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader"TsinNing/Texcoord"{

	Properties{
		_texPlusC("TexPlusC", COLOR) = (1,1,1,1)
		//Assign texture map 
		_MainTex("Main Tex", 2D) = "white"{}
	}


		SubShader{


		Pass{
		//You need to define the correct lighting mode to get the correct Unity built-in lighting variable
		Tags{"LightMode" = "ForwardBase"}


	   CGPROGRAM
		//It's equivalent to that the using file in c contains unity about lighting.
 #include "Lighting.cginc"

 #pragma vertex vert
 #pragma fragment frag

		fixed4 _texPlusC;
		
		sampler2D _MainTex;
		//This variable does not need to be declared in properties. As long as a texture map is declared in properties, the system will automatically assign a value to the variable of this format.
		//The assignment is four floating-point numbers, the first two are scaling, the second two are displacement corresponding to tiling of the inspector panel, and the last two are offset corresponding to the inspector panel.
		//The variable name format is fixed, with the variable name of texture map followed by "ST".
		float4 _MainTex_ST;

		 struct a2v {
			 float4 vertex:POSITION;
			 //When the structure a2v passes in vertex function
			 //The texcoord variable represents the position of the texture map corresponding to this vertex.
			 //texcoord can only be passed in from vertex function, and it needs vertex function as medium to pass it to slice function.
			 //texcoord is used in the movie source function to get the color of this vertex according to the position and map.
			 float4 texcoord:TEXCOORD0;
		 };

		 struct v2f {
			 float4 position:SV_POSITION;
			 float2 uv : TEXCOORD0;
		 };


		v2f vert(a2v v)
		{
			 v2f f;
			 //Convert vertex positions from model space to clipping space
			 f.position = UnityObjectToClipPos(v.vertex);
			 //Offset the texture coordinates of its own vertices in the offset field of the maintex st variable
			 //Expand and shrink the texture coordinates of its own vertices in the tiling field of the maintex st variable
			 //For example, when the xy field is 22, the texture map becomes 1 / 2 of the original size.
			 //Note that the effect after zooming is related to the WrapMode of the picture material.
			 f.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
			 return f;
		}


		fixed4 frag(v2f f) : SV_Target{
			 fixed3 texC = tex2D(_MainTex, f.uv.xy) * _texPlusC.rgb;
			 return fixed4(texC,1.0);
		}

		  ENDCG

	   }
	}

		Fallback "Specular"
}

Let's finish with a picture~

Posted by quickphp on Sun, 27 Oct 2019 04:28:41 -0700