Unity3D Shader实现扫描显示效果
作者:星空不语 时间:2023-03-16 22:51:39
本文实例为大家分享了Unity3D Shader实现扫描显示的具体代码,供大家参考,具体内容如下
通过Shader实现,从左向右的扫描显示,可自定义扫描颜色、宽度、速度。
效果图如下
编辑器界面如下
Shader源码如下
Shader "XM/ScanEffect"
{
Properties
{
_MainTex("Main Tex", 2D) = "white"{}
_lineColor("Line Color", Color) = (0,0,0,0)
_lineWidth("Line width", Range(0, 1.0)) = 0.1
_rangeX("Range X", Range(0,1.0)) = 1.0
}
SubShader
{
Tags {
"Queue" = "Transparent"
}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Cull back
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _lineColor;
float _lineWidth;
float _rangeX;
struct a2v
{
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(a2v v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_TARGET
{
fixed4 col = tex2D(_MainTex, i.uv);
if(i.uv.x > _rangeX)
{
clip(-1);
}
else if (i.uv.x > _rangeX - _lineWidth)
{
float offsetX = i.uv.x - _rangeX +_lineWidth;
fixed xAlpha = offsetX / _lineWidth;
col = col * (1 - xAlpha) + _lineColor * xAlpha;
}
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
代码调用如下
using UnityEngine;
using System.Collections;
public class ScanEffect : MonoBehaviour
{
//默认扫描线的宽
[Range(0,1)]
public float _defaultLineW = 0.2f;
//扫描的速度
[Range(0, 1)]
public float _showSpeed = 0.02f;
private MeshRenderer _render;
private void Awake()
{
_render = GetComponent<MeshRenderer>();
SetX(0);
SetLineWidth(0);
}
public void SetLineWidth(float val)
{
_render.material.SetFloat("_lineWidth", val);
}
public void SetX(float val)
{
_render.material.SetFloat("_rangeX", val);
}
public void Show()
{
StopCoroutine("Showing");
StartCoroutine("Showing");
}
public void Hide()
{
StopCoroutine("Showing");
SetX(0);
SetLineWidth(0);
}
private IEnumerator Showing()
{
float deltaX = 0;
float deltaWidth = _defaultLineW;
SetX(deltaX);
SetLineWidth(deltaWidth);
while (true)
{
if (deltaX != 1)
{
deltaX = Mathf.Clamp01(deltaX + _showSpeed);
SetX(deltaX);
}
else
{
if (deltaWidth != 0)
{
deltaWidth = Mathf.Clamp01(deltaWidth - _showSpeed);
SetLineWidth(deltaWidth);
}
else
{
break;
}
}
yield return new WaitForEndOfFrame();
}
}
public void OnGUI()
{
if (GUILayout.Button("Show"))
{
Show();
}
if (GUILayout.Button("Hide"))
{
Hide();
}
}
}
来源:https://blog.csdn.net/u012741077/article/details/53386209
标签:Unity3D,Shader,扫描
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Security基于JWT实现SSO单点登录详解
2022-02-28 10:56:55
![](https://img.aspxhome.com/file/2023/5/71585_0s.jpg)
基于spring 方法级缓存的多种实现
2022-12-14 18:31:38
Mybatis Plus使用XML编写动态sql的超简易方法
2022-02-20 01:49:16
![](https://img.aspxhome.com/file/2023/2/77172_0s.jpg)
JAVA使用JDBC连接oracle数据库的详细过程
2021-11-20 19:20:19
![](https://img.aspxhome.com/file/2023/4/63414_0s.png)
C#实现将程序运行信息写入日志的方法
2021-11-22 22:30:09
![](https://img.aspxhome.com/file/2023/1/87001_0s.png)
java中Memcached的使用实例(包括与Spring整合)
2021-11-28 13:29:06
springboot中使用Feign整合nacos,gateway进行微服务之间的调用方法
2021-07-12 07:00:28
![](https://img.aspxhome.com/file/2023/4/69304_0s.png)
Java、JavaScript、Oracle、MySQL中实现的MD5加密算法分享
2022-08-24 02:42:11
C#反射内存的处理分析
2022-04-30 00:56:22
Mybatis-Plus设置全局或者局部ID自增的实现
2021-11-27 00:30:03
![](https://img.aspxhome.com/file/2023/8/84328_0s.jpg)
Spring Cloud Alibaba实现服务的无损下线功能(案例讲解)
2022-07-05 08:14:25
![](https://img.aspxhome.com/file/2023/1/80161_0s.jpg)
Android实时文件夹创建方法
2023-11-30 03:13:42
![](https://img.aspxhome.com/file/2023/1/116151_0s.jpg)
Java创建ZIP压缩文件的方法
2022-10-05 05:05:23
Java 接口和抽象类的区别详解
2021-07-26 13:30:21
Java中流的有关知识点详解
2022-07-21 15:38:21
Java Spring5学习之JdbcTemplate详解
2023-11-25 20:17:23
![](https://img.aspxhome.com/file/2023/2/60412_0s.png)
Java调用CXF WebService接口的两种方式实例
2023-11-09 02:25:11
![](https://img.aspxhome.com/file/2023/7/58987_0s.jpg)
Android开发组件化架构设计原理到实战
2023-06-14 12:51:13
![](https://img.aspxhome.com/file/2023/5/98675_0s.jpg)
java代码执行字符串中的逻辑运算方法
2023-11-29 12:13:06
举例讲解Java的Spring框架中AOP程序设计方式的使用
2022-11-21 21:22:15