Is anyone else successfully using the commands:
set effect constant vector element and
get object effect?
I am trying to use the normalmap.fx shader that comes with DarkShader. It has the light information in an array. It has taken me forever to find the above commands (that were added with DarkShader). Now, I am pretty sure I understand how they are supposed to work but can't get them to do anything. It has NO effect on my model when I change the values.
Here is my DBpro code:
sync on
sync rate 0
load object "1.X",1
load effect "normalmap.fx",1,1
set object effect 1,1
null = make vector4(1)
null = get object effect(1,-1)
for i = 0 to 7
set vector4 1,0,50.0,-30.0,200.0
set effect constant vector element "LightPos",i,1
set vector4 1,1.0,1.0,1.0,1.0
set effect constant vector element "LightColor",i,1
next i
set vector4 1,0.7,0.7,0.7,0.0
set effect constant vector 1,"AmbientColor",1
null = delete vector4( 1 )
release effect pointer
position camera 0,60,-30
do
set cursor 0,0
sync mask %001 : sync
loop
Here is the shader:
string Description = "This shader uses preview lights to produce per pixel normal mapped lighting. This shader requires lights.";
string Thumbnail = "Normal Mapping.png";
//--------------
//Untweakables
//--------------
float4x4 WorldViewProj : WorldViewProjection;
float4x4 World : World;
float4x4 WorldT : WorldTranspose;
float4x4 WorldIT : WorldInverseTranspose;
float4 LightPos[8] : LIGHTPOSITION;
float3 LightColor[8] : LIGHTCOLOR;
float3 AmbientColor : AMBIENTCOLOR;
float3 eyepos : CameraPosition;
float time : TIME;
//--------------
//Tweakables
//--------------
float SpecularPower1
<
string UIWidget = "slider";
float UIMax = 30;
float UIMin = 1;
> = 1.000000;
texture BaseTex
<
string ResourceName = "diffuse.bmp";
>;
sampler diffuse_smp = sampler_state
{
Texture = <BaseTex>;
MinFilter = Anisotropic;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
texture NormalMap
<
string ResourceName = "Normal.bmp";
>;
sampler normalmap_smp = sampler_state
{
Texture = <NormalMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
struct app_in
{
float4 pos : POSITION;
float3 normal : NORMAL0;
float3 tangent : TANGENT0;
float3 binormal : BINORMAL0;
float2 uv : TEXCOORD0;
};
struct vs_out
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
float3 tangent : TEXCOORD2;
float3 binormal : TEXCOORD3;
float4 wpos : TEXCOORD4;
};
vs_out VS( app_in IN )
{
vs_out OUT;
float4 pos = mul( IN.pos, WorldViewProj );
OUT.pos = pos;
OUT.wpos = mul( IN.pos, World );
OUT.uv = IN.uv;
float3 normal = normalize(mul(IN.normal.xyz,(float3x3)World ));
float3 tangent = normalize(mul(IN.tangent.xyz,(float3x3)World ));
float3 binormal = normalize(mul(IN.binormal.xyz,(float3x3)World ));
//smooth out the tangents and binormals with the normals
float3 b = normalize(cross( normal,tangent ));
b *= sign(dot(b,binormal));
float3 t = normalize(cross( normal,b ));
t *= sign(dot(t,tangent));
float3 t2 = normalize(cross( normal,binormal ));
t2 *= sign(dot(t2,tangent));
float3 b2 = normalize(cross( normal,t2 ));
b2 *= sign(dot(b2,binormal));
//pass normal, tangent, and binormal to pixel shader
OUT.normal = normal;
OUT.tangent = normalize((t+t2)*0.5);
OUT.binormal = normalize((b+b2)*0.5);
return OUT;
}
float luminance ( float3 rgb )
{
return rgb.r*0.3 + rgb.g*0.59 + rgb.b*0.11;
}
float4 PS( vs_out IN, uniform int numLights ) : COLOR
{
float3 color = AmbientColor;
float3 n = normalize(IN.normal);
float3 t = normalize(IN.tangent);
float3 b = normalize(IN.binormal);
//build transpose matrix
float3x3 TangentSpace = {t,b,n};
TangentSpace = transpose(TangentSpace);
n = normalize(tex2D(normalmap_smp, IN.uv)*2 - 1);
float3 e = normalize(eyepos - IN.wpos);
float eyeDist = length(eyepos - IN.wpos);
e = mul(e,TangentSpace);
float4 texColor = tex2D( diffuse_smp, IN.uv );
//cycle through all lights, number depending on technique chosen
for ( int i = 0; i < numLights; i++ )
{
float range = LightPos[i].w;
if ( range > 0 )
{
float3 l = (LightPos[i].xyz - IN.wpos.xyz);
l = mul(l,TangentSpace);
//calculate attenuation
float dist = length(l);
float att = saturate((range-dist) / range);
//calculate diffuse lighting
l = normalize(l);
float diffuse = saturate(dot(n,l));
//caculate specular lighting
float3 h = normalize(l+e);
float spec = pow( saturate(dot(n,h)), SpecularPower1 )*(1-pow(1-diffuse,10));
//add to final color
color += (diffuse * att * LightColor[i]) + (spec * att * LightColor[i] * smoothstep(0.3,0.6,luminance(texColor.xyz)));
}
}
return float4(color,1.0) * texColor;
}
//choose the technique corresponding to the number of lights in your application
technique PerPixelLighting
{
pass p0
{
VertexShader = compile vs_2_0 VS( );
PixelShader = compile ps_2_0 PS( 0 );
}
}
technique DBLight1
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 1 );
}
}
technique DBLight2
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 2 );
}
}
technique DBLight3
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 3 );
}
}
technique DBLight4
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 4 );
}
}
technique DBLight5
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 5 );
}
}
technique DBLight6
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 6 );
}
}
technique DBLight7
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 7 );
}
}
technique DBLight8
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 8 );
}
}
The ambient light works. I just can't modify any of the 8 lights that are in the array. The frustrating part is that it works nicely in DarkShader. I wish it exported the light settings in the example code.
Any help is appreciated!
Thanks!
Ron

a.k.a WOLF!