void SetParameters(const FRenderingCompositePassContext& Context, IPooledRenderTarget& DistortionRT)
	{
		const FPixelShaderRHIParamRef ShaderRHI = GetPixelShader();

		FTextureRHIParamRef DistortionTextureValue = DistortionRT.GetRenderTargetItem().ShaderResourceTexture;
		FTextureRHIParamRef SceneColorTextureValue = GSceneRenderTargets.GetSceneColor()->GetRenderTargetItem().ShaderResourceTexture;

		// Here we use SF_Point as in fullscreen the pixels are 1:1 mapped.
		SetTextureParameter(
			Context.RHICmdList,
			ShaderRHI,
			DistortionTexture,
			DistortionTextureSampler,
			TStaticSamplerState<SF_Point,AM_Clamp,AM_Clamp,AM_Clamp>::GetRHI(),
			DistortionTextureValue
			);

		SetTextureParameter(
			Context.RHICmdList,
			ShaderRHI,
			SceneColorTexture,
			SceneColorTextureSampler,
			TStaticSamplerState<SF_Bilinear,AM_Clamp,AM_Clamp,AM_Clamp>::GetRHI(),
			SceneColorTextureValue
			);

		FIntPoint SceneBufferSize = GSceneRenderTargets.GetBufferSizeXY();
		FIntRect ViewportRect = Context.GetViewport();
		FVector4 SceneColorRectValue = FVector4((float)ViewportRect.Min.X/SceneBufferSize.X,
												(float)ViewportRect.Min.Y/SceneBufferSize.Y,
												(float)ViewportRect.Max.X/SceneBufferSize.X,
												(float)ViewportRect.Max.Y/SceneBufferSize.Y);
		SetShaderValue(Context.RHICmdList, ShaderRHI, SceneColorRect, SceneColorRectValue);
	}
Exemple #2
0
// Convert f-stop and focal distance into projected size in half resolution pixels.
// Setup depth based blur.
static FVector CircleDofCoc(const FRenderingCompositePassContext& Context)
{
    // Convert FOV to focal length,
	// 
	// fov = 2 * atan(d/(2*f))
	// where,
	//   d = sensor dimension (APS-C 24.576 mm)
	//   f = focal length
	// 
	// f = 0.5 * d * (1/tan(fov/2))
	float HalfFOV = FMath::Atan(1.0f / Context.View.ViewMatrices.ProjMatrix.M[0][0]);
	float FocalLength = 0.5f * 24.576f * (1.0f/FMath::Tan(HalfFOV));
	 
	// Convert focal distance in world position to mm.
	// Conversion is 1 world position = 1 cm.
	float Distance = Context.View.FinalPostProcessSettings.DepthOfFieldFocalDistance;
	Distance *= 10.0f;

	// Convert f-stop, focal length, and focal distance to
	// projected circle of confusion size at infinity in mm.
    //
	// coc = f*f / (n * (d - f))
	// where,
	//   f = focal length
	//   d = focal distance
    //   n = fstop (where n is the "n" in "f/n")
	float Radius = FocalLength * FocalLength / (Context.View.FinalPostProcessSettings.DepthOfFieldFstop * (Distance - FocalLength));

	// Scale so that APS-C 24.576 mm = full frame.
	// Convert mm to pixels.
	float Width = (float)Context.GetViewport().Size().X;
	Radius = Radius * Width * (1.0f/24.576f);

	// Convert diameter to radius at half resolution (algorithm radius is at half resolution).
	Radius *= 0.25f;

	// Comment out for now, allowing settings which the algorithm cannot cleanly do.
	#if 0
		// Limit to algorithm max size.
		if(Radius > 6.0f) 
		{
			Radius = 6.0f; 
		}
	#endif

	// The DepthOfFieldDepthBlurAmount = km at which depth blur is 50%.
	// Need to convert to cm here.
	return FVector(
		Radius, 
		1.0f/(Context.View.FinalPostProcessSettings.DepthOfFieldDepthBlurAmount * 100000.0),
		Context.View.FinalPostProcessSettings.DepthOfFieldDepthBlurRadius * Width / 1920.0f);
}