XNA 4.0 – 透明的3D模型

我在XNA中渲染的3D模型(.fbx)有点问题,请看截图。

截图:

看起来不太对劲...

它应该是一个乐高积木,在Blender中进行调节,然后输出到.fbx,并添加到XNA项目中。 我的问题是透明度,我尝试了很多与深度缓冲区,z缓冲区等,但没有任何帮助。 这是我的代码:

namespace Using3DModels { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; float myModelRotation; Vector3 myModelPosition; /// <summary> /// Stores the model that we are going to draw. /// </summary> private Model model; /// <summary> /// Stores the world matrix for the model, which transforms the /// model to be in the correct position, scale, and rotation /// in the game world. /// </summary> private Matrix world = Matrix.CreateTranslation(new Vector3(6, 4, 0)); /// <summary> /// Stores the view matrix for the model, which gets the model /// in the right place, relative to the camera. /// </summary> private Matrix view = Matrix.CreateLookAt(new Vector3(30, 25, -10), new Vector3(5, 5, 0), Vector3.UnitY); /// <summary> /// Stores the projection matrix, which gets the model projected /// onto the screen in the correct way. Essentially, this defines the /// properties of the camera you are using. /// </summary> private Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 0.1f, 100f); public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferMultiSampling = true; } /// <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() { base.Initialize(); } /// <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); model = Content.Load<Model>("lego6"); myModelPosition = Vector3.Zero; myModelRotation = MathHelper.ToRadians(45f); } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all content. /// </summary> protected override void UnloadContent() { } /// <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(); myModelRotation += MathHelper.ToRadians(1f); 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) { GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; GraphicsDevice.Clear(Color.DarkGray); DrawModel(model, world, view, projection); base.Draw(gameTime); } /// <summary> /// Does the work of drawing a model, given specific world, view, and projection /// matrices. /// </summary> /// <param name="model">The model to draw</param> /// <param name="world">The transformation matrix to get the model in the right place in the world.</param> /// <param name="view">The transformation matrix to get the model in the right place, relative to the camera.</param> /// <param name="projection">The transformation matrix to project the model's points onto the screen correctly.</param> private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection) { GraphicsDevice.DepthStencilState = DepthStencilState.Default; Matrix[] transforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(transforms); foreach (ModelMesh mesh in model.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(myModelRotation) * Matrix.CreateTranslation(myModelPosition); effect.View = view; effect.Projection = projection; } mesh.Draw(); } } } } 

这看起来像一个扑杀问题,就像特雷弗·鲍威尔指出的那样。 一个简单的方法来testing它是否真的是与剔除有关的问题是closures它像这样:

 RasterizerState rasterizerState = new RasterizerState(); rasterizerState.CullMode = CullMode.None; GraphicsDevice.RasterizerState = rasterizerState; 

请注意,对于运输产品来说,禁用剔除并不是最好的办法(除了某些您希望达到特殊效果的角落情况外),因为它也会绘制三角形而不是面向相机,从而减慢绘图的速度。