1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
string description = "Diffuse mapping shaders";
// Default variables, supported by the engine (may not be used here)
// If you don't need any global variable, just comment it out, this way
// the game engine does not have to set it!
float4x4 viewProj : ViewProjection;
float4x4 world : World;
float4x4 viewInverse : ViewInverse;
float4x4 xLightWorldViewProjection; //for ShadowMapping only
float3 lightDir : Direction
<
string UIName = "Light Direction";
string Object = "DirectionalLight";
string Space = "World";
> = {-0.65f, 0.65f, -0.39f};
float xMaxDepth; //for ShadowMapping
// Texture and samplers
texture diffuseTexture : Diffuse
<
string UIName = "Diffuse Texture";
string ResourceName = "asteroid4.dds";
>;
sampler diffuseTextureSampler = sampler_state
{
Texture = <diffuseTexture>;
AddressU = Wrap;//Clamp;
AddressV = Wrap;//Clamp;
AddressW = Wrap;//Clamp;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
texture shadowMapTexture : Diffuse
<
string UIName = "Diffuse Texture";
string ResourceName = "asteroid4.dds";
>;
sampler shadowMapTextureSampler = sampler_state
{
texture = <shadowMapTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = clamp; AddressV = clamp;
};
//----------------------------------------------------
// Vertex input structure (used for ALL techniques here!)
struct VertexInput
{
float4 pos : POSITION;
float2 texCoord : TEXCOORD0;
float3 normal : NORMAL;
float3 tangent : TANGENT;
};
//----------------------------------------------------
//----------------------------------------------------
// vertex shader output structure
struct VertexOutput_Specular20
{
float4 pos : POSITION;
float4 ShadowMapSamplingPos : TEXCOORD0;
float2 TexCoords : TEXCOORD2;
float4 RealDistance : TEXCOORD1;
float3 Normal : TEXCOORD3;
float3 Position3D : TEXCOORD4;
};
VertexOutput_Specular20 VS_Specular20(VertexInput In)
{
VertexOutput_Specular20 Out = (VertexOutput_Specular20) 0;
Out.pos=mul(mul(In.pos, world), viewProj);
Out.TexCoords= In.texCoord;
Out.Normal=In.normal;
Out.Position3D=mul(In.pos,world);
Out.ShadowMapSamplingPos=mul(In.pos,xLightWorldViewProjection);
Out.RealDistance=Out.ShadowMapSamplingPos.z/xMaxDepth;
return Out;
} // VS_Specular20(.)
// Pixel shader function
float4 PS_Specular20(VertexOutput_Specular20 In) : COLOR
{
float4 diffuseTexture = tex2D(diffuseTextureSampler, In.TexCoords );
float2 ProjectedTexCoords;
//PCF Filter
ProjectedTexCoords[0] = In.ShadowMapSamplingPos.x/In.ShadowMapSamplingPos.w/2.0f +0.5f;
ProjectedTexCoords[1] = -In.ShadowMapSamplingPos.y/In.ShadowMapSamplingPos.w/2.0f +0.5f;
float2 vTexCoords[9];
float fTexelSize=1.0f/1600.0f;
// Generate the tecture co-ordinates for the specified depth-map size
// 4 3 5
// 1 0 2
// 7 6 8
vTexCoords[0]=ProjectedTexCoords;
vTexCoords[1]=ProjectedTexCoords+float2(-fTexelSize,0.0f);
vTexCoords[2]=ProjectedTexCoords+float2(fTexelSize,0.0f);
vTexCoords[3]=ProjectedTexCoords+float2(0.0f,-fTexelSize);
vTexCoords[6]=ProjectedTexCoords+float2(0.0f,fTexelSize);
vTexCoords[4]=ProjectedTexCoords+float2(-fTexelSize,-fTexelSize);
vTexCoords[5]=ProjectedTexCoords+float2(fTexelSize,-fTexelSize);
vTexCoords[7]=ProjectedTexCoords+float2(-fTexelSize,fTexelSize);
vTexCoords[8]=ProjectedTexCoords+float2(fTexelSize,fTexelSize);
float4 Color=(float4)0.0f;
Color.a=1.0f;
if ((saturate(ProjectedTexCoords.x) == ProjectedTexCoords.x) && (saturate(ProjectedTexCoords.y) == ProjectedTexCoords.y))
{
float StoredDepthInShadowMap =0.0f; //tex2D(ShadowMapSampler, ProjectedTexCoords).x;
StoredDepthInShadowMap+=tex2D(shadowMapTextureSampler, vTexCoords[1]).x;
for (int i=0; i<9; i++){
float4 colorDistance=tex2D(shadowMapTextureSampler, vTexCoords[i]);
StoredDepthInShadowMap+=colorDistance.x+colorDistance.y/256.0f+colorDistance.z/65536.0f;
}
StoredDepthInShadowMap/=9.0f;
if ((In.RealDistance.x) <= StoredDepthInShadowMap||StoredDepthInShadowMap==0.0f)
{
float4 ColorComponent = tex2D(diffuseTextureSampler, In.TexCoords);
Color=ColorComponent;
}
else Color=tex2D(diffuseTextureSampler, In.TexCoords)*0.2f;
}
else Color=tex2D(diffuseTextureSampler, In.TexCoords);
return Color;
} // PS_Specular20(.)
// Techniques
technique Specular20
{
pass P0
{
VertexShader = compile vs_2_0 VS_Specular20();
PixelShader = compile ps_2_0 PS_Specular20();
} // pass P0
} // Specular20 |
Partager