Jump to content

Importing a mesh into an existing scene


JohnK
 Share

Recommended Posts

Have read done the searching and found and followed the examples but am missing something that is probably pretty obvious.

 

I have created a scene and used

var serializedScene = BABYLON.SceneSerializer.Serialize(scene);var strScene = JSON.stringify(serializedScene);

to create a JSON file testbox.babylon

 

From what I have gathered the code below should extract the mesh named "box0" and import it into the scene I created. The created scene works fine, the ground appears and rotates as expected with the pointer.

 

The import appears to be successful since

console.log(mesh[0])

gives

 

Object { state: "", animations: Array[0], _childrenFlag: -1, _isEnabled: true, _isReady: true, _currentRenderId: 36, _parentRenderId: -1, name: "box0", id: "box0", _scene: Object, 74 more… }

 

However the box does not appear in the scene.

//Set Scenevar canvas = document.getElementById("canvas");var engine = new BABYLON.Engine(canvas, true);var scene = createScene(engine,canvas);                //Create Materialsvar blueInMat = new BABYLON.StandardMaterial("blueIn", scene);blueInMat.emissiveColor = new BABYLON.Color3(0.2,0.2,1);                var redInMat = new BABYLON.StandardMaterial("redIn", scene);redInMat.emissiveColor = new BABYLON.Color3(1,0,0);                //Create Ground var groundin=BABYLON.Mesh.CreateGround("groundIn",1200, 1200, 20, scene,  false, BABYLON.Mesh.DOUBLESIDE);     groundin.material = blueInMat;                                        // Register a render loop to repeatedly render the sceneengine.runRenderLoop(function () {      scene.render(); });    // Watch for browser/canvas resize eventswindow.addEventListener("resize", function () {      engine.resize(); });    BABYLON.SceneLoader.ImportMesh("box0", "./", "testbox.babylon", scene, function (meshes, particleSystems, skeletons) {       console.log(meshes[0]);       //meshes[0].material = redInMat;       console.log(meshes[0].material); });

When the box did not appear I thought maybe it was because the material was not defined so added the redInMat material and set mesh[0] to this - to no avail.

 

I have tried, Firefox, Chrome and IE all give the same result.

 

What am I missing.

 

 

 

Link to comment
Share on other sites

Hi, recently solve this task, but i'm newbee and the decision not to like to me.

 

1. Create new temporary scene

2. Load extend file in new scene

3. Create clones and pushing this in old scene

4. Rebuild parend-children links

5. Dispose temporary scene

 

But i need import all kind objects (materials, lights, animations not only meshes).

Perhaps you can find best decision?

Link to comment
Share on other sites

@Blax

 

I do not have a problem loading the full scene including materials, cameras and lights (never tried with animations). Just need to put the render loop in the onsuccess  parameter and active the camera here as well.

 

Because of scoping rules the camera is created outside the createScene function.  Code below.

//Set Scenevar canvas = document.getElementById("canvas");var engine = new BABYLON.Engine(canvas, true);var scene = createScene(engine,canvas);                //Set cameravar camera = new BABYLON.ArcRotateCamera("Camera", 0, 50, 300, new BABYLON.Vector3(0, 300, 0), scene); camera.setPosition(new BABYLON.Vector3(0, 200, -1400));    camera.lowerBetaLimit = 0.1;camera.upperBetaLimit = (Math.PI / 2) * 0.9; camera.attachControl(canvas, true);                // Watch for browser/canvas resize eventswindow.addEventListener("resize", function () {    engine.resize();});    BABYLON.SceneLoader.Load("./", "testbox.babylon", engine, function(scene) {    scene.activeCameras.push(camera);                        // Register a render loop to repeatedly render the scene    engine.runRenderLoop(function () {        scene.render();     });});    function createScene(engine, canvas) {    var canvas = document.getElementById("canvas");    var engine = new BABYLON.Engine(canvas, true);    var scene = new BABYLON.Scene(engine);        scene.clearColor = new BABYLON.Color3(0.75, 0.75, 0.75);            var frontLight = new BABYLON.PointLight("omni", new BABYLON.Vector3(-3000, 6000, 2000), scene);    var backLight = new BABYLON.PointLight("omni", new BABYLON.Vector3(3000, -6000, 2000), scene);    var bottomLight = new BABYLON.PointLight("omni", new BABYLON.Vector3(-3000, 6000, -2000), scene);    frontLight.intensity = 0.2;    backLight.intensity = 0.2;    bottomLight.intensity = 0.2;        return scene;    }
Link to comment
Share on other sites

have you checked this : http://www.html5gamedevs.com/topic/6737-cant-get-babylon-files-to-load/

 

maybe renaming your file to testbox.json instead will solve the problem too ...

 

Thank you jerome have not seen this. Will have a good read.

 

Having read this post I do not think this is my problem. The testbox.babylon file is loading OK

 

Web Console reports

 

GET XHR http://127.0.0.1:8020/Cubees/testbox.babylon.manifest [HTTP/1.1 200 OK 17ms]

GET XHR http://127.0.0.1:8020/Cubees/testbox.babylon [HTTP/1.1 200 OK 163ms]

 

and the console.log shows that the data in the file seems to have been read.

 

Also the code using BABYLON.SceneLoader.Load works OK.

Link to comment
Share on other sites

Hey, John,

 

as the console.log actually shows the mesh correctly, it is being loaded correctly. for some reason it is not being shown.  Technically, I don't see anything wrong you are doing. 

Do you want to upload the .babylon file somewhere and let us see it? that will help us understand what went wrong.

Link to comment
Share on other sites

Perhaps we talk about this:

	BABYLON.SceneLoader.Load("/", "myScene.babylon", engine, function(newScene){		newScene.executeWhenReady(function (){			extendScene();  // here you settings for camera's, etc (create scene)			engine.runRenderLoop(scRender);		});					});
Link to comment
Share on other sites

Just looked in the file, haven't tested it yet.

Looking at your code and at the file - this might be due to camera configuration... The object's position is 30,30,30. Your camera target is 30,0,0. What can also cause this is z clipping of the camera - the position in the z axis is -1400, which probably clips the imported box. Try getting the camera closer, and set the target correctly, see if that helps.

Link to comment
Share on other sites

Looks like the geometry is not being loaded.

The mesh is there its geometry isn't. I looked in the ImportMesh code, it looks like it doesn't parse the geometry when loading the mesh using ImportMesh. It only parses geometries when using load. The ImportMesh is missing the call to

parseVertexData(...)

when importing a mesh with geometry id (like in your case).

 

This require a small commit, I will try finding the time tomorrow to fix that.

Link to comment
Share on other sites

Just a reminder, there is a BABYLON.SceneLoader.Append() method, if it helps.  It is the same as Load(), except the engine arg is replaced with the scene to append to.

 

For my project I did not want all the meshes in the saved scene loaded. Thanks anyway.

Link to comment
Share on other sites

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...