I wuoldn't combine this shaders, if you do that you'll see that you have to do it for every (objekt)shader in your game. I've changed to code of the shadowmapping.c in the samples folder for you. It uses a different View to create the shadows and the camera just uses a pp shader to add these shadows to the scene, normaly it renders every model with an objekt shader so other shaders, like your terrain shader aren't visible there.
here is the changed shadowmapping.c
Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <mtlView.c>
///////////////////////////////

function mtlDepth_event();
BMAP* shadowT;

MATERIAL* mtlDepth =
{
	effect = "Depth.fx";
	event = mtlDepth_event;
	flags = ENABLE_VIEW;
}

MATERIAL* mtlShadowAdd =
{
	effect = "ShadowFilter.fx";
}

MATERIAL* mtlShadow =
{
	effect = "Shadow.fx";
	flags = AUTORELOAD;
}

VIEW* viewDepth = 
{
	bmap = "#2048x2048x14";
	material = mtlDepth;
	flags = SHOW;
}

VIEW* viewShadow = 
{
	flags = AUTORELOAD;
}

function mtlDepth_event()
{
	float fTexAdj[16] =
	{
		0.5, 0.0, 0.0, 0.0,
		0.0,-0.5,	0.0, 0.0,
		0.0, 0.0, 1.0, 0.0,
		0.0, 0.0, 0.0, 1.0
	};
	fTexAdj[12] = 0.5 + (0.5/(float)viewDepth.bmap.width);
	fTexAdj[13] = 0.5 + (0.5/(float)viewDepth.bmap.height);
	mat_set(mtlShadow.matrix,matViewProj);
	mat_multiply(mtlShadow.matrix,fTexAdj);
	return 0;	
}

function main()
{
	d3d_antialias = 4;
	level_load("small.hmp");
	ENTITY* ent = ent_create("blob.mdl",vector(0,0,30),NULL);
	
	shadowT = bmap_createblack(screen_size.x, screen_size.y, 24);
	viewShadow.bmap = shadowT;
	
	vec_set(sun_angle,vector(90,60,1000)); 
	vec_set(camera.x,vector(-250,0,100));
	camera.tilt = -15;
	pp_set(camera,mtlShadowAdd);
	viewShadow.material = mtlShadow;
	viewDepth.stage = viewShadow;
	
	while(1)
	{
		ent.pan -= 2 * time_step;
		ent.tilt -= time_step;	
		vec_set(viewDepth.x,sun_pos);
		vec_set(viewDepth.pan,vector(180+sun_angle.pan,-sun_angle.tilt,0));
		
		draw_text(str_printf(NULL,
		"Simple shadow mapping demo (Shader-Workshop 6)
		Edit Shadow.fx and observe changes immediately in the scene"),
		15,5,COLOR_WHITE);
		vec_set(viewShadow.x,camera.x);
		vec_set(viewShadow.pan,camera.pan);
		wait(1);
	}
}


the cahnged shadow.fx
Code:
#define USE_PCF // use percentage closer filtering

//Tweakables
static const float fDark = 0.5;
static const float fBright = 1.5;
static const float fDepthOffset = 0.99;
static const float fPCF = 0.5;

// 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;
sampler DepthSampler = sampler_state { Texture = <TargetMap>; };

// Shadow mapping vertex shader
void ShadowVS (in float4 inPos: POSITION,
		in float2 inTex: TEXCOORD0,
		in float3 inNormal: NORMAL,
		out float4 outPos: POSITION,
		out float3 outNormal: TEXCOORD0,
		out float4 outDepth: TEXCOORD1)
{
// 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));
	
// 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 float3 inNormal: TEXCOORD0,
					in float4 inDepth: TEXCOORD1) : COLOR0
{
// Calculate the diffuse term:

// 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 float4(1,1,1,0) * fShadow;// * fDiffuse;
}

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


and the new ShadowFilter.fx
Code:
texture TargetMap;
texture shadowT_bmap;

sampler2D smpScreen = sampler_state
{
	Texture = <TargetMap>;
	
	MinFilter	= linear;
	MagFilter	= linear;
	MipFilter	= linear;
	
	AddressU		= Clamp;
	AddressV		= Clamp;
};

sampler2D smpShadow = sampler_state
{
	Texture = <shadowT_bmap>;
	
	MinFilter	= linear;
	MagFilter	= linear;
	MipFilter	= linear;
	
	AddressU		= Clamp;
	AddressV		= Clamp;
};

float4 Shader(float2 tex : TEXCOORD0) : COLOR
{
	float4 Color = tex2D(smpScreen, tex);
	float4 Color2 = tex2D(smpShadow, tex);
	Color2.a = 1.0 -(Color2.r + Color2.b + Color2.g)/3;
	Color = lerp(Color,float4(0,0,0,1),Color2.a);
	
	return Color;
}

technique tech_00
{
	pass pass_00
	{
		VertexShader = null;
		PixelShader = compile ps_2_0 Shader();
	}
}


but i think it only works on graphics cards, which are suporting "render to texture".
Plese don't forget that tha shadow view must have the same position, rotation, aspect,arc then the camera, elseway it wont work...
Have fun with it! wink
EDIT: i've removed some pointless lines maby i've removed somthing non pointless with it.
xxxxxxx

Last edited by xxxxxxx; 05/11/11 19:19.

Es ist immer wieder erstaunlich, dass Leute die riesen Scripte schreiben die einfachsten sachen nicht können zb. mich mit SIEBEN x zu schreiben! tongue