-﻿//  Copyright (c) 2015, Ben Hopkins (kode80)
+//  Copyright (c) 2015, Ben Hopkins (kode80)
 //  All rights reserved.
 //  
 //  Redistribution and use in source and binary forms, with or without modification, 

				return step( normalDelta.x + normalDelta.y + normalDelta.z, _NormalBias) * step( depthDelta, _DepthBias);
			}
			
			inline void processSample( float2 uv, 
						 			   float3 sourceNormal,
						 			   float sourceDepth,
									   float i, 
									   float sampleCount, 
									   float2 stepSize, 
								  	   inout half4 accumulator, 
								  	   inout half denominator)
			{
				float2 offsetUV = stepSize * i + uv;
				half isSame = compareNormalAndDepth( sourceNormal, sourceDepth, offsetUV);
		        half coefficient = weights[ sampleCount - abs( i)] * isSame;
		        accumulator += tex2D( _MainTex, offsetUV) * coefficient;
		        denominator += coefficient;
			}

			{
				half4 specularSmoothPixel = tex2D( _CameraGBufferTexture1, i.uv);
			    float3 sourceNormal = tex2D( _CameraGBufferTexture2, i.uv).rgb * 2.0 - 1.0;
			    float sourceDepth = Linear01Depth( tex2D( _CameraDepthTexture, i.uv).r) * _ProjectionParams.z - 0.1;
			    
			    float2 stepSize = _TexelOffsetScale.xy * (1.0 - specularSmoothPixel.a);
			    half4 accumulator = tex2D( _MainTex, i.uv) * 0.214607;
			    half denominator = 0.214607;
			    
			    processSample( i.uv, sourceNormal, sourceDepth, 1, _BlurQuality, stepSize, accumulator, denominator);
			    processSample( i.uv, sourceNormal, sourceDepth, 2, _BlurQuality, stepSize, accumulator, denominator);
			    processSample( i.uv, sourceNormal, sourceDepth, 3, _BlurQuality, stepSize, accumulator, denominator);
			    processSample( i.uv, sourceNormal, sourceDepth, 4, _BlurQuality, stepSize, accumulator, denominator);
			    processSample( i.uv, sourceNormal, sourceDepth, 5, _BlurQuality, stepSize, accumulator, denominator);
			    processSample( i.uv, sourceNormal, sourceDepth, 6, _BlurQuality, stepSize, accumulator, denominator);
			    
			    processSample( i.uv, sourceNormal, sourceDepth, -1, _BlurQuality, stepSize, accumulator, denominator);
			    processSample( i.uv, sourceNormal, sourceDepth, -2, _BlurQuality, stepSize, accumulator, denominator);
			    processSample( i.uv, sourceNormal, sourceDepth, -3, _BlurQuality, stepSize, accumulator, denominator);
			    processSample( i.uv, sourceNormal, sourceDepth, -4, _BlurQuality, stepSize, accumulator, denominator);
			    processSample( i.uv, sourceNormal, sourceDepth, -5, _BlurQuality, stepSize, accumulator, denominator);
			    processSample( i.uv, sourceNormal, sourceDepth, -6, _BlurQuality, stepSize, accumulator, denominator);
			    
			    return accumulator / denominator;
			}
