当前位置:网站首页>Unity Shader学习(五)鼠标移动方块
Unity Shader学习(五)鼠标移动方块
2022-07-20 16:48:00 【ToDoNothing】
根据上一节的内容,创建了一个方块
现在我们来移动该方块
原理其实也很简单,只需要将方块的中心点位置等于鼠标位置即可
Shader代码如下:
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
}
}
}
然后只需要控制_MouseCenter这个参数,通过unity脚本对_MouseCenter进行赋值
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);
}
}
脚本拖在物体上即可
边栏推荐
- 微信小程序 22 recommend和songDetail初步搭建
- 《程序设计基础》 第十章 函数与程序结构 7-1 圆形体体积计算器 (20 分)
- Wechat applet 25 NPM module and pubsubjs realize page communication
- sqlilabs less-26~26a
- 【云原生】DevOps(四):集成Sonar Qube
- C中的连接符##
- 需研究的东东
- 《程序设计基础》 第十章 函数与程序结构 6-8 递归求逆序数 (20 分)
- Wechat applet 20 improve video page ①
- 《程序设计基础》 第十章 函数与程序结构 6-11 递归计算P函数 (15 分)
猜你喜欢
Uniapp applet adds custom check box style
Unity spritemask implementation
2021-09-16
Michael Bronstein 系列长文:迈向几何深度学习(之二)——感知器事件
2021-9-18
山东大学、北邮、哈工大| Multimodal Dialog Systems with Dual Knowledge-enhanced Generative Pretrained Language Model(具有双重知识增强生成预训练语言模型的多模态对话系统)
Network Security Learning (IX) comprehensive experiment & PKI
微信小程序 19 视频列表
"Hisense's B-side" technology exhibition opens! Hisense B2B represents the first collective appearance of products!
Leetcode 展望2021
随机推荐
"Fundamentals of program design" Chapter 10 function and program structure 7-1 circular volume calculator (20 points)
2021-9-21
Django queries the queryset object data of MySQL database and converts it into JSON string
《程序设计基础》 第十章 函数与程序结构 6-8 递归求逆序数 (20 分)
Wechat applet 05 obtains basic user information
Wechat applet 23 plays music page
XCTF-ms0_01
网络安全学习(三)基本DOS命令
HTB-Lame
VIM development environment configuration
Improvement of wechat applet 24 playing music page ①
关于PCDet的问题:Can not find file kitti_dbinfos_train.pkl
sqlilabs less-26~26a
sqlilabs less-27
Leetcode 展望2021
【云原生】DevOps(四):集成Sonar Qube
期货开户云上面可以开户吗?在上面开户安全吗?
《程序设计基础》 第十章 函数与程序结构 6-13 分治法求解金块问题 (20 分)
网络安全学习(一)虚拟机
Network Security Learning (VI) DNS deployment and security