首页 > 解决方案 > 为什么我的图元在第一帧后变白?

问题描述

我正在使用 GraphicsDevice.DrawIndexedPrimitives 和 VertexPositionTexture 数组绘制六边形。当我第一次运行游戏时,我可以在屏幕上看到预期的结果闪烁,但我的六边形立即变成白色。

public class Game1 : Game
    {
        private GraphicsDeviceManager _graphics;
        private SpriteBatch _spriteBatch;
        private VertexPositionTexture[] _vertexPositionTexture;
        private short[] _indices;
        private VertexBuffer vertexBuffer;
        private IndexBuffer indexBuffer;
        private BasicEffect _basicEffect;
        Texture2D _texture;

        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        protected override void Initialize()
        {

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _texture = this.Content.Load<Texture2D>("resources/grad");

            _vertexPositionTexture = new[]
            {
                new VertexPositionTexture(new Vector3(320, 300, 0), new Vector2(1, .5f)),
                new VertexPositionTexture(new Vector3(310, 283, 0), new Vector2(.75f, 1)),
                new VertexPositionTexture(new Vector3(290, 283, 0), new Vector2(.25f, 1)),
                new VertexPositionTexture(new Vector3(280, 300, 0), new Vector2(0, .5f)),
                new VertexPositionTexture(new Vector3(290, 317, 0), new Vector2(.25f, 0)),
                new VertexPositionTexture(new Vector3(310, 317, 0), new Vector2(.75f, 0))
            };

            _indices = new short[]
            {
                1,0,2,
                2,0,3,
                3,0,4,
                4,0,5
            };

            vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionTexture), 6, BufferUsage.WriteOnly);
            vertexBuffer.SetData(_vertexPositionTexture);
            indexBuffer = new IndexBuffer(GraphicsDevice, typeof(short), _indices.Length, BufferUsage.WriteOnly);
            indexBuffer.SetData(_indices);

            _basicEffect = new BasicEffect(GraphicsDevice)
            {
                World = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1),
                Texture = _texture,
                TextureEnabled = true
            };
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Transparent);

            GraphicsDevice.SetVertexBuffer(vertexBuffer);
            GraphicsDevice.Indices = indexBuffer;

            EffectTechnique et = _basicEffect.Techniques[0];
            EffectPassCollection epc = et.Passes;

            foreach (EffectPass p in epc)
            {
                p.Apply();

                GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4);
            }

            _spriteBatch.Begin();
            _spriteBatch.Draw(_texture, new Rectangle(new Point(350), new Point(50)), Color.White);
            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }

结果

但是我可以在游戏开始的那一刻看到带有预期颜色的六边形。

标签: c#monogame

解决方案


在您的 Draw() 代码中,而不是

EffectTechnique et = _basicEffect.Techniques[0];

利用

EffectTechnique et = _basicEffect.CurrentTechnique;

调用 p.Apply() 后,当前技术不再位于 Techniques[] 的索引 0 上。为什么?我也不确定。


推荐阅读