I would be awfully glad if somebody could give me a hint and show me how to work with fx. The story so far:
For my first fx test I want to apply a glass material on a limb. I constructed a saucer dummy object consisting of 4 limbs. One of these limbs is the canopy the glass material is supposed to go. I exported the "Colored Glass 2" from Dark Shader, loaded the fx file (to which I made no changes at all) and applied it to the limb.
The program runs OK, but the glass doesn't show as it should: I have no clue where and how I should tell the shader about the background and reflection textures.
the dba code:
rem realtime scaling
global realtimeLastTimeMS = 0
global realtimeNewTimeMs = 0
global realtimeFrame# = 0.0
flush video memory
testWindow()
set spot light 0, 30, 120
color light 0, 255, 255, 255
set light range 0, 5000
position light 0, -20, 160, 400
point light 0, 0, 0, 0
set ambient light 40
color ambient light rgb(255, 255, 255)
saucer = 100
load object "saucer1.x", saucer
colors = 100
load image "swatch.png", colors
texture object saucer, colors
set object transparency saucer, 0
set object filter saucer, 2
set object cull saucer, 0
set object light saucer, 1
set object ambient saucer, 1
set object fog saucer, 1
position object saucer, 0, 100, 0
yrotate object saucer, -90
scale object saucer, 100, 100, 100
autocam off
position camera 0, 20, 120, 400
point camera 0, 0, 0, 0
rem load and apply effect
glassFX = 1
load effect "myGlass.fx", glassFX, 0
canopy = findLimbByName(saucer, "canopy")
if canopy > -1 then set limb effect saucer, canopy, glassFX
rem -----------------------------------------------------------------
sync on
sync rate 0
realtimeLastTimeMS = timer()
saucerTurnSpeed = 50
repeat
rem get realtime frame
nowMS = timer()
realtimeFrame# = (realtimeFrame# + (nowMS - realtimeLastTimeMS) * 0.001) * 0.5
realtimeLastTimeMS = nowMS
control camera using arrowkeys 0, 1, 1
yrotate object saucer, wrapvalue(object angle y(saucer) - saucerTurnSpeed * realtimeFrame#)
text 20, 20, str$(screen fps())
sync
until mouseclick()
end
rem -----------------------------------------------------------------
function findLimbByName(objectN, limbName$)
limbN = 0
foundLimbN = -1
while limb exist(objectN, limbN)
if upper$("M_" + limbName$) = upper$(limb name$(objectN, limbN))
foundLimbN = limbN
exit
endif
inc limbN
endwhile
endfunction foundLimbN
function testWindow()
set window layout 0, 0, 0
set window position (desktop width() - screen width()) / 2, (desktop height() - screen height()) / 2
cls
endfunction
the myGlass.fx file:
string Description = "This shader produces a colored glass effect with refraction, distorting the view through the glass and requires a higher pixel shader version";
string Thumbnail = "Colored Glass 2.png";
float4x4 WorldViewProj : WorldViewProjection;
float4x4 World : World;
float4x4 WorldView : WorldView;
float4 eyePos : CameraPosition;
float Opacity
<
string SasUIControl = "slider";
float SasUIMax = 1.0;
float SasUIMin = 0.0;
float SasUIStep = 0.01;
> = 0.080000;
float4 diffuse : COLOR
<
string SasUIControl = "Color Picker";
> = { 1.000000, 0.627451, 1.000000, 0.000000 };
texture Background : DIFFUSE
<
string ResourceName = "";
string Type = "2D";
string Dynamic = "Background";
>;
sampler texture1 = sampler_state
{
Texture = <Background>;
MinFilter = Anisotropic;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
texture ReflectionTex
<
string ResourceName = "";
string ResourceType = "Cube";
string Dynamic = "Reflection";
>;
samplerCUBE Reflection_smp = sampler_state
{
Texture = <ReflectionTex>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
struct app_in
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct vs_out
{
float4 pos : POSITION;
float4 projUV : TEXCOORD0;
float3 normal : TEXCOORD1;
float3 vNormal : TEXCOORD2;
float3 wPos : TEXCOORD3;
};
vs_out DefaultVertexShader( app_in IN )
{
vs_out OUT;
OUT.pos = mul( IN.pos, WorldViewProj );
float4 wPos = mul( IN.pos, World );
float3 wNormal = mul( IN.normal, (float3x3)World );
float3 vNormal = mul( IN.normal, (float3x3)WorldViewProj );
OUT.wPos = wPos.xyz;
OUT.normal = normalize( wNormal );
OUT.vNormal = normalize( vNormal );
OUT.projUV = float4( OUT.pos.x*0.5 + 0.5*OUT.pos.w, 0.5*OUT.pos.w - OUT.pos.y*0.5, OUT.pos.w, OUT.pos.w );
return OUT;
}
float4 DefaultPixelShader( vs_out IN ) : COLOR
{
float2 uv = IN.projUV.xy / IN.projUV.w;
//float3 viewDir = normalize(eyePos - IN.wPos);
float3 viewDir = normalize(eyePos);
float3 Dir = reflect( -viewDir, IN.normal );
float4 refl = texCUBE( Reflection_smp, Dir );
refl = refl*refl*3;
float angle = 1 - saturate( dot( IN.normal, viewDir ) );
uv += IN.vNormal.xy * angle * float2(-0.2,0.2);
float4 bkgnd = tex2D( texture1, uv );
bkgnd = lerp( bkgnd, float4(1,1,1,1), Opacity );
float angle2 = 1 - angle*angle;
float4 color = lerp( refl, bkgnd*diffuse, angle2 );
return float4(color.xyz,1);
}
technique MyShader
{
pass p0
{
VertexShader = compile vs_1_1 DefaultVertexShader( );
PixelShader = compile ps_2_0 DefaultPixelShader( );
}
}
(all files in the attached zip)