Unity Shader learning (five) mouse movement box

According to the content of the previous section, a block is created.
Now let’s move the block. The
principle is actually very simple. You only need to set the center point of the block equal to the position of the mouse.
The Shader code is as follows:

Shader "Unlit/shader6"
{
    
     
    ///鼠标移动正方形
    Properties
    {
    
    
    _MouseCenter("MouseCenter",Vector)=(0,0,0,0)
    _RectColor("RectColor",Color)=(1,1,1,1)
    }
    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;
        };
        v2f vert(appdata_base v){
    
    
            v2f o;
            o.vertex=UnityObjectToClipPos(v.vertex);
            o.position=v.vertex;
            o.uv=v.texcoord;
            return o;
        }
        //中心点
        float4 _MouseCenter;
        //颜色
        fixed4 _RectColor;
        //绘制正方形
        float rect(float2 pt,float2 size,float2 center){
    
    
            float2 p=pt-center;
            float2 halfsize=size*0.5;
            float hotz=step(-halfsize.x,p.x)-step(halfsize.x,p.x);
            float vert=step(-halfsize.y,p.y)-step(halfsize.y,p.y);
            return hotz*vert;
        }

            fixed4 frag (v2f i) : SV_Target
            {
    
    
                float2 pos=i.uv;
                float2 size=0.2;
                float inRect=rect(pos,size,_MouseCenter);
                fixed3 col=fixed3(1,1,0)*inRect;
                return fixed4(col,1.0);
            }
            ENDCG
        }
    }
}

Then you only need to control the parameter _MouseCenter, and assign a value to _MouseCenter through the unity script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MouseSquare : MonoBehaviour
{
    
    
   public Material mat;
    Vector4 mouse;
    Camera cam;
    void Start()
    {
    
    
        MeshRenderer rend = GetComponent<MeshRenderer>();
        mat = rend.material;
        mouse = new Vector4();
        mouse.z = Screen.height;
        mouse.w = Screen.width;
        cam = Camera.main;
    }

    // Update is called once per frame
    void Update()
    {
    
    
        RaycastHit hit;
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray,out hit))
        {
    
    
       
            mouse.x = hit.textureCoord.x;
            mouse.y = hit.textureCoord.y;
           // Debug.Log(hit.textureCoord);
            Debug.DrawLine(cam.transform.position, hit.point, Color.red);
        }
        mat.SetVector("_MouseCenter", mouse);
    }
}

Just drag the script on the object
insert image description here
insert image description here

Guess you like

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