OasisGames

I'm currently looking into HLSL and am using an effect file that came with Max 9 (StandardFX.fx), which has all the basic effects along with bumps (normal and parallax). When I go to draw my model with the following code:

foreach (ModelMesh mesh in myModel.Meshes)
{
foreach (Effect effect in mesh.Effects)
{

effect.CurrentTechnique = effect.Techniques["Default"];
Matrix WorldMatrix = Matrix.CreateRotationZ(modelRotationGamma) * Matrix.CreateRotationX(modelRotationAlpha) * Matrix.CreateRotationY(modelRotation)
* Matrix.CreateTranslation(modelPosition);
Matrix ViewMatrix = Matrix.CreateLookAt(cameraPosition, camLook, Vector3.Up);
Matrix ProjMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
aspectRatio, 1.0f, 1000000.0f);
effect.Parameters["World"].SetValue(WorldMatrix);
effect.Parameters["View"].SetValue(ViewMatrix);
effect.Parameters["Projection"].SetValue(ProjMatrix);
}
mesh.Draw();
}

The model doesn't appear.
The model is loaded with the following code:
myModel = content.Load<Model>("Content\\Models\\p1_pencil");
Texture diffuse = content.Load<Texture>("Content\\banshee");
Texture specular = content.Load<Texture>("Content\\banshee_specs");
CompiledEffect compiledEffect = Effect.CompileEffectFromFile("Content\\StandardFX.fx", null, null, CompilerOptions.None, TargetPlatform.Windows);
daEffect = new Effect(graphics.GraphicsDevice, compiledEffect.GetEffectCode(), CompilerOptions.None, null);
foreach (ModelMesh mesh in myModel.Meshes)
{
for (int aa = 0; aa < mesh.MeshParts.Count; aa++)
{
mesh.MeshParts[aa].Effect = daEffect.Clone(graphics.GraphicsDevice);
mesh.MeshParts[aa].Effect.Parameters["g_TopTexture"].SetValue(diffuse);
mesh.MeshParts[aa].Effect.Parameters["g_SpecularTexture"].SetValue(specular);
mesh.MeshParts[aa].Effect.Parameters["g_TopDiffuseEnable"].SetValue(true);
mesh.MeshParts[aa].Effect.Parameters["g_SpecularEnable"].SetValue(true);
}
}
The effect file works in Max and nVidea FX Composer, and I've checked to make sure I'm setting the correct parameters. What am I doing wrong
EDIT: If you need to see the effect file to help me out, I'll post it, but otherwise, I'd rather not for now...


Re: XNA Game Studio Express Effect Textured Model Doesn't Render

Catalin Zima

use PIX for windows to see the model before and after applying the Effect.

You can find it in the DirectX SDK





Re: XNA Game Studio Express Effect Textured Model Doesn't Render

OasisGames

Catalin Zima wrote:

use PIX for windows to see the model before and after applying the Effect.

You can find it in the DirectX SDK


I have no idea how I would do that - and I know the model works with a BasicEffect, so there is no point in doing that.




Re: XNA Game Studio Express Effect Textured Model Doesn't Render

Catalin Zima

well, if the model works with basic Effect, then your problem is probably in the effect file



Re: XNA Game Studio Express Effect Textured Model Doesn't Render

OasisGames

I highly doubt it's a problem with the file:

// This is used by 3dsmax to load the correct parser
string ParamID = "0x003";

float Script : STANDARDSGLOBAL <
string UIWidget = "none";
string ScriptClass = "object";
string ScriptOrder = "standard";
string ScriptOutput = "color";
string Script = "Technique=Default;";
> = 0.8; // version #


// light direction (view space)
float3 g_LightDir : Direction <

> = {-0.577, -0.577, 0.577};

float4 g_LightPos : POSITION <
string UIName = "Light Position";
string Object = "PointLight";
string Space = "World";
int refID = 0;
> = {-0.577, -0.577, 0.577,0.0};


float4 g_LightCol : LIGHTCOLOR
<
int LightRef = 0;
string UIWidget = "None";
> = float4(0.0f, 0.0f, 0.0f, 0.0f);

// material reflectivity
float4 k_a <
string UIName = "Ambient";
> = float4( 0.0f, 0.0f, 0.0f, 1.0f ); // ambient

float4 k_d <
string UIName = "Diffuse";
> = float4( 0.47f, 0.47f, 0.47f, 1.0f ); // diffuse

float4 k_s <
string UIName = "Specular";
> = float4( 1.0f, 1.0f, 1.0f, 1.0f ); // diffuse // specular

int n<
string UIName = "Specular Power";
string UIType = "IntSpinner";
float UIMin = 0.0f;
float UIMax = 50.0f;
> = 15;

bool g_AlphaVertex <
string UIName = "Vertex Alpha";
> = true;

bool g_AddVertexColor <
string UIName = "Add Vertex Color";
> =false;

bool g_UseParallax <
string UIName = "Normal Map Parallax";
> =false;

float g_BumpScale <
string UIName = "Bump Amount";
float UIMin = 0.0f;
float UIMax = 5.0f;
float UIStep = 0.01f;
> = 1.0f;


float g_ParallaxScale <
string UIName = "Parallax Scale";
float UIMin = -0.50f;
float UIMax = 0.5f;
float UIStep = 0.01f;
> = 0.02f;

float g_ParallaxBias <
string UIName = "Parallax Bias";
float UIMin = -0.5f;
float UIMax = 0.5f;
float UIStep = 0.01f;
> = 0.0f;

bool g_AmbientOccEnable <
string UIName = "Ambient Occlusion Enable";
> = false;

texture g_AmbientOccTexture <
string UIName = "Ambient Occlusion";
int Texcoord = 4;
int MapChannel = 1;
>;

bool g_TopDiffuseEnable <
string UIName = "Top Diffuse Color Enable";
> = false;

texture g_TopTexture : DiffuseMap<
string UIName = "Top Diffuse Map ";
int Texcoord = 0;
int MapChannel = 1;
>;

bool g_BottomDiffuseEnable <
string UIName = "Bottom Diffuse Color Enable";
> = false;

texture g_BottomTexture <
string UIName = "Bottom Diffuse";
int Texcoord = 5;
int MapChannel = 1;
>;

bool g_SpecularEnable <
string UIName = "Specular Enable";
> = false;

texture g_SpecularTexture <
string UIName = "Specular";
int Texcoord = 6;
int MapChannel = 1;
>;

bool g_NormalEnable <
string UIName = "Normal Enable";
> = false;

texture g_NormalTexture <
string UIName = "Normal";
int Texcoord = 7;
int MapChannel = 1;
>;

bool g_ReflectionEnable <
string UIName = "Reflection Enable";
> = false;

texture g_ReflectionTexture <
string UIName = "Reflection";
string ResourceType = "CUBE";
>;
float g_ReflectAmount <
string UIName = "Reflection Amount";
float UIMin = 0.0f;
float UIMax = 2.0f;
float UIStep = 0.01f;
> = 1.0f;


sampler2D g_AmbientOccSampler = sampler_state
{
Texture = <g_AmbientOccTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};


sampler2D g_TopSampler = sampler_state
{
Texture = <g_TopTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

sampler2D g_BottomSampler = sampler_state
{
Texture = <g_BottomTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

sampler2D g_SpecularSampler = sampler_state
{
Texture = <g_SpecularTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

sampler2D g_NormalSampler = sampler_state
{
Texture = <g_NormalTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};

samplerCUBE g_ReflectionSampler = sampler_state
{
Texture = <g_ReflectionTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};



// transformations
float4x4 World : WORLD;
float4x4 WorldI : WORLDI;
float4x4 View : VIEW;
float4x4 ViewI : VIEWI;
float4x4 Projection : PROJECTION;
float4x4 WorldViewProj : WORLDVIEWPROJ;
float4x4 WorldView : WORLDVIEW;

int texcoord1 : Texcoord
<
int Texcoord = 1;
int MapChannel = 0;
string UIWidget = "None";
>;

int texcoord2 : Texcoord
<
int Texcoord = 2;
int MapChannel = -2;
string UIWidget = "None";
>;

int texcoord3 : Texcoord
<
int Texcoord = 3;
int MapChannel = -1;
string UIWidget = "None";
>;


float3 NormalCalc(float3 mapNorm, float BumpScale)
{
float3 v = {0.5f,0.5f,1.0f};
mapNorm = lerp(v, mapNorm, BumpScale );
mapNorm = ( mapNorm * 2.0f ) - 1.0f;
return mapNorm;
}


struct VSIn
{
float4 Position : POSITION;
float3 Normal : NORMAL;
float3 Tangent : TANGENT;
float3 BiNormal : BINORMAL;
float2 UV0 : TEXCOORD0;
float3 Colour : TEXCOORD1;
float3 Alpha : TEXCOORD2;
float3 Illum : TEXCOORD3;
float3 UV1 : TEXCOORD4;
float3 UV2 : TEXCOORD5;
float3 UV3 : TEXCOORD6;
float3 UV4 : TEXCOORD7;

};


struct VSOut
{
float4 Position : POSITION;
float4 UV0 : TEXCOORD0;
float3 LightDir : TEXCOORD1;
float3 Normal : TEXCOORD2;
float3 ViewDir : TEXCOORD3;
float4 UV1 : TEXCOORD4;
float2 UV2 : TEXCOORD5;
float2 UV3 : TEXCOORD6;
float2 UV4 : TEXCOORD7;

};

struct PSIn
{
float4 UV0 : TEXCOORD0;
float3 LightDir : TEXCOORD1;
float3 Normal : TEXCOORD2;
float3 ViewDir : TEXCOORD3;
float4 UV1 : TEXCOORD4;
float2 UV2 : TEXCOORD5;
float2 UV3 : TEXCOORD6;
float2 UV4 : TEXCOORD7;

};

struct PSOut
{
float4 Colour : COLOR;
};


VSOut VS(VSIn vsIn)
{
VSOut vsOut;
vsOut.Position = mul(vsIn.Position, WorldViewProj);
float4 colour;
colour.rgb = vsIn.Colour * vsIn.Illum;
colour.a = vsIn.Alpha.x;

vsOut.UV0.z = colour.r;
vsOut.UV0.a = colour.g;

vsOut.UV1.z = colour.b;
vsOut.UV1.a = colour.a;


vsOut.Normal = vsIn.Normal;
float4 g_CamPos = mul(ViewI[3],WorldI);
float3 ViewDir = g_CamPos.xyz - vsIn.Position.xyz;
float4 lightPos = mul(g_LightPos, WorldI);
float3 lightVec = (lightPos.xyz - vsIn.Position.xyz);
if(g_NormalEnable)
{
float3x3 objToTangent;

objToTangent[0] = vsIn.BiNormal;
objToTangent[1] = vsIn.Tangent;
objToTangent[2] = vsIn.Normal;

vsOut.LightDir = mul(objToTangent, lightVec);
vsOut.ViewDir = mul(objToTangent, ViewDir);

}
else
{
vsOut.LightDir = lightVec;
vsOut.ViewDir = ViewDir;
}

vsOut.UV0.xy = vsIn.UV0.xy;
vsOut.UV1.xy = vsIn.UV1.xy;
vsOut.UV2 = vsIn.UV2;
vsOut.UV3 = vsIn.UV3;
vsOut.UV4 = vsIn.UV4;
return vsOut;
}

PSOut PS( PSIn psIn )
{
PSOut psOut;
float3 BottomCol = k_d.rgb;
float4 TopCol = k_d;
float3 LightCol = float3(1.0,1.0,1.0);
float3 Normal = psIn.Normal;
float3 LightDir = normalize(psIn.LightDir);
float3 ViewDir = normalize(psIn.ViewDir);
float3 Ambient = k_a.rgb;
float3 SpecLevel = float3(1.0,1.0,1.0);
float3 RefelctContrib=float3(1,1,1);
float Alpha;

float2 normalUV = psIn.UV4;
float2 diffuseUV = psIn.UV0;

float4 newCol;
float4 vertColour = float4(psIn.UV0.z,psIn.UV0.w,psIn.UV1.z,psIn.UV1.w);

if(g_UseParallax && g_NormalEnable)
{
float height = tex2D(g_NormalSampler, psIn.UV4 ).a;
float Altitude = height + g_ParallaxBias;
normalUV = Altitude * g_ParallaxScale*ViewDir;
normalUV += psIn.UV4;
diffuseUV = normalUV;
}


if(g_AmbientOccEnable)
Ambient *= tex2D(g_AmbientOccSampler, psIn.UV1);


if(g_TopDiffuseEnable)
TopCol = tex2D(g_TopSampler, diffuseUV );

if(g_BottomDiffuseEnable)
BottomCol = tex2D(g_BottomSampler, psIn.UV2);


if(g_AlphaVertex)
Alpha = vertColour.a;
else
Alpha = TopCol.a;


if(g_NormalEnable)
Normal = NormalCalc(tex2D(g_NormalSampler, normalUV).xyz, g_BumpScale);

if(g_SpecularEnable)
SpecLevel = tex2D(g_SpecularSampler, psIn.UV3).xyz;


newCol.rgb = TopCol * Alpha;
newCol.rgb += BottomCol * (1.0f - Alpha);
if(g_AddVertexColor)
newCol.rgb *= vertColour.rgb;

psOut.Colour.rgb = (Ambient + (g_LightCol * saturate(dot(Normal, LightDir)))) * newCol.rgb;

if(g_ReflectionEnable)
{
float3 CUV = normalize( reflect( ViewDir, Normal).xzy);
CUV.y = -CUV.y;
float4 env = texCUBE(g_ReflectionSampler, CUV);
env *=g_ReflectAmount;
env *= SpecLevel.r;
psOut.Colour.rgb += env;

}


float3 H = normalize(LightDir + ViewDir);
float Specular = pow(saturate(dot(Normal, H)), n);
psOut.Colour.rgb += (k_s.rgb * Specular) * SpecLevel.rgb;

psOut.Colour.a = 1.0f;
return psOut;
}


technique Default
<
string script = "Pass=p0;";
>
{
pass p0 <
string script = "Draw=Geometry;";
>
{
AlphaBlendEnable = TRUE;
DestBlend = InvSrcAlpha;
SrcBlend = SrcAlpha;
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PS();
}
}





Re: XNA Game Studio Express Effect Textured Model Doesn't Render

Catalin Zima

The first thing you do in the Vertex Shader is

vsOut.Position = mul(vsIn.Position, WorldViewProj);

these are your parameters:

// transformations
float4x4 World      :         WORLD;
float4x4 WorldI        :        WORLDI;
float4x4 View       :         VIEW;
float4x4 ViewI        :        VIEWI;
float4x4 Projection :         PROJECTION;
float4x4 WorldViewProj :     WORLDVIEWPROJ;
float4x4 WorldView :         WORLDVIEW;

and in the code you only set three of them:

effect.Parameters["World"].SetValue(WorldMatrix);
effect.Parameters["View"].SetValue(ViewMatrix);
effect.Parameters["Projection"].SetValue(ProjMatrix);

So, when the VS gets executed, the WorldViewProj's value is 0 (the same with WorldI, ViewI, WorldView).

 

You should set all parameters to corresponding values.