<?xml version="1.0" encoding="utf-8"?>
<!-- 
	@Name: 		DirtShader
	@Info: 		Shader for washable vehicle
	@Author: 	Manuel Leithner
	@Team:		SFM-Modding
	@Version:	v3.0
	@Date:		14/09/2011
	@History:	v1.0 - Inital version
				v2.0 - Converted to UTF-8
				v3.0 - added reflection handling
-->
<CustomShader version="1.0" classRequirement="">
    <LodLevel startDistance="0">
        <Parameters>
            <Parameter name="dirtScale" target="In.mat.dirtScale" type="float4"/>
        </Parameters>
        <Textures>
			<Texture name="dirtTexture" />
        </Textures>
        <CodeInjections>
            <CodeInjection position="SAMPLERS">
				<![CDATA[
					uniform sampler2D dirtTexture : TEXUNITN;
				]]>
            </CodeInjection>
            <CodeInjection position="MATERIALINFO">
				<![CDATA[
				uniform float4 dirtScale;
				]]>
            </CodeInjection>
			<CodeInjection position="GET_REFLECTION_COLOR_FS">
				<![CDATA[
					#if GPU_PROFILE >= GPU_PROFILE_MEDIUM
						#if defined(REFLECTION_MAP_2D) || defined(REFLECTION_MAP_CUBE)
							float fresnel2 = max(min(getFresnel(facing, In.mat.reflectionRefractionCoeffScale[0], 3.0)*0.2, 0.12), 0.05);
							
							#if defined(GLOSS_MAP) && defined(USE_ENVMAP_GLOSS_MODULATION)
								float3 baseMap = tex2D(In.samplers.baseMap, getTexCoords(In, globals, ALBEDOMAP_TEXCOORD).xy).xyz;
								float3 dirtMap = tex2D(In.samplers.dirtTexture, getTexCoords(In, globals, ALBEDOMAP_TEXCOORD).xy).xyz;
								float factor = 1 - saturate((dirtMap.x-baseMap.x) * (dirtMap.y - baseMap.y) + (dirtMap.z - baseMap.z)) * In.mat.dirtScale.x;	
								fresnel2 *= tex2D(In.samplers.glossMap, getTexCoords(In, globals, GLOSSMAP_TEXCOORD)).a * factor;
							#endif
							res = lerp(oColor.xyz, getReflectionColorTex(In, globals, normal, viewDir), fresnel2);
						#endif
					#endif
				]]>
            </CodeInjection>
            <CodeInjection position="NUM_TEXCOORDS">
				<![CDATA[
					#if GPU_PROFILE < GPU_PROFILE_MEDIUM
						#if defined(REFLECTION_MAP_CUBE)
						#undef REFLECTION_MAP_CUBE
						#endif
					#endif
				]]>
            </CodeInjection>
            <CodeInjection position="POST_ALBEDO_FS">
				<![CDATA[
					float3 albedoDirt = tex2D(In.samplers.dirtTexture, getTexCoords(In, globals, ALBEDOMAP_TEXCOORD).xy).xyz;	
					albedo.xyz = albedo.xyz*(1-In.mat.dirtScale.x) + albedoDirt.xyz*In.mat.dirtScale.x;
				]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>
    <LodLevel startDistance="30">
        <Parameters>
            <Parameter name="dirtScale" target="In.mat.dirtScale" type="float4"/>
        </Parameters>
        <Textures>
			<Texture name="dirtTexture" />
        </Textures>		
        <CodeInjections>
            <CodeInjection position="SAMPLERS">
				<![CDATA[
					uniform sampler2D dirtTexture : TEXUNITN;
				]]>
            </CodeInjection>
            <CodeInjection position="MATERIALINFO">
				<![CDATA[
				uniform float4 dirtScale;
				]]>
            </CodeInjection>		
            <CodeInjection position="NUM_TEXCOORDS">
				<![CDATA[
					#if defined(REFLECTION_MAP_CUBE)
						#undef REFLECTION_MAP_CUBE
					#endif
				]]>
            </CodeInjection>
            <CodeInjection position="POST_ALBEDO_FS">
				<![CDATA[
					float3 albedoDirt = tex2D(In.samplers.dirtTexture, getTexCoords(In, globals, ALBEDOMAP_TEXCOORD).xy).xyz;	
					albedo.xyz = albedo.xyz*(1-In.mat.dirtScale.x) + albedoDirt.xyz*In.mat.dirtScale.x;
				]]>
            </CodeInjection>			
        </CodeInjections>
    </LodLevel>	
</CustomShader>
