国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Unity3D Shader實現掃描顯示效果

2020-01-24 00:08:12
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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(); } }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁海县| 三都| 新民市| 行唐县| 长乐市| 临朐县| 蓝山县| 平顺县| 正阳县| 二连浩特市| 台山市| 平遥县| 西吉县| 奉贤区| 北流市| 河南省| 富民县| 柯坪县| 绵竹市| 宜都市| 辉县市| 楚雄市| 安阳县| 虎林市| 高邑县| 兴城市| 威信县| 门源| 永春县| 竹山县| 诸城市| 阳谷县| 普格县| 金川县| 洪洞县| 宜兰县| 盐源县| 房山区| 宜丰县| 田东县| 政和县|