Shader "BorderLands2++"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_thr("threshold", Float) = 0.15
_tweak("???", Float) = -20.0
}
SubShader
{
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#include "UnityCG.cginc"
#pragma surface surf Lambert
#pragma target 3.0
sampler2D _MainTex;
float2 _MainTex_TexelSize;
float _thr;
float _tweak;
struct Input
{
float2 uv_MainTex;
float3 viewDir;
};
void surf (Input IN, inout SurfaceOutput o)
{
float2 off = _MainTex_TexelSize;
// Sample the neighbor pixels
float s00 = Luminance(tex2D(_MainTex, IN.uv_MainTex + float2(-off.x, -off.y)).rgb);
float s01 = Luminance(tex2D(_MainTex, IN.uv_MainTex + float2( 0, -off.y)).rgb);
float s02 = Luminance(tex2D(_MainTex, IN.uv_MainTex + float2( off.x, -off.y)).rgb);
float s10 = Luminance(tex2D(_MainTex, IN.uv_MainTex + float2(-off.x, 0)).rgb);
float s12 = Luminance(tex2D(_MainTex, IN.uv_MainTex + float2( off.x, 0)).rgb);
float s20 = Luminance(tex2D(_MainTex, IN.uv_MainTex + float2(-off.x, off.y)).rgb);
float s21 = Luminance(tex2D(_MainTex, IN.uv_MainTex + float2( 0, off.y)).rgb);
float s22 = Luminance(tex2D(_MainTex, IN.uv_MainTex + float2( off.x, off.y)).rgb);
// Sobel filter in X and Y direction
float sobelX = s00 + 2 * s10 + s20 - s02 - 2 * s12 - s22;
float sobelY = s00 + 2 * s01 + s02 - s20 - 2 * s21 - s22;
// Find edge using a threshold of 0.07 which works generally
// well for general edge detection.
float edgeSqr = (sobelX * sobelX + sobelY * sobelY);
o.Normal = half3(0.0, 0.0, 1.0);
float NdotV = dot(o.Normal, normalize(IN.viewDir));
edgeSqr = (edgeSqr - _thr * NdotV) * _tweak;
edgeSqr = saturate(edgeSqr);
//edgeSqr = (edgeSqr - _thr) * _tweak;
//edgeSqr = saturate(edgeSqr);
o.Emission = tex2D(_MainTex, IN.uv_MainTex);
o.Emission = o.Emission * edgeSqr;
/*
o.Normal = half3(0.0, 0.0, 1.0);
float NdotV = dot(o.Normal, normalize(IN.viewDir));
//mad with NdotV
NdotV = saturate(NdotV * 1.5);
NdotV = pow(NdotV, 2.0);
NdotV = clamp(NdotV, 0.0, 0.9);
o.Emission = lerp(o.Emission * edgeSqr, o.Emission, NdotV);
*/
}
ENDCG
}
Fallback "Diffuse"
}