Hi!
I’m trying to implement a simple tool to generate small png files with one 3d model and full transparent background to be used as assets for GUI in my project. I’m using ScreenshotAppState to get a screenshot of current rendered scene with alpha background but I’m not able to do so.
I’ve tried setting viewport to have alpha=0: viewPort.setBackgroundColor(new ColorRGBA(0,0,0,0)); but didn’t make the trick.
Maybe it’s not possible with ScreenshotAppState and I will need to implement it myself rendering current scene to a back buffer… but just in case here is my code:
public class Main extends SimpleApplication
{
static String assetStr=null;
int frame=0;
Vector3f defaultCameraLocation=new Vector3f(2, 2, 2);
public static void main(String[] args)
{
Main app = new Main();
int width=0;
int height=0;
if(args.length<3)
{
System.err.println("Missing arguments");
Main.printUsage();
return;
}
else
{
try
{
width=Integer.parseInt(args[0]);
}
catch(Exception e)
{
System.err.println("Witdh must be an integer");
Main.printUsage();
return;
}
try
{
height=Integer.parseInt(args[1]);
}
catch(Exception e)
{
System.err.println("Height must be an integer");
Main.printUsage();
return;
}
}
assetStr=args[2];
AppSettings settings=new AppSettings(true);
settings.setResolution(width, height);
app.setSettings(settings);
app.showSettings=false;
app.setDisplayStatView(false);
app.setDisplayFps(false);
app.start();
}
public static void printUsage()
{
System.out.println("Usage:");
System.out.println("\tscreenshot_generator <width> <height> <3d model>");
}
@Override
public void simpleInitApp()
{
viewPort.setClearColor(true);
viewPort.setBackgroundColor(new ColorRGBA(0,0,0,0));
flyCam.setDragToRotate(true);
cam.setLocation(defaultCameraLocation);
cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
assetManager.registerLoader(BlenderLoader.class, "blend");
// find path for assets and set locator
int lastSlash=assetStr.lastIndexOf("/");
String assetsPath="./";
if(lastSlash>0)
{
assetsPath=assetStr.substring(0, lastSlash+1);
assetStr=assetStr.substring(lastSlash+1);
}
assetManager.registerLocator(assetsPath, FileLocator.class);
// load model
Spatial model=this.assetManager.loadModel(assetStr);
rootNode.attachChild(model);
// add a light
DirectionalLight dl0=new DirectionalLight(new Vector3f(-1,-1,-1));
rootNode.addLight(dl0);
// configure screenshot
String pngOutput=assetStr.substring(0,assetStr.lastIndexOf("."));
ScreenshotAppState screenshot=new ScreenshotAppState(assetsPath, pngOutput);
screenshot.setIsNumbered(false);
screenshot.takeScreenshot();
getStateManager().attach(screenshot);
}
@Override
public void simpleUpdate(float tpf)
{
if(frame>1)
{
System.exit(0);
}
else
{
++frame;
}
}
}
Thanks