Add the mesh, not the textures

Here's a common mistake for people new to the Content Pipeline...

If you have a mesh file that references a bunch of texture files, and you want to use this with XNA, you only need to add the mesh to your C# Express project. When we build the mesh we will notice what textures it references and automatically go build those too, so there is no need to explicitly add the textures to your project.

And for bonus points, here's a common gotcha when adding content files to C# Express...

By default, when you choose Add / Existing Item, pick a mesh file, and then click the Add button, if that mesh isn't already inside your project folder, C# Express will automatically copy it into your project folder. This can give surprising results for two reasons:

- If you then go back and edit the original mesh, your changes will be ignored because C# is referencing its own copy of your file.

- If the mesh references textures, those won't have been copied, so the mesh won't be able to find its textures in the relative path where it is expecting them to be.

There are two ways to prevent this problem. Either put your mesh and texture files inside the project folder to start with, so C# Express will not try to copy them, or explicitly tell it not to copy. To do that, in the "Add Existing Item" file selector, rather than clicking the Add button, click the little down arrow at the right of the Add button, and select "Add As Link".

Comments (4)

  1. CeeJ100 says:

    Thats an interesting point. I just tried the XNA tutorial (No 1) to "display a 3D model on the screen". It expressly states that the texture should also be added to the project.

    "Right-click on the Textures folder in the Solution Explorer, select Add and then Existing Item…. Using the dialog that appears, browse back to the path you placed the Spacewar project in, and find the ContentsTextures folder. Select wedge_p1_diff_v1.tga. Click OK. "

    Before I read this I already tried removing the texture from the project & realised it still worked OK as long as the texture is resident in the appropriate directory. Looks ike the tutorial text needs correcting.

    Chris Johnson

  2. LuckyWolf19 says:

    Your articles are always great, thank you.

    One question:

    When importing a model with textures, I copy the Model.xnb to my ContentModels directory and I copy the textures for it to my ContentTextures directory.

    However, during runtime, I load the textures into the content manager first, then the model and get an error stating that the model can not find the texture.

    I thought once a texture was loaded into the content manager with a name, any models loaded after would look in the content manager by name and use it. It seems that the model looks in the content manager and looks for a texture by Path and name.

    Is there a way to accomplish this? It seems very messy to have to have my models and textures together in the same directory.

    Thanks in advance for the help.

    Scott Peal


  3. ShawnHargreaves says:

    LuckyWolf19: you should not move the .xnb files after the build has finished. The Content Pipeline build process will leave its output files in the correct locations (inside your project output folder). If you move the files around after that, you will break any links it may have stored from one file to another. If you want to change where the output content goes, you should change the output path settings for your content project, rather than moving individual .xnb files by hand.

Skip to main content