<!--
Rotate UV Shader


Rotates UVs relative to a fixed center point

Material parameter "rotateSpeed" defines the speed
Material parameter "rotationCenter" defines the fixed center pont (UV coodinates)


Credits:
Modding Welt (www.modding-welt.com)

License:
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
http://creativecommons.org/licenses/by-nc-sa/4.0/

-->

<?xml version="1.0" encoding="utf-8"?>

<CustomShader version="3">
	<Parameters>
		<Parameter name="rotateSpeed" target="rotateSpeed" type="float" defaultValue="1"/>
		<Parameter name="rotationCenter" target="rotationCenter" type="float2" defaultValue="0.5 0.5"/>
	</Parameters>
	<LodLevel startDistance="0">
		<CodeInjections>
			<CodeInjection position="MATERIALINFO">
				<![CDATA[
					float rotateSpeed;
					float2 rotationCenter;
				]]>
			</CodeInjection>

			<CodeInjection position="POST_SET_TEXCOORDS_VS">
				<![CDATA[
				#if defined(ALBEDO_MAP) || defined(EMISSIVE_MAP)
					float2 uv = getDefaultTexCoords(In);
					
					float2 pos = float2(uv.x - rotationCenter.x, uv.y - rotationCenter.y);
					float radius = sqrt(pos.x * pos.x + pos.y * pos.y);
					float alpha  = atan(pos.y / pos.x);
					
					alpha += rotateSpeed * cTime_s;
					
					if(pos.x < 0){
						uv.x = (-cos(alpha) * radius) + rotationCenter.x;
						uv.y = (-sin(alpha) * radius) + rotationCenter.y;
					}else{
						uv.x = (cos(alpha) * radius) + rotationCenter.x;
						uv.y = (sin(alpha) * radius) + rotationCenter.y;
					}
					
					Out.defaultTexCoords = uv;
				#endif
				]]>
			</CodeInjection>

		</CodeInjections>
	</LodLevel>
</CustomShader>
