これは古い質問ですが、昨日自分でこれを理解しなければならなかったので、フォローアップを投稿すると思いました:
デフォルトの FBX コンテンツ プロセッサを使用していて、DefaultEffect
プロパティが に設定されている場合は、次の方法でオブジェクトBasicEffect
の を取得できます。Texture2D
texture = ((BasicEffect)model.Meshes[0].Effects[0]).Texture;
モデル内の各メッシュのテクスチャが異なる場合があることに注意してください。
テクスチャ座標は、位置などとともに に格納されます。2 つの頂点宣言を見てきましたMeshPart
。VertexBuffer
単一のテクスチャ (3DS Max のビットマップ マテリアル) を使用したモデル/メッシュの場合、頂点宣言はVertexPositionNormalTexture
.
2 つのテクスチャ (ビットマップと不透明度/アルファ マップ) を持つモデルの場合、宣言には次の要素が含まれます。
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
これを投稿する前にand .Normal
fromVector4
とVector3
tofloat4
を変更しfloat3
、テストしていないことに注意してください。Vector4
およびに戻す必要がある可能性がありfloat4
ます。
もちろん、各テクスチャを読み取るには、ピクセル シェーダーにサンプラーといくつかの基本的なロジックが必要です。Texture2D
カラー テクスチャと不透明度マップを含むオブジェクトに 2 つのエフェクト パラメータ xTexture0 と xTexture1 を設定したと仮定すると、
// texture sampler
sampler Sampler0 = sampler_state
{
Texture = (xTexture0);
};
sampler Sampler1 = sampler_state
{
Texture = (xTexture1);
};
これは単純な 2 テクスチャ ピクセル シェーダーです。テクスチャが 1 つだけ必要な場合は、最初のサンプラーから読み取って値を返すか、ライティングなどを適用します。
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
ていることです。そのため、テクスチャを抽出し、それをグローバル エフェクト パラメータとしてシェーダに渡し、通常どおり処理を進めるだけです。 .