Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[XNA] [HLSL] Drawing a model's mesh parts with its original textures doesn't work.

This topic is 3080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When importing a model I grab the original textures from the model and store them in a list so they can be used with a custom HLSL effect in XNA.

When drawing the model I iterate through the list for each mesh part, assigning the correct texture to the correct part:


int i = 0;
int count = originalTextures.Count;

effect.Parameters["World"].SetValue(transforms[model.Meshes[0].ParentBone.Index] * worldTransform);

effect.Begin();
effect.CurrentTechnique.Passes[0].Begin();

foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
if (count != 0)
{
effect.Parameters["Texture0"].SetValue(originalTextures[i++]);
}

graphics_Device.VertexDeclaration = meshPart.VertexDeclaration;
graphics_Device.Vertices[0].SetSource(mesh.VertexBuffer, meshPart.StreamOffset, meshPart.VertexStride);
graphics_Device.Indices = mesh.IndexBuffer;

graphics_Device.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
meshPart.BaseVertex,
0,
meshPart.NumVertices,
meshPart.StartIndex,
meshPart.PrimitiveCount
);
}
}

effect.CurrentTechnique.Passes[0].End();
effect.End();



The problem is that the different textures are not being drawn and the entire model is drawn with the last texture stored in the originalTextures list instead.

I have drawn each mesh part separately to test everything is working ok and each mesh part will draw correctly. I've drawn all 3 parts using just one of the original textures (all 3 textures tested) and that works fine.

Trying to draw all 3 parts using all 3 textures doesn't work though, why is this the case?

Share this post


Link to post
Share on other sites
Advertisement
When you set an effect parameter inside an EffectPass.Begin/End block, you need to call CommitChanges to have the effect propogate the changes to the GraphicsDevice.

Share this post


Link to post
Share on other sites
Thanks MJP, that did the trick :) I'll make a note of that for future reference.

The working code now looks like the following:


foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
if (count != 0)
{
effect.Parameters["Texture0"].SetValue(originalTextures[i++]);
effect.CommitChanges();
}
else
{
effect.Parameters["Texture0"].SetValue(Game1.Instance.Textures[texture]);
}

graphics_Device.VertexDeclaration = meshPart.VertexDeclaration;
graphics_Device.Vertices[0].SetSource(mesh.VertexBuffer, meshPart.StreamOffset, meshPart.VertexStride);
graphics_Device.Indices = mesh.IndexBuffer;

graphics_Device.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
meshPart.BaseVertex,
0,
meshPart.NumVertices,
meshPart.StartIndex,
meshPart.PrimitiveCount
);
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!