Libgdx 如何基于它接触的其他对象裁剪纹理?
我有一个线条演员,可能有其他物体和它相交,我需要裁剪出那个部分 上面是演员的形象 这个矩形也是一个图像,演员可能会沿着线条随机出现 这是我想要得到的结果的样本。我需要关于如何使用libgdx实现这一点的建议 [编辑] 正如我所建议的,我正在尝试使用fbo来提取缓冲区。下面是我目前正在处理的代码Libgdx 如何基于它接触的其他对象裁剪纹理?,libgdx,Libgdx,我有一个线条演员,可能有其他物体和它相交,我需要裁剪出那个部分 上面是演员的形象 这个矩形也是一个图像,演员可能会沿着线条随机出现 这是我想要得到的结果的样本。我需要关于如何使用libgdx实现这一点的建议 [编辑] 正如我所建议的,我正在尝试使用fbo来提取缓冲区。下面是我目前正在处理的代码 @Override public void draw(Batch batch, float parentAlpha) { fbo.begin(); getStage().getVi
@Override
public void draw(Batch batch, float parentAlpha) {
fbo.begin();
getStage().getViewport().apply();
Gdx.gl.glClearColor(0f,0f,0f,0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.draw(trLine,position.x,position.y);
batch.flush();
fbo.end();
getStage().getViewport().apply();
batch.draw(fbo.getColorBufferTexture(),0,0);
}
我可以将绘图缓冲到缓冲区中,稍后再绘制,但大小刚好不同。下面是创建和处置fbo的代码。它在绘图循环之外
fbo = new FrameBuffer(Pixmap.Format.RGBA8888,getStage().getViewport().getWidth(),getStage().getViewport().getHeight(),false,true);
[固定基地运营商]
下面是具有工作fbo但混合未按预期工作的编码。我会一直努力直到成功
fbo.begin();
Gdx.gl.glClearColor(0f,0f,0f,0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(trLine,position.x,position.y);
batch.end();
int srcFunc = batch.getBlendSrcFunc();
int dstFunc = batch.getBlendDstFunc();
batch.enableBlending();
batch.begin();
batch.setBlendFunction(GL20.GL_ONE, GL20.GL_FUNC_REVERSE_SUBTRACT);
for(int i = 0 ; i < cropRectangles.size() ; i++){ batch.draw(cropTexture.get(i),cropRectangles.get(i).x,cropRectangles.get(i).y);
}
batch.end();
fbo.end();
getStage().getViewport().apply();
//reset blending before drawing the desire result
batch.begin();
batch.setBlendFunction(srcFunc, dstFunc);
batch.draw(fbo.getColorBufferTexture(),0,0);
batch.end();
fbo.begin();
Gdx.gl.glClearColor(0f,0f,0f,0f);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
batch.begin();
批次图(trLine,位置x,位置y);
batch.end();
int srcFunc=batch.getBlendSrcFunc();
int dstFunc=batch.getblendstfunc();
batch.enablembling();
batch.begin();
batch.setBlendFunction(GL20.GL\u ONE,GL20.GL\u FUNC\u REVERSE\u SUBTRACT);
对于(inti=0;i
但是输出没有得到任何混合效果。它仍然是一个填充了白色的矩形
[完整代码]
我最终正确地应用了这个方程,并且能够重置它,这样它就不会影响我在此之后绘制的其他东西
fbo.begin();
Gdx.gl.glClearColor(0f,0f,0f,0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(trLine,position.x,position.y);
batch.end();
int srcFunc = batch.getBlendSrcFunc();
int dstFunc = batch.getBlendDstFunc();
batch.enableBlending();
batch.begin();
batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glBlendEquation(GL20.GL_FUNC_REVERSE_SUBTRACT);
for(int i = 0 ; i < cropRectangles.size() ; i++){
batch.draw(cropTexture.get(i),cropRectangles.get(i).x,cropRectangles.get(i).y);
}
batch.end();
batch.flush();
fbo.end();
Gdx.gl.glBlendEquation(GL20.GL_FUNC_ADD);
getStage().getViewport().apply();
batch.begin();
batch.setBlendFunction(srcFunc, dstFunc);
batch.draw(fbo.getColorBufferTexture(),0,0);
batch.end();
fbo.begin();
Gdx.gl.glClearColor(0f,0f,0f,0f);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
batch.begin();
批次图(trLine,位置x,位置y);
batch.end();
int srcFunc=batch.getBlendSrcFunc();
int dstFunc=batch.getblendstfunc();
batch.enablembling();
batch.begin();
batch.setBlendFunction(GL20.GL_ONE,GL20.GL_ONE减去SRC_ALPHA);
Gdx.gl.glBlendEquation(GL20.gl函数逆减法);
对于(int i=0;i
对于这种情况,一种解决方案是用纯色背景填充矩形(我的意思是在矩形环内制作一个矩形)。它将裁剪出该部分。您可以使用混合模式来实现这一点。您的矩形应该有两个部分。
外部部分和透明部分
外部部分是你的实际部分,将像往常一样绘制
透明部分将是另一个具有完整alpha的矩形,您应该对此部分使用混合
这种模式清除交叉口区域,似乎是正确的模式
您可以很容易地在libgdx中找到混合的示例用法
SpriteBatch sb = (SpriteBatch)batch;
// draw our destination image
sb.draw(dst, 0, 0);
sb.end();
// remember SpriteBatch's current functions
int srcFunc = sb.getBlendSrcFunc();
int dstFunc = sb.getBlendDstFunc();
// Let's enable blending
sb.enableBlending();
sb.begin();
// blend them
sb.setBlendFunction(GL20.GL_ONE, GL20.ONE_MINUS_SRC_ALPHA);
sb.draw(src, 0, 0);
// Reset
sb.end();
sb.begin();
sb.setBlendFunction(srcFunc, dstFunc);
此外,还必须更改混合方程。
而且它不是唯一的雪碧批,所以我们需要改变所有的游戏
//Equation for effect you want
Gdx.gl.glBlendEquation(GL20.GL_FUNC_REVERSE_SUBTRACT);
//After draw you should also reset this
Gdx.gl.glBlendEquation(GL20.GL_FUNC_ADD);
现在我们应该将这个绘制到FrameBufferObject,因为透明区域将显示spritebatch的背景色。
如果对你来说没问题,那么就完成了,但你想在这个透明区域看到另一个纹理,比如背景图像或其他东西,那么我们还有一个步骤
您应该阅读本文了解FBO(FrameBufferObject)的用途
您需要使用它来合并精灵和透明区域,以便可以将它们作为整个图像使用,并从透明区域中透视背景图像
根据您的游戏,也许使用第二个视口或“精灵批处理”会更简单、更高效。不太清楚裁剪部分图片是什么意思,因为您没有上述示例。在上一张图片中,您只需绘制直线,然后绘制矩形。当使用舞台时,这意味着演员必须按照您希望绘制的顺序添加到舞台。使用表示层的组可以更容易地控制这一点:帧是透明的,而且因为帧下几乎没有对象。有没有什么方法可以在碰到特定的演员时使断线?我不太了解剪刀是如何工作的,但是有没有其他方法可以反转剪刀?就像我放一个长方形,然后剪下一部分线?(只有线)矩形必须是透明的,它下面会有一个要显示的对象。你能提供一些代码来说明如何在混合中实现帧缓冲区吗?或者我应该更新我的代码来回答这个问题,我仍然在努力完成这项工作,因为它可能会影响我的整个projectFramebuffer,并返回给你一个新的纹理。framebuffer.getTexture();然后你所要做的就是混合成规则纹理。你可以在google上找到libgdx混合的例子。很抱歉,我甚至不能让fbo和我的演员一起工作,因为它在演员里面。我只是对fbo进行常规的抽签,结果与正常的抽签阶段不同。尺寸和坐标都很乱,我已经更新了我的问题,以提供更多关于fbo部件当前工作的信息。然后删除getStage().getViewport().apply();就在fbo绘制之前,因为你的fbo已经被转换到你的视口中了。
//Equation for effect you want
Gdx.gl.glBlendEquation(GL20.GL_FUNC_REVERSE_SUBTRACT);
//After draw you should also reset this
Gdx.gl.glBlendEquation(GL20.GL_FUNC_ADD);