XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Effect textures not loading automatically

Last post 05-23-2008 11:25 AM by JSpiel. 4 replies.
  • 02-03-2008 7:38 AM

    Effect textures not loading automatically

    Hi,

    I'm porting some RenderMonkey shaders to XNA and I've run into a little problem. I'm adding my .fx shader files and my .dds (or whatever) texture files to the project as content, and I can manually load them fine with content.Load<type>(). However, when I load an effect file the textures that it references in its parameters don't seem to get loaded, i.e. paramer.GetValueTexture2D() returns null.

    This problem extends to the loading of models. If the model references a .fx file then the effect is loaded properly, but again all the textures are null. If I manually load a texture and assign it to the relevent model effect parameter with SetValue then it's all good and my effect displays properly.

    After experimenting I've discovered that if the .X file explicitly inlines the texture name in the EffectInstance block like this...

    EffectParamString {
       "TextureField_Tex"; 
       "myTexture.dds;
      }

    ...then XNA loads the texture, otherwise it doesn't. If the texture isn't explicitly specified in the .X model file then shouldn't XNA load the the default texture as specified in the .FX effect file? Cause as far as I can tell, it doesn't. This is problematic, because the Panda exporter doesn't seem to be able to inline texture parameters in the .X files. I suppose I could create a custom content filter to strip texture parameters from the .FX files and add them to the .X files manually at compile time, but this seem like an aweful lot of work for what should be standard behaviour.

    Am I missing something obvious?

    Mark

  • 02-03-2008 8:02 AM In reply to

    Re: Effect textures not loading automatically

    Myndale:
    If I manually load a texture and assign it to the relevent model effect parameter with SetValue then it's all good and my effect displays properly.

    After experimenting I've discovered that if the .X file explicitly inlines the texture name in the EffectInstance block like this...

    EffectParamString {
       "TextureField_Tex"; 
       "myTexture.dds;
      }

    ...then XNA loads the texture, otherwise it doesn't.


    This is how it's supposed to work. The semantics and annotations inside the effect file are not taken into account when loading a effect. In an effect file, annotations are there to offer the application / programmer help or indications. If an application supports SAS, it (the application) should look at them, otherwise, they are ignored. The content pipeline does not take it upon itself to process semantics and annotations.

    When they are specified inside the model file, they are part of the model definition, so the content processor for the model, loads them.
  • 02-03-2008 8:20 AM In reply to

    Re: Effect textures not loading automatically

    Hmmm...ok, I guess that makes sense, so how are people exporting their .X files from Max if Panda doesn't export in the correct format?

    Interestingly, if I delete any of the inlined float parameters (e.g. my texture scale value) then the default value from the .FX effect file does indeed propegate through at runtime. Also the texture field name itself is also propegating through, it's just the texture itself that isn't being loaded.

    Mark

  • 02-03-2008 5:26 PM In reply to

    Re: Effect textures not loading automatically

    Answer

    All sorted, it's working fine now. Thanks for the help. For anyone else that runs into this problem, the "ResourceName" annotation is accessible in the parameter's Annotations collection, you just have to enumerate them all, load the textures and assign them. The following code does the job fine:

    // load the model
    Model model = content.Load(name);

    // load and assign all the textures
    foreach (ModelMesh mesh in model.Meshes)
    foreach (Effect effect in mesh.Effects)
    foreach (EffectParameter param in effect.Parameters)
    if (param.ParameterType.ToString() == "Texture")
    foreach (EffectAnnotation annotation in param.Annotations)
    if (annotation.Name == "ResourceName")
    {
    string filename = annotation.GetValueString();
    filename = Path.GetDirectoryName(filename) +
    @"\" + Path.GetFileNameWithoutExtension(filename);
    Texture texture = content.Load(filename);
    param.SetValue(texture);
    }

    Cheers,

    Mark

  • 05-23-2008 11:25 AM In reply to

    Re: Effect textures not loading automatically

    I'd suggest using the kW X-port exporter.  When using a DirectX effect in 3DS Max 9, it has no problem exporting shaders with multi-texturing, multiple texture coordinate channels, etc., and all textures and parameters load fine on their own, so you don't really need to deal with annotations and manual parameter value assignments like that.  See my thread on multi-texturing effects here http://forums.xna.com/forums/t/11975.aspx
Page 1 of 1 (5 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG