andriscs

Hi,

I am unable to understand shaders so I use FX Composer. I created (loaded) an effect to one of my meshes and saved its code. When I try to apply it, I can't see anything. I don't know what parameters should I set so I ask for help. How can I make this reflection effect work

Here is the XNA code:

foreach (ModelMesh spheremesh in sphere.Meshes)
            {
                foreach (ModelMeshPart modmeshpart in spheremesh.MeshParts)
                {
                    modmeshpart.Effect = reflection.Clone(graphics.GraphicsDevice);
                }
            }
for (int i = 0; i < spheres.Count; i++)
            {

                    foreach (ModelMesh spheremesh in sphere.Meshes)
                    {
                        Matrix world = transforms_sphere[spheremesh.ParentBone.Index]
                            * Matrix.CreateTranslation((Vector3)spheresIdea) * Matrix.CreateScale(50); ;
                        Matrix view = Matrix.CreateLookAt(cameraPosition, (modelPosition), Vector3.Up);
                        Matrix proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
                                aspectRatio, 1.0f, 100000.0f);

                        foreach (Effect currenteffect in spheremesh.Effects)
                        {
                            currenteffect.CurrentTechnique = currenteffect.Techniques["Untextured"];
                           
                            sphereTrans.Add(new Vector3(world.Translation.X, world.Translation.Y, world.Translation.Z));
                            currenteffect.Parameters["World"].SetValue(world);
                            currenteffect.CommitChanges();

                        }
                           
                        spheremesh.Draw();
                    }

At debugging, I can see that the Effect is in the mesh but at drawing nothing is visible.
What else parameter should I set

Heres is the effect code (long one):

/*************************************************************************



Comments:
A phong-shaded metal-style surface + mirror term.
Textured or untextured surfaces.
Metallic reflections have no appreciable Fresnel term.

******************************************************************************/

float Script : STANDARDSGLOBAL <
string UIWidget = "none";
string ScriptClass = "object";
string ScriptOrder = "standard";
string ScriptOutput = "color";
string Script = "Technique=Technique Untextured:Textured;";
> = 0.8;

/************* UN-TWEAKABLES **************/

half4x4 WorldIT : WorldInverseTranspose < string UIWidget="None"; >;
half4x4 WorldViewProj : WorldViewProjection < string UIWidget="None"; >;
half4x4 World : World < string UIWidget="None"; >;
half4x4 ViewI : ViewInverse < string UIWidget="None"; >;

/************* TWEAKABLES **************/

half3 LightPos : Position <
string Object = "PointLight";
string Space = "World";
> = {100.0f, 100.0f, -100.0f};

half3 LightColor <
string UIName = "Light Color";
string Object = "PointLight";
string UIWidget = "Color";
> = {1.0f, 1.0f, 1.0f};

half3 AmbiColor : Ambient <
string UIName = "Ambient Light Color";
string UIWidget = "Color";
> = {0.07f, 0.07f, 0.07f};

half3 SurfColor : DIFFUSE <
string UIName = "Surface Color";
string UIWidget = "Color";
> = {1.0f, 1.0f, 1.0f};

half SpecExpon : SpecularPower <
string UIWidget = "slider";
half UIMin = 1.0;
half UIMax = 128.0;
half UIStep = 1.0;
string UIName = "Specular Power";
> = 12.0;

half Kd <
string UIWidget = "slider";
half UIMin = 0.0;
half UIMax = 1.0;
half UIStep = 0.05;
string UIName = "Diffuse (from dirt)";
> = 0.1;

half Kr <
string UIWidget = "slider";
half UIMin = 0.0;
half UIMax = 1.0;
half UIStep = 0.05;
string UIName = "Reflection";
> = 0.4;

//////////

texture ColorTexture : DIFFUSE <
string ResourceName = "default_color.dds";
string ResourceType = "2D";
>;

sampler2D ColorSampler = sampler_state {
Texture = <ColorTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

texture CubeEnvMap : ENVIRONMENT <
string ResourceName = "default_reflection.dds";
string ResourceType = "Cube";
>;

samplerCUBE EnvSampler = sampler_state {
Texture = <CubeEnvMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

/************* DATA STRUCTS **************/

/* data from application vertex buffer */
struct appdata {
half3 Position : POSITION;
half4 UV : TEXCOORD0;
half4 Normal : NORMAL;
};

/* data passed from vertex shader to pixel shader */
struct vertexOutput {
half4 HPosition : POSITION;
half4 TexCoord : TEXCOORD0;
half3 LightVec : TEXCOORD1;
half3 WorldNormal : TEXCOORD2;
half3 WorldView : TEXCOORD5;
};

/*********** vertex shader ******/

vertexOutput mainVS(appdata IN) {
vertexOutput OUT;
half4 normal = normalize(IN.Normal);
OUT.WorldNormal = mul(normal, WorldIT).xyz;
half4 Po = half4(IN.Position.xyz,1);
half3 Pw = mul(Po, World).xyz;
OUT.LightVec = normalize(LightPos - Pw);
OUT.TexCoord = IN.UV;
OUT.WorldView = normalize(ViewI[3].xyz - Pw);
OUT.HPosition = mul(Po, WorldViewProj);
return OUT;
}

/********* pixel shader ********/

void metal_refl_shared(vertexOutput IN,
out half3 DiffuseContrib,
out half3 SpecularContrib,
out half3 ReflectionContrib)
{
half3 Ln = normalize(IN.LightVec);
half3 Nn = normalize(IN.WorldNormal);
half3 Vn = normalize(IN.WorldView);
half3 Hn = normalize(Vn + Ln);
half4 litV = lit(dot(Ln,Nn),dot(Hn,Nn),SpecExpon);
DiffuseContrib = litV.y * Kd * LightColor + AmbiColor;
SpecularContrib = litV.z * LightColor;
half3 reflVect = -reflect(Vn,Nn);
ReflectionContrib = Kr * texCUBE(EnvSampler,reflVect).xyz;
}

half4 mainPS(vertexOutput IN) : COLOR {
half3 diffContrib;
half3 specContrib;
half3 reflColor;
metal_refl_shared(IN,diffContrib,specContrib,reflColor);
half3 result = diffContrib +
(SurfColor * (specContrib + reflColor));
return half4(result,1);
}

half4 mainPS_t(vertexOutput IN) : COLOR {
half3 diffContrib;
half3 specContrib;
half3 reflColor;
metal_refl_shared(IN,diffContrib,specContrib,reflColor);
half3 map = tex2D(ColorSampler,IN.TexCoord.xy).xyz;
half3 result = diffContrib +
(SurfColor * map * (specContrib + reflColor));
return half4(result,1);
}

/*************/

technique Untextured <
string Script = "Pass=p0;";
> {
pass p0 <
string Script = "Draw=geometry;";
> {
VertexShader = compile vs_2_0 mainVS();
ZEnable = true;
ZWriteEnable = true;
CullMode = None;
PixelShader = compile ps_2_0 mainPS();
}
}

technique Textured <
string Script = "Pass=p0;";
> {
pass p0 <
string Script = "Draw=geometry;";
> {
VertexShader = compile vs_2_0 mainVS();
ZEnable = true;
ZWriteEnable = true;
CullMode = None;
PixelShader = compile ps_2_0 mainPS_t();
}
}

/***************************** eof ***/


I have the required texture in every directory, but nothing is visible. What else should I set to make them visible Sorry for the long post.



Re: XNA Game Studio Express Effect file parameters

andriscs

Hi,

If I use WorldViewProj parameters, I have my meshes visible but full black. Any idea why





Re: XNA Game Studio Express Effect file parameters

Jon Watte

If data is black, then the pixel shader is outputting black. This could be for many reasons, such as not finding a texture when texturing, not calculating lighting correctly, some input parameters being 0, etc.

In general, to debug drawing problems, I find that Pix for Windows is very helpful. Bind some hotkey to "dump single frame," then have Pix start the program. Dump a frame, and quit the program. Now, step through the drawing command stream until you get to your problem area, and investigate the device state -- constants, textures, bound data, etc. You can also right-click on a pixel and choose "debug this pixel" to debug the pixel shader (although at assembly level).






Re: XNA Game Studio Express Effect file parameters

Fluxtah

Your shader has a lot of parameters, and in your example code, you do not set any of them, some of them have default values, but some of them don't.

edit: actually if you have set the worldviewproj looking through the shader code this is the only one thats being used.

edit2: have you tried setting the light position, it may be behind the model I noticed its z component is set to -100.0f, try inverting that. (XNA's positive z axis points out of the screen)

 





Re: XNA Game Studio Express Effect file parameters

andriscs

Hi,

So you say it is enough to set the WVP parameter for the shader code I set the light but still black spheres appear. I don't understand that light position. Is it relative to the mesh Or it has a fix position in the world





Re: XNA Game Studio Express Effect file parameters

Fluxtah

Actually sorry I did not look at it to well I thought it was just the worldviewprojection but it seems its working on the other input matrices also:

vertexOutput mainVS(appdata IN) {
vertexOutput OUT;
half4 normal = normalize(IN.Normal);
OUT.WorldNormal = mul(normal, WorldIT).xyz;
half4 Po = half4(IN.Position.xyz,1);
half3 Pw = mul(Po, World).xyz;
OUT.LightVec = normalize(LightPos - Pw);
OUT.TexCoord = IN.UV;
OUT.WorldView = normalize(ViewI[3].xyz - Pw);
OUT.HPosition = mul(Po, WorldViewProj);
return OUT;
}

Looks like it needs all of these set:

half4x4 WorldIT : WorldInverseTranspose < string UIWidget="None"; >;
half4x4 WorldViewProj : WorldViewProjection < string UIWidget="None"; >;
half4x4 World : World < string UIWidget="None"; >;
half4x4 ViewI : ViewInverse < string UIWidget="None"; >;

I am not an expert at HLSL but I do use them and code my own 'basic' shaders, so I am not the best person to help, but I tried :)





Re: XNA Game Studio Express Effect file parameters

andriscs

Hi,

I tested the project with another effect file (with a simpler one), and it was working. So - if we assume that this effect file contains no error - the only problem could be the wrong position of the lighting, because the PS produces blackness. I tried to set the LightPos parameter to other values but no luck yet. If someone tries to help me, I put the 2 files here.

Edit:
I just noticed the new post. So I have to set the other matrices. But I don't know how to get them in my project. The "I" stands for Inverted, so should I use Matrix.Invert(view) and Matrix.Invert(world) to produce those matrices

Edit2:
I set the inverted matrices as I mentioned above, now I have white spheres with reflection from some view angles. The result should be a taut picture on the sphere with reflection. I guess my matrices aren't correct.

                            currenteffect.Parameters["WorldViewProj"].SetValue(world*view*proj);
                           currenteffect.Parameters["World"].SetValue(world);
                            currenteffect.Parameters["ViewI"].SetValue(Matrix.Invert(view));
                           
                            currenteffect.Parameters["WorldIT"].SetValue(Matrix.Invert(world) );                           currenteffect.Parameters["ColorTexture"].SetValue(colorTex);
                            currenteffect.Parameters["CubeEnvMap"].SetValue(reflectionTex);
                           
                            currenteffect.CommitChanges();