I am working on a 2D project now, and I really need to be able to position my objects in 2D space, but with screen pixels as space units. Much akin to the GUI node. Only the problem is that the GUI node has troubles with the Z order and is in a specific gui bucket.
My current code is, as I looked in the sources and tried to invent something:
public void init2dViewport() {
cam2d = new Camera(settings.getWidth(), settings.getHeight());
cam.setFrustumPerspective(45f, (float) cam2d.getWidth() / cam2d.getHeight(), 1f, 1000f);
cam.setLocation(new Vector3f(0, 0, 0));
cam.lookAt(new Vector3f(0, 0, 0), Vector3f.UNIT_Y);
cam.setParallelProjection(true);
vp2d = getRenderManager().createMainView("2d", cam2d);
vp2d.setClearFlags(true, true, true);
node2d = new Node("2d root");
vp2d.attachScene(node2d);
}
and I call this method from simpleInitApp
. However, it causes this error:
SEVERE: Uncaught exception thrown in Thread[jME3 Main,5,main]
java.lang.IllegalStateException: Scene graph is not properly updated for rendering.
State was changed after rootNode.updateGeometricState() call.
Make sure you do not modify the scene from another thread!
Problem spatial name: 2d root
at com.jme3.scene.Spatial.checkCulling(Spatial.java:358)
at com.jme3.renderer.RenderManager.renderSubScene(RenderManager.java:719)
at com.jme3.renderer.RenderManager.renderScene(RenderManager.java:712)
at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:1086)
at com.jme3.renderer.RenderManager.render(RenderManager.java:1145)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:253)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:193)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:232)
at java.lang.Thread.run(Thread.java:745)
I understand what it means, but I don’t get why is this happening. And also, I am not sure that the code I crafted would give me the desired view, I feel like something is missing.
Could you please, tell me, how do I create such a Node that would work all like the rootNode, but with the coordinate system of and without the problems of the GUI node?