Skip to content

Instantly share code, notes, and snippets.

@EsProgram

EsProgram/MaskPattern.cs

Last active Jan 31, 2016
Embed
What would you like to do?
Unityでフィルター処理してみた ref: http://qiita.com/Es_Program/items/3198f0a66eb2434c4e43
X = \sum_{i=1}^{9} S_iM_i
using UnityEngine;
using System.Collections;
using System;
public class MaskPattern : MonoBehaviour {
[Tooltip("マスクパターンを実装するシェーダを持つマテリアル")]
public Material mat;
[Tooltip("マスク。3x3の部分しか使用しない。")]
public Matrix4x4 matrix;
[Tooltip("基本的にはマスク数の9、適用しないマスク値0の分の明度が必要な場合等で変更。")]
public float div = 9;
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
mat.SetFloat("_TexWidth", source.width);
mat.SetFloat("_TexHeight", source.height);
mat.SetMatrix("_Matrix", matrix);
mat.SetFloat("_Div", div);
Graphics.Blit(source, destination, mat);
}
}
Shader "Hidden/MaskPattern"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
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 = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
uniform float _TexWidth;
uniform float _TexHeight;
uniform matrix _Matrix;
uniform float _Div;
fixed4 frag (v2f i) : SV_Target
{
fixed4 ret_col;
int u, v;
for (u = -1; u < 2; ++u)
for (v = -1; v < 2; ++v)
{
float x = i.uv.x + u / _TexWidth;
float y = i.uv.y + v / _TexHeight;
ret_col += tex2D(_MainTex, float2(x, y))*_Matrix[u + 1][v + 1];
}
return ret_col / _Div;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment