[SOLVED] Saving Light Probe information

since the process of creating a light probe with the light probe factory takes a while on low end systems, is there a way to save the data to a file, so it can then be reloaded from a file without having to make a new one?

    @Override
    public void simpleUpdate(float tpf) {
        frame++;

        if (frame == 2) {
            modelNode.removeFromParent();
            final LightProbe probe = LightProbeFactory.makeProbe(stateManager.getState(EnvironmentCamera.class), rootNode, new JobProgressAdapter<LightProbe>() {

                @Override
                public void done(LightProbe result) {
                    System.err.println("Done rendering env maps");
                    tex = EnvMapUtils.getCubeMapCrossDebugViewWithMipMaps(result.getPrefilteredEnvMap(), assetManager);
                }
            });
            ((BoundingSphere) probe.getBounds()).setRadius(100);
            rootNode.addLight(probe);
            //getStateManager().getState(EnvironmentManager.class).addEnvProbe(probe);

        }
        if (frame > 10 && modelNode.getParent() == null) {
            rootNode.attachChild(modelNode);
        }
    }

Yes you would just want to save your light probe as a .j3o file after generating it, and then you can load it with the asset manager at run time the same way you’d load other models.

Here’s a quick and simple method I wrote for saving my light probes

private void saveProbeNode(String filePathString, Node probeNode){
        String userHome = System.getProperty("user.home");
        BinaryExporter exporter = BinaryExporter.getInstance();
        File file = new File(filePathString);
        try {
            exporter.save(probeNode, file);
        } catch (IOException ex) {
            System.err.println("Error: Failed to save LightProbe: " + filePathString  + "      <--------");
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Error: Failed to save Light Probe: " + filePathString  + "      <--------", ex);
        }
    }

And here’s the tutorial link that describes more abou j3o saving if you’re interested https://wiki.jmonkeyengine.org/jme3/advanced/save_and_load.html

2 Likes

Is there any chance I could see how your method is used within the rest of the class, because im not sure exactly what Node i should be saving.

I can’t save the rootNode, since it has other spatials and nodes attached, I can’t make a seperate node just for the light as none of my other spatials are children to the seperate node, and i cannot cast the LightProbe to a node?

Create an empty node, attach light probe to it using addLight(); then save node as j3o.
Next time you can load it and get light probe from that and attach it back to your rootNode.

Node probeNode = (Node) assetManager.loadModel("lightprobe/test-scene.j3o");
        LightProbe probe = (LightProbe) probeNode.getLocalLightList().iterator().next();
        rootNode.addLight(probe);

Many thanks, I’ve been wondering how to do that for ages.

1 Like

Sorry for the late reply, but I managed to acheive my goals, I editied it to use @Ali_RS code, and i’ll post it here if anyone want a look.

 @Override
    public void simpleUpdate(float tpf) {
              frame++;

              
        if (frame == 2) {
            if (!fileExists()) {
            
            modelNode.removeFromParent();
            probe = LightProbeFactory.makeProbe(stateManager.getState(EnvironmentCamera.class), rootNode, new JobProgressAdapter<LightProbe>() {

                @Override
                public void done(LightProbe result) {
                    System.err.println("Done rendering env maps");
                    tex = EnvMapUtils.getCubeMapCrossDebugViewWithMipMaps(result.getPrefilteredEnvMap(), assetManager);
                    
                    makeSaveNode();
                }
            });
            ((BoundingSphere) probe.getBounds()).setRadius(100);
            rootNode.addLight(probe);
            //getStateManager().getState(EnvironmentManager.class).addEnvProbe(probe);
                
            
            } else {
                loadProbeNode();
            }
            
        }
        if (frame > 10 && modelNode.getParent() == null) {
            rootNode.attachChild(modelNode);
        }
    }
 
    private boolean fileExists() {
        String userHome = System.getProperty("user.home");
        File f = new File(userHome+Settings.SHADER_PATH);
        
        return f.exists() && !f.isDirectory();
    }
    
    private void makeSaveNode() {
        Node p = new Node("p");
        p.addLight(probe);
        saveProbeNode(Settings.SHADER_PATH,p);
    }
    
    private void saveProbeNode(String filePathString, Node p) {
        String userHome = System.getProperty("user.home");
        BinaryExporter exporter = BinaryExporter.getInstance();
        File file = new File(userHome+filePathString);
        try {
            exporter.save(p,file);
        } catch (IOException e) {
            System.err.println("Error: Failed to save lightProbe: " + userHome+filePathString);
            System.err.println(e);
        }
    }
    
    private void loadProbeNode() {
        String userHome = System.getProperty("user.home");
        assetManager.registerLocator(userHome,FileLocator.class);
        Node probeNode = (Node) assetManager.loadModel(Settings.SHADER_PATH);
        LightProbe probee = (LightProbe) probeNode.getLocalLightList().iterator().next();
        rootNode.addLight(probee);
    }
4 Likes