/*=============================================================================
	DistortionAccumulatePixelShader.usf: Pixel shader for accumulating distortion offsets
	Copyright © 2004-2006 Epic Games, Inc. All Rights Reserved.
=============================================================================*/

#include "Common.usf"
#include "Material.usf"
#include "VertexFactory.usf"

static const half DistortionScaleBias = 4.0f;

/** output distortion offsets as color so they can be accumulated (via blending) */
void Main(
	FVertexFactoryInterpolants Interpolants,
	float4	ScreenPosition	: TEXCOORD5,
	float3	CameraVector	: TEXCOORD6,
	out float4 OutColor		: COLOR0
	)
{
	// material parameter inputs
	FMaterialParameters MaterialParameters = GetMaterialParameters(Interpolants);
	CalcMaterialParameters(MaterialParameters,CameraVector,ScreenPosition);
	
	// clip 
	GetMaterialClipping(MaterialParameters);
	
	// material distortion offset
	float2 Distortion = GetMaterialDistortion(MaterialParameters);	
	
	// scale up offsets for better precision
	// clamp [-255,255] and normalize to [-1,1]
	Distortion = clamp(Distortion*DistortionScaleBias,-255,255)/255.f;
	// store positive and negative offsets separately
	float2 PosOffset = max(Distortion,0);
	float2 NegOffset = abs(min(Distortion,0));
	
	// output positives in R|G channels and negatives in B|A channels
	OutColor = float4(PosOffset.x,PosOffset.y,NegOffset.x,NegOffset.y);
}


