c# - 启用抗锯齿绘图 Primitives Monogame
问题描述
我正在尝试使用 Monogame 绘制三角形图元,并启用 AA。但是,我得到了这个结果-锯齿状三角形
我想要一个具有平滑抗锯齿边缘的三角形。
这是我的代码,我已经启用了 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, 0), Color.Green), new VertexPositionColor(new Vector3(250.0f, 200.0f, 0), 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, 0, 0, 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, 0, 1);
}
base.Draw(gameTime);
}
}
}
`
解决方案
最简单的解决方案是将基元渲染到两倍大小的渲染目标,然后以一半大小将目标渲染到屏幕上。
调整大小过滤将为您处理抗锯齿。
推荐阅读
- python - 使用 python 在字典上将时区从 utc 转换为 america/new_york
- ant-media-server - 无法在 ANT 媒体服务器上查看流
- ruby-on-rails - 获取通过 Rails 中的值列表连接的所有行
- highcharts - Highcharts 甘特图:任务进度指示器需要以不同颜色显示各种状态的单个任务,如已完成、进行中等
- c# - .Net core Action Filter 被应用于所有控制器忽略装饰器
- apache-flink - Flink作业错误java.util.concurrent.ExecutionException:org.apache.flink.runtime.client.JobSubmissionException:无法提交JobGraph
- r - 用于测试来自一个面板回归的回归系数之间差异的 R 代码
- json - 将嵌套的 JSON 提供给 rapidJSON 文档导致 kParseErrorDocumentRootNotSingular
- css - 如何使用css在元素之间显示右侧元素
- azure - Azure - 如何使用在 Yaml 文件中编码的管道构建 WebJob