Last active
October 14, 2018 11:37
-
-
Save TheStoneBook/f11772496afab8585ab959202f04265b to your computer and use it in GitHub Desktop.
【Unity】ソーベルフィルターでエッジ検出【Shader】
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 "Unlit/SobelEdge" | |
{ | |
Properties | |
{ | |
_MainTex ("Texture", 2D) = "white" {} | |
_Width("Width",float)=1920 | |
_Height("Height",float)=1080 | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" } | |
LOD 100 | |
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; | |
}; | |
sampler2D _MainTex; | |
float _Width; | |
float _Height; | |
float gray(fixed4 c) | |
{ | |
return 0.2126*c.r+0.7152*c.g+0.0722*c.b; | |
} | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.uv = v.uv; | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float dx=1/_Width; | |
float dy=1/_Height; | |
float c00=gray(tex2D(_MainTex,i.uv+float2(-dx,-dy))); | |
float c01=gray(tex2D(_MainTex,i.uv+float2(0,-dy))); | |
float c02=gray(tex2D(_MainTex,i.uv+float2(dx,-dy))); | |
float c10=gray(tex2D(_MainTex,i.uv+float2(-dx,0))); | |
float c12=gray(tex2D(_MainTex,i.uv+float2(dx,0))); | |
float c20=gray(tex2D(_MainTex,i.uv+float2(-dx,dy))); | |
float c21=gray(tex2D(_MainTex,i.uv+float2(0,dy))); | |
float c22=gray(tex2D(_MainTex,i.uv+float2(dx,dy))); | |
float sx=-1.0*c00+-2.0*c10+-1.0*c20+1.0*c02+2.0*c12+1.0*c22; | |
float sy=1.0*c00+2.0*c01+1.0*c02+-1.0*c20+-2.0*c21+-1.0*c22; | |
return sqrt(sx*sx+sy*sy); | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment