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