Skip to content

Instantly share code, notes, and snippets.

@RINNUXEI
Last active October 28, 2020 02:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RINNUXEI/a1d7a14297cc3233ad48289a170d32a1 to your computer and use it in GitHub Desktop.
Save RINNUXEI/a1d7a14297cc3233ad48289a170d32a1 to your computer and use it in GitHub Desktop.
放射状ブラー
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);
}
}
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