I also need to generate transparent icon from model.
This is my test code, but the generated png file has no transparency
public class TestIconGenerator extends SimpleApplication {
public static final FunctionId F_GENERATE_ICON = new FunctionId("Generate Icon");
private FrameBuffer fb;
public TestIconGenerator() {
}
public static void main(String[] args) {
TestIconGenerator main = new TestIconGenerator();
AppSettings settings = new AppSettings(true);
settings.setRenderer(AppSettings.LWJGL_OPENGL3);
settings.setGammaCorrection(true);
settings.setVSync(true);
main.setSettings(settings);
//main.setShowSettings(false);
main.start();
}
@Override
public void simpleInitApp() {
GuiGlobals.initialize(this);
viewPort.setBackgroundColor(ColorRGBA.BlackNoAlpha);
//Camera camera = app.getCamera().clone();
//camera.resize(256, 256, true);
//camera.resize(1024, 256, false);
fb = new FrameBuffer(getCamera().getWidth(), getCamera().getHeight(), 1);
viewPort.setOutputFrameBuffer(fb);
//Texture2D fbTex = new Texture2D(getCamera().getWidth(), getCamera().getHeight(), Format.RGBA8);
fb.setDepthBuffer(Image.Format.Depth);
fb.setColorBuffer(Format.BGRA8);
//fb.setColorTexture(fbTex);
initLight();
//initFilters();
initInput();
Spatial model = getAssetManager().loadModel("tree/model-1/green-tree.gltf");
rootNode.attachChild(model);
}
private void initLight() {
Node probeNode = (Node) assetManager.loadModel("lightprobe/test-scene.j3o");
LightProbe probe = (LightProbe) probeNode.getLocalLightList().iterator().next();
rootNode.addLight(probe);
/**
* A white ambient light source.
*/
AmbientLight ambient = new AmbientLight();
ambient.setColor(ColorRGBA.White);
rootNode.addLight(ambient);
}
private void initInput() {
InputMapper inputMapper = GuiGlobals.getInstance().getInputMapper();
inputMapper.addDelegate(F_GENERATE_ICON, this, "generateIcon");
inputMapper.map(F_GENERATE_ICON, KeyInput.KEY_SPACE);
}
private void initFilters() {
FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
fpp.addFilter(new FXAAFilter());
fpp.addFilter(new ToneMapFilter(Vector3f.UNIT_XYZ.mult(11.0f)));//4.0f
fpp.addFilter(new SSAOFilter(0.5f, 3, 0.2f, 0.2f));
viewPort.addProcessor(fpp);
}
public void savePng(File f, Image img) throws IOException {
OutputStream out = new FileOutputStream(f);
try {
JmeSystem.writeImageFile(out, "png", img.getData(0), img.getWidth(), img.getHeight());
} finally {
out.close();
}
}
protected Image createFrameBufferImage(FrameBuffer fb) {
int width = fb.getWidth();
int height = fb.getHeight();
int size = width * height * 4;
ByteBuffer buffer = BufferUtils.createByteBuffer(size);
//Image.Format format = fb.getColorBuffer().getFormat();
// I guess readFrameBuffer always writes in the same
// format regardless of the frame buffer format
Image.Format format = Image.Format.BGRA8;
return new Image(format, width, height, buffer);
}
public void generateIcon() {
Renderer renderer = getRenderer();
Image image = createFrameBufferImage(fb);
//Texture2D texture = new Texture2D(image);
renderer.readFrameBuffer(fb, image.getData(0));
//image.setUpdateNeeded();
try {
savePng(new File("/home/ali/Desktop/icon.png"), image);
} catch (IOException ex) {
Logger.getLogger(IconGeneratorState.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
this is how my model looks at runtime :
and this png file i get after generating icon :
It looks darker and there is no transparency.
Something should be wrong in my code, I have no deep understanding of how this FrameBuffer stuff works.
Will appreciate any help.