Shader "Custom/Ramp" {
	Properties {
		_MainTint ("Diffuse Tint", Color) = (1,1,1,1)
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_RampTexture("Texture", 2D) = "white" {}
		_FallofPower("Fallof Spread", Range (.01,10)) =3
		_DiffusePower ( "Diffuse Power", Range(.01,1)) = .5
		_SpecularColor ("Specular Color", Color) = (1,1,1,1)
		_SpecPower ( "Specular Power", Range (.01,30)) = 3
	}
	SubShader {
		
		CGPROGRAM
		#pragma surface surf CarPaint

		sampler2D _MainTex;
		fixed4 _MainTint;
		sampler2D _RampTexture;
		fixed _FallofPower;
		fixed _DiffusePower;
		fixed4 _SpecularColor;
		fixed _SpecPower;
		
		
		inline fixed4 LightingCarPaint (SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten){
			half3 h = normalize (lightDir + viewDir);
			fixed diff = max (0, dot(s.Normal, lightDir));
			
			float abdn = 1-dot(h, normalize(s.Normal));
			abdn = pow(clamp(abdn, 0, 1), _DiffusePower );
			half4 brdf = tex2D( _RampTexture, float2(diff, 1-abdn));
			
			float nh = max (0, dot (s.Normal, h));
			float spec = pow(nh, s.Specular * _SpecPower) * s.Gloss;
			
			fixed4 c ;
			c.rgb = (s.Albedo  * brdf.rgb *_SpecularColor.rgb ) * (atten *2);
			c.s = s.Alpha + _LightColor0.a * _SpecularColor.a * spec * atten;
			return c;
		} 

		struct Input {
			float2 uv_MainTex;
			float3 world_Refl;
			float3 viewDir;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			fixed falloff = saturate (1-dot(normalize(IN.viewDir), o.Normal));
			falloff = pow(falloff, _FallofPower);
			
			o.Albedo = c.rgb * _MainTint;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}
