标签: spritebatch

Monogame使用Graphics.DrawString(而不是SpriteBatch.DrawString)渲染文本

使用Graphics.DrawString将一组(相当静态的)文本渲染到屏幕外位图,将其转换为Texture2D一次,然后简单地调用SpriteBatch.Draw,而不是使用内容管道并使用渲染文本,是否有任何缺点SpriteFont?这基本上是一页文本,绘制成"纸张",但用户也可以选择调整字体大小,这意味着我必须包含不同大小的spritefonts.

由于这是一个仅限Windows的应用程序(不打算移植它),我可以访问所有字体,例如普通的WinForms应用程序,我相信使用Graphics.DrawString(甚至TextRenderer)使用精灵字体时渲染质量会好得多.

此外,似乎性能可能更好,因为SpriteBatch.DrawString需要在每次迭代中"渲染"整个文本(即分别为每个字母发送顶点),而使用位图我只做一次,所以它应该稍微减少工作量CPU方面.

  1. 我没有看到任何问题或缺点吗?
  2. 是否可以使用spritefonts获得alpha混合文本?我已经看过周围提到的Nuclex框架,但它并没有被移植到Monogame AFAIK.

[更新]

从技术方面来看,它似乎比通过精灵字体更好地完成文本渲染.如果它们是水平渲染的,我甚至会得到ClearType.可能存在的一个问题是字体的spritesheets(可能是?)在纹理内存方面比为文本页面创建单独的纹理更有效.

drawstring monogame spritefont spritebatch

13
推荐指数
1
解决办法
2665
查看次数

Visual Studio说"方法必须有一个返回类型"

它说

"方法必须具有返回类型"

每当我尝试调试它.

我不知道怎么修这门课

这是ac#编码的2d游戏的玩家类

public class player
{
    public float moveSpeed;
    public Vector2 position;
    public Texture2D texture;

    //default constructer
    public Player(Texture2D tex, Vector2 startPos)
    {
        position  = startPos;
        texture   = tex;
        moveSpeed = 5.0f;
    }
    public void Update(GameTime gameTime)
    {
        //------------------------------------------
        //check for keyboard input(keyboard IF statements)

    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, Color.White);
    }
}
Run Code Online (Sandbox Code Playgroud)

c# methods spritebatch

8
推荐指数
3
解决办法
5万
查看次数

libgdx SpriteBatch UnsatisfiedLinkError桌面

当我试图运行桌面独立版本时,我在LibGdx项目中得到UnsatisfiedLinkError.请注意,如果我只是直接运行桌面版本而不构建一个独立的jar,只有我先构建一个桌面jar,然后尝试运行它,才会出现此问题.有趣的是,直接运行桌面版本总体上要容易得多,而且我甚至没有接近运行独立的桌面jar,直到我搜索了一大堆jar文件,并手动添加它们到LibGdx项目.

我在Windows 10 x64上使用Android Studio 3.3,gradle 4.10和Java 1.8.0_201,并使用'gradle'命令将我的LibGdx应用程序版本部署到桌面:

gradle desktop:dist 来自ProjectRoot.

然后在运行由此创建的jar文件时,通过从ProjectRoot键入命令行:

cd desktop/build/libs, 然后 java -jar desktop-1.0.jar

这样做我得到了 UnsatisfiedLinkError error

我尝试将jar文件复制到Linux并在那里进行测试,我也遇到了同样的错误.

我做了一些搜索,我发现一个建议可能是调用new SpriteBatch()过早发生了:

就像在create()核心模块中的函数之前一样,但这不是问题,因为new SpriteBatch()调用从来没有比这更早.

此链接使用的建议LibGdx Setup Generator也没有帮助我,因为我已经使用该LibGdx Setup GeneratorGUI工具来创建同一个项目,我仍然遇到这个问题.

我注意到,虽然从程序的输出中,它在new SpriteBatch()调用时抛出了UnsatifisedLinkError异常,因为该链接确实引用了,但它是从create()函数调用的,而不是早于该函数.

我做了一个实验,发现如果我new SpriteBatch()完全删除了调用以及依赖于该调用的所有其他代码,例如创建纹理,并使用精灵批处理来绘制该纹理.我发现通过这样做,程序运行没有得到错误,当然我也无法绘制任何东西.

所以,然后我做了另一个实验,并按照以下方式将new SpriteBatch()调用从create函数移到render()函数中,正如您将在代码中注意到的那样.我只是尝试在每次执行render()函数时创建SpriteBatch(),如果尚未创建它,并输出它抛出的次数UnsatisfiedLinkError exception

------------------------- MyGdxGame.java ---------------------- ----------------

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class …
Run Code Online (Sandbox Code Playgroud)

desktop-application gradle libgdx unsatisfiedlinkerror spritebatch

8
推荐指数
1
解决办法
291
查看次数

XNA/Monogame,绘制多个剪切/倾斜精灵的最快方法

我通常SpriteBatch在XNA/Monogame中使用2D游戏,并且最近刚刚研究DrawUserIndexedPrimatives过诸如此类的3D绘图方法.我正在开展一个项目,我们的动画师希望能够剪切精灵和纹理.

有了SpriteBatch你可以在一个矩阵传递SpriteBatch开始剪切的对象.就像是:

//translate object to origin
Matrix translate1 = Matrix.CreateTranslation(-rectangle.X, -rectangle.Y, 0);

//skew the sprite 33 degrees on the X and Y axis
Matrix skew = Matrix.Identity;
skew.M12 = (float)Math.Tan(33 * 0.0174532925f);
skew.M21 = (float)Math.Tan(33 * 0.0174532925f);

//translate object back
Matrix translate2 = Matrix.CreateTranslation(rectangle.X, rectangle.Y, 0);
Matrix transform = translate1 * skew * translate2;

_spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied,
                    SamplerState.PointWrap, DepthStencilState.Default,
                    RasterizerState.CullCounterClockwise, null, transform);
_spriteBatch.Draw(_texture, rectangle, Color.White);
_spriteBatch.End();
Run Code Online (Sandbox Code Playgroud)

明显的缺点是它需要你SpriteBatch为每个剪切的精灵进行一个新的开始和结束调用.我们目前只需要2个电话即可SpriteBatch开始游戏.一个用于UI,一个用于世界的东西.我们的艺术家想要使用剪切来做摇晃的树木或动物的腿和四肢的动物,所以如果我们给他们选择,我可以看到这个数字跳到10多个不同的批次.

平均水平有大约250个元素,每个元素包含10-20个精灵.

我已经为Android编写了一个测试,调用1000个精灵.没有任何偏斜,它可以在大约11秒或大约53fps中绘制所有1000,600次.但是,如果我倾斜每十个精灵(增加100个新的 …

3d xna drawing monogame spritebatch

7
推荐指数
1
解决办法
2817
查看次数

libgdx:使用PerspectiveCamera不显示SpriteBatch

虽然我确实掌握了OpenGL的基本知识,但我只是从libgdx开始.

我的问题是:为什么,拥有完全相同的代码,但只从OrthographicCamera切换到PerspectiveCamera有不再显示我的任何SpriteBatches的效果?

这是我使用的代码:

create()方法:

public void create() {
    textureMesh = new Texture(Gdx.files.internal("data/texMeshTest.png"));
    textureSpriteBatch = new Texture(Gdx.files.internal("data/texSpriteBatchTest.png"));

    squareMesh = new Mesh(true, 4, 4, 
            new VertexAttribute(Usage.Position, 3, "a_position")
            ,new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")

    );

    squareMesh.setVertices(new float[] {
            squareXInitial, squareYInitial, squareZInitial,             0,1,    //lower left
            squareXInitial+squareSize, squareYInitial, squareZInitial,  1,1,    //lower right
            squareXInitial, squareYInitial+squareSize, squareZInitial,  0,0,    //upper left
            squareXInitial+squareSize, squareYInitial+squareSize, squareZInitial,1,0});  //upper right 

    squareMesh.setIndices(new short[] { 0, 1, 2, 3});

    spriteBatch = new SpriteBatch();        
}
Run Code Online (Sandbox Code Playgroud)

和render()方法:

public void render() {
    GLCommon gl = Gdx.gl;

    camera.update();
    camera.apply(Gdx.gl10); …
Run Code Online (Sandbox Code Playgroud)

java opengl perspectivecamera libgdx spritebatch

6
推荐指数
1
解决办法
2876
查看次数

SpriteBatch:"在成功调用End之前,不能再次调用Begin."

所以我启动了我当前的项目,我做的第一件事是运行它,它给了我上面的例外.昨晚运行得很好.这是我的Draw事件中的所有代码.spriteBatch.Begin不会出现在项目的任何其他位置.在这里删除Begin会导致spriteBatch.Draw抛出异常,在begin之前放置spriteBatch.End也会引发异常.我不知道什么是错的,以及如何解决这个问题.

GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();
spriteBatch.Draw(background, Vector2.Zero, Color.White);

player.Draw(spriteBatch);
level1.Draw(spriteBatch);

spriteBatch.End();

base.Draw(gameTime);
Run Code Online (Sandbox Code Playgroud)

c# xna exception spritebatch

6
推荐指数
1
解决办法
3570
查看次数

设置SpriteBatch颜色(用于着色)会影响所有绘图

我创建了一个AnimatedSprite类,绘制一个特定的TextureRegion.有时我需要一种色调效果,所以我设置(this.color是Color我的一个字段AnimatedSprite):

super.draw(batch, parentAlpha);

batch.setColor(this.color);
batch.draw(this.frames[this.currentFrame], x, y, originX, originY, width, height, scaleX, scaleY, rotation)
batch.setColor(Color.WHITE);
Run Code Online (Sandbox Code Playgroud)

但是,当我将AnimatedSprite颜色设置为黑色或任何颜色时,其他所有颜色都会有颜色.我甚至尝试flush(),结束批次并开始新的等等......但似乎没有任何工作.

请帮我正确应用色调效果.我会很感激任何想法.

java tint sprite libgdx spritebatch

6
推荐指数
1
解决办法
5795
查看次数

LibGDX精灵批量字体坏刻度渲染

在尝试将文本/对话框系统放入我的游戏项目时,我遇到了一个问题.当我创建一个字体并在其上调用draw方法传递相机更新的spriteBatch时,该字体的每个像素都具有一个sprite的相同维度.

我得到以下渲染:

字体绘制比例问题

您可以在图片上看到的是每个像素超大的"你好"的"h"的顶部.相同的相机用于渲染瓷砖/精灵.

我想要实现的效果类似于:

艾略特任务的例子

这是代码:

    // 15 * 12 tile size
    camera = new OrthographicCamera(Const.VIEWPORT_WIDTH, Const.VIEWPORT_HEIGHT);
    BitmapFont font = new BitmapFont(Gdx.files.internal("data/fonts/myfont.fnt"));

    // ....

    // p => player position   
    camera.position.x = p.getX();
    camera.position.y = p.getY();
    camera.update();

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    font.draw(batch, "hello", p.getX(), p.getY());
    batch.end();
Run Code Online (Sandbox Code Playgroud)

我尝试过使用但font.setScale()没有成功.

有人知道如何实现这一目标吗?

java fonts scale libgdx spritebatch

6
推荐指数
1
解决办法
2672
查看次数

在libgdx中的3D空间中渲染SpriteBatch + ParticleEffect?

我使用的是libgdx 0.9.9版.我想在3D空间中使用ParticleEffect以及其他3D模型渲染火焰效果.

我的代码的逻辑流程:

  1. ModelBatch开始
  2. 使用ModelBatch在Bullet World中渲染所有3D模型
  3. ModelBatch结束
  4. SpriteBatch开始
  5. 使用SpriteBatch使用ParticleEffect(effect.draw)渲染火焰效果
  6. SpriteBatch结束
  7. 使用Stage绘制HUD

问题:火焰效果在3D空间中的某个点上呈现正常.但是当我旋转相机以使3D模型位于相机和火焰效果之间时,火焰效果会渲染3D模型,而不是隐藏在3D模型后面.

我尝试过的事情:

  1. 我先尝试渲染SpriteBatch然后再渲染3D模型:在这种情况下,火焰效果不可见.我猜测3D模型(图层)正在渲染火焰效果(图层),因此效果不可见.
  2. 我尝试在步骤1和3之间渲染SpriteBatch,即在modelbatch.begin和modelbatch.end之间渲染SpriteBatch.在这种情况下,根本看不到火焰效果.
  3. 我尝试将粒子效果渲染为Actor(添加到HUD Stage).正如预期的那样,火焰效果呈现为最顶层HUD层的一部分,同样的问题仍然存在.
  4. 我尝试过探索贴花,但发现粒子效果不适用于DebcalBatch.我不想在贴花上显示动画火.gif,因此没有尝试.

有没有人遇到类似的问题?任何建议的解决方法都可以使ParticleEffect成为3D世界的一部分,以便在被其他3D模型阻挡时隐藏它?我看过Xoppa在youtube上发布了关于libgdx中3D粒子的视频,但没有提到步骤/解决方案.任何帮助将受到高度赞赏.

3d particle-system libgdx spritebatch

6
推荐指数
1
解决办法
1308
查看次数

Monogame XNA从少量Draw()调用中获得低帧率

我一直在使用MonoGame将我在XNA中制作的滚动射击游戏移植到Linux上.几乎所有事情都进展顺利,但我在一个特定的地方遇到问题,调用SpriteBatch.Draw()会削弱帧速率.大多数游戏运行良好,没有任何打嗝.它同时吸引了大量的敌人和大量的子弹而没有任何减速.但是,导致丢帧的部分是分层滚动背景.代码的相关部分在这里.

Level.cs:

public void Draw(SpriteBatch spriteBatch)
{
    foreach (ScrollingBackgroundLayer sbl in scrollingBackground)
    {
        sbl.Draw(spriteBatch);
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的"scrollingBackground"是ScrollingBackgroundLayers的列表,其相关部分是:

ScrollingBackgroundLayers.cs:

Vector2[] scrollingBackgroundImages;
public float DrawLayer;

public ScrollingBackgroundLayer(GameScene newScene, Texture2D texture, float scrollSpeed, Color color)
{
    layerColor = color;
    layerSpeed = scrollSpeed;
    layerTexture = texture;
    thisScene = newScene;

    Initialize();
}

public void Initialize()
{
    scrollingBackgroundImages = new Vector2[2];

    for (int i = 0; i < 2; i++)
    {
        scrollingBackgroundImages[i] = new Vector2(0, thisScene.ScreenArea.Height - (layerTexture.Height * i));
    }
}


public void Update(GameTime gameTime) …
Run Code Online (Sandbox Code Playgroud)

xna frame-rate draw monogame spritebatch

5
推荐指数
1
解决办法
1844
查看次数