<?xml version="1.0" encoding="utf-8"?>
<!--
	DirtShader
	Shader to animate a dynamic fruit
	
	Author: Manuel Leithner - SFM-Modding
	Date:	22.10.2012
	
	Copyright 2012 - SFM-Modding
	free for noncommerical-usage
-->
<CustomShader version="2" classRequirement="">
    <LodLevel startDistance="0">
        <Parameters>
            <Parameter name="dirtScale" target="dirtScale" type="float4"/>
        </Parameters>
        <Textures>
			<Texture name="dirtTexture" />
        </Textures>
        <CodeInjections>
            <CodeInjection position="SAMPLERS">
				<![CDATA[
					sampler2D dirtTexture : TEXUNITN;
				]]>
            </CodeInjection>
            <CodeInjection position="MATERIALINFO">
				<![CDATA[
					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, reflectionRefractionCoeffScale[0], 3.0)*0.2, 0.12), 0.08);
						#if defined(GLOSS_MAP) && defined(USE_ENVMAP_GLOSS_MODULATION)
							float3 baseMap = tex2D(baseMap, getTexCoords(In, globals, ALBEDOMAP_TEXCOORD).xy).xyz;
							float3 dirtMap = tex2D(dirtTexture, getTexCoords(In, globals, ALBEDOMAP_TEXCOORD).xy).xyz;
							float factor = 1 - saturate((dirtMap.x-baseMap.x) * (dirtMap.y - baseMap.y) + (dirtMap.z - baseMap.z)) * dirtScale.x;
							fresnel2 *= tex2D(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(dirtTexture, getTexCoords(In, globals, ALBEDOMAP_TEXCOORD).xy).xyz;	
					albedo.xyz = albedo.xyz*(1-dirtScale.x) + albedoDirt.xyz*dirtScale.x;
				]]>
            </CodeInjection>
        </CodeInjections>
    </LodLevel>	
    <LodLevel startDistance="30">
        <Parameters>
            <Parameter name="dirtScale" target="dirtScale" type="float4"/>
        </Parameters>
        <Textures>
			<Texture name="dirtTexture" />
        </Textures>		
        <CodeInjections>
            <CodeInjection position="SAMPLERS">
				<![CDATA[
					sampler2D dirtTexture : TEXUNITN;
				]]>
            </CodeInjection>
            <CodeInjection position="MATERIALINFO">
				<![CDATA[
					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(dirtTexture, getTexCoords(In, globals, ALBEDOMAP_TEXCOORD).xy).xyz;	
					albedo.xyz = albedo.xyz*(1-dirtScale.x) + albedoDirt.xyz*dirtScale.x;
				]]>
            </CodeInjection>			
        </CodeInjections>
    </LodLevel>	
</CustomShader>
