I’m using the standard .fbx importer with custom shaders in XNA. The .fbx model is UV wrapped properly and is textured appropriately when I use BasicEffect. However when I use my custom effect I have to load the texture in as a parameter, and it is not mapped correctly.
Questions: 1) How can I texture my .fbx model properly using the included texture’s coordinates with custom effects? 2) Is there a way to access the texture from the loaded .fbx model object? Where does this texture go?
Note: I’ve studied custom content pipelines and don’t believe writing my own Fbx importer/processor will be efficient. However if someone can descriptively supply me with firsthand experience of this being the answer than I will use the custom pipeline.
Thank you for your time and for reading this post.
This is an old question, but I had to figure this out myself yesterday so I thought I’d post a followup:
If you’re using the default FBX content processor and have the
DefaultEffectproperty set toBasicEffect, you can get theTexture2Dfor the object via:Note that the each mesh in the model may have a different texture.
The texture coordinates are stored in the
MeshPart‘sVertexBufferalong with position, etc. I’ve seen two vertex declarations. For a model/mesh that used a single texture (bitmap material in 3DS Max), the vertex declaration wasVertexPositionNormalTexture.For a model that had two textures (a bitmap and an opacity/alpha map), the declaration had the elements:
or, wrapped into an
IVertexTypestructure,and the equivelant HLSL structure:
Note that I changed
.Positionand.NormalfromVector4andVector3tofloat4andfloat3before I posted this, and haven’t tested it. It’s possible that they may need to be changed back toVector4andfloat4.Of course, you’ll need a sampler and some basic logic in your pixel shader to read each texture. Assuming you’ve set two effect parameters xTexture0 and xTexture1 to
Texture2Dobjects containing your color texture and opacity map,and here’s a simple two-texture pixel shader. If you only want one texture, just read from the first sampler and return the value, or apply lighting, etc.
The relevant points here are that the texture coordinates are already present in the FBX and are already stored in each
MeshPart‘sVertexBuffer, so all you need to do is extract the texture, pass it into your shader as a global effect parameter, and proceed as normal.