Unity Shader learning (seven) simple use of texture images

Today we will learn how to use the texture and render the texture.
First, the code:

Shader "Unlit/shader9"
{
    
     
    ///鼠标移动正方形
    Properties
    {
    
    
        _MainTex("Main Texture",2D)="white"{
    
    }
    }
    SubShader
    {
    
    
        Tags {
    
     "RenderType"="Opaque" }
        LOD 100

        Pass
        {
    
    

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag


            #include "UnityCG.cginc"
        struct v2f{
    
    
            float4 vertex:SV_POSITION;
            float4 position:TEXCOORD1;
            float2 uv:TEXCOORD;
            float4 screenPos:TEXCOORD2;
        };
        v2f vert(appdata_base v){
    
    
            v2f o;
            o.vertex=UnityObjectToClipPos(v.vertex);
            o.position=v.vertex;
            o.uv=v.texcoord;
            o.screenPos=ComputeScreenPos(o.vertex);
            return o;
        }
        
            sampler2D _MainTex;
            fixed4 frag (v2f i) : SV_Target
            {
    
    
                fixed3 color=tex2D(_MainTex,i.uv).rgb;
                return fixed4(color,1.0);
            }
            ENDCG
        }
    }
}

insert image description here

In fact, it is to obtain the color information of the texture, and then output it directly. The tex2D function is used here, and two parameters are passed in
: sampler2D: texture information
uv: UV value. If the uv value is (0, 0), the pixel in the lower left corner is returned. , if the uv value is (1, 1), return the upper right pixel, if the uv value is (0.5, 0.5), return the middle pixel

In the code, we stripped the alpha value and only took the three colors of rgb.
If we need to flip the image from left to right or upside down, we can do it by manipulating the coordinate value of uv, as follows:

     float2 uv=float2(1-i.uv.x,i.uv.y);
     fixed3 color=tex2D(_MainTex,uv).rgb;
     return fixed4(color,1.0);

insert image description here
Flip up and down:

  float2 uv=float2(i.uv.x,1-i.uv.y);
  fixed3 color=tex2D(_MainTex,uv).rgb;
  return fixed4(color,1.0);

insert image description here
Rotate 90°

Shader "Unlit/shader9"
{
    
     
    ///鼠标移动正方形
    Properties
    {
    
    
        _MainTex("Main Texture",2D)="white"{
    
    }
    }
    SubShader
    {
    
    
        Tags {
    
     "RenderType"="Opaque" }
        LOD 100

        Pass
        {
    
    

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag


            #include "UnityCG.cginc"
        struct v2f{
    
    
            float4 vertex:SV_POSITION;
            float4 position:TEXCOORD1;
            float2 uv:TEXCOORD;
            float4 screenPos:TEXCOORD2;
        };
        v2f vert(appdata_base v){
    
    
            v2f o;
            o.vertex=UnityObjectToClipPos(v.vertex);
            o.position=v.vertex;
            o.uv=v.texcoord;
            o.screenPos=ComputeScreenPos(o.vertex);
            return o;
        }
        float2 rotate(float2 pt,float theta,float aspect){
    
    
            float c=cos(theta);
            float s=sin(theta);
            float2x2 mat=float2x2(c,s,-s,c);
            pt.y/=aspect;
            pt=mul(pt,mat);
            pt.y*=aspect;
            return pt;
        }
            sampler2D _MainTex;
            fixed4 frag (v2f i) : SV_Target
            {
    
    
                float center=0.5;
                float2 uv=rotate(i.uv-center,UNITY_HALF_PI,2.0/1.5)+center;
                fixed3 color;
                if(uv.x<0||uv.x>1||uv.y<0||uv.y>1){
    
    
                    color=fixed3(0,0,0);
                }else{
    
    
                    color=tex2D(_MainTex,uv).rgb;
                }
                return fixed4(color,1.0);
            }
            ENDCG
        }
    }
}

A rotate function is added during the rotation process. This function mainly uses the matrix to achieve the corresponding degree theta of the rotation. The aspect parameter mainly deals with stretching.
After the rotation, there will be more of the same picture, and the judgment process is carried out here, and the extra part becomes black
insert image description here

Guess you like

Origin blog.csdn.net/qq_14942529/article/details/126095258