libgdx SpriteBatch 渲染到纹理

发布于 2024-12-06 08:50:00 字数 225 浏览 3 评论 0原文

是否可以使用 libGdx(适用于 Android/桌面的 Java 引擎)中的 SpriteBatch 渲染到纹理?如果是这样,怎么办?

基本上我想将所有内容渲染到 512 x 256 纹理的 320 x 240 区域,然后缩放区域以适合屏幕(在横向模式下)。这样我想消除当我独立缩放 alpha 混合纹理时发生的伪像。如果有任何其他方法可以删除此类工件,请指出:)

是否有 libGdx 的在线文档?

Is it possible to render to texture using SpriteBatch in libGdx (Java engine for Android/Desktop)? If so, how do it?

Basically I want to render everything to 320 x 240 region of 512 x 256 texture and than scale region to fit screen (in landscape mode). This way I want to eliminate artifacts which happen when I scale alpha blended textures independently. If there is any other way to remove such artifacts, please point them out :)

And is there any online documentation for libGdx?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时常饿 2024-12-13 08:50:00

这段代码是在 LibGDX 论坛上给我的,它运行得非常完美。

private float m_fboScaler = 1.5f;
private boolean m_fboEnabled = true;
private FrameBuffer m_fbo = null;
private TextureRegion m_fboRegion = null;

public void render(SpriteBatch spriteBatch)
{
    int width = Gdx.graphics.getWidth();
    int height = Gdx.graphics.getHeight();

    if(m_fboEnabled)      // enable or disable the supersampling
    {                  
        if(m_fbo == null)
        {
            // m_fboScaler increase or decrease the antialiasing quality

            m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false);
            m_fboRegion = new TextureRegion(m_fbo.getColorBufferTexture());
            m_fboRegion.flip(false, true);
        }

        m_fbo.begin();
    }

    // this is the main render function
    my_render_impl();

    if(m_fbo != null)
    {
        m_fbo.end();

        spriteBatch.begin();         
        spriteBatch.draw(m_fboRegion, 0, 0, width, height);               
        spriteBatch.end();
    }   
}

This snippet was given to me on the LibGDX forum and it works flawlessly.

private float m_fboScaler = 1.5f;
private boolean m_fboEnabled = true;
private FrameBuffer m_fbo = null;
private TextureRegion m_fboRegion = null;

public void render(SpriteBatch spriteBatch)
{
    int width = Gdx.graphics.getWidth();
    int height = Gdx.graphics.getHeight();

    if(m_fboEnabled)      // enable or disable the supersampling
    {                  
        if(m_fbo == null)
        {
            // m_fboScaler increase or decrease the antialiasing quality

            m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false);
            m_fboRegion = new TextureRegion(m_fbo.getColorBufferTexture());
            m_fboRegion.flip(false, true);
        }

        m_fbo.begin();
    }

    // this is the main render function
    my_render_impl();

    if(m_fbo != null)
    {
        m_fbo.end();

        spriteBatch.begin();         
        spriteBatch.draw(m_fboRegion, 0, 0, width, height);               
        spriteBatch.end();
    }   
}
吾家有女初长成 2024-12-13 08:50:00

目前,我建议您使用Pixmap。你看,为它们编写的函数并不多,但显然你可以将一个 Pixmap (带有 alpha 通道)写入另一个 (pm1.drawPixmap(pm2, srcx, srcy, w, h, ...)),前提是您不想缩放它(您可以稍后缩放合成,但合成中使用的图片的比例不会调整大小...除非您编写调整大小功能)。

如果您想做更复杂的事情(例如使用 BitmapFont 将字符串写入 Texture 以便以后操作,或将其写入 Pixmap然后将其上传到视频内存),然后...然后告诉我你是否成功(因为我想进入这个方向进行优化)。我认为最好的办法是将所需的功能添加到libgdx中...

无论如何,不​​要将我在这里写的任何内容视为不可否认的事实 - 如果我进一步了解,我会更新。

我发现的文档有些有限 - 我确信您已经阅读过它。 badlogic 的博客和 googlecode 项目页面中有相关信息,还有 Mario 写的一本相对不错的书,《Beginning Android Games》。此外,还有一些(非常基本的)视频。并且不要忘记您可以查阅源代码和漂亮的示例...

此外,您可以随时在论坛中提问:http ://badlogicgames.com/forum/

更新:使用 FrameBufferTextureRegion 的答案无疑要好得多。我留下这个只是因为它提到了文档。

For the moment, I would recommend you to use Pixmaps. You see, there are not many functions written for them, but apparently you can write a Pixmap (with alpha channel) to another one (pm1.drawPixmap(pm2, srcx, srcy, w, h, ...)), providing that you don't want to scale it (you may scale the composition later, but the proportion the pictures used in the composition won't be resized... unless you write a resizing function).

If you want to do more complex stuff (like writing a string using a BitmapFont into a Texture for later manipulation, or writing it into a Pixmap and then uploading it to the video memory), then... then tell me if you succeed (as I want to go into that direction to optimize). I think that the best would be to add the needed functions to libgdx...

Anyway, don't take anything I wrote here as an undeniable truth- if I get further, I will update.

The documentation I found is somewhat limited -I'm sure you've gone through it already. There's information in the badlogic's blog and in the googlecode project page -and also there's a relatively good book written by Mario, "Beginning Android Games". Also, there are a few (very basic) videos. And don't forget that you can consult source and the beautiful examples...

Also, you can always ask in the forum: http://badlogicgames.com/forum/

UPDATE: Your answer using the FrameBuffer and TextureRegion is undeniably much better. I leave this one just because it mentions the documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文