Shader效果源码(二)——OutLine轮廓+屏幕模糊
1、OutLine轮廓
原理,pass块对顶点多次渲染,像素覆盖。或者找到边缘 ,这个后期写
Shader "Hidden/OutLine轮廓"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Length("轮廓倍数",Range(1,3)) = 1
_OutLineColor("轮廓颜色",Color) = (0,0,0,1)
}
SubShader
{
// No culling or depth
//Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float _Length; //再次声明
fixed4 _OutLineColor;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
v.vertex.xy *= _Length; //放大
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
// just invert the colors
// col.rgb = 1 - col.rgb;
return _OutLineColor;
}
ENDCG
}
Pass
{
ZTest Always
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
// just invert the colors
//col.rgb = 1 - col.rgb;
return fixed4(0,1,0.5,1);
}
ENDCG
}
}
}
2、屏幕模糊
Shader "Timor/04/屏幕模糊"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Smoothing("模糊度",Range(0,0.05)) = 0.01
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float _Smoothing; //平滑度
fixed4 frag (v2f i) : SV_Target
{
float2 tempUV = i.uv;
fixed4 col = tex2D(_MainTex, i.uv);
//取上下左右的像素,做平均值
fixed4 col1 = tex2D(_MainTex,tempUV + float2(_Smoothing,0));
fixed4 col2 = tex2D(_MainTex,tempUV + float2(- _Smoothing,0));
fixed4 col3 = tex2D(_MainTex,tempUV + float2(0,_Smoothing));
fixed4 col4 = tex2D(_MainTex,tempUV + float2(0,-_Smoothing));
col = (col + col1 + col2 + col3 + col4)/5;
return col;
}
ENDCG
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 相机模糊特效
/// </summary>
public class CameraVagueEffect : MonoBehaviour {
public Material mat;
void Start () {
}
// Update is called once per frame
void Update () {
}
//函数系统调用,渲染顺序
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Graphics.Blit(source, destination,mat);
}
}