├── README.md ├── pic ├── Bloom.jpg ├── DepthOfFieldBlend.jpg ├── Disssolve.jpg ├── Fresnel.jpg ├── GaussianBlur.jpg ├── Glass.jpg ├── OcclusionPerspective.jpg ├── Outline.jpg ├── RimLight.jpg ├── SnowScreen.jpg ├── TextureAni.jpg └── VertexAni.jpg ├── scripts ├── Bloom.cs ├── DepthOfField.cs ├── Dissolve.cs ├── GaussianBlur.cs ├── SnowScreen.cs └── WaterScreen.cs └── shader ├── Bloom.shader ├── DepthOfFieldBlend.shader ├── Dissolve.shader ├── Fresnel.shader ├── GaussianBlur.shader ├── Glass.shader ├── OcclusionPerspective.shader ├── Outline.shader ├── RimLightOff.shader ├── RimLightOn.shader ├── SnowScreen.shader ├── TextureAnimation.shader ├── VertexAnimation_1.shader ├── VertexAnimation_2.shader ├── VertexAnimation_3.shader └── WaterScreen.shader /README.md: -------------------------------------------------------------------------------- 1 | # Unity Shader 着色器特效 2 | 3 | ### 1、顶点动画 4 | 5 |

6 | 7 | ### 2、纹理动画 8 | 9 |

10 | 11 | ### 3、边缘发光 12 | 13 |

14 | 15 | ### 4、描边效果 16 | 17 |

18 | 19 | ### 5、遮挡透视效果 20 | 21 |

22 | 23 | ### 6、菲尼尔效果 24 | 25 |

26 | 27 | ### 7、高斯模糊 28 | 29 |

30 | 31 | ### 8、Bloom 效果 32 | 33 |

34 | 35 | ### 9、景深 36 | 37 |

38 | 39 | ### 10、积雪效果 40 | 41 |

42 | 43 | ### 11、浴室玻璃 44 | 45 |

46 | 47 | ### 12、消融效果 48 | 49 |

-------------------------------------------------------------------------------- /pic/Bloom.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/Bloom.jpg -------------------------------------------------------------------------------- /pic/DepthOfFieldBlend.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/DepthOfFieldBlend.jpg -------------------------------------------------------------------------------- /pic/Disssolve.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/Disssolve.jpg -------------------------------------------------------------------------------- /pic/Fresnel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/Fresnel.jpg -------------------------------------------------------------------------------- /pic/GaussianBlur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/GaussianBlur.jpg -------------------------------------------------------------------------------- /pic/Glass.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/Glass.jpg -------------------------------------------------------------------------------- /pic/OcclusionPerspective.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/OcclusionPerspective.jpg -------------------------------------------------------------------------------- /pic/Outline.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/Outline.jpg -------------------------------------------------------------------------------- /pic/RimLight.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/RimLight.jpg -------------------------------------------------------------------------------- /pic/SnowScreen.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/SnowScreen.jpg -------------------------------------------------------------------------------- /pic/TextureAni.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/TextureAni.jpg -------------------------------------------------------------------------------- /pic/VertexAni.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/pic/VertexAni.jpg -------------------------------------------------------------------------------- /scripts/Bloom.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | //编辑状态下也运行 5 | [ExecuteInEditMode] 6 | [RequireComponent(typeof(Camera))] 7 | public class Bloom: MonoBehaviour 8 | { 9 | public Material _Material;//图像处理材质 10 | 11 | public Color colorMix = new Color(1, 1, 1, 1);//特效的颜色 12 | 13 | [Range(0.0f, 1.0f)] 14 | public float threshold = 0.25f;//Bloom效果范围 15 | 16 | [Range(0.0f, 2.5f)] 17 | public float intensity = 0.75f;//Bloom特效强度 18 | 19 | [Range(0.2f, 1.0f)] 20 | public float BlurSize = 1.0f;//模糊范围与质量 21 | 22 | //降分辨率 23 | public int downSample = 2; 24 | 25 | void OnRenderImage(RenderTexture source, RenderTexture destination) 26 | { 27 | if (_Material) 28 | { 29 | _Material.SetColor("_ColorMix", colorMix); 30 | _Material.SetVector("_Parameter", new Vector4(BlurSize * 1.5f, 0.0f, intensity, 0.8f - threshold)); 31 | 32 | //申请RenderTexture,RT的分辨率按照downSample降低 33 | RenderTexture rt1 = RenderTexture.GetTemporary(source.width >> downSample, source.height >> downSample, 0, source.format); 34 | RenderTexture rt2 = RenderTexture.GetTemporary(source.width >> downSample, source.height >> downSample, 0, source.format); 35 | 36 | //第一步:根据阀值提取图像 37 | Graphics.Blit(source, rt1, _Material, 0); 38 | 39 | //第二步:高斯模糊 40 | //第一次高斯模糊,设置offsets,竖向模糊 41 | _Material.SetVector("_offsets", new Vector4(0, 1, 0, 0)); 42 | Graphics.Blit(rt1, rt2, _Material, 1); 43 | //第二次高斯模糊,设置offsets,横向模糊 44 | _Material.SetVector("_offsets", new Vector4(1, 0, 0, 0)); 45 | Graphics.Blit(rt2, rt1, _Material, 1); 46 | 47 | //第三步:与原图像混合 48 | _Material.SetTexture("_Bloom", rt1); 49 | Graphics.Blit(source, destination, _Material, 2); 50 | 51 | //释放申请的两块RenderBuffer内容 52 | RenderTexture.ReleaseTemporary(rt1); 53 | RenderTexture.ReleaseTemporary(rt2); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /scripts/DepthOfField.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | using System.Collections; 4 | 5 | //编辑状态下也运行 6 | [ExecuteInEditMode] 7 | [RequireComponent(typeof(Camera))] 8 | public class DepthOfField : MonoBehaviour 9 | { 10 | //模糊处理材质 11 | public Material _Material; 12 | //混合处理材质 13 | public Material _BlendMaterial; 14 | //模糊半径 15 | public float BlurRadius = 1.0f; 16 | //降分辨率 17 | public int downSample = 2; 18 | //迭代次数 19 | public int iteration = 1; 20 | //景深距离 21 | public float dist = 0; 22 | 23 | void OnEnable() 24 | { 25 | GetComponent().depthTextureMode |= DepthTextureMode.Depth; 26 | } 27 | 28 | void OnRenderImage(RenderTexture source, RenderTexture destination) 29 | { 30 | if (_Material) 31 | { 32 | //申请RenderTexture,RT的分辨率按照downSample降低 33 | RenderTexture rt1 = RenderTexture.GetTemporary(source.width >> downSample, source.height >> downSample, 0, source.format); 34 | RenderTexture rt2 = RenderTexture.GetTemporary(source.width >> downSample, source.height >> downSample, 0, source.format); 35 | 36 | //直接将原图拷贝到降分辨率的RT上 37 | Graphics.Blit(source, rt1); 38 | 39 | //进行迭代高斯模糊 40 | for (int i = 0; i < iteration; i++) 41 | { 42 | //第一次高斯模糊,设置offsets,竖向模糊 43 | _Material.SetVector("_offsets", new Vector4(0, BlurRadius, 0, 0)); 44 | Graphics.Blit(rt1, rt2, _Material); 45 | //第二次高斯模糊,设置offsets,横向模糊 46 | _Material.SetVector("_offsets", new Vector4(BlurRadius, 0, 0, 0)); 47 | Graphics.Blit(rt2, rt1, _Material); 48 | } 49 | Camera camera = GetComponent(); 50 | float focalDist = Mathf.Clamp(dist, camera.nearClipPlane, camera.farClipPlane); 51 | focalDist = focalDist / (camera.farClipPlane - camera.nearClipPlane); 52 | Debug.Log(focalDist); 53 | //设置混合材质的纹理 54 | _BlendMaterial.SetTexture("_BlurTex", rt1); 55 | _BlendMaterial.SetFloat("_Dist", focalDist); 56 | Graphics.Blit(source, destination, _BlendMaterial); 57 | 58 | //释放申请的两块RenderBuffer内容 59 | RenderTexture.ReleaseTemporary(rt1); 60 | RenderTexture.ReleaseTemporary(rt2); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /scripts/Dissolve.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class Dissolve : MonoBehaviour { 6 | 7 | public Material DissolveMaterial;//消融材质 8 | void Start () { 9 | DissolveMaterial.SetFloat("_Threshold", 0f); 10 | } 11 | 12 | void Update () { 13 | DissolveMaterial.SetFloat("_Threshold", DissolveMaterial.GetFloat("_Threshold")+0.005f); 14 | if (DissolveMaterial.GetFloat("_Threshold") >= 1.5) 15 | { 16 | DissolveMaterial.SetFloat("_Threshold", 0f); 17 | transform.GetComponent().Play(); 18 | } 19 | } 20 | 21 | private void OnDisable() 22 | { 23 | DissolveMaterial.SetFloat("_Threshold", 0f); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /scripts/GaussianBlur.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | //编辑状态下也运行 5 | [ExecuteInEditMode] 6 | [RequireComponent(typeof(Camera))] 7 | public class GaussianBlur : MonoBehaviour 8 | { 9 | 10 | public Material _Material; 11 | //模糊半径 12 | public float BlurRadius = 1.0f; 13 | //降分辨率 14 | public int downSample = 2; 15 | //迭代次数 16 | public int iteration = 1; 17 | 18 | void OnRenderImage(RenderTexture source, RenderTexture destination) 19 | { 20 | if (_Material) 21 | { 22 | //申请RenderTexture,RT的分辨率按照downSample降低 23 | RenderTexture rt1 = RenderTexture.GetTemporary(source.width >> downSample, source.height >> downSample, 0, source.format); 24 | RenderTexture rt2 = RenderTexture.GetTemporary(source.width >> downSample, source.height >> downSample, 0, source.format); 25 | 26 | //直接将原图拷贝到降分辨率的RT上 27 | Graphics.Blit(source, rt1); 28 | 29 | //进行迭代高斯模糊 30 | for (int i = 0; i < iteration; i++) 31 | { 32 | //第一次高斯模糊,设置offsets,竖向模糊 33 | _Material.SetVector("_offsets", new Vector4(0, BlurRadius, 0, 0)); 34 | Graphics.Blit(rt1, rt2, _Material); 35 | //第二次高斯模糊,设置offsets,横向模糊 36 | _Material.SetVector("_offsets", new Vector4(BlurRadius, 0, 0, 0)); 37 | Graphics.Blit(rt2, rt1, _Material); 38 | } 39 | 40 | //将结果输出 41 | Graphics.Blit(rt1, destination); 42 | 43 | //释放申请的两块RenderBuffer内容 44 | RenderTexture.ReleaseTemporary(rt1); 45 | RenderTexture.ReleaseTemporary(rt2); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /scripts/SnowScreen.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; using System.Collections; 2 | 3 | [ExecuteInEditMode] public class SnowScreen : MonoBehaviour { 4 | public Texture2D SnowTexture; 5 | 6 | public Color SnowColor = Color.white; 7 | 8 | public float SnowTextureScale = 0.1f; 9 | 10 | [Range(0, 1)] public float BottomThreshold = 0f; [Range(0, 1)] public float TopThreshold = 1f; 11 | 12 | public Material _Material; 13 | 14 | void OnEnable() { 15 | 16 | GetComponent().depthTextureMode |= DepthTextureMode.DepthNormals; } 17 | 18 | void OnRenderImage(RenderTexture src, RenderTexture dest) 19 | { 20 | // set shader properties 21 | _Material.SetMatrix("_CamToWorld", GetComponent().cameraToWorldMatrix);//摄像机到世界坐标系的转换矩阵 22 | _Material.SetColor("_SnowColor", SnowColor);//雪的颜色 _Material.SetFloat("_BottomThreshold", BottomThreshold); _Material.SetFloat("_TopThreshold", TopThreshold); _Material.SetTexture("_SnowTex", SnowTexture);//雪纹理 _Material.SetFloat("_SnowTexScale", SnowTextureScale); 23 | 24 | Graphics.Blit(src, dest, _Material); } } -------------------------------------------------------------------------------- /scripts/WaterScreen.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | [ExecuteInEditMode] 5 | public class WaterScreen : MonoBehaviour 6 | { 7 | //-------------------变量声明部分------------------- 8 | #region Variables 9 | 10 | public Material _Material;//当前的材质 11 | 12 | //时间变量和素材图的定义 13 | private float TimeX = 1.0f;//时间变量 14 | public Texture2D ScreenWaterDropTex;//屏幕水滴的素材图 15 | 16 | //可以在编辑器中调整的参数值 17 | [Range(5, 64), Tooltip("溶解度")] 18 | public float Distortion = 8.0f; 19 | [Range(0, 7), Tooltip("水滴在X坐标上的尺寸")] 20 | public float SizeX = 1f; 21 | [Range(0, 7), Tooltip("水滴在Y坐标上的尺寸")] 22 | public float SizeY = 0.5f; 23 | [Range(0, 10), Tooltip("水滴的流动速度")] 24 | public float DropSpeed = 3.6f; 25 | 26 | //用于参数调节的中间变量 27 | public static float ChangeDistortion; 28 | public static float ChangeSizeX; 29 | public static float ChangeSizeY; 30 | public static float ChangeDropSpeed; 31 | #endregion 32 | //-------------------------------------【OnRenderImage()函数】------------------------------------ 33 | // 说明:此函数在当完成所有渲染图片后被调用,用来渲染图片后期效果 34 | //-------------------------------------------------------------------------------------------------------- 35 | void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture) 36 | { 37 | //着色器实例不为空,就进行参数设置 38 | if (_Material != null) 39 | { 40 | //时间的变化 41 | TimeX += Time.deltaTime; 42 | //时间大于100,便置0,保证可以循环 43 | if (TimeX > 100) TimeX = 0; 44 | 45 | //设置Shader中其他的外部变量 46 | _Material.SetFloat("_CurTime", TimeX); 47 | _Material.SetFloat("_Distortion", Distortion); 48 | _Material.SetFloat("_SizeX", SizeX); 49 | _Material.SetFloat("_SizeY", SizeY); 50 | _Material.SetFloat("_DropSpeed", DropSpeed); 51 | _Material.SetTexture("_ScreenWaterDropTex", ScreenWaterDropTex); 52 | 53 | //拷贝源纹理到目标渲染纹理,加上我们的材质效果 54 | Graphics.Blit(sourceTexture, destTexture, _Material); 55 | } 56 | //着色器实例为空,直接拷贝屏幕上的效果。此情况下是没有实现屏幕特效的 57 | else 58 | { 59 | //直接拷贝源纹理到目标渲染纹理 60 | Graphics.Blit(sourceTexture, destTexture); 61 | } 62 | 63 | 64 | } 65 | 66 | } -------------------------------------------------------------------------------- /shader/Bloom.shader: -------------------------------------------------------------------------------- 1 | //Bloom效果 2 | Shader "Custom/Bloom" 3 | { 4 | Properties 5 | { 6 | _MainTex("Base (RGB)", 2D) = "white" {} 7 | _Bloom ("Bloom (RGB)", 2D) = "black" {} 8 | } 9 | CGINCLUDE 10 | #include "UnityCG.cginc" 11 | 12 | sampler2D _MainTex; 13 | sampler2D _Bloom; 14 | 15 | fixed4 _ColorMix; 16 | half4 _MainTex_TexelSize; 17 | fixed4 _Parameter; 18 | float4 _offsets; 19 | 20 | //第一步:根据阀值提取图像结构体 21 | struct v2f_withMaxCoords { 22 | half4 pos : SV_POSITION; 23 | half2 uv2[5] : TEXCOORD0; 24 | }; 25 | //第一步:根据阀值提取图像顶点着色器 26 | //在vert函数里对uv坐标做了四次偏移,对原像素周围临近的像素采样 27 | v2f_withMaxCoords vertMax (appdata_img v) 28 | { 29 | v2f_withMaxCoords o; 30 | o.pos = UnityObjectToClipPos (v.vertex); 31 | o.uv2[0] = v.texcoord + _MainTex_TexelSize.xy * half2(1.5,1.5); 32 | o.uv2[1] = v.texcoord + _MainTex_TexelSize.xy * half2(-1.5,1.5); 33 | o.uv2[2] = v.texcoord + _MainTex_TexelSize.xy * half2(-1.5,-1.5); 34 | o.uv2[3] = v.texcoord + _MainTex_TexelSize.xy * half2(1.5,-1.5); 35 | o.uv2[4] = v.texcoord ; 36 | return o; 37 | } 38 | //第一步:根据阀值提取图像片元着色器 39 | fixed4 fragMax ( v2f_withMaxCoords i ) : COLOR 40 | { 41 | fixed4 color = tex2D(_MainTex, i.uv2[4]); 42 | color = max(color, tex2D (_MainTex, i.uv2[0])); 43 | color = max(color, tex2D (_MainTex, i.uv2[1])); 44 | color = max(color, tex2D (_MainTex, i.uv2[2])); 45 | color = max(color, tex2D (_MainTex, i.uv2[3])); 46 | return saturate(color - _Parameter.w); 47 | } 48 | 49 | 50 | //第二步:高斯模糊结构体 51 | //blur结构体,从blur的vert函数传递到frag函数的参数 52 | struct v2f_blur 53 | { 54 | float4 pos : SV_POSITION; //顶点位置 55 | float2 uv : TEXCOORD0; //纹理坐标 56 | float4 uv01 : TEXCOORD1; //一个vector4存储两个纹理坐标 57 | float4 uv23 : TEXCOORD2; //一个vector4存储两个纹理坐标 58 | float4 uv45 : TEXCOORD3; //一个vector4存储两个纹理坐标 59 | }; 60 | //第二步:高斯模糊顶点着色器 61 | v2f_blur vert_blur(appdata_img v) 62 | { 63 | v2f_blur o; 64 | o.pos = UnityObjectToClipPos(v.vertex); 65 | //uv坐标 66 | o.uv = v.texcoord.xy; 67 | 68 | //计算一个偏移值,offset可能是(0,1,0,0)也可能是(1,0,0,0)这样就表示了横向或者竖向取像素周围的点 69 | _offsets *= _MainTex_TexelSize.xyxy * _Parameter.x; 70 | 71 | o.uv01 = v.texcoord.xyxy + _offsets.xyxy * float4(1, 1, -1, -1); 72 | o.uv23 = v.texcoord.xyxy + _offsets.xyxy * float4(1, 1, -1, -1) * 2.0; 73 | o.uv45 = v.texcoord.xyxy + _offsets.xyxy * float4(1, 1, -1, -1) * 3.0; 74 | 75 | return o; 76 | } 77 | //第二步:高斯模糊片元着色器 78 | fixed4 frag_blur(v2f_blur i) : SV_Target 79 | { 80 | fixed4 color = fixed4(0,0,0,0); 81 | //将像素本身以及像素左右(或者上下,取决于vertex shader传进来的uv坐标)像素值的加权平均 82 | color += 0.4 * tex2D(_MainTex, i.uv); 83 | color += 0.15 * tex2D(_MainTex, i.uv01.xy); 84 | color += 0.15 * tex2D(_MainTex, i.uv01.zw); 85 | color += 0.10 * tex2D(_MainTex, i.uv23.xy); 86 | color += 0.10 * tex2D(_MainTex, i.uv23.zw); 87 | color += 0.05 * tex2D(_MainTex, i.uv45.xy); 88 | color += 0.05 * tex2D(_MainTex, i.uv45.zw); 89 | return color; 90 | } 91 | 92 | //第三步:混合 93 | struct v2f_mix { 94 | half4 pos : SV_POSITION; 95 | half4 uv : TEXCOORD0; 96 | }; 97 | v2f_mix vertMix (appdata_img v) 98 | { 99 | v2f_mix o; 100 | o.pos = UnityObjectToClipPos (v.vertex); 101 | o.uv = v.texcoord.xyxy; 102 | #if UNITY_UV_STARTS_AT_TOP//SHADER_API_D3D9 103 | if (_MainTex_TexelSize.y < 0.0) 104 | o.uv.w = 1.0 - o.uv.w; 105 | #endif 106 | return o; 107 | } 108 | fixed4 fragMix( v2f_mix i ) : COLOR 109 | { 110 | fixed4 color = tex2D(_MainTex, i.uv.xy); 111 | color += tex2D(_Bloom, i.uv.zw)*_Parameter.z*_ColorMix; 112 | return color; 113 | } 114 | 115 | ENDCG 116 | 117 | SubShader { 118 | ZTest Always ZWrite Off Cull Off 119 | //0 120 | Pass { 121 | CGPROGRAM 122 | #pragma vertex vertMax 123 | #pragma fragment fragMax 124 | ENDCG 125 | } 126 | //1 127 | Pass { 128 | CGPROGRAM 129 | #pragma vertex vert_blur 130 | #pragma fragment frag_blur 131 | ENDCG 132 | } 133 | //2 134 | Pass { 135 | CGPROGRAM 136 | #pragma vertex vertMix 137 | #pragma fragment fragMix 138 | ENDCG 139 | } 140 | } 141 | } -------------------------------------------------------------------------------- /shader/DepthOfFieldBlend.shader: -------------------------------------------------------------------------------- 1 | //景深混合 2 | Shader "Custom/DepthOfFieldBlend" 3 | { 4 | Properties { 5 | _MainTex ("Base (RGB)", 2D) = "" {} 6 | } 7 | Subshader { 8 | Pass { 9 | ZTest Always Cull Off ZWrite Off 10 | CGPROGRAM 11 | #pragma vertex vert 12 | #pragma fragment frag 13 | #include "UnityCG.cginc" 14 | struct v2f { 15 | float4 pos : POSITION; 16 | float2 uv : TEXCOORD0; 17 | }; 18 | // 摄像机所见纹理 19 | sampler2D _MainTex; 20 | // 摄像机深度纹理 21 | sampler2D _CameraDepthTexture; 22 | // 模糊纹理 23 | sampler2D _BlurTex; 24 | // 景深距离 25 | float _Dist; 26 | v2f vert (appdata_img v) { 27 | v2f o; 28 | o.pos = UnityObjectToClipPos(v.vertex); 29 | o.uv.xy = v.texcoord.xy; 30 | return o; 31 | } 32 | half4 frag (v2f i) : COLOR { 33 | // 摄像机纹理 34 | half4 ori = tex2D(_MainTex,i.uv); 35 | // 模糊纹理 36 | half4 blur = tex2D(_BlurTex,i.uv); 37 | // 获取摄像机深度值颜色值,取rgb都可以 38 | float dep = tex2D(_CameraDepthTexture,i.uv).r; 39 | // 深度Z缓存,从摄像机到最远平截面[0,1] 40 | dep = Linear01Depth(dep); 41 | // (1-dep) * ori + dep * blur 42 | // 所以靠近摄像机的不模糊,越远越模糊 43 | return lerp(ori,blur,dep - _Dist); 44 | 45 | } 46 | ENDCG 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /shader/Dissolve.shader: -------------------------------------------------------------------------------- 1 | //消融效果 2 | Shader "Custom/Dissolve" 3 | { 4 | Properties 5 | { 6 | _MainTex ("Texture", 2D) = "white" {}//主纹理 7 | _NoiseTex("Noise", 2D) = "white" {}//噪声纹理 8 | _Threshold("Threshold", Range(0.0, 1.0)) = 0.5//消融阀值 9 | _EdgeLength("Edge Length", Range(0.0, 0.2)) = 0.1//边缘宽度 10 | _EdgeFirstColor("First Edge Color", Color) = (1,1,1,1)//边缘颜色值1 11 | _EdgeSecondColor("Second Edge Color", Color) = (1,1,1,1)//边缘颜色值2 12 | } 13 | SubShader 14 | { 15 | Tags { "Queue"="Geometry" "RenderType"="Opaque" }//标签 16 | 17 | Pass 18 | { 19 | Cull Off //要渲染背面保证效果正确 20 | 21 | CGPROGRAM 22 | #pragma vertex vert//声明顶点着色器 23 | #pragma fragment frag//声明片元着色器 24 | 25 | #include "UnityCG.cginc" 26 | 27 | struct a2v//顶点着色器输入结构体 28 | { 29 | float4 vertex : POSITION; 30 | float2 uv : TEXCOORD0; 31 | }; 32 | 33 | struct v2f 34 | { 35 | float4 vertex : SV_POSITION; 36 | float2 uvMainTex : TEXCOORD0; 37 | float2 uvNoiseTex : TEXCOORD1; 38 | }; 39 | 40 | sampler2D _MainTex; 41 | float4 _MainTex_ST; 42 | sampler2D _NoiseTex; 43 | float4 _NoiseTex_ST; 44 | float _Threshold; 45 | float _EdgeLength; 46 | fixed4 _EdgeFirstColor; 47 | fixed4 _EdgeSecondColor; 48 | 49 | v2f vert (a2v v)//顶点着色器 50 | { 51 | v2f o; 52 | o.vertex = UnityObjectToClipPos(v.vertex);//将顶点坐标变化到剪裁坐标系 53 | o.uvMainTex = TRANSFORM_TEX(v.uv, _MainTex);//进行主纹理坐标变换 54 | o.uvNoiseTex = TRANSFORM_TEX(v.uv, _NoiseTex);//进行噪声纹理坐标变换 55 | return o; 56 | } 57 | 58 | fixed4 frag (v2f i) : SV_Target//片元着色器 59 | { 60 | fixed cutout = tex2D(_NoiseTex, i.uvNoiseTex).r;//获取灰度图的R通道 61 | clip(cutout - _Threshold);//根据消融阀值裁剪片元 62 | 63 | float degree = saturate((cutout - _Threshold) / _EdgeLength);//规范化 64 | fixed4 edgeColor = lerp(_EdgeFirstColor, _EdgeSecondColor, degree);//对颜色值进行插值 65 | 66 | fixed4 col = tex2D(_MainTex, i.uvMainTex);//对主纹理进行采样 67 | 68 | fixed4 finalColor = lerp(edgeColor, col, degree);//对边缘颜色与片元颜色进行插值 69 | return fixed4(finalColor.rgb, 1); 70 | } 71 | ENDCG 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /shader/Fresnel.shader: -------------------------------------------------------------------------------- 1 | Shader "Custom/Fresnel" { 2 | Properties{ 3 | _Color("Color Tint", Color) = (1, 1, 1, 1)//主颜色 4 | _ReflectColor("Reflection Color", Color) = (1, 1, 1, 1)//反射光颜色 5 | _RefractColor("Refraction Color", Color) = (1, 1, 1, 1)//折射光颜色 6 | _RefractRatio("Refraction Ratio", Range(0.1, 1)) = 0.5//折射率 7 | _Cubemap("Reflection Cubemap", Cube) = "_Skybox" {}//立方体纹理 8 | _MaxH("Max Value", Range(0, 1)) = 0.7//入射角大于此值,仅计算折射 9 | _MinH("Min Value", Range(0, 1)) = 0.2//入射角小于此值,仅计算发射 10 | } 11 | SubShader{ 12 | Tags{ "RenderType" = "Opaque" "Queue" = "Geometry" } 13 | Pass{ 14 | Tags{ "LightMode" = "ForwardBase" } 15 | CGPROGRAM 16 | #pragma vertex vert//声明顶点着色器 17 | #pragma fragment frag//声明片元着色器 18 | #include "Lighting.cginc"//导入"Lighting"工具包 19 | #include "AutoLight.cginc"//导入"AutoLight"工具包 20 | fixed4 _Color;//定义主颜色变量 21 | fixed4 _ReflectColor;//定义反射光颜色变量 22 | fixed4 _RefractColor;//定义折射光颜色变量 23 | fixed _RefractRatio;//定义折射率变量 24 | samplerCUBE _Cubemap;//定义立方体纹理对象 25 | fixed _MaxH; 26 | fixed _MinH; 27 | 28 | struct a2v {//定义顶点着色器输入结构体 29 | float4 vertex : POSITION;//顶点位置 30 | float3 normal : NORMAL;//法向量 31 | }; 32 | struct v2f {//定义顶点着色器输出结构体 33 | float4 pos : SV_POSITION;//顶点在剪裁坐标系中的坐标 34 | float3 worldPos : TEXCOORD0;//顶点在世界空间中的坐标 35 | fixed3 worldNormal : TEXCOORD1;//世界空间中的法线 36 | fixed3 worldViewDir : TEXCOORD2;//世界空间中的视线方向 37 | }; 38 | v2f vert(a2v v) { 39 | v2f o; 40 | 41 | o.pos = UnityObjectToClipPos(v.vertex);//从模型坐标系到剪裁坐标系 42 | 43 | o.worldNormal = UnityObjectToWorldNormal(v.normal);//世界坐标系下的法向量 44 | 45 | o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;//世界坐标系下的顶点位置 46 | 47 | o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos);//视线方向 48 | 49 | return o; 50 | } 51 | 52 | fixed4 frag(v2f i) : SV_Target{ 53 | fixed3 worldNormal = normalize(i.worldNormal); 54 | fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos)); 55 | fixed3 worldViewDir = normalize(i.worldViewDir); 56 | 57 | fixed3 vTextureCoord;//用于进行立方图纹理采样的向量 58 | fixed3 reflection;//反射采样结果 59 | fixed3 refraction;//折射采样结果 60 | fixed3 color;//最终颜色 61 | 62 | //计算视线向量与法向量的余弦值(有错) 63 | fixed testValue = abs(dot(worldViewDir,worldNormal)); 64 | if (testValue > _MaxH) {//余弦值大于MaxH仅折射 65 | vTextureCoord = refract(-worldViewDir, worldNormal, _RefractRatio); 66 | refraction = texCUBE(_Cubemap, vTextureCoord).rgb * _RefractColor.rgb; 67 | color = refraction; 68 | } 69 | else if (testValue > _MinH && testValue < _MaxH) {//折射与反射融合 70 | vTextureCoord = reflect(-worldViewDir, worldNormal); 71 | reflection = texCUBE(_Cubemap, vTextureCoord).rgb * _ReflectColor.rgb; 72 | vTextureCoord = refract(-worldViewDir, worldNormal, _RefractRatio); 73 | refraction = texCUBE(_Cubemap, vTextureCoord).rgb * _RefractColor.rgb; 74 | fixed ratio = (testValue - _MinH) / (_MaxH - _MinH); 75 | color = refraction * ratio + reflection * (1.0 - ratio); 76 | } 77 | else {//只有反射 78 | vTextureCoord = reflect(-worldViewDir, worldNormal); 79 | reflection = texCUBE(_Cubemap, vTextureCoord).rgb * _ReflectColor.rgb; 80 | color = reflection; 81 | } 82 | 83 | return fixed4(color, 0.5); 84 | } 85 | 86 | ENDCG 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /shader/GaussianBlur.shader: -------------------------------------------------------------------------------- 1 | //高斯模糊 2 | Shader "Custom/GaussianBlur" 3 | { 4 | 5 | Properties 6 | { 7 | _MainTex("Base (RGB)", 2D) = "white" {} 8 | } 9 | //通过CGINCLUDE我们可以预定义一些下面在Pass中用到的struct以及函数, 10 | //这样在pass中只需要设置渲染状态以及调用函数,shader更加简洁明了 11 | CGINCLUDE 12 | #include "UnityCG.cginc" 13 | 14 | //blur结构体,从blur的vert函数传递到frag函数的参数 15 | struct v2f_blur 16 | { 17 | float4 pos : SV_POSITION; //顶点位置 18 | float2 uv : TEXCOORD0; //纹理坐标 19 | float4 uv01 : TEXCOORD1; //一个vector4存储两个纹理坐标 20 | float4 uv23 : TEXCOORD2; //一个vector4存储两个纹理坐标 21 | float4 uv45 : TEXCOORD3; //一个vector4存储两个纹理坐标 22 | }; 23 | 24 | //shader中用到的参数 25 | sampler2D _MainTex; 26 | //XX_TexelSize,XX纹理的像素相关大小width,height对应纹理的分辨率,x = 1/width, y = 1/height, z = width, w = height 27 | float4 _MainTex_TexelSize; 28 | //给一个offset,这个offset可以在外面设置,是我们设置横向和竖向blur的关键参数 29 | float4 _offsets; 30 | 31 | //vertex shader 32 | v2f_blur vert_blur(appdata_img v) 33 | { 34 | v2f_blur o; 35 | o.pos = UnityObjectToClipPos(v.vertex); 36 | //uv坐标 37 | o.uv = v.texcoord.xy; 38 | 39 | //计算一个偏移值,offset可能是(0,1,0,0)也可能是(1,0,0,0)这样就表示了横向或者竖向取像素周围的点 40 | _offsets *= _MainTex_TexelSize.xyxy; 41 | 42 | //由于uv可以存储4个值,所以一个uv保存两个vector坐标,_offsets.xyxy * float4(1,1,-1,-1)可能表示(0,1,0-1),表示像素上下两个 43 | //坐标,也可能是(1,0,-1,0),表示像素左右两个像素点的坐标,下面*2.0,*3.0同理 44 | o.uv01 = v.texcoord.xyxy + _offsets.xyxy * float4(1, 1, -1, -1); 45 | o.uv23 = v.texcoord.xyxy + _offsets.xyxy * float4(1, 1, -1, -1) * 2.0; 46 | o.uv45 = v.texcoord.xyxy + _offsets.xyxy * float4(1, 1, -1, -1) * 3.0; 47 | 48 | return o; 49 | } 50 | 51 | //fragment shader 52 | fixed4 frag_blur(v2f_blur i) : SV_Target 53 | { 54 | fixed4 color = fixed4(0,0,0,0); 55 | //将像素本身以及像素左右(或者上下,取决于vertex shader传进来的uv坐标)像素值的加权平均 56 | color += 0.4 * tex2D(_MainTex, i.uv); 57 | color += 0.15 * tex2D(_MainTex, i.uv01.xy); 58 | color += 0.15 * tex2D(_MainTex, i.uv01.zw); 59 | color += 0.10 * tex2D(_MainTex, i.uv23.xy); 60 | color += 0.10 * tex2D(_MainTex, i.uv23.zw); 61 | color += 0.05 * tex2D(_MainTex, i.uv45.xy); 62 | color += 0.05 * tex2D(_MainTex, i.uv45.zw); 63 | return color; 64 | } 65 | 66 | ENDCG 67 | 68 | //开始SubShader 69 | SubShader 70 | { 71 | //开始一个Pass 72 | Pass 73 | { 74 | //后处理效果一般都是这几个状态 75 | ZTest Always 76 | Cull Off 77 | ZWrite Off 78 | 79 | //使用上面定义的vertex和fragment shader 80 | CGPROGRAM 81 | #pragma vertex vert_blur 82 | #pragma fragment frag_blur 83 | ENDCG 84 | } 85 | 86 | } 87 | //后处理效果一般不给fallback,如果不支持,不显示后处理即可 88 | } -------------------------------------------------------------------------------- /shader/Glass.shader: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/shader/Glass.shader -------------------------------------------------------------------------------- /shader/OcclusionPerspective.shader: -------------------------------------------------------------------------------- 1 | // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 2 | 3 | Shader "Custom/OcclusionPerspective" { 4 | Properties { 5 | _MainTex ("Albedo (RGB)", 2D) = "white" {} 6 | _PColor("Perspective Color", Color) = (1,1,1,0.5) 7 | } 8 | SubShader { 9 | Tags{"Queue" = "Geometry+1000" "RenderType" = "Opaque"} 10 | Pass{ 11 | ZWrite off 12 | Lighting off 13 | Ztest Greater 14 | Blend SrcAlpha OneMinusSrcAlpha 15 | CGPROGRAM 16 | #pragma vertex vert 17 | #pragma fragment frag 18 | #include "UnityCG.cginc" 19 | 20 | float4 _PColor; 21 | 22 | struct v2f{ 23 | float4 pos : SV_POSITION; 24 | }; 25 | 26 | v2f vert(appdata_img v){ 27 | v2f o; 28 | o.pos = UnityObjectToClipPos(v.vertex); 29 | return o; 30 | } 31 | 32 | float4 frag(v2f i) : COLOR{ 33 | return _PColor; 34 | } 35 | ENDCG 36 | } 37 | 38 | Pass{ 39 | ZWrite on 40 | ZTest less 41 | 42 | CGPROGRAM 43 | #pragma vertex vert 44 | #pragma fragment frag 45 | #include "UnityCG.cginc" 46 | 47 | sampler2D _MainTex; 48 | //float4 _MainTex_ST; 49 | 50 | struct v2f{ 51 | float4 pos : SV_POSITION; 52 | float2 uv : TEXCOORD0; 53 | }; 54 | 55 | v2f vert(appdata_img v){ 56 | v2f o; 57 | o.pos = UnityObjectToClipPos(v.vertex); 58 | o.uv = v.texcoord; 59 | return o; 60 | } 61 | 62 | float4 frag(v2f i) : COLOR{ 63 | float4 col = tex2D(_MainTex, i.uv); 64 | return col; 65 | } 66 | ENDCG 67 | } 68 | } 69 | FallBack "Diffuse" 70 | } 71 | -------------------------------------------------------------------------------- /shader/Outline.shader: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/str818/Unity-Shader-Effect/889a1f33768e0108fda3746b109f9380f84a4a32/shader/Outline.shader -------------------------------------------------------------------------------- /shader/RimLightOff.shader: -------------------------------------------------------------------------------- 1 | //边缘发光关闭对比效果 2 | Shader "Custom/RimLightOff" { 3 | Properties { 4 | _MainTex ("Base (RGB)", 2D) = "white" {} 5 | /* _Color ("Main Color", Color) = (1,1,1,1) 6 | _RimColor ("Rim Color", Color) = (1, 1, 1, 1) 7 | _RimWidth ("Rim Width", float) = 0.9 */ 8 | } 9 | SubShader { 10 | Pass { 11 | Lighting Off 12 | CGPROGRAM 13 | #pragma vertex vert 14 | #pragma fragment frag 15 | #include "UnityCG.cginc" 16 | 17 | struct appdata 18 | { 19 | float4 vertex : POSITION; 20 | float2 texcoord : TEXCOORD0; 21 | }; 22 | 23 | struct v2f 24 | { 25 | float4 pos : SV_POSITION; 26 | float2 uv : TEXCOORD0; 27 | }; 28 | 29 | 30 | v2f vert (appdata_base v) { 31 | v2f o; 32 | o.pos = UnityObjectToClipPos (v.vertex); 33 | o.uv = v.texcoord.xy; 34 | return o; 35 | } 36 | 37 | uniform sampler2D _MainTex; 38 | uniform fixed4 _Color; 39 | 40 | fixed4 frag(v2f i) : COLOR { 41 | fixed4 texcol = tex2D(_MainTex, i.uv); 42 | return texcol; 43 | } 44 | ENDCG 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /shader/RimLightOn.shader: -------------------------------------------------------------------------------- 1 | //边缘发光效果 2 | Shader "Custom/RimLightOn" { 3 | Properties { 4 | _MainTex ("Base (RGB)", 2D) = "white" {}//主纹理 5 | _Color ("Main Color", Color) = (1, 1, 1, 1)//主颜色值 6 | _RimColor ("Rim Color", Color) = (1, 1, 1, 1)//边缘发光颜色 7 | _RimWidth ("Rim Width", Float) = 0.8//边缘发光宽度 8 | } 9 | SubShader { 10 | Pass { 11 | Lighting Off//关闭光照效果 12 | CGPROGRAM 13 | #pragma vertex vert//声明顶点着色器 14 | #pragma fragment frag//声明片元着色器 15 | #include "UnityCG.cginc"//导入UnityCG工具包 16 | 17 | struct a2f{//顶点着色器输入结构体 18 | float4 pos : POSITION; 19 | float3 normal : NORMAL; 20 | float2 uv : TEXCOORD0; 21 | }; 22 | 23 | struct v2f{//顶点着色器输出结构体 24 | float4 pos : SV_POSITION; 25 | float2 uv : TEXCOORD0; 26 | fixed3 color : COLOR; 27 | }; 28 | 29 | fixed4 _RimColor; 30 | float _RimWidth; 31 | sampler2D _MainTex; 32 | fixed4 _Color; 33 | 34 | v2f vert (a2f v) {//顶点着色器 35 | v2f o;//定义输出结构体 36 | o.pos = UnityObjectToClipPos (v.pos);//将顶点坐标从物体坐标系转换到剪裁坐标系 37 | float3 viewDir = normalize(ObjSpaceViewDir(v.pos));//获取顶点对应的视线方向 38 | float dotValue = 1 - dot(v.normal, viewDir);//构造平滑差值的参数 39 | 40 | o.color = smoothstep(1 - _RimWidth, 1.0, dotValue);//根据因子计算边缘发光强度 41 | o.color *= _RimColor;//混合边缘发光颜色 42 | o.uv = v.uv.xy;//将纹理坐标传递到片元着色器 43 | return o; 44 | } 45 | 46 | fixed4 frag(v2f i) : COLOR {//片元着色器 47 | fixed4 texcol = tex2D(_MainTex, i.uv);//纹理采样 48 | texcol *= _Color;//混合主颜色 49 | texcol.rgb += i.color;//混合边缘发光片元的颜色值 50 | return texcol; 51 | } 52 | ENDCG 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /shader/SnowScreen.shader: -------------------------------------------------------------------------------- 1 | Shader "Unlit/SnowScreen"{ 2 | Properties 3 | { 4 | _MainTex ("Texture", 2D) = "white" {} 5 | } 6 | SubShader 7 | { 8 | Pass 9 | { 10 | Cull Off ZWrite Off ZTest Always 11 | CGPROGRAM 12 | #pragma vertex vert 13 | #pragma fragment frag 14 | 15 | #include "UnityCG.cginc" 16 | 17 | struct v2f 18 | { 19 | float2 uv : TEXCOORD0; 20 | float4 vertex : SV_POSITION; 21 | }; 22 | 23 | sampler2D _MainTex;//主纹理 24 | sampler2D _CameraDepthNormalsTexture;//摄像机深度法线纹理 25 | float4x4 _CamToWorld;//摄像机到世界坐标系的转换矩阵 26 | 27 | sampler2D _SnowTex;//雪纹理 28 | float _SnowTexScale; 29 | 30 | half4 _SnowColor;//雪的颜色 31 | 32 | fixed _BottomThreshold; 33 | fixed _TopThreshold; 34 | 35 | v2f vert (appdata_img v) 36 | { 37 | v2f o; 38 | o.vertex = UnityObjectToClipPos(v.vertex); 39 | o.uv = v.texcoord.xy; 40 | return o; 41 | } 42 | 43 | half3 frag (v2f i) : SV_Target 44 | { 45 | half3 normal;//法线 46 | float depth;//深度值 47 | 48 | DecodeDepthNormal(tex2D(_CameraDepthNormalsTexture, i.uv), depth, normal);//获取深度值与法线 49 | normal = mul((float3x3)_CamToWorld, normal);//将法线从摄像机坐标系转换到世界坐标系 50 | 51 | // find out snow amount 52 | half snowAmount = normal.g;//获取法线沿Y方向的分量 53 | half scale = (_BottomThreshold + 1 - _TopThreshold) / 1 + 1;//计算厚度因子 54 | snowAmount = saturate( (snowAmount - _BottomThreshold) * scale);//计算雪的厚度 55 | 56 | // find out snow color 57 | float2 p11_22 = float2(unity_CameraProjection._11, unity_CameraProjection._22); 58 | float3 vpos = float3( (i.uv * 2 - 1) / p11_22, -1) * depth; 59 | float4 wpos = mul(_CamToWorld, float4(vpos, 1)); 60 | 61 | wpos += float4(_WorldSpaceCameraPos, 0) / _ProjectionParams.z; 62 | 63 | wpos *= _SnowTexScale * _ProjectionParams.z; 64 | half3 snowColor = tex2D(_SnowTex, wpos.xz) * _SnowColor; 65 | 66 | // get color and lerp to snow texture 67 | half4 col = tex2D(_MainTex, i.uv); 68 | return lerp(col, snowColor, snowAmount); 69 | //return half3(1 ,0,0); 70 | } 71 | ENDCG 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /shader/TextureAnimation.shader: -------------------------------------------------------------------------------- 1 | //序列帧动画 2 | Shader "Custom/TextureAnimation" { 3 | Properties{ 4 | _MainTex ("Image Sequence", 2D) = "white" {}//主纹理 5 | _HorizontalAmount ("Horizontal Amount", Float) = 4//水平图像数量 6 | _VerticalAmount ("Vertical Amount", Float) = 4//竖直图像数量 7 | _Speed ("Speed", Range(1, 20)) = 1//序列帧播放速度 8 | } 9 | SubShader{ 10 | //设置透明所需标签 11 | Tags{"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"} 12 | 13 | Pass{ 14 | 15 | ZWrite Off//关闭深度写入 16 | Blend SrcAlpha OneMinusSrcAlpha//开启混合 17 | 18 | CGPROGRAM 19 | #pragma vertex vert//声明顶点着色器 20 | #pragma fragment frag//声明片元着色器 21 | 22 | #include "UnityCG.cginc" 23 | struct a2v { 24 | float4 vertex : POSITION; 25 | float2 texcoord : TEXCOORD0; 26 | }; 27 | 28 | struct v2f { 29 | float4 pos : SV_POSITION; 30 | float2 uv : TEXCOORD0; 31 | }; 32 | 33 | sampler2D _MainTex; 34 | float4 _MainTex_ST; 35 | float _HorizontalAmount; 36 | float _VerticalAmount; 37 | float _Speed; 38 | 39 | 40 | v2f vert(a2v v){ 41 | v2f o; 42 | o.pos = UnityObjectToClipPos(v.vertex);//将顶点坐标转化到剪裁空间坐标系 43 | o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);//进行纹理坐标变换 44 | return o; 45 | } 46 | 47 | fixed4 frag(v2f i) :SV_Target{ 48 | float time = floor(_Time.y * _Speed); 49 | float row = floor(time / _HorizontalAmount); 50 | float column = time - row * _HorizontalAmount; 51 | 52 | half2 uv = i.uv + half2(column, -row); 53 | uv.x /= _HorizontalAmount; 54 | uv.y /= _VerticalAmount; 55 | 56 | fixed4 c = tex2D(_MainTex, uv); 57 | return c; 58 | 59 | } 60 | ENDCG 61 | } 62 | 63 | } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /shader/VertexAnimation_1.shader: -------------------------------------------------------------------------------- 1 | //x方向动画 2 | Shader "Custom/VertexAnimation_1" { 3 | Properties { 4 | _MainTex ("Albedo (RGB)", 2D) = "white" {} 5 | _WidthSpan("WidthSpan", Range(4,5)) = 4.5 6 | _StartAngle("StartAngle", Range(1,5)) = 1 7 | _Speed("Speed", Range(1,15)) = 5 8 | } 9 | SubShader { 10 | Pass{ 11 | CGPROGRAM 12 | 13 | #pragma vertex vert//声明顶点着色器 14 | #pragma fragment frag//声明片元着色器 15 | 16 | #include "UnityCG.cginc" 17 | 18 | struct a2v { 19 | float4 pos : POSITION; 20 | float2 uv : TEXCOORD0; 21 | }; 22 | 23 | struct v2f { 24 | float4 pos : SV_POSITION; 25 | float2 uv : TEXCOORD0; 26 | }; 27 | 28 | sampler2D _MainTex; 29 | float _WidthSpan; 30 | float _StartAngle; 31 | float _Speed; 32 | 33 | v2f vert(a2v v){ 34 | v2f o; 35 | float angleSpanH = 2 * 3.14159265; 36 | float startX = -_WidthSpan / 2.0;//起始X坐标 37 | 38 | float currAngleX = _StartAngle * _Time.y * _Speed + ((v.pos.x - startX) / _WidthSpan) * angleSpanH; 39 | float tz = sin(currAngleX) * 6; 40 | 41 | o.pos = UnityObjectToClipPos(float4(v.pos.x, v.pos.y, tz, 1)); 42 | o.uv = v.uv; 43 | return o; 44 | } 45 | 46 | fixed4 frag(v2f i) :SV_Target{ 47 | fixed4 c = tex2D(_MainTex,i.uv); 48 | return c; 49 | } 50 | 51 | ENDCG 52 | } 53 | } 54 | FallBack "Diffuse" 55 | } 56 | -------------------------------------------------------------------------------- /shader/VertexAnimation_2.shader: -------------------------------------------------------------------------------- 1 | //斜向下方向动画 2 | Shader "Custom/VertexAnimation_2" { 3 | Properties { 4 | _MainTex ("Albedo (RGB)", 2D) = "white" {} 5 | _WidthSpan("WidthSpan", Range(4,5)) = 4.2 6 | _StartAngle("StartAngle", Range(1,4)) = 1 7 | _Speed("Speed", Range(1,15)) = 1 8 | } 9 | SubShader { 10 | Pass{ 11 | CGPROGRAM 12 | 13 | #pragma vertex vert//声明顶点着色器 14 | #pragma fragment frag//声明片元着色器 15 | 16 | #include "UnityCG.cginc" 17 | 18 | struct a2v { 19 | float4 pos : POSITION; 20 | float2 uv : TEXCOORD0; 21 | }; 22 | 23 | struct v2f { 24 | float4 pos : SV_POSITION; 25 | float2 uv : TEXCOORD0; 26 | }; 27 | 28 | sampler2D _MainTex; 29 | float _WidthSpan; 30 | float _StartAngle; 31 | float _Speed; 32 | 33 | v2f vert(a2v v){ 34 | v2f o; 35 | float angleSpanH = 2 * 3.14159265; 36 | float startX = -_WidthSpan / 2.0;//起始X坐标 37 | 38 | float currAngleX = _StartAngle * _Time.y * _Speed + ((v.pos.x - startX) / _WidthSpan) * angleSpanH /** _Speed*/; 39 | 40 | float HeightSpan = 0.618 * _WidthSpan; 41 | float startY = -HeightSpan / 2.0;//起始Y坐标 42 | 43 | float currAngleY = _Time.y * _Speed + ((v.pos.y - startY) / HeightSpan) * angleSpanH ; 44 | 45 | float tz = sin(currAngleX - currAngleY) * 4; 46 | 47 | o.pos = UnityObjectToClipPos(float4(v.pos.x, v.pos.y, tz, 1)); 48 | o.uv = v.uv; 49 | return o; 50 | } 51 | 52 | fixed4 frag(v2f i) :SV_Target{ 53 | fixed4 c = tex2D(_MainTex,i.uv); 54 | return c; 55 | } 56 | 57 | 58 | ENDCG 59 | } 60 | } 61 | FallBack "Diffuse" 62 | } 63 | -------------------------------------------------------------------------------- /shader/VertexAnimation_3.shader: -------------------------------------------------------------------------------- 1 | //xy方向动画 2 | Shader "Custom/VertexAnimation_3" { 3 | Properties { 4 | _MainTex ("Albedo (RGB)", 2D) = "white" {} 5 | _WidthSpan("WidthSpan", Range(4,5)) = 4.2 6 | _StartAngle("StartAngle", Range(1,4)) = 1 7 | _Speed("Speed", Range(1,15)) = 1 8 | } 9 | SubShader { 10 | Pass{ 11 | CGPROGRAM 12 | 13 | #pragma vertex vert//声明顶点着色器 14 | #pragma fragment frag//声明片元着色器 15 | 16 | #include "UnityCG.cginc" 17 | 18 | struct a2v { 19 | float4 pos : POSITION; 20 | float2 uv : TEXCOORD0; 21 | }; 22 | 23 | struct v2f { 24 | float4 pos : SV_POSITION; 25 | float2 uv : TEXCOORD0; 26 | }; 27 | 28 | float4 _Color; 29 | sampler2D _MainTex; 30 | float4 _MainTex_ST; 31 | float _WidthSpan; 32 | float _StartAngle; 33 | float _Speed; 34 | 35 | v2f vert(a2v v){ 36 | v2f o; 37 | float angleSpanH = 2 * 3.14159265; 38 | float startX = -_WidthSpan / 2.0;//起始X坐标 39 | 40 | float currAngleX = _StartAngle * _Time.y * _Speed + ((v.pos.x - startX) / _WidthSpan) * angleSpanH /** _Speed*/; 41 | 42 | float HeightSpan = 0.618 * _WidthSpan; 43 | float startY = -HeightSpan / 2.0;//起始Y坐标 44 | 45 | float currAngleY = _Time.y * _Speed + ((v.pos.y - startY) / HeightSpan) * angleSpanH ; 46 | 47 | float tzX = sin(currAngleX) * 4; 48 | float tzY = sin(currAngleY) * 4; 49 | 50 | o.pos = UnityObjectToClipPos(float4(v.pos.x, v.pos.y, tzX + tzY, 1)); 51 | o.uv = v.uv; 52 | return o; 53 | } 54 | 55 | fixed4 frag(v2f i) :SV_Target{ 56 | fixed4 c = tex2D(_MainTex,i.uv); 57 | return c; 58 | } 59 | 60 | 61 | ENDCG 62 | } 63 | } 64 | FallBack "Diffuse" 65 | } 66 | -------------------------------------------------------------------------------- /shader/WaterScreen.shader: -------------------------------------------------------------------------------- 1 | // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 2 | 3 | Shader "Custom/WaterScreen" { 4 | Properties{ 5 | //主纹理 6 | _MainTex ("Base (RGB)", 2D) = "white" {} 7 | //屏幕水滴的素材图 8 | _ScreenWaterDropTex ("Base (RGB)", 2D) = "white" {} 9 | //当前时间 10 | _CurTime ("Time", Range(0.0, 1.0)) = 1.0 11 | //X坐标上的水滴尺寸 12 | _SizeX ("SizeX", Range(0.0, 1.0)) = 1.0 13 | //Y坐标上的水滴尺寸 14 | _SizeY ("SizeY", Range(0.0, 1.0)) = 1.0 15 | //水滴的流动速度 16 | _DropSpeed ("Speed", Range(0.0, 10.0)) = 1.0 17 | //溶解度 18 | _Distortion ("_Distortion", Range(0.0, 1.0)) = 0.87 19 | } 20 | SubShader{ 21 | Pass { 22 | //设置深度测试模式:渲染所有像素.等同于关闭透明度测试(AlphaTest Off) 23 | ZTest Always 24 | 25 | //===========开启CG着色器语言编写模块=========== 26 | CGPROGRAM 27 | 28 | //编译指令:告知编译器顶点和片段着色函数的名称 29 | #pragma vertex vert 30 | #pragma fragment frag 31 | #pragma fragmentoption ARB_precision_hint_fastest 32 | //编译指令: 指定着色器编译目标为Shader Model 3.0 33 | #pragma target 3.0 34 | 35 | //包含辅助CG头文件 36 | #include "UnityCG.cginc" 37 | 38 | //外部变量的声明 39 | uniform sampler2D _MainTex; 40 | uniform sampler2D _ScreenWaterDropTex; 41 | uniform float _CurTime; 42 | uniform float _DropSpeed; 43 | uniform float _SizeX; 44 | uniform float _SizeY; 45 | uniform float _Distortion; 46 | uniform float2 _MainTex_TexelSize; 47 | 48 | //顶点输入结构 49 | struct vertexInput 50 | { 51 | float4 vertex : POSITION;//顶点位置 52 | float2 texcoord : TEXCOORD0;//一级纹理坐标 53 | }; 54 | 55 | //顶点输出结构 56 | struct v2f 57 | { 58 | half2 texcoord : TEXCOORD0;//一级纹理坐标 59 | float4 vertex : SV_POSITION;//像素位置 60 | }; 61 | 62 | //--------------------------------【顶点着色函数】----------------------------- 63 | // 输入:顶点输入结构体 64 | // 输出:顶点输出结构体 65 | //--------------------------------------------------------------------------------- 66 | v2f vert(vertexInput Input) 67 | { 68 | v2f o; 69 | o.vertex = UnityObjectToClipPos(Input.vertex); 70 | //输出的纹理坐标也就是输入的纹理坐标 71 | o.texcoord = Input.texcoord; 72 | return o; 73 | } 74 | 75 | //--------------------------------【片段着色函数】----------------------------- 76 | // 输入:顶点输出结构体 77 | // 输出:float4型的颜色值 78 | //--------------------------------------------------------------------------------- 79 | fixed4 frag(v2f Input) : COLOR 80 | { 81 | //【1】获取顶点的坐标值 82 | float2 uv = Input.texcoord.xy; 83 | 84 | //【2】解决平台差异的问题。校正方向,若和规定方向相反,则将速度反向并加1 85 | #if UNITY_UV_STARTS_AT_TOP 86 | if (_MainTex_TexelSize.y < 0) 87 | _DropSpeed = 1 - _DropSpeed; 88 | #endif 89 | 90 | //【3】设置三层水流效果,按照一定的规律在水滴纹理上分别进行取样 91 | float3 rainTex1 = tex2D(_ScreenWaterDropTex, float2(uv.x * 1.15* _SizeX, (uv.y* _SizeY *1.1) + _CurTime* _DropSpeed *0.15)).rgb / _Distortion; 92 | float3 rainTex2 = tex2D(_ScreenWaterDropTex, float2(uv.x * 1.25* _SizeX - 0.1, (uv.y *_SizeY * 1.2) + _CurTime *_DropSpeed * 0.2)).rgb / _Distortion; 93 | float3 rainTex3 = tex2D(_ScreenWaterDropTex, float2(uv.x* _SizeX *0.9, (uv.y *_SizeY * 1.25) + _CurTime * _DropSpeed* 0.032)).rgb / _Distortion; 94 | 95 | //【4】整合三层水流效果的颜色信息,存于finalRainTex中 96 | float2 finalRainTex = uv.xy - (rainTex1.xy - rainTex2.xy - rainTex3.xy) / 3; 97 | 98 | //【5】按照finalRainTex的坐标信息,在主纹理上进行采样 99 | float3 finalColor = tex2D(_MainTex, float2(finalRainTex.x, finalRainTex.y)).rgb; 100 | 101 | //【6】返回加上alpha分量的最终颜色值 102 | return fixed4(finalColor, 1.0); 103 | 104 | 105 | } 106 | 107 | //===========结束CG着色器语言编写模块=========== 108 | ENDCG 109 | } 110 | } 111 | } --------------------------------------------------------------------------------