如何解决启用抗锯齿绘制 Primitives Monogame
我正在尝试使用 Monogame 绘制三角形基元并启用 AA。但是,我得到了这个结果 - Jagged Triangle
我想要一个具有平滑抗锯齿边缘的三角形。
这是我的代码,其中我已经启用了 AA 并正确设置了所有内容(可能不是,因为线条仍然非常参差不齐)-
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Game1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
VertexBuffer vertexBuffer;
BasicEffect basicEffect;
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()
{
graphics.PreferMultiSampling = true;
graphics.GraphicsProfile = GraphicsProfile.HiDef;
graphics.ApplyChanges();
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);
basicEffect = new BasicEffect(GraphicsDevice);
VertexPositionColor[] vertices = new[]{new VertexPositionColor(new Vector3(160.0f,200.0f,0),Color.Red),new VertexPositionColor(new Vector3(200.0f,140.0f,Color.Green),new VertexPositionColor(new Vector3(250.0f,Color.Blue) };
vertexBuffer = new VertexBuffer(GraphicsDevice,typeof(VertexPositionColor),3,BufferUsage.WriteOnly);
vertexBuffer.SetData<VertexPositionColor>(vertices);
}
/// <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();
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.Clear(Color.CornflowerBlue);
basicEffect.World = Matrix.CreateOrthographicOffCenter(0,GraphicsDevice.Viewport.Width,GraphicsDevice.Viewport.Height,1);
basicEffect.VertexColorEnabled = true;
GraphicsDevice.SetVertexBuffer(vertexBuffer);
RasterizerState rasterizerState = new RasterizerState();
rasterizerState.CullMode = CullMode.None;
GraphicsDevice.RasterizerState = rasterizerState;
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList,1);
}
base.Draw(gameTime);
}
}
}
`
解决方法
最简单的解决方案是将图元渲染到两倍大小的渲染目标,然后将目标渲染到屏幕的一半大小。
调整大小过滤将为您处理抗锯齿。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。