当前位置:网站首页>Unity shader learning (V) mouse moving box
Unity shader learning (V) mouse moving box
2022-07-21 11:47:00 【ToDoNothing】
According to the previous section , Created a square
Now let's move the box
The principle is very simple , Just make the central point of the square equal to the mouse position
Shader The code is as follows :
Shader "Unlit/shader6"
{
/// Mouse move square
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;
}
// Center point
float4 _MouseCenter;
// Color
fixed4 _RectColor;
// Draw a square
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 just control _MouseCenter This parameter , adopt unity Script pair _MouseCenter Assign a value
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);
}
}
Drag the script onto the object
边栏推荐
- 微信小程序 21 完善视频页②
- Wechat applet 19 video list
- 《程序设计基础》 第十章 函数与程序结构 6-12 有序表的简单增删查操作 (20 分)
- sqlilabs less-26~26a
- "Fundamentals of program design" Chapter 10 function and program structure 6-8 recursion for reverse order number (20 points)
- 【云原生】DevOps(四):集成Sonar Qube
- Unity Shader学习(五)鼠标移动方块
- AIX挂载NFS写入效率低的解决方案
- 让软件开发民主化的低代码
- mysql数据库 if buf[0] == 251: # \xfb IndexError: bytearray index out of range
猜你喜欢
Netflix、麻省理工学院 | Uncertainty in Contrastive Learning: On the Predictability of Downstream Performance(对比学习中的不确定性:关于下游性能的可预测性)
DEBUG:Generate Bitstream失败
XCTF-ms0_01
山东大学、北邮、哈工大| Multimodal Dialog Systems with Dual Knowledge-enhanced Generative Pretrained Language Model(具有双重知识增强生成预训练语言模型的多模态对话系统)
微信小程序 22 recommend和songDetail初步搭建
水泽-信息收集自动化工具安装
Try to understand the essence of low code platform design from another angle
Uniapp applet adds custom check box style
微信小程序 21 完善视频页②
微信小程序 19 视频列表
随机推荐
微信小程序 22 recommend和songDetail初步搭建
MongoDB-复制(副本集)
Acmstreamopen return value problem
《程序设计基础》 第十章 函数与程序结构 6-5 递归求简单交错幂级数的部分和 (15 分)
Why this$ refs[formName]. Validate ((valid) = > {} invalid (no response)
微信小程序 05 获取用户基本信息
DEBUG:Generate Bitstream失败
2021-09-16
关于PCDet的问题:Can not find file kitti_dbinfos_train.pkl
How to configure multiple sites using Apache Web server
When antd Table + tooltip, the tooltip cannot move with the cell -- getpopupcontainer changes the tooltip floating layer rendering parent node
试着换个角度理解低代码平台设计的本质
Disassembly of Huawei P30 Pro: compact structure, fine workmanship, exposure of core device suppliers!
Intelligent science innovation lecture hall | Institute of automation he Huiguang: Research on deep learning brain mechanism based on visual information encoding and decoding
Leetcode 展望2021
sqlilabs less-27
力扣每日一题-第40天-643. 字数组最大平均数Ⅰ
使用Room所遇到的 Execution failed for task ‘:app:kaptDebugKotlin‘
2021-09-17
416. Segmentation and subsets