Last active
October 28, 2020 02:49
-
-
Save RINNUXEI/a1d7a14297cc3233ad48289a170d32a1 to your computer and use it in GitHub Desktop.
放射状ブラー
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
[ExecuteInEditMode] | |
public class RadialBlur : MonoBehaviour | |
{ | |
[SerializeField] private Shader _shader; | |
[Tooltip("テクスチャをサンプリングする回数、多いほど重くなる")] | |
[Range(4, 32)] public int Samples = 16; | |
[Tooltip("ブラーの中心")] | |
public Vector2 Center = new Vector2(0.5f, 0.5f); | |
[Tooltip("ブラーが中心から離れる距離")] | |
public float Offset = 1; | |
private Material _material; | |
void Awake() | |
{ | |
_material = new Material(_shader); | |
} | |
void OnRenderImage(RenderTexture source, RenderTexture dest) | |
{ | |
_material.SetFloat("_Samples", Samples); | |
_material.SetFloat("_CenterX", Center.x); | |
_material.SetFloat("_CenterY", Center.y); | |
_material.SetFloat("_Offset", Offset); | |
Graphics.Blit(source, dest, _material); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/RadialBlur" | |
{ | |
Properties | |
{ | |
_MainTex ("Texture", 2D) = "white" {} | |
_Samples("Samples", Range(4, 32)) = 16 | |
_CenterX("Center X", float) = 0.5 | |
_CenterY("Center Y", float) = 0.5 | |
_Offset("Offset", float) = 1 | |
} | |
SubShader | |
{ | |
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 _Samples; | |
float _CenterX; | |
float _CenterY; | |
float _Offset; | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float2 diff = i.uv - float2(_CenterX, _CenterY); | |
fixed4 col = fixed4(0,0,0,0); | |
for(int j = 0; j < _Samples; j++) | |
{ | |
float offset = _Offset * (j / _Samples) * length(diff); | |
col += tex2D(_MainTex, i.uv + offset * normalize(diff)); | |
} | |
col /= _Samples; | |
return col; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment