So, I've looked around and found some of evolved shaders. I've been playing with his "Flashlight" shader, and it is pretty cool.
But, I've run into some problems. It seems that the shader is intricately linked to the textures. For instance, in the code it shows three textures:
//--------------
// Textures
//--------------
texture LightmapTX
<
string Name="";
>;
sampler2D Lightmap = sampler_state
{
texture = <LightmapTX>;
};
texture BaseTX
<
string Name="";
>;
sampler2D Base = sampler_state
{
texture = <BaseTX>;
};
texture CubeLightTX
<
string Name = "";
>;
sampler CubeLight = sampler_state
{
Texture = <CubeLightTX>;
};
There are three indices for three different textures to be mapped to the object. The light map texture, the diffuse texture, and the flashlight texture.
Now I can get this shader to work on simple objects that have only these three textures, in that exact order. But I can't get the shader to work on anything complex, Like an fpsc level. Now I assume that an fpsc level (compiled by version 1.09), has many more layers of textures than just three, so I assume that is what's causing the problems.
So I was wondering if there was any way to rewrite the shader code to allow for complex scenes with an unknown amount of textures?
Here is the complete fx code:
//====================================================
// Flash Light
// By EVOLVED
//====================================================
//--------------
// un-tweaks
//--------------
matrix WorldVP:WorldViewProjection;
matrix World:World;
//--------------
// tweaks
//--------------
float4 LightPosition = {150.0f, 150.0f, 0.0f, 1.0f};
float LightRange = 1500.0f;
float BrightNess = 1.0f;
float3x3 LightAngle;
float3 LightPosition_1 = {0.0f, 50.0f, 0.0f};
float3 LightColor_1 = {1.0f, 1.0f, 1.0f};
float LightRange_1 = 55.0f;
float3 LightPosition_2 = {50.0f, 50.0f, 0.0f};
float3 LightColor_2 = {1.0f, 0.0f, 0.0f};
float LightRange_2 = 55.0f;
float3 LightPosition_3 = {100.0f, 50.0f, 0.0f};
float3 LightColor_3 = {0.0f, 1.0f, 0.0f};
float LightRange_3 = 55.0f;
float3 LightPosition_4 = {150.0f, 50.0f, 0.0f};
float3 LightColor_4 = {0.0f, 0.0f, 1.0f};
float LightRange_4 = 55.0f;
float3 LightPosition_5 = {0.0f, 50.0f, 50.0f};
float3 LightColor_5 = {1.0f, 1.0f, 1.0f};
float LightRange_5 = 55.0f;
float3 LightPosition_6 = {50.0f, 50.0f, 50.0f};
float3 LightColor_6 = {1.0f, 0.0f, 0.0f};
float LightRange_6 = 55.0f;
//--------------
// Textures
//--------------
texture LightmapTX
<
string Name="";
>;
sampler2D Lightmap = sampler_state
{
texture = <LightmapTX>;
};
texture BaseTX
<
string Name="";
>;
sampler2D Base = sampler_state
{
texture = <BaseTX>;
};
texture CubeLightTX
<
string Name = "";
>;
sampler CubeLight = sampler_state
{
Texture = <CubeLightTX>;
};
//--------------
// structs
//--------------
struct Finput
{
float4 Pos:POSITION;
float2 UVA:TEXCOORD0;
float2 UVB:TEXCOORD1;
float3 Normal:NORMAL;
};
struct Foutput
{
float4 OPos:POSITION;
float2 TexA:TEXCOORD0;
float2 TexB:TEXCOORD1;
float3 Attenuation:TEXCOORD2;
float Diffuse:TEXCOORD3;
float3 CubeLight:TEXCOORD4;
};
struct input
{
float4 Pos:POSITION;
float2 UV:TEXCOORD1;
float3 Normal:NORMAL;
};
struct output
{
float4 OPos:POSITION;
float2 Tex:TEXCOORD0;
float3 Attenuation:TEXCOORD1;
float Diffuse:TEXCOORD2;
};
struct output0
{
float4 OPos:POSITION;
float2 Tex:TEXCOORD0;
float3 Attenuation:TEXCOORD1;
float Diffuse:TEXCOORD2;
float3 Attenuation0:TEXCOORD3;
float Diffuse0:TEXCOORD4;
};
//--------------
// vertex shader
//--------------
Foutput VS(Finput IN)
{
Foutput OUT;
OUT.OPos=mul(IN.Pos,WorldVP);
OUT.TexA=IN.UVA;
OUT.TexB=IN.UVB;
float3 Wnor=mul(IN.Normal,World); Wnor=normalize(Wnor);
float3 WPos=mul(IN.Pos,World);
float3 LightPos=LightPosition-WPos;
OUT.Attenuation=-LightPos/LightRange;
OUT.Diffuse=saturate(0.015f+mul(Wnor,LightPos)*0.015f);
OUT.CubeLight=mul(LightAngle,-LightPos);
return OUT;
}
output VS_L1(input IN)
{
output OUT;
OUT.OPos=mul(IN.Pos,WorldVP);
OUT.Tex=IN.UV;
float3 Wnor=mul(IN.Normal,World); Wnor=normalize(Wnor);
float3 WPos=mul(IN.Pos,World);
float3 LightPos=LightPosition_1-WPos;
OUT.Attenuation=-LightPos/LightRange_1;
OUT.Diffuse=saturate(0.02f+mul(Wnor,LightPos)*0.02f);
return OUT;
}
output0 VS_L2(input IN)
{
output0 OUT;
OUT.OPos=mul(IN.Pos,WorldVP);
OUT.Tex=IN.UV;
float3 Wnor=mul(IN.Normal,World); Wnor=normalize(Wnor);
float3 WPos=mul(IN.Pos,World);
float3 LightPos=LightPosition_1-WPos;
OUT.Attenuation=-LightPos/LightRange_1;
OUT.Diffuse=saturate(0.02f+mul(Wnor,LightPos)*0.02f);
LightPos=LightPosition_2-WPos;
OUT.Attenuation0=-LightPos/LightRange_2;
OUT.Diffuse0=saturate(0.02f+mul(Wnor,LightPos)*0.02f);
return OUT;
}
output VS_L3(input IN)
{
output OUT;
OUT.OPos=mul(IN.Pos,WorldVP);
OUT.Tex=IN.UV;
float3 Wnor=mul(IN.Normal,World); Wnor=normalize(Wnor);
float3 WPos=mul(IN.Pos,World);
float3 LightPos=LightPosition_3-WPos;
OUT.Attenuation=-LightPos/LightRange_3;
OUT.Diffuse=saturate(0.02f+mul(Wnor,LightPos)*0.02f);
return OUT;
}
output0 VS_L4(input IN)
{
output0 OUT;
OUT.OPos=mul(IN.Pos,WorldVP);
OUT.Tex=IN.UV;
float3 Wnor=mul(IN.Normal,World); Wnor=normalize(Wnor);
float3 WPos=mul(IN.Pos,World);
float3 LightPos=LightPosition_3-WPos;
OUT.Attenuation=-LightPos/LightRange_3;
OUT.Diffuse=saturate(0.02f+mul(Wnor,LightPos)*0.02f);
LightPos=LightPosition_4-WPos;
OUT.Attenuation0=-LightPos/LightRange_4;
OUT.Diffuse0=saturate(0.02f+mul(Wnor,LightPos)*0.02f);
return OUT;
}
output VS_L5(input IN)
{
output OUT;
OUT.OPos=mul(IN.Pos,WorldVP);
OUT.Tex=IN.UV;
float3 Wnor=mul(IN.Normal,World); Wnor=normalize(Wnor);
float3 WPos=mul(IN.Pos,World);
float3 LightPos=LightPosition_5-WPos;
OUT.Attenuation=-LightPos/LightRange_5;
OUT.Diffuse=saturate(0.02f+mul(Wnor,LightPos)*0.02f);
return OUT;
}
output0 VS_L6(input IN)
{
output0 OUT;
OUT.OPos=mul(IN.Pos,WorldVP);
OUT.Tex=IN.UV;
float3 Wnor=mul(IN.Normal,World); Wnor=normalize(Wnor);
float3 WPos=mul(IN.Pos,World);
float3 LightPos=LightPosition_5-WPos;
OUT.Attenuation=-LightPos/LightRange_5;
OUT.Diffuse=saturate(0.02f+mul(Wnor,LightPos)*0.02f);
LightPos=LightPosition_6-WPos;
OUT.Attenuation0=-LightPos/LightRange_6;
OUT.Diffuse0=saturate(0.02f+mul(Wnor,LightPos)*0.02f);
return OUT;
}
//--------------
// pixel shader
//--------------
float4 PS(Foutput IN) : COLOR
{
float3 LightM=tex2D(Lightmap,IN.TexA);
LightM=LightM+LightM+LightM+LightM;
float3 Texture=tex2D(Base,IN.TexB);
float PixelLight=1-saturate(dot(IN.Attenuation,IN.Attenuation));
PixelLight=(PixelLight*IN.Diffuse)*BrightNess;
float3 FlashLight=texCUBE(CubeLight,IN.CubeLight)*PixelLight;
return float4(Texture*(LightM+FlashLight),1);
}
float4 PS_L1(output IN) : COLOR
{
float3 Texture=tex2D(Base,IN.Tex);
float PixelLight=1-saturate(dot(IN.Attenuation,IN.Attenuation));
float3 Light=PixelLight*LightColor_1*IN.Diffuse;
return float4(Texture*Light,1);
}
float4 PS_L2(output0 IN) : COLOR
{
float3 Texture=tex2D(Base,IN.Tex);
float PixelLight=1-saturate(dot(IN.Attenuation,IN.Attenuation));
float3 Light=PixelLight*LightColor_1*IN.Diffuse;
PixelLight=1-saturate(dot(IN.Attenuation0,IN.Attenuation0));
Light=Light+PixelLight*LightColor_2*IN.Diffuse0;
return float4(Texture*Light,1);
}
float4 PS_L3(output IN) : COLOR
{
float3 Texture=tex2D(Base,IN.Tex);
float PixelLight=1-saturate(dot(IN.Attenuation,IN.Attenuation));
float3 Light=PixelLight*LightColor_3*IN.Diffuse;
return float4(Texture*Light,1);
}
float4 PS_L4(output0 IN) : COLOR
{
float3 Texture=tex2D(Base,IN.Tex);
float PixelLight=1-saturate(dot(IN.Attenuation,IN.Attenuation));
float3 Light=PixelLight*LightColor_3*IN.Diffuse;
PixelLight=1-saturate(dot(IN.Attenuation0,IN.Attenuation0));
Light=Light+PixelLight*LightColor_4*IN.Diffuse0;
return float4(Texture*Light,1);
}
float4 PS_L5(output IN) : COLOR
{
float3 Texture=tex2D(Base,IN.Tex);
float PixelLight=1-saturate(dot(IN.Attenuation,IN.Attenuation));
float3 Light=PixelLight*LightColor_5*IN.Diffuse;
return float4(Texture*Light,1);
}
float4 PS_L6(output0 IN) : COLOR
{
float3 Texture=tex2D(Base,IN.Tex);
float PixelLight=1-saturate(dot(IN.Attenuation,IN.Attenuation));
float3 Light=PixelLight*LightColor_5*IN.Diffuse;
PixelLight=1-saturate(dot(IN.Attenuation0,IN.Attenuation0));
Light=Light+PixelLight*LightColor_6*IN.Diffuse0;
return float4(Texture*Light,1);
}
//--------------
// techniques
//--------------
technique Light0
{
pass p0
{
vertexShader = compile vs_1_1 VS();
pixelShader = compile ps_1_4 PS();
}
}
technique Light1
{
pass p0
{
vertexShader = compile vs_1_1 VS();
pixelShader = compile ps_1_4 PS();
}
pass p1
{
vertexShader = compile vs_1_1 VS_L1();
pixelShader = compile ps_1_4 PS_L1();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
}
technique Light2
{
pass p0
{
vertexShader = compile vs_1_1 VS();
pixelShader = compile ps_1_4 PS();
}
pass p1
{
vertexShader = compile vs_1_1 VS_L2();
pixelShader = compile ps_1_4 PS_L2();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
}
technique Light3
{
pass p0
{
vertexShader = compile vs_1_1 VS();
pixelShader = compile ps_1_4 PS();
}
pass p1
{
vertexShader = compile vs_1_1 VS_L2();
pixelShader = compile ps_1_4 PS_L2();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
pass p2
{
vertexShader = compile vs_1_1 VS_L3();
pixelShader = compile ps_1_4 PS_L3();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
}
technique Light4
{
pass p0
{
vertexShader = compile vs_1_1 VS();
pixelShader = compile ps_1_4 PS();
}
pass p1
{
vertexShader = compile vs_1_1 VS_L2();
pixelShader = compile ps_1_4 PS_L2();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
pass p2
{
vertexShader = compile vs_1_1 VS_L4();
pixelShader = compile ps_1_4 PS_L4();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
}
technique Light5
{
pass p0
{
vertexShader = compile vs_1_1 VS();
pixelShader = compile ps_1_4 PS();
}
pass p1
{
vertexShader = compile vs_1_1 VS_L2();
pixelShader = compile ps_1_4 PS_L2();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
pass p2
{
vertexShader = compile vs_1_1 VS_L4();
pixelShader = compile ps_1_4 PS_L4();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
pass p3
{
vertexShader = compile vs_1_1 VS_L5();
pixelShader = compile ps_1_4 PS_L5();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
}
technique Light6
{
pass p0
{
vertexShader = compile vs_1_1 VS();
pixelShader = compile ps_1_4 PS();
}
pass p1
{
vertexShader = compile vs_1_1 VS_L2();
pixelShader = compile ps_1_4 PS_L2();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
pass p2
{
vertexShader = compile vs_1_1 VS_L4();
pixelShader = compile ps_1_4 PS_L4();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
pass p3
{
vertexShader = compile vs_1_1 VS_L6();
pixelShader = compile ps_1_4 PS_L6();
AlphaBlendEnable = True;
SrcBlend = One;
DestBlend = One;
}
}
I don't think I need to attach any media, because that isn't what I think the problem is, but if you need me to then tell me.
Thanks for any help.