RenderToTexture on GUI

Ok I am doing some render to texture of some fake scene, and I am rendering this texture to GUI quad in part of the screen.The thing that is bothering me is how to make this texture’s background transparent. My fake scene contains some objects of different colors, but background is black, so I would like to make this black color transparent when rendered on GUI quad but other objects should still be visible normally and drawn on top of normal scene.

English is not my main language but hopefully you can understand what I am trying to achieve and help me. :slight_smile:

I probably forgot to mention that I am doing this in JME3, based on TestRenderToTexture. :slight_smile:

Here is my code:



private void createAxisRods()

{

offScreenCam = new Camera(axisRodsOffRenderSize, axisRodsOffRenderSize);

offScreenCam.setFrustumPerspective(45f, 1f, 1f, 1000f);



// create a pre-view. a view that is rendered before the main view

ViewPort offView = renderManager.createPreView(“Offscreen View”, offScreenCam);

offView.setClearEnabled(true);

offView.setBackgroundColor(ColorRGBA.BlackNoAlpha);



// create offscreen framebuffer

FrameBuffer offBuffer = new FrameBuffer(axisRodsOffRenderSize, axisRodsOffRenderSize, 0);



//setup framebuffer’s texture

Texture2D offTex = new Texture2D(axisRodsOffRenderSize, axisRodsOffRenderSize, Format.RGB8);



//setup framebuffer to use texture

offBuffer.setDepthBuffer(Format.Depth);

offBuffer.setColorTexture(offTex);



//set viewport to render to offscreen framebuffer

offView.setOutputFrameBuffer(offBuffer);



// setup framebuffer’s scene

float longSide = 2;

float shortSide = 0.2f;

Geometry xAxis = new Geometry(“X-Axis”, new Box(Vector3f.ZERO, longSide, shortSide, shortSide));

Geometry yAxis = new Geometry(“Y-Axis”, new Box(Vector3f.ZERO, shortSide, longSide, shortSide));

Geometry zAxis = new Geometry(“Z-Axis”, new Box(Vector3f.ZERO, shortSide, shortSide, longSide));

xAxis.setMaterial(new SolidColorMaterial(ColorRGBA.Red));

yAxis.setMaterial(new SolidColorMaterial(ColorRGBA.Green));

zAxis.setMaterial(new SolidColorMaterial(ColorRGBA.Blue));



xAxis.setLocalTranslation(longSide - shortSide, 0, 0);

yAxis.setLocalTranslation(0, longSide - shortSide, 0);

zAxis.setLocalTranslation(0, 0, longSide - shortSide);



fakeScene = new Node(“Fake node”);

fakeScene.attachChild(xAxis);

fakeScene.attachChild(yAxis);

fakeScene.attachChild(zAxis);



// attach the scene to the viewport to be rendered

offView.attachScene(fakeScene);



Geometry quad = new Geometry(“Quad”, new Quad(axisRodsQuadSize, axisRodsQuadSize));

// Material mat = new Material(assetManager, “Common/MatDefs/Gui/Gui.j3md”);

// mat.setTexture(“m_Texture”, offTex);

// mat.setColor(“m_Color”, ColorRGBA.White);

Material mat = new Material(assetManager, “Common/MatDefs/Misc/SimpleTextured.j3md”);

mat.setTexture(“m_ColorMap”, offTex);

mat.getAdditionalRenderState().setBlendMode(BlendMode.AlphaAdditive);

mat.setTransparent(true);

quad.setMaterial(mat);



guiNode.attachChild(quad);

}



First, I don’t know what material to use, second I don’t really know what blend mode to use. :slight_smile:

Current code is working ok, background is transparent, but axis rods are merged with normal scene on the screen. I would like them to appear over it and not blend in…

There are two parts to this, first you have to make sure your framebuffer colorbuffer has an alpha channel, this is needed for alpha blending. And second, you need to use the right blend mode (Alpha) for alpha blending.

So looking at your code, you’ll have to change the offscreen texture’s format to RGBA8, and set the quad’s BlendMode to Alpha instead of AlphaAdditive.

Thanks!

Works great! :slight_smile: