Pawel J

Hi!

I'm currently using 3D Studio Max to create scenes, then export them into .x file format and then import with a standard content pipeline. What I miss is that after the content pipeline processes my scenes I can't find the shader's parameter values I've set up in 3D Studio Max. Is there a way to overcome this problem

Any help appreciated,

Pawel Jarzebski



Re: XNA Framework XNA shader parameters from 3d studio

Kyle_W

Well I'm not a user of 3D Studio Max and so I'm not familiar with exactly what is supported by its X file exporter, but once you get a Model imported into an XNA project, and assuming each ModelMeshPart within your Model has a valid Effect (FX file) associated with it, you can access the HLSL global parameters of the effect (which is what I assume you are referring to) by making a call similar to the following:

MyModel.Meshes[0].MeshParts[0].Effect.Parameters["WorldViewProj"].SetValue(worldViewProjection);

In the above example, WorldViewProj is the HLSL global variable name and worldViewProjection is the Matrix in your XNA project that you want to assign to the HLSL variable.

If you Model does not have effects automatically associated with it after import, you can use the Content Pipeline to manually load in each Effect (FX file) and then associate each effect with each MeshPart similar to:

Effect MyEffect = MyContentManager.Load<Effect>("MyEffect");

MyModel.Meshes[0].MeshParts[0].Effect = MyEffect;

If you load the effect in this way, you have a direct reference for the effect, so you can also set parameters with just:

MyEffect.Parameters["WorldViewProj"].SetValue(worldViewProjection);

You also need to set the Effect.CurrentTechnique property for each Effect, and then to draw each ModelMeshPart (using its associated Effect) within a given ModelMesh on your Model, call:

MyModel.Meshes[0].Draw();

There is no need to manually iterate through the passes of each Effect. As I understand it, the Draw() call should take care of it.





Re: XNA Framework XNA shader parameters from 3d studio

Pawel J

Kyle,

Thanks for your answer - what you tell is exactly true but I must have not been clear enough with my question, sorry about this.

In environment like 3D Studio Max you can set parameters to some values and then natural would be something like:

MyModel.Meshes[0].MeshParts[0].Effect.Parameters["GlowStrenght"].SetValue(0.75);

where 0.75 is an example of value I set in 3D Studio Max during scene design. KW and Panda x file exporters do export those values, but the XNA importer doesn't import them and I wonder what is the way programmers use to get those data anyway...

Pawel





Re: XNA Framework XNA shader parameters from 3d studio

Kyle_W

Well, to my knowledge, the only rendering variables that get actually stored in the X file on export are those associated with Fixed Function Pipeline material settings. These FFP materials get stored on import as BasicEffect objects within the Model.Meshes[].Effects[] collection, so you could iterate through them and read or modify their properties as shown in this example:

http://msdn2.microsoft.com/en-us/library/bb197293.aspx

However there is no "GlowStrength" property on a BasicEffect. If the properties exposed by BasicEffect are still not what you are referring to, then what makes you think these parameters are even being exported into the X file

My advice is to use 3D Studio Max simply to create the shape of the mesh and properly lay out its UV map, then use a tool like ATI's RenderMonkey to design the materials for the object and export them as FX files. Here's a link:

http://ati.amd.com/developer/rendermonkey/index.html





Re: XNA Framework XNA shader parameters from 3d studio

leclerc9

I was nearly positive that XNA does in fact set these values if they are specified in the file. Are you sure that the .fx file you are using has a global parameter named "GlowStrength " You are using your own .fx file, correct




Re: XNA Framework XNA shader parameters from 3d studio

Pawel J

GlowStrength was just an example - yes, I have a custom .fx file with a parameter like:

float GlowStrength : DiffuseMap <
string UIName = "Glow strength";
string UIType = "MaxSpinner";
float UIMin = 0.0f;
float UIMax = 1.0f;
float UIStep = 0.1f;
> = 1.0f;

This one makes 3D Studio Max draw a control on the material editor that lets me edit the glow value. Then the value I set in 3D Studio gets exported into .x file:

EffectParamFloats {
"GlowStrength";
1;
0.750000;
}

And when I set it during the game runtime:

MyEffect.Parameters["GlowStrength"].SetValue(*);

Where * would mean the 0.750000 but I don't know how to get those data there... This is a feature all proffesional game projects need but somehow XNA lacks it directly (just like the animation feature) - what are the ways to support this Is there any example on the web of how somebody did that

Thanks,
Pawel





Re: XNA Framework XNA shader parameters from 3d studio

Kyle_W

Well if you are saying that this "EffectParamFloats" is getting saved into the X file, then it must be a custom template and you must therefore create your own custom parser to get the data out of the X file. Here is some information on the X file format:

http://local.wasp.uwa.edu.au/~pbourke/dataformats/directx/#xfilefrm_Templates

On the other hand, if you have assigned a default value to the GlowStrength parameter inside the FX file, which in your above example the default value would appear to be 1.0f, then you should be able to read this default value using:

float GlowStrength = MyEffect.Parameters["GlowStrength"].GetValueSingle();

Note that the "float" data type is just a C# alias for the .NET Framework System.Single data type. Other methods such as GetValueMatrix() and GetValueVector3(), etc are available. It seems like your best bet is just to manually copy the appropriate default values into your FX file. Although there is a little labor involved, you only have to do it once.





Re: XNA Framework XNA shader parameters from 3d studio

Pawel J

This is great! Although you did not realize that, the function you pointed out GetValueSingle() does exactly what I need!

Great thanks!
Pawel





Re: XNA Framework XNA shader parameters from 3d studio

Leaf.

The .x model importer in the content pipeline already recognises the effect param templates, you dont need to create your own parser. The parameters in the .x file are imported into the EffectMaterialContent.OpaqueData dictionary.

If you are using the standard Model processor then the effect parameters are set automatically. When you load a Model that uses an fx file, an Effect object is created and the parameters are set with the values that were in the .x file.

This is why, when you get the "GlowStrength" value from your effect it has the value 0.75.

Cheers,
Leaf.