Jump to content

Save/Load terrain


Dad72
 Share

Recommended Posts

Hello,

How to save a terrain change. I used WORLDMONGER scene of example and I would like to save the terrain in a file .babylon, .json or other. How it is happening?

This feature does not seem available. We can import a HeightMap but not export or save in a json file.

 

Thank you for your help.

Link to comment
Share on other sites

It can be something like that:

var vb = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
var nm = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);

 

// save VB and nm with a JSON.stringify

 

To reload you can create a mesh and then call these functions:

newMesh.setVerticesData(savedPosition, BABYLON.VertexBuffer.PositionKind, false);

newMesh.setVerticesData(savedNormal, BABYLON.VertexBuffer.NormalKind, false);

Link to comment
Share on other sites

This me returns nothing. the ground is not displayed. What have I done wrong?

 

I saved like this:

$("#img_save").on('click', function() {            var vb = mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);            var nm = mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);                            $.ajax({                url:'Data/Noyau/Fonctions/Save_terrain.fonction.php',                type:'post',                data:"vb=" + JSON.stringify(vb) + "&nm=" + JSON.stringify(nm) + "&terrainName=" + terrainName,                success:function(data){ }            });        });

 And load like this:

function openTerrain(TerrainName){     $.ajax({            url:'Data/Noyau/Fonctions/Open_terrain.fonction.php',            type:'post',            data: "terrainName=" + TerrainName,            success:function(data){                data = data.split(";");                var savedPosition = new Array(data[0]);                var savedNormal = new Array(data[1]);                LoadTerrain(savedPosition, savedNormal);                            }        });}
savedPosition  = [-50,0,50,-49.130434782608695,0,50,-48.26086956521739,0,50,-47.391304347826086,0,50, ....]savedNormal = [0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1, ...]

I reloaded the terrain as this:

function LoadTerrain(savedPosition, savedNormal){    // Code.....    // ....    ground = BABYLON.Mesh.CreateGround("ground", 100, 100, 115, scene, true);            ground.setVerticesData(savedPosition, BABYLON.VertexBuffer.PositionKind, false);    ground.setVerticesData(savedNormal, BABYLON.VertexBuffer.NormalKind, false);    var groundMaterial = new WORLDMONGER.GroundMaterial("ground", scene, sun);    ground.material = groundMaterial;    ground.position.y = -2.0;    // Code.....    // ....}
Link to comment
Share on other sites

I am not on understand what you want to say.
You want to say if it has the same dimension.

In made if i fact:(use CreateGround)

ground = BABYLON.Mesh.CreateGround("ground", 100, 100, 115, scene, true);    ground.setVerticesData(savedPosition, BABYLON.VertexBuffer.PositionKind, false);ground.setVerticesData(savedNormal, BABYLON.VertexBuffer.NormalKind, false);

The terrain not displayed.

 

If i done : (use CreateGroundFromHeightMap)

ground = BABYLON.Mesh.CreateGroundFromHeightMap("ground", "Terrain/heightMap/heightMap.png", 100, 100, 115, 0, 14, scene, true);ground.setVerticesData(savedPosition, BABYLON.VertexBuffer.PositionKind, false);ground.setVerticesData(savedNormal, BABYLON.VertexBuffer.NormalKind, false);

The ground remains with the values of the heightmap. The changes are not taken into account.

 

PS: No problem in the console.

Link to comment
Share on other sites

I found the problem
 
I had to convert Json in Array => $.parseJSON();
I have therefore done:

ground.setVerticesData($.parseJSON(savedPosition), BABYLON.VertexBuffer.PositionKind, true);ground.setVerticesData($.parseJSON(savedNormal), BABYLON.VertexBuffer.NormalKind, true);

It must be also correct this
Error (new Array)

var savedPosition = new Array(data[0]);var savedNormal = new Array(data[1]);

Fixe

var savedPosition = data[0];var savedNormal = data[1];

 
It works correctly

Link to comment
Share on other sites

I've add in the file ElevationControl.js of WorlMonger this code to supplement the engine. I share my contribution.

    WORLDMONGER.Ground = function (meshGround, savedPosition, savedNormal)    {        this.meshGround = meshGround;        this.savedPosition = savedPosition;        this.savedNormal = savedNormal;        this.VerticesPosition = 0;        this.VerticesNormal = 0;    };        WORLDMONGER.Ground.prototype.Save = function ()    {                this.VerticesPosition = JSON.stringify(this.meshGround.getVerticesData(BABYLON.VertexBuffer.PositionKind));        this.VerticesNormal = JSON.stringify(this.meshGround.getVerticesData(BABYLON.VertexBuffer.NormalKind));    };        WORLDMONGER.Ground.prototype.Load = function ()    {        this.meshGround.setVerticesData($.parseJSON(this.savedPosition), BABYLON.VertexBuffer.PositionKind, true);        this.meshGround.setVerticesData($.parseJSON(this.savedNormal), BABYLON.VertexBuffer.NormalKind, true);        };   

Use Save :

var buffer = new WORLDMONGER.Ground(ground);buffer.Save();    var vb = buffer.VerticesPosition;var nm= buffer.VerticesNormal;$.ajax({ url: "Save_terrain.function.php", type:'post', data:"vb=" + vb + "&nm=" + mn }); //Create the file, but you can do otherwise

Use Load :

$.ajax({url:'Open_terrain.function.php', type:'post', success:function(data){ // Load the file saved, but you can do otherwise    data = data.split(";"); var savedPosition = data[0]; var savedNormal = data[1]; LoadTerrain(savedPosition, savedNormal);}});function LoadTerrain(savedPosition, savedNormal){    //Other code (Create engin, scene, cam, light ...)    ground = BABYLON.Mesh.CreateGround("ground", 100, 100, 115, scene, true);    var buffer = new WORLDMONGER.Ground(ground, savedPosition, savedNormal);    buffer.Load();    //Other code (Create material, water...)}
Link to comment
Share on other sites

  • 3 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...