3d - How to load *.babylon exported from blender using JavaScript/FileApi/XHR? -
i'm pretty fine working .babylon file format. exporter developed blender 3d editor works , if load exported model using next code:
// won't write full code // because fetched playground , it's standard , works babylon.sceneloader.load("", "filename.babylon", engine, function (newscene) { ...
works , webgl renderer in browser shows model.
but, if don't want load models static files must saved in public folder of http-server ( iis, apache, lighttpd, nginx, etc.. ).
for e.g. wanna load .babylon file user's side or secure access .babylon files @ backend.
all right, let's watch situation, if provide kind of uploader (using file api browser) in web-application, user able load 3d-models pc or other devices.
i'm trying load models way:
file uploading ( change
event of input-file ) works well:
function handlefiles( event ) { var uploader = event.srcelement || event.currenttarget; var files = uploader.files; var reader = new filereader(); reader.onload = function( event ) { var data = json.parse( event.target.result ); loadcustommesh( data ); }; // passing single mesh because of testing purpose reader.readastext( files[ 0 ] ); }
handling geometry , adding scene:
function loadcustommesh( data ) { var mesh = new babylon.mesh( math.random().tostring(), scene ); mesh.setverticesdata( babylon.vertexbuffer.positionkind, data.meshes[ 0 ].positions, true ); mesh.setverticesdata( babylon.vertexbuffer.normalkind, data.meshes[ 0 ].normals, true ); mesh.setindices( data.meshes[ 0 ].indices ); mesh.position = new babylon.vector3( 0, 0, 0 ); ...
it works fine! but!!! without materials...
i've found multimaterial here uploaded data:
but if use next code:
mesh.material = data.multimaterials[ 0 ];
which valid sample, throws next error:
uncaught typeerror: t.needalphablending not function
and don't know next, ideas?
the problem solved here:
function handlefiles( event ) { var uploader = event.srcelement || event.currenttarget; var files = uploader.files; var reader = new filereader(); reader.onload = function( event ) { var result = event.target.result; babylon.sceneloader.importmesh( null, event.target.result, '', scene, function( newmeshes, particlesystems, skeletons ) { var mesh = newmeshes[ 0 ]; mesh.position = new babylon.vector3( 0, 0, 0 ); } ); }; reader.readasdataurl( files[ 0 ] ); }
Comments
Post a Comment