looking for "Z Buffer" Shadow

Posted By: Oxy

looking for "Z Buffer" Shadow - 02/24/11 20:41

Hi,
Im looking for a simple Shadow Shader, that uses the
Z-Buffer technique.

Such as:
http://en.wikipedia.org/wiki/Shadow_mapping

The shadows dont need to have a good resolution.
They mainly need to be fast.

Just like this example:

Posted By: Superku

Re: looking for "Z Buffer" Shadow - 02/24/11 21:02

There is a shadow mapping example in the GStudio folder (samples/shadowmapping.c).
Posted By: Oxy

Re: looking for "Z Buffer" Shadow - 02/24/11 21:04

Ok, i will try that one.
Posted By: JibbSmart

Re: looking for "Z Buffer" Shadow - 02/24/11 21:10

If you don't mind getting a little more complicated, there's a CSM tutorial on my website here. More specifically, it's how to adapt the shadowmapping.c code Superku pointed you to into Cascaded Shadow Mapping, which is relatively easy but effective over huge distances.

Jibb
Posted By: Oxy

Re: looking for "Z Buffer" Shadow - 02/24/11 21:37

Cool, I will play around with the standard example, and
then look into your advancements.
Posted By: Oxy

Re: looking for "Z Buffer" Shadow - 02/25/11 00:54

The shadow rendering looks nice.
But is there a way to keep other material and lightsettings?
They are overwritten by the shadow material.
And the "bottom" (non lightened) parts of the
models are totally black.

Can the shadows be added / mixed to the scene with
the standard material/lightning?
Posted By: JibbSmart

Re: looking for "Z Buffer" Shadow - 02/25/11 01:05

I believe my tutorial also makes the shadows mix more nicely with the standard lighting.

Jibb
Posted By: Oxy

Re: looking for "Z Buffer" Shadow - 02/25/11 01:37

ok, I put in
return tex2D(TexSampler,inTex) * fShadow * max(0.15,fDiffuse);

to keep the bootoms from getting totally black.
(works for now)


Jibb, you example is cool for explanation.
Its just a bit slow with the 3 views.
Is it possible to only use the "widerange" view, but
then add other materials and lightemittions onto the map?
Posted By: JibbSmart

Re: looking for "Z Buffer" Shadow - 02/25/11 02:06

It is possible to use fewer views. There is also room to make it faster, with functions that have been added since then in A8 (such as view_check). If you want to use just the widest view, it's mostly be just the same as the original shadowmapping.c example with a couple of changes.

If I were you, I'd use:
Code:
return tex2D(TexSampler,inTex) * min(fShadow, fDiffuse);

This will make a smooth transition from the cast shadow to the diffuse lighting. Then, if you need the shadow to be less dark, change fDark near the beginning of the .fx file.

The way it is set up, you'll need to add the shadow stuff into each different material manually if you want them to receive shadows.

Out of curiosity, what are your system specs? And what kind of framerate are you getting?

Jibb
Posted By: Oxy

Re: looking for "Z Buffer" Shadow - 02/25/11 02:13

its about 25 FPS on 1024x768 on your examplelevel.

on the original demo its about 130 FPS on 1024x768.
(I added a few models too, so its not the models themself)

I dont have a very new Computer, but (beeing conservative) I dont want to
see an FPS drop that early on.

Posted By: JibbSmart

Re: looking for "Z Buffer" Shadow - 02/25/11 02:26

That's fair enough.

Consider this: an entity only needs to be rendered in a wider shadow-view if it is not completely enclosed in a narrower one. Using view_check you could (and should if using CSM in a proper game) have entities that are completely within the frustum of narrower views be ignored in wider views. In many situations this will probably improve your framerate quite a bit.

I don't use CSM yet in KarBOOM, but I will make it an option in the near future when levels are complex enough to warrant it.

Jibb
Posted By: Oxy

Re: looking for "Z Buffer" Shadow - 02/25/11 02:43

I will try to fuzz around with the code,
thanks for your help.
Posted By: Oxy

Re: looking for "Z Buffer" Shadow - 02/25/11 02:56

Ok, this is quite good looking now in my eyes,
and still very fast

Code:
////////////////////////////////////////////////////
// Simple shadow mapping shader
// Copyright (c) 2007 Conitec.
// (with reduced darkness..)
// based on modifications by Jibb Smart 
////////////////////////////////////////////////////

#define USE_PCF // use percentage closer filtering

//Tweakables
static const float fDark = 0.6;
static const float fDarkDiffuse = 0.2;
static const float fBright = 1.4;
static const float fDepthOffset = 0.99;
static const float fPCF = 0.9;

// Application fed data:
const float4x4 matWorldViewProj;	// World*view*projection matrix.
const float4x4 matWorld; // World matrix.
const float4x4 matMtl;   // Precalculated texture projection matrix
const float4 vecSunDir;	// Sun direction vector.

texture TargetMap;
texture entSkin1;
sampler DepthSampler = sampler_state { Texture = <TargetMap>; };
sampler TexSampler = sampler_state { Texture = <entSkin1>; Mipfilter = Linear; };

// Shadow mapping vertex shader
void ShadowVS (in float4 inPos: POSITION,
		in float2 inTex: TEXCOORD0,
		in float3 inNormal: NORMAL,
		out float4 outPos: POSITION,
		out float2 outTex: TEXCOORD0,
		out float3 outNormal: TEXCOORD1,
		out float4 outDepth: TEXCOORD2)
{
// Transform the vertex from object space to clip space:
	outPos = mul(inPos, matWorldViewProj);
	
// Transform the normal from object space to world space:
	outNormal = normalize(mul(inNormal,matWorld));
	
// Pass the texture coordinate to the pixel shader:
	outTex = inTex;
	
// Output the projective texture coordinates
	outDepth = mul( mul(inPos,matWorld), matMtl );
}

// distance comparison function
float fDist(float4 DepthCoord,float fDepth)
{
	return 
		tex2Dproj(DepthSampler,DepthCoord).r < (fDepth*fDepthOffset)? fDark : fBright;
}

#ifdef USE_PCF
static const float4 fTaps_PCF[9] = {
	{-1.0,-1.0, 0.0, 0.0},
	{-1.0, 0.0, 0.0, 0.0},
	{-1.0, 1.0, 0.0, 0.0},
	{ 0.0,-1.0, 0.0, 0.0},
	{ 0.0, 0.0, 0.0, 0.0},
	{ 0.0, 1.0, 0.0, 0.0},
	{ 1.0,-1.0, 0.0, 0.0},
	{ 1.0, 0.0, 0.0, 0.0},
	{ 1.0, 1.0, 0.0, 0.0}};
#endif

// Shadow mapping pixel shader
float4 ShadowPS (in float4 inPos: POSITION,
					in float2 inTex: TEXCOORD0,
					in float3 inNormal: TEXCOORD1,
					in float4 inDepth: TEXCOORD2) : COLOR0
{
// Calculate the diffuse term:

	float fDiffuse = lerp(fDarkDiffuse, fBright, saturate(dot(-vecSunDir, normalize(inNormal))));


// Calculate the shadow term
#ifdef USE_PCF
	float fShadow = 0.0;
	for (int i=0; i < 9; i++)
	{
		float4 fTap = inDepth + fPCF*fTaps_PCF[i];
		fShadow += fDist(fTap,inDepth.z)/9;
	}
#else
	float fShadow = fDist(inDepth,inDepth.z);
#endif		


		return tex2D(TexSampler,inTex) * min(fShadow, fDiffuse);
}

technique techShadow
{
	pass p0
	{
		VertexShader = compile vs_2_0 ShadowVS();
		PixelShader  = compile ps_2_0 ShadowPS();
	}
}


Posted By: JibbSmart

Re: looking for "Z Buffer" Shadow - 02/25/11 03:12

Looks good to me. I actually prefer without PCF usually, as I think it takes too many samples to look nice. If you were had PCF enabled in the CSM example, that might explain the relatively low framerate.

Jibb
© 2024 lite-C Forums