我正在使用XNA框架构建商业智能工具来构建图形。我只使用2D对象,而且我正在使用Texture2D和SpriteFont从头开始构建我的2D对象。
目前我认为我的物体在大显示屏上看起来“边缘粗糙”的问题。我知道抗锯齿技术可以摆脱这些“粗糙边缘”,但是我无法在自定义的Texture2D对象上成功实现它。
在我的开发工作中,在XNA中使用抗锯齿的最佳方法是什么?
请注意,我不是XNA专家,我对XNA中的3D建模一无所知,并且我已经研究过其他问题。
我的XNA框架代码如下所示:
public class Game1 : Microsoft.Xna.Framework.Game
{
public GraphicsDeviceManager graphics;
public SpriteBatch spriteBatch;
public bool drawn = false;
public Timer timer;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
Settings.LoadSettings();
if (Settings.GetSettings("FullScreen") == "True") { graphics.IsFullScreen = true; } else { graphics.IsFullScreen = false; }
graphics.PreferredBackBufferWidth = Convert.ToInt32(Settings.GetSettings("ScreenWidth"));
graphics.PreferredBackBufferHeight = Convert.ToInt32(Settings.GetSettings("ScreenHeight"));
graphics.PreferMultiSampling = true;
graphics.PreferredBackBufferFormat = SurfaceFormat.Color;
graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
//Update LOS every 6 minutes
timer = new Timer(360000);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
this.IsMouseVisible = true;
//System.Windows.Forms.Form MyGameForm = (System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(Window.Handle);
//MyGameForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
}
/// <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
var form = (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle(this.Window.Handle);
form.Location = new System.Drawing.Point(0, 0);
base.Initialize();
this.TargetElapsedTime = TimeSpan.FromSeconds(0.1);
}
/// <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.
graphics.GraphicsDevice.PresentationParameters.MultiSampleCount = 2;
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
}
/// <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
if (drawn == true) { SuppressDraw(); }
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)
{
//MouseState mouseState = Mouse.GetState();
//mouseState.LeftButton = ButtonState.Pressed
//This boolean will pause the 'game' so that it does not update every x milli seconds
drawn = true;
//Create the interface with a black background
GraphicsDevice.Clear(Color.Black);
// Add pointers to create global references to XNA
Pointers.graphics = this.graphics;
Pointers.spriteBatch = this.spriteBatch;
Pointers.content = this.Content;
//Run data initialiazations and sql queries to retrieve all data
Objects.ObjectsData.CleareObjectsData();
Data.SqlData.SetDates();
Data.SqlData.GetAllData();
Data.DataFunctions.RunDataFunctions();
LocationRef.InitializeLocationRef();
//Create 2D Objects
Sections.Drilling.CreateDrillingObjects();
Sections.BlastShovels.CreateBlastShovelsObjects();
Sections.Pits.CreatePitObjects();
Sections.Trucks.CreateTrucksObjects();
//Sections.StocksCrushers.CreateStocksCrushersObjects();
Sections.Other.CreateOtherObjects();
//Begin processing of 2D objects
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null);
foreach (var x in Objects.ObjectsData.textObjects) { x.DrawObject(); }
foreach (var x in Objects.ObjectsData.circleObjects) { x.DrawObject(); }
foreach (var x in Objects.ObjectsData.rectangleObjects) { x.DrawObject(); }
foreach (var x in Objects.ObjectsData.triangleObjects) { x.DrawObject(); }
foreach (var x in Objects.ObjectsData.trapesiumObjects) { x.DrawObject(); }
//End processing of 2D objects
spriteBatch.End();
base.Draw(gameTime);
}
//Timer used to refresh the LOS
public void timer_Elapsed(object sender, ElapsedEventArgs e)
{
drawn = false;
System.Media.SystemSounds.Beep.Play();
}
}
示例对象类:
public class Circle
{
public Texture2D graphic;
public SpriteFont font;
public Vector2 graphicCoor;
public Vector2 fontCoor;
Color graphicColor;
Color fontColor;
public string fontText;
public string fontType;
public string objectID;
public Circle(int xCord, int yCord, int radius, Color graphicColor_, string fontType_, string fontText_, Color fontColor_, string ID)
{
objectID = ID;
graphicColor = graphicColor_;
fontColor = fontColor_;
fontText = fontText_;
fontType = fontType_;
graphic = new Texture2D(Pointers.graphics.GraphicsDevice, radius, radius);
font = Pointers.content.Load<SpriteFont>(fontType_);
graphicCoor = new Vector2(xCord, yCord);
Vector2 FontOrigin = font.MeasureString(fontText_);
//fontCoor = new Vector2(xCord + (int)FontOrigin.Length(), yCord + radius/2);
fontCoor = new Vector2(xCord + (radius - (int)FontOrigin.X) / 2, yCord + (radius - (int)FontOrigin.Y) / 2);
//Set color data
Color[] colorData = new Color[radius * radius];
float diam = radius / 2f;
float diamsq = diam * diam;
for (int x = 0; x < radius; x++)
{
for (int y = 0; y < radius; y++)
{
int index = x * radius + y;
Vector2 pos = new Vector2(x - diam, y - diam);
if (pos.LengthSquared() <= diamsq)
{
colorData[index] = graphicColor_;
}
else
{
colorData[index] = Color.Transparent;
}
}
graphic.SetData(colorData);
}
}
public void DrawObject()
{
Pointers.spriteBatch.Draw(graphic, graphicCoor, Color.White);
Pointers.spriteBatch.DrawString(font, fontText, fontCoor, fontColor, 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 0.5f);
}
}