libgdx SpriteBatch render to texture

前端 未结 2 981
有刺的猬
有刺的猬 2020-11-28 20:53

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 2

2条回答
  •  隐瞒了意图╮
    2020-11-28 21:55

    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();
        }   
    }
    

提交回复
热议问题