You can split the screen and look into the 3D scene from different camera angles at the same time. In this example, we create four views (2x2) with the same aspect ratio as the normal view, but half the size.
The packages used in this example are com.jme3.renderer.Camera
and com.jme3.renderer.ViewPort
. You can get the full sample code here: TestMultiViews.java
We use the preconfigured Camera cam
and viewPort
from SimpleApplication
for the first view.
viewPort.setBackgroundColor(ColorRGBA.Blue); cam.setViewPort(.5f, 1f, 0f, 0.5f); // resize the viewPort cam.setLocation(new Vector3f(3.3212643f, 4.484704f, 4.2812433f)); cam.setRotation(new Quaternion (-0.07680723f, 0.92299235f, -0.2564353f, -0.27645364f));
Place the main camera in the scene and rotate it in its start position.
We will have a detailed look at how we use setViewPort() to position and resize the default view later.
Here is the outline for how you create the three other cams and viewPorts (Full code sample is here.) In the code snippet, cam_n
stand for cam_2
- cam_4
, respectively, same for view_n
.
Camera cam_n = cam.clone(); cam_n.setViewPort(...); // resize the viewPort cam_n.setLocation(new Vector3f(...)); cam_n.setRotation(new Quaternion(...)); ViewPort view_n = renderManager.createMainView("View of camera #n", cam_n); view_n.setClearEnabled(true); view_n.attachScene(rootNode); view_n.setBackgroundColor(ColorRGBA.Black);
How does jme know which of the four views should appear where on the screen?
Imagine the view as a 1x1-sized box. By default, the settings is cam.setViewPort(0f, 1f, 0f, 1f);
. This means the view takes up the whole box, from 0 to 1 left to right, and from 0 to 1 bottom to top.
In the code sample, note the following four lines:
cam.setViewPort( 0.5f, 1.0f, 0.0f, 0.5f); ... cam_2.setViewPort(0.0f, 0.5f, 0.0f, 0.5f); ... cam_3.setViewPort(0.0f, 0.5f, 0.5f, 1.0f); ... cam_4.setViewPort(0.5f, 1.0f, 0.5f, 1.0f);
These viewport parameters are, in this order, the left - right - bottom - top extend of a camera's box on the screen. Note that we have set a few values to 0.5f – this is where we resize each view to half its default height and width.
0.0 , 1.0 1.0 , 1.0 +----+----+ | | | |cam3|cam4| +---------+ | | | |cam2|cam | +----+----+ 0.0 , 0.0 1.0 , 0.0
Example: Cam3's rect extends from bottom-left (0.0 , 0.5) to top-right (0.5 , 1.0)
This layout shows 2x2 views. For a split screen you may want to lay out two views, one above the other, or one next to the other.
If you scale the views in a way so that the aspect ratio changes, the views will obviously be distorted. In these cases, create custom camera objects with the right aspect ratio (redefine the default cam).