我是MonoGame开发人员,我正在开发一个允许用户用手指画画的项目
手机屏幕,我通过在手指路径上重复绘制图像来做到这一点:
我使用了这样的代码:
LoadConent方法
texture = Content.Load<Texture2D>("b3");
renderTarget = new RenderTarget2D(
this.GraphicsDevice,
this.GraphicsDevice.PresentationParameters.BackBufferWidth,
this.GraphicsDevice.PresentationParameters.BackBufferHeight,
false,
this.GraphicsDevice.PresentationParameters.BackBufferFormat,
DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
更新方法
protected override void Update(GameTime gameTime)
{
TouchCollection touches = TouchPanel.GetState();
foreach (TouchLocation touch in touches)
{
if (touch.State == TouchLocationState.Moved)
{
GraphicsDevice.SetRenderTarget(renderTarget);
spriteBatch.Begin();
spriteBatch.Draw(texture, touch.Position, Color.White);
spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
}
}
base.Update(gameTime);
}
绘制方法
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(renderTarget, Vector2.Zero, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
我有这个结果:
所以,我的问题是:
如何加速绘图,我得到连续草图。
由于
答案 0 :(得分:1)
试试这个。首先创建一个方法来绘制精灵间隔开的精灵:
// increment is how far apart each sprite is drawn
private void DrawEvenlySpacedSprites(Texture2D texture, Vector2 point1, Vector2 point2, float increment)
{
var distance = Vector2.Distance(point1, point2); // the distance between two points
var iterations = (int)(distance / increment); // how many sprites with be drawn
var normalizedIncrement = 1.0f / iterations; // the Lerp method needs values between 0.0 and 1.0
var amount = 0.0f;
// not sure if this is needed but it's here to make sure at least one
// sprite is drawn if the two points are very close together
if(iterations == 0)
iterations = 1;
for (int i = 0; i < iterations; i++)
{
var drawPoint = Vector2.Lerp(point1, point2, amount);
_spriteBatch.Draw(texture, drawPoint, Color.White);
amount += normalizedIncrement;
}
}
然后将Update方法更改为以下内容:
private Vector2? _previousPoint;
protected override void Update(GameTime gameTime)
{
TouchCollection touches = TouchPanel.GetState();
foreach (TouchLocation touch in touches)
{
if (touch.State == TouchLocationState.Moved)
{
GraphicsDevice.SetRenderTarget(renderTarget);
_spriteBatch.Begin();
if (_previousPoint.HasValue)
DrawEvenlySpacedSprites(texture, _previousPoint.Value, touch.Position, 0.5f);
_spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
}
}
if (touches.Any())
_previousPoint = touches.Last().Position;
else
_previousPoint = null;
base.Update(gameTime);
}
它应该在每个触摸点之间绘制许多均匀间隔的精灵,从而提供您正在寻找的效果,而无需加快渲染速度(无论如何都无法实现)。
让我知道它是怎么回事。祝你好运:))