小編這次要給大家分享的是Unity3D Shader如何實現(xiàn)掃描顯示效果,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
創(chuàng)新互聯(lián)成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供成都網(wǎng)站建設(shè)、網(wǎng)站制作網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計,成都響應(yīng)式網(wǎng)站建設(shè)公司,網(wǎng)頁設(shè)計師打造企業(yè)風(fēng)格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:028-86922220
通過Shader實現(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();
}
}
}看完這篇關(guān)于Unity3D Shader如何實現(xiàn)掃描顯示效果的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。
新聞標(biāo)題:Unity3DShader如何實現(xiàn)掃描顯示效果
分享鏈接:http://chinadenli.net/article42/goichc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、面包屑導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)、手機網(wǎng)站建設(shè)、網(wǎng)站排名、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)