ozRocker

How to use OBJ loader in playground

8 posts in this topic

I'm trying to load an OBJ in the playground.  There are 2 meshes in the OBJ with exact same number of vertices.  This works in normal code when I use BABYLON.SceneLoader.Load() but when I use this special playground technique I get funny side-effects.  One of the meshes is twice the number of vertices as the other one: http://www.babylonjs-playground.com/#28YUR5#202  You can see it in the console output.

Is there another way to load an OBJ in the playground?

Share this post


Link to post
Share on other sites

I've made a playground here http://www.babylonjs-playground.com/#28YUR5#209

What you can see from the console output is that the textures get loaded after a slight delay.  If I try to access materials straight away its undefined.  I have to wait until the textures are fully loaded to access materials.  Is there a callback function I can use to access the material after the textures have been loaded?

Share this post


Link to post
Share on other sites

ok I understand the issue: OBJ file are based on a couple of extension: obj for the meshes and mtl for the materials

In this case the obj is loaded first and the callback is called as expected. Then the mtl is loaded and affected to the mesh.

To be sure to get the mesh with material you will have to do what you do in the PG or to register for scene.registerBeforeRender

Share this post


Link to post
Share on other sites
7 hours ago, Deltakosh said:

ok I understand the issue: OBJ file are based on a couple of extension: obj for the meshes and mtl for the materials

In this case the obj is loaded first and the callback is called as expected. Then the mtl is loaded and affected to the mesh.

To be sure to get the mesh with material you will have to do what you do in the PG or to register for scene.registerBeforeRender

Thank you for that.  Now I know how it works I found where I can insert a little hack.

in babylon.objFileLoader.ts I changed calls:

return new BABYLON.Texture(url, scene);

to

return new BABYLON.Texture(url, scene, true, true, 1, textureLoad);

then in my OBJ loading code I have this:

texturesLoaded = 0;
var newMesh;

//Callback function
textureLoad = function() {
	texturesLoaded++;
	if (texturesLoaded == 2) {
		var material = newMesh.material;
		material.backFaceCulling = false;
		material.emissiveColor = new BABYLON.Color3(0.7, 0.7, 0.7);
	}
}

//.OBJ loading
BABYLON.SceneLoader.ImportMesh("", "uploads/models/" + directory + "/", objFile, Assets.scene, (meshes) => {
	Tools.debug(1, "Mesh imported");
	newMesh = meshes[0];
}, () => {
	Tools.debug(1, "Progress");
}, (scene, message, exception) => {
	Tools.debug(1, "Error: " + message);
});

texturesLoaded and textureLoad are global variables.  Of course this is assuming there are only 2 textures

 

I like to use the callback function instead of polling because polling will make asynchronous back to synchronous

Deltakosh likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.