3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1]

Author Topic: Depth of field Script  (Read 152 times)

« on: June 10, 2014, 03:12:52 PM »
I need help I found a DOF shader and it is compatible with 3drad because I get no errors from 3drad but I need to get it to actually work instead of the screen going black but I don't know what I need to put in it to get it to work correctly.
Code: [Select]
   // These are set by the game engine. 
   // The render target size is one-quarter the scene rendering size. 

/*HLSLSTART DOF
// Number of outer filter taps
#define NUM_DOF_TAPS 12
 
float4 viewportScale;
float4 viewportBias;
 
float maxCoC;
 
float2 filterTaps[NUM_DOF_TAPS];
 
texture tSceneColor;
texture tBlurDepth;
 
///////////////////////////////////////////////////////////////////////////////
 
struct VS_INPUT
{
float4 vPos: POSITION;
float2 vTexCoord: TEXCOORD;
};
 
struct VS_OUTPUT
{
float4 vPos: POSITION;
float2 vTexCoord: TEXCOORD;
};
 
///////////////////////////////////////////////////////////////////////////////
 
struct PS_INPUT
{
float2 vTexCoord: TEXCOORD;
};
 
sampler SceneColorSampler = sampler_state
{
texture = (tSceneColor);
 
MinFilter = Point;
MagFilter = Point;
AddressU = Clamp;
AddressV = Clamp;
};
 
sampler DepthBlurSampler = sampler_state
{
texture = (tBlurDepth);
 
MinFilter = Point;
MagFilter = Point;
AddressU = Clamp;
AddressV = Clamp;
};
 
///////////////////////////////////////////////////////////////////////////////
 
// Screen space vertex shader
VS_OUTPUT filter_dof_vs(VS_INPUT v)
{
VS_OUTPUT o;
 
// Scale and bias viewport
o.vPos = v.vPos * viewportScale + viewportBias;
 
// Output tex. coordinates
o.vTexCoord = v.vTexCoord;
 
return o;
}
 
// DoF filter shader
float4 filter_dof_ps(PS_INPUT v) : COLOR
{
// Get center sample
float4 colorSum = tex2D(SceneColorSampler, v.vTexCoord);
float2 centerDepthBlur = tex2D(DepthBlurSampler, v.vTexCoord);
 
// Compute CoC size based on blurriness
float sizeCoC = centerDepthBlur.y * maxCoC;
 
float totalContribution = 5.0f;
 
// Run through all taps
for (int i = 0; i < NUM_DOF_TAPS; i++)
{
// Compute tap coordinates
float2 tapCoord = v.vTexCoord + filterTaps[i] * sizeCoC;
 
// Fetch tap sample
float4 tapColor = tex2D(SceneColorSampler, tapCoord);
float2 tapDepthBlur = tex2D(DepthBlurSampler, tapCoord);
 
// Compute tap contribution
float tapContribution = (tapDepthBlur.x > centerDepthBlur.x) ? 1.0f : tapDepthBlur.y;
 
// Accumulate color and contribution
colorSum += tapColor * tapContribution;
totalContribution += tapContribution;
}
 
// Normalize to get proper luminance
float4 finalColor = colorSum / totalContribution;
 
return finalColor;
}
 
technique FilterDoF
{
pass P0
{
VertexShader = compile vs_3_0 filter_dof_vs();
PixelShader = compile ps_3_0 filter_dof_ps();
 
CullMode = NONE;
}
}
 
///////////////////////////////////////////////////////////////////////////////
 
float depthScale;
 
// Helper shader for visualizing depth/blurriness
float4 depth_blur_overlay_ps(PS_INPUT v) : COLOR
{
float4 BlurDepth = tex2D(DepthBlurSampler, v.vTexCoord);
 
return float4(BlurDepth.x * depthScale, BlurDepth.y, 0, 0);
}
 
technique DepthBlurOverlay
{
pass P0
{
VertexShader = compile vs_3_0 filter_dof_vs();
PixelShader = compile ps_3_0 depth_blur_overlay_ps();
 
CullMode = NONE;
}

HLSLEND*/
///////////////////////////////////////////////////////////////////////////////////////

void Main()
{
   if (iInitializing())
   {
     
      iShaderSet(OBJ_0,"DOF");

   }

}

and here is were I got the code from.
http://read.pudn.com/downloads47/sourcecode/graph/159419/DepthOfField-HLSL/shaders/dof.fx__.php
« Last Edit: June 10, 2014, 03:17:27 PM by dogtorque »
I be trippin!
« Reply #1 on: June 10, 2014, 10:13:43 PM »
you won't be able to use that shader as 3drad doesn't give you access to the depth buffer as texture input unless there is a way to write the depth to a render target but the precision probably wouldn't be high enough to give good results. You might be able to render a mask of objects to blur faking depth but your still going to get halo's over foreground objects, which seems to only be best solved with a "circle of confusion" algorithm which is heavy.
using 3Drad 7.22

system specs:
Windows 7 Home Premium 64-bit
Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz (4 CPUs), ~3.2Ghz
8 gig ram
Geforce GTX 650 1024 MB GDDR5
DirectX 11
« Reply #2 on: June 11, 2014, 07:55:50 AM »
you won't be able to use that shader as 3drad doesn't give you access to the depth buffer as texture input unless there is a way to write the depth to a render target but the precision probably wouldn't be high enough to give good results. You might be able to render a mask of objects to blur faking depth but your still going to get halo's over foreground objects, which seems to only be best solved with a "circle of confusion" algorithm which is heavy.
oh okay well... thanks for the awnser genetransfer.
oh and gene would there be a way to modify the radial motion blur script so the blurry part will only be located on the bottom and both sides of the screen instead of being all around the screen?
I be trippin!
« Reply #3 on: June 11, 2014, 07:44:59 PM »
no problem.
Quote
there be a way to modify the radial motion blur script so the blurry part will only be located on the bottom and both sides of the screen instead of being all around the screen?
I never looked into 3drads post process pipeline, but if it's like 3Impacts allowing for multiple stages of the fx pipeline, you could load in a custom texture mask (like an alpha map) and interpolate between the original screen texture and the final blurred texture using gradients (black to white - 0.0 - 1.0) in the custom mask texture. that would give that effect of blurring around the edges.
using 3Drad 7.22

system specs:
Windows 7 Home Premium 64-bit
Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz (4 CPUs), ~3.2Ghz
8 gig ram
Geforce GTX 650 1024 MB GDDR5
DirectX 11
« Reply #4 on: June 12, 2014, 06:26:01 AM »
no problem.
Quote
there be a way to modify the radial motion blur script so the blurry part will only be located on the bottom and both sides of the screen instead of being all around the screen?
I never looked into 3drads post process pipeline, but if it's like 3Impacts allowing for multiple stages of the fx pipeline, you could load in a custom texture mask (like an alpha map) and interpolate between the original screen texture and the final blurred texture using gradients (black to white - 0.0 - 1.0) in the custom mask texture. that would give that effect of blurring around the edges.
okay...man this is confusing.
« Last Edit: June 12, 2014, 06:39:57 AM by dogtorque »
I be trippin!
Pages: [1]