【问题标题】:LibGDX - Issue with FrameBuffer, Scene2D Table and clippingLibGDX - FrameBuffer、Scene2D 表和剪辑问题
【发布时间】:2016-05-20 12:34:52
【问题描述】:

我有一个自定义 ImageTextButton,我首先将按钮渲染到 FrameBuffer,然后使用 frameBuffer.getColorBufferTexture() 进行绘制。我真的不想这样做,但我使用带有此按钮的自定义着色器来创建一些视觉效果,而我能够实现它的唯一方法是使用 FrameBuffer。我惊讶地发现这实际上工作得非常顺利和快速,整个过程在慢速设备上需要 1-2 毫秒,并且有几个实例不会导致任何形式的帧率下降,所以我对这一点很满意。

我遇到的问题是当我在 ImageTextButton 上启用剪辑时(使用 setClip(true))。这样做的原因是按钮可以改变宽度,我希望它可以在按钮范围内剪辑文本。如果我禁用 FrameBuffer 并正常渲染,这部分也可以很好地工作。如果我将 2 结合起来,似乎剪辑过程会变得混乱,结果要么没有文本,要么文本的一小部分。

所以这里是相关的代码。我认为这是因为我设置 FrameBuffer 和 SpriteBatch 大小/投影矩阵只是为了处理活动区域(为了提高效率)但是如果我不修改任何这些并使用相同的批处理/投影矩阵,那么 FrameBuffer 管理整个屏幕,还是一样的结果。

public void initFrameBuffer(){
    xCache = (int) super.getX(); yCache = (int) super.getY();
    widthCache = (int) super.getWidth(); heightCache = (int) super.getHeight();

    frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, widthCache, heightCache, false);

    fboProjectionMatrix.setToOrtho2D(xCache, yCache+heightCache, widthCache, -heightCache);

    this.fbBatch = new SpriteBatch();
    this.fbBatch.setProjectionMatrix(fboProjectionMatrix);

    this.frameBufferReady = true;
}

public void doFrameBuffer(Batch batch, float parentAlpha){
    batch.end();

    frameBuffer.begin();
    fbBatch.begin();
    Gdx.gl20.glClearColor(0f, 0.0f, 0.0f, 0.0f);
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
    super.draw(fbBatch, parentAlpha);
    fbBatch.end();
    frameBuffer.end();

    batch.begin();
}

public void drawFrameBufferObject(Batch batch, float parentAlpha){
    batchColorCache = batch.getColor();
    batch.setColor(1.0f, 1.0f, 1.0f, parentAlpha);
    batch.draw(frameBuffer.getColorBufferTexture(), getX(), getY());
    batch.setColor(batchColorCache);
}

@Override
public void draw(Batch batch, float parentAlpha) {
    if (!this.frameBufferReady) initFrameBuffer();
    doFrameBuffer(batch, parentAlpha);
    drawFrameBufferObject(batch, parentAlpha);   
}

抱歉,代码太长了,它实际上已经为必要的部分进行了大量修剪。 一如既往地非常感谢您的帮助!

【问题讨论】:

    标签: android libgdx


    【解决方案1】:

    玩了很多之后,我发现的解决方案可能在其他情况下可能有用,那就是通过顶点修改真正剪切 BitmapFontCache,不涉及剪刀!因此,如果有人会觉得这很有用,代码就是:

            float xStart = ...start position of clip
            float xEnd = ...end position of clip
    
            //vertex offset numbers
            int x_1 = 0, x_2 = 5, x2_1 = 10, x2_2 = 15;
            int u_1 = 3, u_2 = 8, u2_1 = 13, u2_2 = 18;
    
            for (int j = 0, n = pageCount; j < n; j++) {
                int c = cache.getVertexCount(j);
                int newIdx = 0;
                if (c > 0) { // ignore if this texture has no glyphs
                    float[] vertices = cache.getVertices(j);
                    for(int i = 0; i < vertices.length; i+=20){
    
                        //if any of the vertices are outside the label, don't put them in the new cache
                        if(vertices[i+x2_1] > xStart && vertices[i+x_1] < xEnd){
                            for(int k = 0; k < 20; k++){
                                clippedVerts[j][newIdx+k] = vertices[i+k];
                            }
    
                            //case on major left glyph
                            if(vertices[i+x_1] < xStart){
                                float xDiff = vertices[i+x2_1]-xStart; //difference between right of glyph and clip
                                float xRatio = xDiff / (vertices[i+x2_1]-vertices[i+x_1]);
                                float uDiff = vertices[i+u2_1] - vertices[i+u_1];
                                float newU = vertices[i+u2_1] - uDiff*xRatio;
    
                                clippedVerts[j][newIdx+x_1] = xStart;
                                clippedVerts[j][newIdx+x_2] = xStart;
                                clippedVerts[j][newIdx+u_1] = newU;
                                clippedVerts[j][newIdx+u_2] = newU;
                            }
    
                            //case on major right glyph
                            if(vertices[i+x2_1] > xEnd){
                                float xDiff = xEnd-vertices[i+x_1]; //difference between left of glyph and clip
                                float xRatio = xDiff / (vertices[i+x2_1]-vertices[i+x_1]);
                                float uDiff = vertices[i+u2_1] - vertices[i+u_1];
                                float newU_2 = vertices[i+u_1] + uDiff*xRatio;
    
                                clippedVerts[j][newIdx+x2_1] = xEnd;
                                clippedVerts[j][newIdx+x2_2] = xEnd;
                                clippedVerts[j][newIdx+u2_1] = newU_2;
                                clippedVerts[j][newIdx+u2_2] = newU_2;
                            }
                            newIdx += 20;
                        }
                    }
                }
                clippedIdx[j] = newIdx;
            }
    
            for (int j = 0, n = pageCount; j < n; j++) {
                int idx = clippedIdx[j];
                if (idx > 0) { // ignore if this texture has no glyphs
                    float[] vertices = clippedVerts[j];
                    batch.draw(regions.get(j).getTexture(), vertices, 0, idx);
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      相关资源
      最近更新 更多