[SOLVED] Simple terrain editor: How to reload heightmap terrain

I am following the heightmap tutorial on this site and wanted to extend it to get a bit more practice.
I would like to reload the heightmap every e.g. 10 seconds, so that I can have paint.net open for editing and can ‘simultaniously’ see the new heightmap in my sample application.
I tried to do this by overriding the ’ update’ method of the application and call reloadTerrain() from there, but it does not seem to work.
(I know that the approach below is quite inefficient, but I just started out on it)

    private void reloadTerrain() {
        assetManager.clearCache(); // clearing cache as mointain image might have changed

        AbstractHeightMap heightmap = null;
        Texture heightMapImage = assetManager.loadTexture(
                "Textures/Terrain/splat/mountains512.png");
        heightmap = new ImageBasedHeightMap(heightMapImage.getImage());
        heightmap.load();
        int patchSize = 65;

        rootNode.detachAllChildren(); // detach the old terrain and further below we add the new terrain

        terrain = new TerrainQuad("my terrain", patchSize, 513, heightmap.getHeightMap());

        /** 4. We give the terrain its material, position & scale it, and attach it. */
        terrain.setMaterial(mat_terrain);
        terrain.setLocalTranslation(0, -100, 0);
        terrain.setLocalScale(2f, 1f, 2f);
            rootNode.attachChild(terrain);
     }

You are likely to be having an issue that Textures/Terrain/splat/mountains512.png isn’t where you think it is. During the build (depending on what your build process is, are you using gradle?) it will be copied somewhere else (e.g. /build/resources/main/Textures). Its likely that is the one you should be editing if you want to hack it

3 Likes

I suspect the asset cache is caching the old version. Try deleting it from the cache or clearing the cache.

You can use assetManager.deleteFromCache(key).

Thank you for the quick replies @richtea was right, I forgot that maven is copying artifacts to the target folder and the IDE picks them up from there.

Now it’s better, BUT I still need to switch back to the jmonkey window to force the reload. Any idea why it is not reloading when the window is in the background?

1 Like

I think it is because pause on lost focus is enabled by default. You can disable it with app.setPauseOnLostFocus(false)

2 Likes

Thank you @Ali_RS that fixed it. Awesome! What a speedy and brilliant support :grinning:

3 Likes