Skip to content

Instantly share code, notes, and snippets.

@EsProgram
Last active January 31, 2016 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EsProgram/708cd015d19bca4b5d3b to your computer and use it in GitHub Desktop.
Save EsProgram/708cd015d19bca4b5d3b to your computer and use it in GitHub Desktop.
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