2012-10-11 57 views
0

以下代碼是完整的XNA 3.1程序,幾乎未改變Visual Studio在創建新項目時創建的代碼框架。如何在模型上繪製紋理

我已經改變了唯一的東西是

  • 進口.X模型來VS解決方案的內容文件夾。

    (該模型與紋理跨越這一個簡單的方形 - 在谷歌Sketchup的製作,並與幾個.X出口商出口)

  • 在Load()方法,我加載.X模式進入遊戲。

  • Draw()方法使用BasicEffect來渲染模型。

除了這三件事我沒有添加任何代碼。

爲什麼模型不顯示紋理?我可以做些什麼來使紋理可見?

這是紋理文件(從調色板中選擇標準SketchUp的紋理):

enter image description here

而這正是我的程序看起來像 - 你可以看到:沒有質感!

enter image description here

這是該程序的完整源代碼:

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); 
    } 
} 

}

+0

紋理文件是否與模型位於同一文件夾中,並且您是否已將紋理添加到「內容文件夾」? – keyboardP

+0

@keyboardP是的,這兩個文件都在解決方案資源管理器的Content文件夾中的相同文件夾中... –

+0

您在哪裏加載紋理,以及何時將它分配給模型的BasicEffect? – MerickOWA

回答

4

我認爲你想要做太多。它將取決於「.x」文件中的內容,但是這些CAN可以包含着色和紋理模型所需的所有着色器。

我認爲問題在於加載模型後,您將使用BasicEffect重寫模型的效果,該效果不知道要用於模型的紋理。

我認爲最容易做的事情就是將與LoadContent中的BasicEffect相關的代碼註釋掉。 「EnableDefaultLighting()」和/或「TextureEnabled」的設置可能也是不必要的。

+0

正是這樣!在模型加載過程中我已經放棄了網格零件的所有BasicEffects!它突然作品!而你只是讓這個星球上的人成爲最幸運的人! –

1

我不得不使用.X模型在過去的一些問題。嘗試使用.fbx導出文件。從您的3D應用程序導出後,將模型資源及其紋理放入項目中的相同地圖中。不要重命名它,因爲紋理的名稱在.FBX中,可能不可讀。

+0

我只是從SketchUp導出一個.fbx模型..不幸的是導致了相同的結果。 –