FrameBuffer to Texture, Alpha not working

I’m probably doing it wrong or missing an option. I create a viewport and render that to a texture. Then I take the texture and attach it to the guiNode.

When I attach the texture to the guiNode, all the areas where nothing was rendered to is BLACK, instead of blank so that area can be transparent.
What am I missing to render to a texture so that the background is not black, it is blank.

image

Let me know if this helps, explain what I’m asking for.
Kevin

We would need to see your code to correct your setup. AKA: “Your code looks fine from here.”

public class RenderToFrameBuffer
{
	Camera cam;
    private Node fbNode;
	private ViewPort offView;
	private int width;
	private int height;
	private ColorRGBA backgroundColor;


	
	public RenderToFrameBuffer(String name, int width, int height)
	{
		this.fbNode = new Node(name);
		this.width = width;
		this.height = height;
		backgroundColor = ColorRGBA.BlackNoAlpha;
	}
	
	
    public Texture setupOffscreenViewPort(RenderManager renderManager)
    {
    	cam = new Camera(width, height);
        float aspect = (float) cam.getWidth() / cam.getHeight();
    	cam.setFrustum(1, 3000, -0.3636997f, 0.3636997f, 0.3636997f / aspect, -1 * (0.3636997f / aspect));
    	cam.setFrustumPerspective(90f, 1f, 1f, 1000f);

    	cam.setLocation(new Vector3f(0f, 0f, -5f));
    	cam.lookAt(new Vector3f(0f, 0f, 0f), Vector3f.UNIT_Y);
    	cam.update();

    	
        offView = renderManager.createPreView("Offscreen View", cam);
        offView.setClearFlags(true, true, true);
        offView.setBackgroundColor(backgroundColor);

        // create offscreen framebuffer
        FrameBuffer offBuffer = new FrameBuffer(width, height, 1);

        //setup framebuffer's texture
        Texture2D offTex = new Texture2D(width, height, Format.ABGR8);
        offTex.setMinFilter(Texture.MinFilter.Trilinear);
        offTex.setMagFilter(Texture.MagFilter.Bilinear);

        //setup framebuffer to use texture
        offBuffer.setDepthBuffer(Format.Depth);
//        offBuffer.setDepthTarget(null);
        offBuffer.setMultiTarget(true);
        
        
        FrameBufferTextureTarget buffTar =  FrameBufferTarget.newTarget((Texture)offTex);
        offBuffer.addColorTarget(buffTar);
        
        //set viewport to render to offscreen framebuffer
        offView.setOutputFrameBuffer(offBuffer);

        // attach the scene to the viewport to be rendered
        offView.attachScene(fbNode);
        
//        fbNode.setQueueBucket(Bucket.Gui);
        
        return offTex;
    }

    public void simpleUpdate(float tpf) 
    {
    	//We need to make sure the game Node gets updated, since it is not
    	//tied to guiNode or rootNode.  We have to call it manually.
        fbNode.updateLogicalState(tpf);
        fbNode.updateGeometricState();
    	
    }
    
    public void render(RenderManager rm)
    {
    	
    }
    
    public Node getMainNode()
    {
    	return fbNode;
    }

    
    public ViewPort getViewPort()
    {
    	return this.offView;
    }

    public void setBackGroundColor(ColorRGBA color)
    {
    	this.backgroundColor = color;
    	offView.setBackgroundColor(color);
    }
    
    public Camera getCamera()
    {
    	return cam;
    }
}

    }

//Here is the code to create the framebuffer
        renderToGUIFrameBuffer = new RenderToFrameBuffer("Gui FrameBuffer", width, height);
        framebufferGUITextureNode = new FramebufferTextureNode(assetManager, renderToGUIFrameBuffer.setupOffscreenViewPort(rm), actualWindowScale, settings, videoWidth);
        framebufferGUITextureNode.setLocalTranslation(0, 0, 0);
        guiNode.attachChild(framebufferGUITextureNode );


Here is the framebuffer class and a small clip of the code to create the framebuffer.

Even test example “TestRenderToTexture” doesn’t render with alpha on the texture. The background is solid in the example also.
I’ve not locate an example that actually shows the framebuffer using Alpha values for area not rendered too. Even though the background color is set to 0,0,0,0

Thanks.

2 Likes

Are you sure you geometry is setup for transparency? Have you tried setting a prerendered texture with known good alpha to see if it also has the problem?

For the portion of the code I can see, I didn’t spot any immediate issues. If the above suggestion doesn’t yield any new evidence then you may want to create a single class test case that illustrates the issue.

If it’s helpful, here is some code that generates textures with alpha that I used to automatically generate a tree’s texture atlas:

The solution is that Texture sets the Format type to ‘BGR8’ by default. Also, not an easy way to change it through the construction of the texture class.

        //setup framebuffer's texture
        Texture2D offTex = new Texture2D(width, height, Format.RGBA8);
        offTex.setMinFilter(Texture.MinFilter.Trilinear);
        offTex.setMagFilter(Texture.MagFilter.Bilinear);

        FrameBufferTextureTarget buffTar =  FrameBufferTarget.newTarget((Texture)offTex);
        Texture texture = buffTar.getTexture();
        texture.getImage().setFormat(Format.RGBA8);
        offBuffer.addColorTarget(buffTar);

SO, after creating everything. Need to get the texture back out of it, because it doesn’t care how you defined the texture above. it over write the format to ‘BGR8’ even thought you selected RGBA8.
So if you get the texture back out of the FrameBufferTextureTarget and then set the “Format” back to RGBA8.

Then is works, Alpha mode is active. Just the default FrameBufferTextureTarget is HARD CODED to not support Alpha. You have to change it.

I would assume this is a bug in 3.4.

You code works because that was for <= 3.3 version. 3.4 that changed it to use FrameBufferTextureTarget and it was changed to no ALPHA.

THanks,

4 Likes

I suspect that this is a bug. I opened https://github.com/jMonkeyEngine/jmonkeyengine/issues/1606 for investigation. Probably not the best description I gave there since I don’t fully grasp this.

3 Likes

I updated the bug report. hope it helps.

4 Likes