Created
January 12, 2013 12:30
-
-
Save Eugeny/4517834 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 "Custom/HumanEye" { | |
Properties { | |
_MainTex ("Base (RGB)", 2D) = "white" {} | |
} | |
SubShader { | |
Tags { "RenderType"="Opaque" } | |
LOD 200 | |
CGINCLUDE | |
#pragma fragmentoption ARB_precision_hint_fastest | |
#pragma target 3.0 | |
#include "UnityCG.cginc" | |
sampler2D _CameraDepthNormalsTexture; | |
float4 _CameraDepthNormalsTexture_ST; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
sampler2D _NoiseTex; | |
float4 _NoiseTex_ST; | |
float4 _Randomness; | |
uniform float _BlurDistance, _LuminanceThreshold, _CamDepth, _Noise, _NoiseScale; | |
inline half4 blur (sampler2D tex, float2 uv, float dist) { | |
#define BLUR_SAMPLE_COUNT 16 | |
const float3 RAND_SAMPLES[26] = { | |
float3(0.2196607,0.9032637,0.2254677), | |
float3(0.05916681,0.2201506,-0.1430302), | |
float3(-0.4152246,0.1320857,0.7036734), | |
float3(-0.3790807,0.1454145,0.100605), | |
float3(0.3149606,-0.1294581,0.7044517), | |
float3(-0.1108412,0.2162839,0.1336278), | |
float3(0.658012,-0.4395972,-0.2919373), | |
float3(0.5377914,0.3112189,0.426864), | |
float3(-0.2752537,0.07625949,-0.1273409), | |
float3(-0.1915639,-0.4973421,-0.3129629), | |
float3(-0.2634767,0.5277923,-0.1107446), | |
float3(0.8242752,0.02434147,0.06049098), | |
float3(0.06262707,-0.2128643,-0.03671562), | |
float3(-0.1795662,-0.3543862,0.07924347), | |
float3(0.06039629,0.24629,0.4501176), | |
float3(-0.7786345,-0.3814852,-0.2391262), | |
float3(0.2792919,0.2487278,-0.05185341), | |
float3(0.1841383,0.1696993,-0.8936281), | |
float3(-0.3479781,0.4725766,-0.719685), | |
float3(-0.1365018,-0.2513416,0.470937), | |
float3(0.1280388,-0.563242,0.3419276), | |
float3(-0.4800232,-0.1899473,0.2398808), | |
float3(0.6389147,0.1191014,-0.5271206), | |
float3(0.1932822,-0.3692099,-0.6060588), | |
float3(-0.3465451,-0.1654651,-0.6746758), | |
float3(0.2448421,-0.1610962,0.1289366), | |
}; | |
half4 result = 0; | |
for (int s = 0; s < BLUR_SAMPLE_COUNT; ++s) | |
result += tex2D(tex, uv + RAND_SAMPLES[s].xy * dist); | |
result /= BLUR_SAMPLE_COUNT; | |
return result; | |
} | |
ENDCG | |
// Main Pass | |
Pass { | |
CGPROGRAM | |
#pragma vertex main_vert | |
#pragma fragment main_frag | |
#define DEPTH_BLUR_START 3 | |
#define FAR_BLUR_START 40 | |
#define FAR_BLUR_LENGTH 20 | |
struct v2f { | |
float4 pos : POSITION; | |
float2 uv : TEXCOORD0; | |
float2 uv_depth : TEXCOORD1; | |
}; | |
v2f main_vert (appdata_img v) | |
{ | |
v2f o; | |
o.pos = mul (UNITY_MATRIX_MVP, v.vertex); | |
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex); | |
o.uv_depth = TRANSFORM_TEX(v.texcoord, _CameraDepthNormalsTexture); | |
return o; | |
} | |
half4 main_frag (v2f i) : COLOR | |
{ | |
half4 cColor = tex2D(_MainTex, i.uv); | |
half4 cBlurred = blur(_MainTex, i.uv, _BlurDistance); | |
half kLum = (cColor.r + cColor.g + cColor.b) / 3; | |
kLum = 1 - clamp(kLum / _LuminanceThreshold, 0, 1); | |
float depth; | |
float3 normal; | |
DecodeDepthNormal(tex2D(_CameraDepthNormalsTexture, i.uv_depth), depth, normal); | |
depth *= _CamDepth; // depth in meters | |
half kDepth = clamp(depth - DEPTH_BLUR_START, 0, 1); | |
half kFarBlur = clamp((depth - FAR_BLUR_START) / FAR_BLUR_LENGTH, 0, 1); | |
half kBlur = clamp(kLum * kDepth + kFarBlur, 0, 1); | |
half noiseValue = tex2D(_NoiseTex, i.uv * _NoiseScale + _Randomness.xy); | |
half kNoise = kLum * _Noise; | |
half kDesaturate = kLum; | |
half4 result = cColor; | |
result = (1 - kBlur) * result + kBlur * cBlurred; | |
result *= (1.5 - kNoise + noiseValue * kNoise); | |
half resultValue = result; | |
result = (1 - kDesaturate) * result + kDesaturate * resultValue; | |
return result; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment