joshcamas Posted March 7, 2015 Share Posted March 7, 2015 Ello people of the world! On my editor I'm working on a mesh database system thingy. So how it works is you drag your babylon files into a folder, and then the editor will automatically categorize everything into separate folders. Problem is, after grabbing the babylon file, how would I read it and know what image files go with what babylon file, and so on?? Thanks! Quote Link to comment Share on other sites More sharing options...
joshcamas Posted March 7, 2015 Author Share Posted March 7, 2015 OK, so it turns out it wasn't that hard at all!!! Here's the code: (Just thrown together) function scanBabylonImages(dir) { var attachedImages = []; $.ajax({ url: dir, async: true, success: function(data) { onComplete(data) }, error: function(data,error) { console.error(error); } }); function onComplete(data) { //Go through the babylon file's materials. Find any texture images var data = JSON.parse(data) data.materials.forEach(function(mat,m) { var toScan = ["diffuseTexture","ambientTexture","opacityTexture","reflectionTexture","emissiveTexture","specularTexture","bumpTexture"] toScan.forEach(function(texture,t) { if(mat[texture] != null) { attachedImages.push(mat[texture].name) } }); }) //Clean any duplicates finalAttachedImages = []; for(var i = 0, n = attachedImages.length; i < n; i++) { for(var x = 0, y = finalAttachedImages.length; x < y; x++) { if(finalAttachedImages[x]==attachedImages[i]) { continue; } } finalAttachedImages[finalAttachedImages.length] = attachedImages[i]; } console.log(finalAttachedImages); }} Basically it first loads the file using jquery, then it finds any texture files, and then gets rid of any duplicates. Then it logs what it finds.If anyone wants to use it, feel free to do whatever. :) Quote Link to comment Share on other sites More sharing options...
Dad72 Posted March 7, 2015 Share Posted March 7, 2015 When reading a file babylon he sufit to go see the given material: mesh.material.diffuseTexture.urlormesh.material.subMaterials[1].diffuseTexture.url this gives you the path of the image and the image that the model use. Quote Link to comment Share on other sites More sharing options...
Dad72 Posted March 7, 2015 Share Posted March 7, 2015 Your method does not support the subMaterials Quote Link to comment Share on other sites More sharing options...
joshcamas Posted March 7, 2015 Author Share Posted March 7, 2015 Ahhhh ok. Is this how that would be done? function scanBabylonImages(dir) { var attachedImages = []; $.ajax({ url: dir, async: true, success: function(data) { onComplete(data) }, error: function(data,error) { console.error(error); } }); function onComplete(data) { //Go through the babylon file's materials. Find any texture images var data = JSON.parse(data) data.materials.forEach(function(mat,m) { var toScan = ["diffuseTexture","ambientTexture","opacityTexture","reflectionTexture","emissiveTexture","specularTexture","bumpTexture"] toScan.forEach(function(texture,t) { if(mat[texture] != null) { attachedImages.push(mat[texture].name) } }); if(mat.subMaterials != null && mat.subMaterials.length != 0) { mat.subMaterials.forEach(function(texture,t) { if(mat.subMaterials[texture] != null) { attachedImages.push(mat[texture].name) } }); } }) //Clean any duplicates finalAttachedImages = []; for(var i = 0, n = attachedImages.length; i < n; i++) { for(var x = 0, y = finalAttachedImages.length; x < y; x++) { if(finalAttachedImages[x]==attachedImages[i]) { continue; } } finalAttachedImages[finalAttachedImages.length] = attachedImages[i]; } console.log(finalAttachedImages) }} Quote Link to comment Share on other sites More sharing options...
Dad72 Posted March 8, 2015 Share Posted March 8, 2015 I think yes. Quote Link to comment Share on other sites More sharing options...
joshcamas Posted March 8, 2015 Author Share Posted March 8, 2015 cool, thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.