Unity3D Shader實(shí)現(xiàn)掃描顯示效果
本文實(shí)例為大家分享了Unity3D Shader實(shí)現(xiàn)掃描顯示的具體代碼,供大家參考,具體內(nèi)容如下
通過Shader實(shí)現(xiàn),從左向右的掃描顯示,可自定義掃描顏色、寬度、速度。
效果圖如下

編輯器界面如下

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"
}
代碼調(diào)用如下
using UnityEngine;
using System.Collections;
public class ScanEffect : MonoBehaviour
{
//默認(rèn)掃描線的寬
[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();
}
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程(下)
這篇文章主要介紹了深入學(xué)習(xí)C#網(wǎng)絡(luò)編程之HTTP應(yīng)用編程的相關(guān)知識(shí),文中講解的非常詳細(xì),幫助大家更好的學(xué)習(xí)c#網(wǎng)絡(luò)編程,感興趣的朋友可以了解下2020-06-06
C#用websocket實(shí)現(xiàn)簡易聊天功能(客戶端)
這篇文章主要為大家詳細(xì)介紹了C#用websocket實(shí)現(xiàn)簡易聊天功能,客戶端方向,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
C#使用代碼實(shí)現(xiàn)春晚撲克牌魔術(shù)
這篇文章主要為大家詳細(xì)介紹了C#如何使用代碼實(shí)現(xiàn)龍年春晚撲克牌魔術(shù)(守歲共此時(shí)),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-02-02
winform基于異步委托實(shí)現(xiàn)多線程搖獎(jiǎng)器
這篇文章主要介紹了winform基于異步委托實(shí)現(xiàn)多線程搖獎(jiǎng)器的方法,包含了線程的運(yùn)用及隨機(jī)數(shù)的生成,需要的朋友可以參考下2014-10-10
C#中靜態(tài)方法和實(shí)例化方法的區(qū)別、使用
這篇文章主要介紹了C#中靜態(tài)方法和實(shí)例化方法的區(qū)別、使用,文中講解的非常細(xì)致,對(duì)大家的學(xué)習(xí)有所幫助,感興趣的朋友可以了解下2020-06-06
C#調(diào)用OpenXml讀取excel行數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了C#如何調(diào)用OpenXml實(shí)現(xiàn)讀取excel行數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
基于mvc5+ef6+Bootstrap框架實(shí)現(xiàn)身份驗(yàn)證和權(quán)限管理
最近剛做完一個(gè)項(xiàng)目,項(xiàng)目架構(gòu)師使用mvc5+ef6+Bootstrap,用的是vs2015,數(shù)據(jù)庫是sql server2014。下面小編把mvc5+ef6+Bootstrap項(xiàng)目心得之身份驗(yàn)證和權(quán)限管理模塊的實(shí)現(xiàn)思路分享給大家,需要的朋友可以參考下2016-06-06

