Jump to content

Using information in file .babylon to create new mesh


Kei 27
 Share

Recommended Posts

Using information in file .babylon to create new a mesh.
 
I want to make a content to load file from local, but due to security, browser can't load files .babylon or .obj.
My question is that how to use information in file .babylon to create new mesh. 
 
Eg
var infomesh = { information in file .babylon };
var newmesh = BABYLON.Mesh.CreateMesh(infomesh, scene);
 
 
I find an example like that in Three.js. 
 
var lh_model = {
    "metadata" : { "formatVersion" : 3.1, ..., "morphTargets"  : 0 },
    "vertices": [
          0.001000,    0.001000,  -0.001000,
        102.616974,  -61.974983,  19.303007,
        102.108978,  -62.482986,  -7.620989,
        123.952972,  -49.274994,  -3.048992,
        123.952972,  -48.766994,  14.223002
    ],
    "uvs": [0.9458,0.5134,0.9419,0.5177,0.9513,0.5177,........],
    "faces": [ 0, 4, 1, 2 ],
    "morphTargets": []
}
var loader = new THREE.JSONLoader();
var model = loader.parse( lh_model );
 
var mesh = new THREE.Mesh( model.geometry, new THREE.MeshBasicMaterial() );
scene.add( mesh );
Link to comment
Share on other sites

There seems to be two issues here.

  1. Browser security;
  2. Knowledge of how to load a file from a mesh.

 

  1. Which browser? What error messages are you getting? I know I have successfully loaded .babylon files locally.
  2. The playground http://www.babylonjs-playground.com/# shows an example of importing a mesh from a .babylon file when you choose number 16 import meshes from the basic scene drop down menu top right. The api for this is here
Link to comment
Share on other sites

22 hours ago, JohnK said:

There seems to be two issues here.

  1. Browser security;
  2. Knowledge of how to load a file from a mesh.

 

  1. Which browser? What error messages are you getting? I know I have successfully loaded .babylon files locally.
  2. The playground http://www.babylonjs-playground.com/# shows an example of importing a mesh from a .babylon file when you choose number 16 import meshes from the basic scene drop down menu top right. The api for this is here

Hi JohnK

I want to run content in local. I mean I open file html with Chrome but I can’t load .babylon or .obj file.
 
babylon.js:2 XMLHttpRequest cannot load file:///C:/.....
 
I know that issue only happens when opening file without server.
I don’t have any issue to load file .babylon if I create a server and run on the browser.
 
With BabylonJS, can I use information in .babylon file to make a new mesh that I don't need import.babylon file?
Link to comment
Share on other sites

xhr (xmlHttpRequests) can't request another Web Origin , it is to say the triplet [protocol, domain, port] (ex : proto = http, domain = foo.com, port = 80) than the one of the main page that embbeds the xhr code.

A browser considers each local file (file://some/path/to/a/file) as a different Web Origin for security reasons : you can't modify with some javascript code downloaded from the Web any local file.

That's why you should ever use a web server to deal with javascript, xhr, different files or assets.

Link to comment
Share on other sites

Try this

To load local resources in Chrome when just using your local computer and not using a webserver you need to add the --allow-file-access-from-files flag.

You can have a shortcut to Chrome that allows files access and one that does not.

Create a shortcut for Chrome on the desktop, right click on shortcut, select properties. In the dialog box that opens find the target for the short cut and add the parameter after chrome.exe leaving a space

eg (Windows) C:\PATH TO\chrome.exe --allow-file-access-from-files

(Linux) /usr/bin/chromium-browser --allow-file-access-from-files

This shortcut will allow access to files without affecting any other shortcut to Chrome you have.

When you open Chrome with this shortcut it should allow local resources to be loaded using HTML5 and the filesystem api

A search for allow file access from files chrome in your favourite search engine will give you more information.

Link to comment
Share on other sites

 In ThreeJS, you can create a new mesh without importing .js ( json) file.
Eg:
var lh_model = { mesh information };
var loader = new THREE.JSONLoader();
var model = loader.parse( lh_model );
var mesh = new THREE.Mesh( model.geometry, new THREE.MeshBasicMaterial() );
scene.add( mesh );
 
My question is :  
Can I use information in .babylon file to make a new mesh that I don't need import.babylon file as the same way of ThreeJS.
Link to comment
Share on other sites

You should be able to do this:

	var data = '{"producer":{"name":"Blender","version":"2.76 (sub 0)","exporter_version":"4.4.2","file":"untitled.babylon"},
"autoClear":true,"clearColor":[0.0509,0.0509,0.0509],"ambientColor":[0,0,0],"gravity":[0,-9.81,0],
"materials":[{"name":"untitled.Material","id":"untitled.Material","ambient":[0.8,0.8,0.8],"diffuse":[0.64,0.64,0.64],"specular":[0.5,0.5,0.5],"emissive":[0,0,0],"specularPower":50,"alpha":1,"backFaceCulling":true,"checkReadyOnlyOnce":false}],
"multiMaterials":[],
"skeletons":[],
"meshes":[{"name":"Cube","id":"Cube","materialId":"untitled.Material","billboardMode":0,"position":[0,0,0],"rotation":[0,0,0],"scaling":[1,1,1],"isVisible":true,"freezeWorldMatrix":false,"isEnabled":true,"checkCollisions":false,"receiveShadows":false
,"positions":[1,-1,-1,-1,-1,-1,-1,-1,1,-1,1,1,-1,1,-1,1,1,-1,1,1,1,1,-1,1]
,"normals":[0.5773,-0.5773,-0.5773,-0.5773,-0.5773,-0.5773,-0.5773,-0.5773,0.5773,-0.5773,0.5773,0.5773,-0.5773,0.5773,-0.5773,0.5773,0.5773,-0.5773,0.5773,0.5773,0.5773,0.5773,-0.5773,0.5773]
,"indices":[0,1,2,3,4,5,6,5,0,5,4,1,1,4,3,7,2,3,7,0,2,6,3,5,7,6,0,0,5,1,2,1,3,6,7,3]
,"subMeshes":[{"materialIndex":0,"verticesStart":0,"verticesCount":8,"indexStart":0,"indexCount":36}]
,"instances":[]}
],
"cameras":[{"name":"Camera","id":"Camera","position":[7.4811,5.3437,-6.5076],"rotation":[0.4615,-0.8149,0.0108],"fov":0.8576,"minZ":0.1,"maxZ":100,"speed":1,"inertia":0.9,"checkCollisions":false,"applyGravity":false,"ellipsoid":[0.2,0.9,0.2],"cameraRigMode":0,"interaxial_distance":0.0637,"type":"FreeCamera"}],"activeCamera":"Camera",
"lights":[{"name":"Lamp","id":"Lamp","type":0,"position":[4.0762,5.9039,1.0055],"intensity":1,"diffuse":[1,1,1],"specular":[1,1,1]}],
"shadowGenerators":[]
}';

	var loader = BABYLON.SceneLoader.GetPluginForExtension('babylon');
	
	loader.load(scene, data);

I tried making a PG that would work, but it doesn't like the data string (says there is an illegal token).  I'm pretty sure this will work outside the playground though.

 

Link to comment
Share on other sites

  • 6 months 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...