次のコードは、新しいプロジェクトの作成時に Visual Studio が作成するコード スケルトンをほとんど変更していない完全な XNA 3.1 プログラムです。
私が変えた唯一のことは
VS ソリューションのコンテンツ フォルダーに .x モデルをインポートしました。
(モデルはテクスチャが広がっているシンプルな正方形です - Google Sketchup で作成され、いくつかの .x エクスポーターでエクスポートされます)
Load() メソッドで、.x モデルをゲームにロードしています。
Draw() メソッドは、BasicEffect を使用してモデルをレンダリングします。
これら 3 つのことを除いて、コードは追加していません。
モデルにテクスチャが表示されないのはなぜですか? テクスチャを表示するにはどうすればよいですか?
これはテクスチャ ファイルです (パレットからの標準の SketchUp テクスチャ)。
そして、これが私のプログラムの外観です-ご覧のとおり、テクスチャがありません!
これは、プログラムの完全なソース コードです。
namespace WindowsGame1 {
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game {
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1() {
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize() {
// TODO: Add your initialization logic here
base.Initialize();
}
Model newModel;
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent() {
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: usse this.Content to load your game content here
newModel = Content.Load<Model>(@"aau3d");
foreach (ModelMesh mesh in newModel.Meshes) {
foreach (ModelMeshPart meshPart in mesh.MeshParts) {
meshPart.Effect = new BasicEffect(this.GraphicsDevice, null);
}
}
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent() {
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime) {
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime) {
if (newModel != null) {
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix[] transforms = new Matrix[newModel.Bones.Count];
newModel.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in newModel.Meshes) {
foreach (BasicEffect effect in mesh.Effects) {
effect.EnableDefaultLighting();
effect.TextureEnabled = true;
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(0)
* Matrix.CreateTranslation(new Vector3(0, 0, 0));
effect.View = Matrix.CreateLookAt(new Vector3(200, 1000, 200), Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
0.75f, 1.0f, 10000.0f);
}
mesh.Draw();
}
}
base.Draw(gameTime);
}
}
}