/// <summary> /// Performs tone mapping on the specified render target /// </summary> /// <param name="source">The source render target</param> /// <param name="result">The render target to which the result will be output</param> /// <param name="depthTexture">The render target containing scene depth</param> /// <param name="camera">The camera used to render the scene</param> /// <param name="dofType">The type of DOF effect to apply</param> /// <param name="focalDistance">The distance to the camera focal point</param> /// <param name="focalWidth">The width of the camera focal point</param> public void DepthOfField(RenderTarget2D source, RenderTarget2D result, RenderTarget2D depthTexture, Camera camera, DepthOfFieldType dofType, float focalDistance, float focalWidth, float time) { if (dofType == DepthOfFieldType.DiscBlur) { // Scale tap offsets based on render target size float dx = 0.5f / (float)source.Width; float dy = 0.5f / (float)source.Height; // Generate the texture coordinate offsets for our disc Vector2[] discOffsets = new Vector2[12]; discOffsets[0] = new Vector2(-0.326212f * dx, -0.40581f * dy); discOffsets[1] = new Vector2(-0.840144f * dx, -0.07358f * dy); discOffsets[2] = new Vector2(-0.840144f * dx, 0.457137f * dy); discOffsets[3] = new Vector2(-0.203345f * dx, 0.620716f * dy); discOffsets[4] = new Vector2(0.96234f * dx, -0.194983f * dy); discOffsets[5] = new Vector2(0.473434f * dx, -0.480026f * dy); discOffsets[6] = new Vector2(0.519456f * dx, 0.767022f * dy); discOffsets[7] = new Vector2(0.185461f * dx, -0.893124f * dy); discOffsets[8] = new Vector2(0.507431f * dx, 0.064425f * dy); discOffsets[9] = new Vector2(0.89642f * dx, 0.412458f * dy); discOffsets[10] = new Vector2(-0.32194f * dx, -0.932615f * dy); discOffsets[11] = new Vector2(-0.791559f * dx, -0.59771f * dy); // Set array of offsets dofEffect.Parameters["g_vFilterTaps"].SetValue(discOffsets); dofEffect.CurrentTechnique = dofEffect.Techniques["DOFDiscBlur"]; dofEffect.Parameters["g_fFocalDistance"].SetValue(focalDistance); dofEffect.Parameters["g_fFocalWidth"].SetValue(focalWidth / 2.0f); dofEffect.Parameters["g_fFarClip"].SetValue(camera.far); dofEffect.Parameters["g_fAttenuation"].SetValue(1); dofEffect.Parameters["time"].SetValue(time); //wave effect RenderTarget2D[] sources = doubleSourceArray; sources[0] = source; sources[1] = depthTexture; PostProcess(sources, result, dofEffect); } else { // Downscale to 1/16th size and blur IntermediateTexture downscaleTexture = GetIntermediateTexture(source.Width / 4, source.Height / 4, SurfaceFormat.Color); GenerateDownscaleTargetSW(source, downscaleTexture.RenderTarget); // For the "dumb" DOF type just do a blur, otherwise use a special blur // that takes depth into account if (dofType == DepthOfFieldType.BlurBuffer) Blur(downscaleTexture.RenderTarget, downscaleTexture.RenderTarget, 2.5f); else if (dofType == DepthOfFieldType.BlurBufferDepthCorrection) DepthBlur(downscaleTexture.RenderTarget, downscaleTexture.RenderTarget, depthTexture, 2.5f); dofEffect.CurrentTechnique = dofEffect.Techniques["DOFBlurBuffer"]; dofEffect.Parameters["g_fFocalDistance"].SetValue(focalDistance); dofEffect.Parameters["g_fFocalWidth"].SetValue(focalWidth / 2.0f); dofEffect.Parameters["g_fFarClip"].SetValue(camera.far); dofEffect.Parameters["g_fAttenuation"].SetValue(1.0f); RenderTarget2D[] sources = tripleSourceArray; sources[0] = source; sources[1] = downscaleTexture.RenderTarget; sources[2] = depthTexture; PostProcess(sources, result, dofEffect); downscaleTexture.InUse = false; } }
private static extern void Internal_settype(IntPtr thisPtr, DepthOfFieldType value);
/// <summary> /// Performs tone mapping on the specified render target /// </summary> /// <param name="source">The source render target</param> /// <param name="result">The render target to which the result will be output</param> /// <param name="depthTexture">The render target containing scene depth</param> /// <param name="camera">The camera used to render the scene</param> /// <param name="dofType">The type of DOF effect to apply</param> /// <param name="focalDistance">The distance to the camera focal point</param> /// <param name="focalWidth">The width of the camera focal point</param> public void DepthOfField(RenderTarget2D source, RenderTarget2D result, RenderTarget2D depthTexture, Camera camera, DepthOfFieldType dofType, float focalDistance, float focalWidth, float time) { if (dofType == DepthOfFieldType.DiscBlur) { // Scale tap offsets based on render target size float dx = 0.5f / (float)source.Width; float dy = 0.5f / (float)source.Height; // Generate the texture coordinate offsets for our disc Vector2[] discOffsets = new Vector2[12]; discOffsets[0] = new Vector2(-0.326212f * dx, -0.40581f * dy); discOffsets[1] = new Vector2(-0.840144f * dx, -0.07358f * dy); discOffsets[2] = new Vector2(-0.840144f * dx, 0.457137f * dy); discOffsets[3] = new Vector2(-0.203345f * dx, 0.620716f * dy); discOffsets[4] = new Vector2(0.96234f * dx, -0.194983f * dy); discOffsets[5] = new Vector2(0.473434f * dx, -0.480026f * dy); discOffsets[6] = new Vector2(0.519456f * dx, 0.767022f * dy); discOffsets[7] = new Vector2(0.185461f * dx, -0.893124f * dy); discOffsets[8] = new Vector2(0.507431f * dx, 0.064425f * dy); discOffsets[9] = new Vector2(0.89642f * dx, 0.412458f * dy); discOffsets[10] = new Vector2(-0.32194f * dx, -0.932615f * dy); discOffsets[11] = new Vector2(-0.791559f * dx, -0.59771f * dy); // Set array of offsets dofEffect.Parameters["g_vFilterTaps"].SetValue(discOffsets); dofEffect.CurrentTechnique = dofEffect.Techniques["DOFDiscBlur"]; dofEffect.Parameters["g_fFocalDistance"].SetValue(focalDistance); dofEffect.Parameters["g_fFocalWidth"].SetValue(focalWidth / 2.0f); dofEffect.Parameters["g_fFarClip"].SetValue(camera.far); dofEffect.Parameters["g_fAttenuation"].SetValue(1); dofEffect.Parameters["time"].SetValue(time); //wave effect RenderTarget2D[] sources = doubleSourceArray; sources[0] = source; sources[1] = depthTexture; PostProcess(sources, result, dofEffect); } else { // Downscale to 1/16th size and blur IntermediateTexture downscaleTexture = GetIntermediateTexture(source.Width / 4, source.Height / 4, SurfaceFormat.Color); GenerateDownscaleTargetSW(source, downscaleTexture.RenderTarget); // For the "dumb" DOF type just do a blur, otherwise use a special blur // that takes depth into account if (dofType == DepthOfFieldType.BlurBuffer) { Blur(downscaleTexture.RenderTarget, downscaleTexture.RenderTarget, 2.5f); } else if (dofType == DepthOfFieldType.BlurBufferDepthCorrection) { DepthBlur(downscaleTexture.RenderTarget, downscaleTexture.RenderTarget, depthTexture, 2.5f); } dofEffect.CurrentTechnique = dofEffect.Techniques["DOFBlurBuffer"]; dofEffect.Parameters["g_fFocalDistance"].SetValue(focalDistance); dofEffect.Parameters["g_fFocalWidth"].SetValue(focalWidth / 2.0f); dofEffect.Parameters["g_fFarClip"].SetValue(camera.far); dofEffect.Parameters["g_fAttenuation"].SetValue(1.0f); RenderTarget2D[] sources = tripleSourceArray; sources[0] = source; sources[1] = downscaleTexture.RenderTarget; sources[2] = depthTexture; PostProcess(sources, result, dofEffect); downscaleTexture.InUse = false; } }