@Override
    public void simpleInitApp() {
        addPointLight();
        addAmbientLight();

        Material material = createInstancedLightingMaterial();

        instancedNode = new InstancedNode("theParentInstancedNode");
        rootNode.attachChild(instancedNode);
        Sphere sphereMesh = new Sphere(16, 16, 1f);
        Box boxMesh = new Box(0.7f, 0.7f, 0.7f);
        // create 10 spheres & boxes, positioned along Z-axis successively further from the camera
        for (int i = 0; i < 10; i++) {
            Vector3f location = new Vector3f(0, -3, -(i*5));
            locations[i] = location;

            Geometry sphere = new Geometry("sphere", sphereMesh);
            sphere.setMaterial(material);
            sphere.setLocalTranslation(location);
            instancedNode.attachChild(sphere);       // initially just add the spheres to the InstancedNode
            spheres[i] = sphere;

            Geometry box = new Geometry("box", boxMesh);
            box.setMaterial(material);
            box.setLocalTranslation(location);
            boxes[i] = box;
        }
        instancedNode.instance();

        flyCam.setMoveSpeed(30);


        addCrossHairs();

        // when you left-click, print the distance to the object to system.out
        inputManager.addMapping("leftClick", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        inputManager.addListener(new ActionListener() {
            @Override
            public void onAction(String name, boolean isPressed, float tpf) {
                if( isPressed ) {
                    CollisionResult result = pickFromCamera();
                    if( result != null ) {
                        System.out.println("Picked = " + result.getGeometry() + ", Distance = "+result.getDistance());
                    }
                }
            }
        }, "leftClick");
    }
A test case for using instancing with ray casting.

Based on distance from camera, swap in/out more/less detailed geometry to/from an InstancedNode.

@author duncanj
jme3-examples/src/main/java/jme3test/scene/instancing/TestInstancedNodeAttachDetachWithPicking.java