这是一个老问题,但我有,所以我想我会发布一个后续昨天这出自己:如果您使用的是默认FBX内容处理器
,并有DefaultEffect
属性设置为BasicEffect
,您可以通过获得Texture2D
的对象:
texture = ((BasicEffect)model.Meshes[0].Effects[0]).Texture;
注意,在模型中的每个网格可以有不同的质感。
纹理坐标存储在MeshPart
的VertexBuffer
以及位置等我见过两个顶点声明。对于使用单个纹理的模型/网格(3DS Max中的位图材质),顶点声明为VertexPositionNormalTexture
。
对于有两个纹理(位图和不透明/α-地图)的模型,声明有元素:
Position
Normal
Texture (usage index 0)
Texture (usage index 1)
,或者裹入IVertexType
结构,
public struct VertexPositionNormalTextureTexture : IVertexType
{
public Vector3 Position;
public Vector3 Normal;
public Vector4 Texture0;
public Vector4 Texture1;
public static VertexDeclaration VertexDeclaration
{
get
{
return new VertexDeclaration
(
new VertexElement(0,VertexElementFormat.Vector3, VertexElementUsage.Position, 0)
,
new VertexElement(0,VertexElementFormat.Vector3, VertexElementUsage.Normal, 0)
,
new VertexElement(0,VertexElementFormat.Vector3, VertexElementUsage.TextureCoordinate, 0)
,
new VertexElement(0,VertexElementFormat.Vector3, VertexElementUsage.TextureCoordinate, 1)
);
}
}
VertexDeclaration IVertexType.VertexDeclaration
{
get { return VertexDeclaration; }
}
}
和等效HLSL结构:
struct VertexPositionNormalTextureTexture
{
float3 Position : POSITION0;
float3 Normal : NORMAL0;
float4 Texture0 : TEXCOORD0;
float4 Texture1 : TEXCOORD1;
};
请注意,我更改了.Position
和.Normal
从Vector4
和Vector3
到float4
和float3
之前我发布了这个,并没有测试它。可能需要将它们更改回Vector4
和float4
。
当然,您需要在像素着色器中使用一个采样器和一些基本逻辑来读取每个纹理。假设您设定了两种效果参数xTexture0和xTexture1到包含颜色纹理和透明度地图Texture2D
对象,
// texture sampler
sampler Sampler0 = sampler_state
{
Texture = (xTexture0);
};
sampler Sampler1 = sampler_state
{
Texture = (xTexture1);
};
,这里是一个简单的双纹理像素着色器。如果你只想要一个纹理,只是从第一采样读取和返回值,或应用照明等
float4 TwoTexturePixelShader(VertexPositionNormalTextureTexture input) : COLOR0
{
float4 texel0;
float4 texel1;
float4 pixel;
// check global effect parameter to see if we want textures turned on
// (useful for debugging geometry weirdness)
if (TexturesEnabled)
{
texel0 = tex2D(Sampler0, input.Texture0);
texel1 = tex2D(Sampler1, input.Texture1);
/// Assume texel1 is an alpha map, so just multiple texel0 by that alpha.
pixel.rgb=texel0.rgb;
pixel.a=texel0.a;
}
else
/// return 100% green
pixel = float4(0,1,0,1);
return pixel;
}
相关穴位这里有纹理坐标在FBX已经存在和已经存储在每个MeshPart
的VertexBuffer
中,所有您需要做的就是提取纹理,并将其作为全局效果参数传递到着色器,然后按正常方式继续。
这是您用来从FBX文件中的3DS max重现效果的过程吗? – 2010-01-04 21:14:41
是的,它是...... – 2010-01-05 14:12:30
(万一任何人遇到这种情况并需要它)GS 3.1的皮肤效应样本(在他们引入股票SkinnedEffect之前)[http://create.msdn.com/en- US/education/catalog/sample/skinned_model]显示了如何在设计时加载自定义着色器的BasicEffect。 (ConvertMaterial方法) – sebf 2011-02-16 23:12:53