Hi!
I've been trying to use a custom shader on an entity. All I see is the unshaded entity.
To set up an entity using a shader like this, you need to apply texture stages. But how does FPSC handle this?
The custom shader I try to use is from dark shader. Do I need to modify the code somehow to make them work. I know little when it comes to shader programming, but I can see the code for the textures part looks different when I compare my code with bumpent.fx that is a stock shader.
Here is the code for the custom shader:
string Description = "This shader takes two textures and blends them by the blend factor, then tints the output with a diffuse color";
string Thumbnail = "BlendShader.png";
float4x4 WorldViewProj : WorldViewProjection;
float4 diffuse
<
string SasUIControl = "color picker";
> = { 1.000000, 1.000000, 1.000000, 0.000000 };
float blend
<
string SasUIControl = "slider";
float SasUIMax = 1.0;
float SasUIMin = 0.0;
float SasUIStep = 0.01;
> = 0.500000;
texture Texture1
<
string ResourceName = "Wall_D2.png";
>;
sampler texture1 = sampler_state
{
Texture = <Texture1>;
MinFilter = Linear; MagFilter = Linear; MipFilter = Linear;
AddressU = Wrap; AddressV = Wrap;
};
texture Texture2
<
string ResourceName = "Wall_I.tga";
>;
sampler texture2 = sampler_state
{
Texture = <Texture2>;
MinFilter = Linear; MagFilter = Linear; MipFilter = Linear;
AddressU = Wrap; AddressV = Wrap;
};
struct app_in
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
struct vs_out
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float2 uv1 : TEXCOORD1;
};
vs_out BlendVS( app_in IN )
{
vs_out OUT;
OUT.pos = mul( IN.pos, WorldViewProj );
OUT.uv = IN.uv;
OUT.uv1 = IN.uv;
return OUT;
}
float4 BlendPS( vs_out IN ) : COLOR
{
blend = saturate( blend );
return diffuse * ( tex2D( texture1, IN.uv )*(1.0-blend) + tex2D( texture2, IN.uv1 )*blend );
}
technique MyShader
{
pass p0
{
VertexShader = compile vs_1_1 BlendVS( );
PixelShader = compile ps_1_1 BlendPS( );
}
}
bumpent.fx:
//
// Bump + Diffuse (entity bump)
//
/************* UNTWEAKABLES **************/
float4x4 WorldIT : WorldInverseTranspose;
float4x4 WorldViewProj : WorldViewProjection;
float4x4 World : World;
float4x4 ViewInv : ViewInverse;
/************* SURFACE **************/
string XFile = "x.x";
float4 FixedLightSource
<
string UIType = "Fixed Light Source";
> = {0.0f, 50000.0f, 0.0f, 0.0f};
float4 AmbiColor : Ambient
<
string UIName = "Ambient Light Color";
> = {0.5f, 0.5f, 0.5f, 1.0f};
float4 SurfColor : Diffuse
<
string UIName = "Surface Color";
string UIType = "Color";
> = {1.0f, 1.0f, 1.0f, 1.0f};
float Bumpy
<
string UIType = "slider";
float UIMin = 0.0;
float UIMax = 10.0;
float UIStep = 0.1;
string UIName = "bump power";
> = 5.0;
/************* TEXTURES **************/
texture colorTexture2 : DiffuseMap
<
string Name = "D.tga";
string type = "2D";
>;
texture normalTexture : DiffuseMap
<
string Name = "N.tga";
string type = "2D";
>;
sampler2D colorSampler2 = sampler_state
{
Texture = <colorTexture2>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D normalSampler = sampler_state
{
Texture = <normalTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
/************* DATA STRUCTS **************/
/* data from application vertex buffer */
struct appdata {
float3 Position : POSITION;
float4 UV1 : TEXCOORD0;
float4 Normal : NORMAL;
float4 Tangent : TANGENT0;
float4 Binormal : BINORMAL0;
};
/* data passed from vertex shader to pixel shader */
struct vertexOutput {
float4 HPosition : POSITION;
float4 TexCoord1 : TEXCOORD0;
float3 LightVec : TEXCOORD1;
float3 WorldNormal : TEXCOORD2;
float3 WorldEyeVec : TEXCOORD3;
float3 WorldTangent : TEXCOORD4;
float3 WorldBinorm : TEXCOORD5;
};
/*********** vertex shader ******/
vertexOutput mainVS(appdata IN)
{
vertexOutput OUT;
OUT.WorldNormal = mul(IN.Normal, WorldIT).xyz;
OUT.WorldTangent = mul(IN.Tangent, WorldIT).xyz;
OUT.WorldBinorm = mul(IN.Binormal, WorldIT).xyz;
float4 tempPos = float4(IN.Position.x,IN.Position.y,IN.Position.z,1.0);
float3 worldSpacePos = mul(tempPos, World).xyz;
OUT.LightVec = FixedLightSource - worldSpacePos;
OUT.TexCoord1 = IN.UV1;
OUT.WorldEyeVec = normalize(ViewInv[3].xyz - worldSpacePos);
OUT.HPosition = mul(tempPos, WorldViewProj);
return OUT;
}
/********* pixel shader ********/
float4 mainPS(vertexOutput IN) : COLOR
{
float4 map = tex2D(colorSampler2,IN.TexCoord1.xy);
float3 bumps = Bumpy * (tex2D(normalSampler,IN.TexCoord1.xy).xyz-(0.5).xxx);
float3 Ln = normalize(IN.LightVec);
float3 Nn = normalize(IN.WorldNormal);
float3 Tn = normalize(IN.WorldTangent);
float3 Bn = normalize(IN.WorldBinorm);
float3 Nb = Nn + (bumps.x * Tn + bumps.y * Bn);
Nb = normalize(Nb);
float4 mapcolor = map * (0.5+dot(Ln,Nb));
float intensity = dot(Ln,Nb) * (AmbiColor*0.5);
float4 result = (mapcolor * (SurfColor+AmbiColor)) + intensity;
result.w=map.w;
return result;
}
/****** technique *******/
technique dx9textured
{
pass p0
{
// lighting
Lighting = FALSE;
SpecularEnable = FALSE;
FogEnable = FALSE;
// same vertex shader for both DX9 techniques
VertexShader = compile vs_1_1 mainVS();
PixelShader = compile ps_2_0 mainPS();
}
}
/***************************** eof ***/
Here is my custom fpe:
;Entity Spec
;Header
desc = Wall blend shader
;AI
aiinit = appear1.fpi
aimain =
aidestroy =
;Spawn
spawnmax = 0
spawndelay = 0
spawnqty = 0
;Orientation
model = cube.dbo
offx = 0
offy = 0
offz = 0
rotx = 0
roty = 0
rotz = 0
materialindex = 3
textured = Wall_D2.png
effect = blendshader.dbs
;identity details
strength = 0
isimmobile = 0
physics = 2
speed = 50
;Animationinfo
animmax = 0
textures, shader, fpe file and dbo file are placed in the same folder within the fpsc/files directory
Thankful for help!
BEST!
www.memblockgames.com