## 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:

## Emergence of ‘Apoptosis’

Some snapshots of today’s work with Ofer Smilansky for the project ‘Apoptosis’. We are starting to feel very happy with the results. The depth of field really gives a drama to the material. The most rewarding part is everything is real time! Still, we need to optimize more the system and start adding the audio and performance interaction.

## Apoptosis go deep

‘Apoptosis’ is the name of the project I am currently developing together with audiovisual artist Ofer Smilansky and musician Steindor Kristinsson. The idea is to create narrative interactive cinematic space where performers can play. The projections will be made with semi-transparent screens.

As we move forward with the development of the material we are discovering interesting effects. For instance, we really wanted to have a depth of field onthe projection(who doesn’t?). After much tweaking(this is supposed to be real time!) we came with a non so-expensive solution using HLSL shaders. These are the tests we made.

## Nvidia to vvvv

HLSL is a very interesting language to create shaders. The last days I’ve been porting some of the Nvidia examples to vvvv, just as an exercise to deepen my knowledge on the way to write shaders using HLSL. Some of the results are rather nice and promising. The bottleneck is, still, the use of multipass shading.

Some snapshots:

Can these beauties be embedded on Cinder using Visual Studio?…we will see.

## Trying to speak French

I have a rather curious relationship with vvvv, DirectX and Windows: for me it is like studying French(my native language is Spanish), with all the ‘far away so close’ sensation on it. If you are a OSX-Unix user maybe you know what I mean!

So, I decided to make a nice partition in one of my computers and install Windows again(last time was one year ago!) with the only propose of learning how to make HLSL shaders and the play more with vvvv.

It is very funny to see how HLSL uses a rawer c-ish style, with the inclusion of some new types for expressing matrices and vectors(float4x4 and so). Besides that, the varying variables are managed quite differently altogether with the compilation of the shaders. The implementation of ‘techniques’ is something very smart and interesting.

The uniform declaration also changes a little bit. In GLSL you have something like:

**uniform vec4 Color;**

whereas HLSL would declare the same thing as:

**float4 Color: COLOR**

finally, I am not sure how much is the vvvv wrapper, but i find some close similarities inthe way jxs files are written in order to load shaders with jitter.

In the end the concepts are all the same, so I am enjoying the process of discovering the way those little phong shaders work with DirectX!