Hello,
I’m trying to add a custom view port inside a game and have encountered an interesting issue. When I get a texture from buffer used by my view port, put it into a Quad and attach to guiNode, everything works fine, but when I take the same texture and put it into NiftyImage to be rendered as element in niftyGUI, parts that are covered in texture become transparent.
Here is a code of a small text case:
public class Main extends SimpleApplication {
public static void main(String[] args) {
Main app = new Main();
app.setShowSettings(false);
AppSettings settings = new AppSettings(true);
settings.put("Width", 1280);
settings.put("Height", 720);
settings.put("Title", "JME3 testing");
settings.put("VSync", true);
settings.put("Samples", 4);
app.setSettings(settings);
app.setPauseOnLostFocus(false);
app.start();
}
@Override
public void simpleInitApp() {
initLights();
initBoxWithTexture();
initBoxWithColor();
initCameraWithRotation();
Texture2D tex1 = initDirectViewPort();
Geometry camView = new Geometry("selection camera view node", new Quad(300, 200));
Material camMaterial = new Material(getAssetManager(),
"Common/MatDefs/Misc/Unshaded.j3md");
camMaterial.setTexture("ColorMap", tex1);
camView.setMaterial(camMaterial);
camView.setLocalTranslation(300, 100, 0);
guiNode.attachChild(camView);
Nifty nifty = initNifty();
Element image1 = nifty.getScreen("start").findElementById("image1");
image1.getRenderer(ImageRenderer.class).setImage(
new NiftyImage(nifty.getRenderEngine(), new RenderImageJme(tex1)));
}
private Texture2D initDirectViewPort() {
ViewPort modelView = getRenderManager().createMainView("view", cam);
modelView.setClearFlags(true, true, true);
Texture2D texture2D = new Texture2D(300, 200, Image.Format.RGBA8);
FrameBuffer offBuffer = new FrameBuffer(300, 200, 1);
offBuffer.setDepthBuffer(Image.Format.Depth);
offBuffer.setColorTexture(texture2D);
modelView.setOutputFrameBuffer(offBuffer);
modelView.attachScene(rootNode);
return texture2D;
}
private void initCameraWithRotation() {
cam.setLocation(new Vector3f(0, -3, -5));
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
stateManager.attach(new AbstractAppState() {
private float rotation = 0;
public void update(float tpf) {
rotation += tpf/5;
cam.setLocation(new Vector3f(5 * FastMath.sin(rotation), 3, 5 * FastMath.cos(rotation)));
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
}
});
}
private void initBoxWithTexture() {
Geometry box = new Geometry("box", new Box(1, 1, 1));
Material material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
material.setTexture("DiffuseMap", assetManager.loadTexture("sun.jpg"));
material.setBoolean("UseMaterialColors", true);
material.setColor("Ambient", ColorRGBA.White);
material.setFloat("Shininess", 0f);
box.setMaterial(material);
box.setLocalTranslation(1.2f, 0, 0);
rootNode.attachChild(box);
}
private void initBoxWithColor() {
Geometry box = new Geometry("box", new Box(1, 1, 1));
Material material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
material.setColor("Diffuse", ColorRGBA.Brown);
material.setBoolean("UseMaterialColors", true);
material.setColor("Ambient", ColorRGBA.White);
material.setFloat("Shininess", 0f);
box.setMaterial(material);
box.setLocalTranslation(-1.2f, 0, 0);
rootNode.attachChild(box);
}
private void initLights() {
AmbientLight ambient = new AmbientLight();
ambient.setColor(ColorRGBA.White.mult(0.3f));
rootNode.addLight(ambient);
DirectionalLight directionalLight = new DirectionalLight();
directionalLight.setColor(ColorRGBA.White.mult(0.7f));
directionalLight.setDirection(new Vector3f(1, -1, 1).normalize());
rootNode.addLight(directionalLight);
}
private Nifty initNifty() {
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(
assetManager,
inputManager,
getAudioRenderer(),
getGuiViewPort());
Nifty nifty = niftyDisplay.getNifty();
nifty.fromXml("nifty.xml", "start");
Camera guiCam = new Camera(settings.getWidth(), settings.getHeight());
ViewPort niftyGuiViewPort = getRenderManager().createPostView("Nifty GUI", guiCam);
niftyGuiViewPort.addProcessor(niftyDisplay);
niftyGuiViewPort.setClearFlags(false, false, false);
return nifty;
}
}
plus nifty.xml:
<?xml version="1.0" encoding="UTF-8"?>
<nifty xmlns="http://nifty-gui.sourceforge.net/nifty-1.3.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://nifty-gui.sourceforge.net/nifty-1.3.xsd http://nifty-gui.sourceforge.net/nifty-1.3.xsd">
<useStyles filename="nifty-default-styles.xml"/>
<useControls filename="nifty-default-controls.xml"/>
<screen id="start">
<layer id="foreground" childLayout="center">
<panel height="100%" width="100%" valign="center" align="center" childLayout="vertical">
<panel width="100%" height="50px"/>
<panel width="100%" height="200px" childLayout="horizontal">
<panel width="300px" height="100%"/>
<panel width="300px" height="100%" childLayout="center">
<image id="image1" width="100%" height="100%"/>
</panel>
</panel>
</panel>
</layer>
</screen>
</nifty>
Both views are using the same texture and should look the same as main view. Top left view is one from niftyImage, bottom left is Quad on guiNode - as you can see, top one was displayed with transparent background and displayed part of textured box became transparent also - but colored box is displayed ok.
Is this some kind of bug in niftyGUI rendering images from view ports or some kind of misconfiguration on my part?