Home > HLSL, vvvv > Phongy depth

Phongy depth

Made variation to Natan Sinigaglia’s ‘DOF’ patch: implemented a phong light model for the depth pass. It makes the image ‘crunchier’.
Notice that there are several techniques. In those with a ‘viewmatrix depth’ I found a kind of discontinuity with the shading interpolation. In the case of the ‘distance to camera’ techniques I used the next formula:

float4 interpolation = lerp(input, col, pow(Interpolation,2));

Remember to place the “PhongDirectional.fxh” file on the same root folder as the shader. You can find the original file on the effects folder inside vvvv.

I like the result, next step would be to explore more light models and, perhaps, to implement a crepuscular ray model on top of the depth!


//transform
float4x4 tW: WORLD;
float4x4 tV: VIEW;
float4x4 tWV: WORLDVIEW;
float4x4 tVP: VIEWPROJECTION;
float4x4 tWVP: WORLDVIEWPROJECTION;
float4x4 tP: PROJECTION;

#include “PhongDirectional.fxh”

float dNear = 1;
float dFocus = 0;
float dFar = 2;
float clampFar = .9;
float3 camPos;

//textures
//not used but feasible to implement
texture Tex;
sampler Samp = sampler_state{
Texture = (Tex);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};
float4x4 tTex: TEXTUREMATRIX;
float4x4 tColor: COLOR;

//struct
struct vs2ps{
float4 Pos: POSITION;
float4 TexCd: TEXCOORD0;
float3 LightDirV: TEXCOORD1;
float3 NormV: TEXCOORD2;
float3 ViewDirV: TEXCOORD3;
float4 col: COLOR;
};

vs2ps VS(
float4 Pos: POSITION)
{
vs2ps Out = (vs2ps) 1;

Pos = mul(Pos, tW);
float depth = distance(Pos, camPos);

if(depth<dFocus){
depth = (depth-dNear)/(dFocus-dNear)/2;
}else{
depth = (depth-dFocus)/(dFar – dFocus)/2 + 0.5;
//optional:
depth = clamp(depth, 0 , clampFar);
}

Out.Pos = mul(Pos, tVP);
Out.col.rgb = depth;

return Out;
}

vs2ps VS2(
float4 Pos: POSITION)
{
vs2ps Out = (vs2ps) 1;

Out.Pos = mul(Pos, tWVP);

float depth = Out.Pos.z;

if (depth < dFocus)
{
depth = (depth – dNear)/(dFocus – dNear)/2;
}

else
{
depth = (depth-dFocus)/(dFar – dFocus)/2 + 0.5;
//OPTIONAL:
depth = clamp (depth, 0, clampFar) ;
}

Out.col.rgb = depth;

return Out;
}

////
vs2ps VS_PHONG_DCD(
float4 Pos: POSITION,
float3 NormO: NORMAL,
float4 TexCd : TEXCOORD0
)
{
vs2ps Out = (vs2ps) 1;

//depth stuff
Pos = mul(Pos, tW);
float depth = distance(Pos, camPos);

if (depth < dFocus)
{
depth = (depth – dNear)/(dFocus – dNear)/2;
}

else
{
depth = (depth-dFocus)/(dFar – dFocus)/2 + 0.5;
//OPTIONAL:
depth = clamp (depth, 0, clampFar) ;
}

//phong stuff
//inverse light direction in view space
Out.LightDirV = normalize(-mul(lDir, tV));

//normal in view space
Out.NormV = normalize(mul(NormO, tWV));

Out.Pos = mul(Pos, tVP);
Out.TexCd = mul(TexCd, tTex);
Out.ViewDirV = -normalize(mul(Pos, tWV));

Out.col.rgb = depth;

return Out;
}

////
vs2ps VS_PHONG_VMD(
float4 Pos: POSITION,
float3 NormO: NORMAL,
float4 TexCd : TEXCOORD0
)
{
vs2ps Out = (vs2ps) 1;

//depth stuff
Out.Pos = mul(Pos, tWVP);

float depth = Out.Pos.z;

if (depth < dFocus)
{
depth = (depth – dNear)/(dFocus – dNear)/2;
}

else
{
depth = (depth-dFocus)/(dFar – dFocus)/2 + 0.5;
//OPTIONAL:
depth = clamp (depth, 0, clampFar) ;
}

//phong stuff
//inverse light direction in view space
Out.LightDirV = normalize(-mul(lDir, tV));

//normal in view space
Out.NormV = normalize(mul(NormO, tWV));

Out.Pos = mul(Pos, tWVP);
Out.TexCd = mul(TexCd, tTex);
Out.ViewDirV = -normalize(mul(Pos, tWV));

Out.col.rgb = depth;

return Out;
}

///////////////////////////////////////////////////////
//pixel
float4 PS(vs2ps In): COLOR{
return In.col;
}

float Alpha = 1;

float4 PS_PHONG(vs2ps In):COLOR{
//In.TexCd = In.TexCd / In.TexCd.w; // for perpective texture projections (e.g. shadow maps) ps_2_0

//orthodox phong!
//here we can implement the texture
//float4 col = tex2D(Samp, In.TexCd);

float4 input = In.col;
float4 col = In.col;

col.rgb *= PhongDirectional(In.NormV, In.ViewDirV, In.LightDirV);
col = col * input;
col.a *= Alpha;

return mul(col, tColor);
}

//softer interpolation
float Interpolation = 0.1f;
float4 PS_PHONG2(vs2ps In):COLOR{
float4 input = In.col;
float4 col = In.col;

col.rgb *= PhongDirectional(In.NormV, In.ViewDirV, In.LightDirV);

//float4 interpolation = lerp(input, col, Interpolation);
float4 interpolation = lerp(input, col, pow(Interpolation,2));

col.a *= Alpha;

return col;

}

//techniques
//distance camera depth
technique DCD{
pass P0{
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS();
}
}

//view matrix depth
technique VMD{
pass P0{
VertexShader = compile vs_2_0 VS2();
PixelShader = compile ps_2_0 PS();
}
}

//phong + DCD
technique phDCDInter{
pass P0{
VertexShader = compile vs_2_0 VS_PHONG_DCD();
PixelShader = compile ps_2_0 PS_PHONG2();
}
}

//phong + VMD
technique phVMD{
pass P0{
VertexShader = compile vs_2_0 VS_PHONG_VMD();
PixelShader = compile ps_2_0 PS_PHONG();
}
}

//phong + VMD + interpolation
technique phVMDInter{
pass P0{
VertexShader = compile vs_2_0 VS_PHONG_VMD();
PixelShader = compile ps_2_0 PS_PHONG2();
}
}

The final result looks like this, once the DOF is implemented:

phong depth with HLSL

Advertisements
Categories: HLSL, vvvv
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: