<?xml version="1.0" encoding="utf-8"?>
<!--
	CrawlerShader
	Shader to animate dynamic crawlers
	
	Author: Manuel Leithner - SFM-Modding
	Date:	22.11.2011
	
	Copyright 2011 - SFM-Modding
	free for noncommerical-usage
-->
<CustomShader version="1.0" classRequirement="">
    <LodLevel startDistance="0">
        <Parameters>
            <Parameter name="Offset" target="In.mat.offsetUV" type="float4"/>
			<Parameter name="Wheels" target="In.mat.wheels" type="float4" />
        </Parameters>
        <CodeInjections>
			<CodeInjection position="MATERIALINFO">
				<![CDATA[
					uniform float4 offsetUV;
					uniform float4 wheels;
				]]>
			</CodeInjection>
			<CodeInjection position="POST_SET_TEXCOORDS_VS">
				<![CDATA[
					#if defined(ALBEDO_MAP) || defined(EMISSIVE_MAP)
						float2 uv = getTexCoords(In, ALBEDOMAP_TEXCOORD);
						uv.y += In.mat.offsetUV.x;
						setTexCoords(Out.texCoords[ALBEDOMAP_TEXCOORD/2], ALBEDOMAP_TEXCOORD, uv);
					#endif
				]]>
        	</CodeInjection>
			<CodeInjection position="GET_POSITION_VS">
				<![CDATA[
					int index = (int) In.texCoords[1].y;
					if(index > 0) 
					{
						float maxDistance = abs(In.mat.wheels.z - In.mat.wheels.w);	
						float wheel1factor = saturate((maxDistance - abs(In.position.z - In.mat.wheels.z)) / maxDistance);
						float wheel2factor = saturate((maxDistance - abs(In.position.z - In.mat.wheels.w)) / maxDistance);
						float addHeight = wheel1factor * In.mat.wheels.x + wheel2factor * In.mat.wheels.y;
						return float4(In.position.x, In.position.y + addHeight , In.position.z, 1.0);
					} else
					{
						return In.position.xyzw;
					}
				]]>
	        	</CodeInjection>
     	  </CodeInjections>
    </LodLevel>
</CustomShader>