juanmajr93 Posted June 5, 2017 Share Posted June 5, 2017 Hi team, @jerome @Wingnut I have an question relative to solid particle system (SPS). I need to create a SPS composed with different buildings (.obj files). This is my code but i only can add one of them. How can I solve it? var models = function(edificios_texto) { var t = 0; var loader = new BABYLON.AssetsManager(scene); edificios_texto.forEach(function() { if(edificios_texto[t].length!=2){ return; } var edificio = loader.addMeshTask(t, "","<?=$url?>assets/modelos/",edificios_texto[t]+".obj"); var nM; //mesh of building edificio.onSuccess = function (task) { task.loadedMeshes.forEach(function(b) { b.scaling = new BABYLON.Vector3(2.65, 2.65, 2.65); b.rotation.y = Math.PI; b.computeWorldMatrix(true); var vertex_data = BABYLON.VertexData.ExtractFromMesh(b); for (var i = 0; i < vertex_data.normals.length; i+=3) { vertex_data.positions[i] *= -1; } vertex_data.applyToMesh(b); }); nM = BABYLON.Mesh.MergeMeshes(task.loadedMeshes); for (var i = 0; i < edificios.length; i++) { if(edificios[i].descripcion == edificios_texto[edificio.name]){ var myPositionFunction = function(particle, s) { var utmPlaceX = edificios[i].x; var utmPlaceZ = edificios[i].z; var utmPlaceXFromCentre = utmPlaceX - mapCentreX; var utmPlaceZFromCentre = utmPlaceZ - mapCentreZ; var x = utmPlaceXFromCentre/scaleX; var z = utmPlaceZFromCentre/scaleZ; particle.position.x = x; particle.position.z = z; particle.position.y = alturas[ edificios_texto[edificio.name]]; particle.color = new BABYLON.Color4(0, 0, 1,0.5); }; spsEdificios.addShape(nM, 1, {positionFunction: myPositionFunction}); nM.dispose(); break; } } var buildings = spsEdificios.buildMesh(); spsEdificios.mesh.hasVertexAlpha = true; } t++; }); loader.load(); }; Thanks! Quote Link to comment Share on other sites More sharing options...
jerome Posted June 6, 2017 Share Posted June 6, 2017 Well, as it looks quite impossible to guess why from the part of code you published, I would suggest the usual way to isolate the issue : - to try to call addShape() without the positionFunction to check if the error is here - to log the values (edificios.length, etc) to be sure your loop iterates many times, to check every value - to be sure you need the call to MergeMeshes() - etc ... Quote Link to comment Share on other sites More sharing options...
juanmajr93 Posted June 6, 2017 Author Share Posted June 6, 2017 @jerome I understand you but all of my code works fine. However, to each onSuccess function I am building a mesh (var buildings) and this is the error. Could you show me an example of SPS with different obj models? Thanks! Quote Link to comment Share on other sites More sharing options...
jerome Posted June 6, 2017 Share Posted June 6, 2017 Did you check how many particles your SPS finally got ? Maybe they're just all on the same position and you can see only one. Quote Link to comment Share on other sites More sharing options...
juanmajr93 Posted June 6, 2017 Author Share Posted June 6, 2017 @jerome I only have one particle that is the first building model. The others buildings are meshes of the scene. Theese lines of code are repetied to each loaded, I think that this is the error... but I don't know the solution. var buildings = spsEdificios.buildMesh(); spsEdificios.mesh.hasVertexAlpha = true; If I change the code by this way I have the follow error: babylon.js:14 Uncaught TypeError: Cannot read property '6' of null var models = function(edificios_texto) { var t = 0; var loader = new BABYLON.AssetsManager(scene); edificios_texto.forEach(function() { if(edificios_texto[t].length!=2){ return; } var edificio = loader.addMeshTask(t, "","<?=$url?>assets/modelos/modelos/",edificios_texto[t]+".obj"); var nM;// malla de edificio edificio.onSuccess = function (task) { task.loadedMeshes.forEach(function(b) { b.scaling = new BABYLON.Vector3(2.65, 2.65, 2.65); b.rotation.y = Math.PI; b.computeWorldMatrix(true); // cambiar las X var vertex_data = BABYLON.VertexData.ExtractFromMesh(b); for (var i = 0; i < vertex_data.normals.length; i+=3) { vertex_data.positions[i] *= -1; } vertex_data.applyToMesh(b); }); nM = BABYLON.Mesh.MergeMeshes(task.loadedMeshes); nM.checkCollisions = true; var myPositionFunction = function(particle, s) { var utmPlaceX = edificios[i].x; var utmPlaceZ = edificios[i].z; var utmPlaceXFromCentre = utmPlaceX - mapCentreX; var utmPlaceZFromCentre = utmPlaceZ - mapCentreZ; var x = utmPlaceXFromCentre/scaleX; var z = utmPlaceZFromCentre/scaleZ; particle.position.x = x; particle.position.z = z; particle.position.y = alturas[ edificios_texto[edificio.name]]; particle.name = edificios_texto[edificio.name]; particle.color = new BABYLON.Color4(0, 0, 1,0.5); }; spsEdificios.addShape(nM, 1, {positionFunction: myPositionFunction}); nM.dispose(); } t++; }); var redEdificios = spsEdificios.buildMesh(); loader.load(); }; Quote Link to comment Share on other sites More sharing options...
juanmajr93 Posted June 6, 2017 Author Share Posted June 6, 2017 My main question is: is possible creating a SPS with different obj files imported? Quote Link to comment Share on other sites More sharing options...
jerome Posted June 6, 2017 Share Posted June 6, 2017 The SPS doesn't care about the model origin : it uses one or several meshes. As long as you pass it a BJS mesh, whatever the way it was built/designed, it can handle it. Quote Link to comment Share on other sites More sharing options...
juanmajr93 Posted June 6, 2017 Author Share Posted June 6, 2017 @jerome Well, it is a good news. How can I show you my problem?? I will try to explain my code, I load different models in my scene with this: var edificio = loader.addMeshTask(t, "","<?=$url?>assets/modelos/",edificios_texto[t]+".obj"); After this, I have implemented a "edificio.onSuccess = function (task){}" and there I have added the follow lines: spsEdificios.addShape(nM, 1, {positionFunction: myPositionFunction}); //I use only "1" because each particle is a different building model nM.dispose(); Finally, when bucle (forEach) is finished I create a mesh: var redEdificios = spsEdificios.buildMesh(); However, It doesn't work SPS has only one particle and it doesnt appear in the scene... Thanks! Quote Link to comment Share on other sites More sharing options...
adam Posted June 6, 2017 Share Posted June 6, 2017 Try calling SPS.refreshVisibleSize() http://doc.babylonjs.com/overviews/solid_particle_system#sps-visibility Quote Link to comment Share on other sites More sharing options...
juanmajr93 Posted June 6, 2017 Author Share Posted June 6, 2017 I have added this line and nothing... To visuaze something I have to create the mesh spsEdificios.buildMesh(); after the function addShape()... In this case, only one of the building model appears like a particle of SPS... Quote Link to comment Share on other sites More sharing options...
jerome Posted June 6, 2017 Share Posted June 6, 2017 it's really difficult to help without a running example ... Could you make a PG ? Did you check that your loop iterates over several downloaded meshes ? Quote Link to comment Share on other sites More sharing options...
juanmajr93 Posted June 6, 2017 Author Share Posted June 6, 2017 @jerome I share my scene: http://150.214.174.25:8029/vr Quote Link to comment Share on other sites More sharing options...
JohnK Posted June 6, 2017 Share Posted June 6, 2017 Out of curiosity why are you using SPS for different meshes? A major benefit of SPS is to repeat one mesh very many times. If all the buildings are different and you wanted to deal with them as one group could you parent them all to one mesh? If you want to see an example with SPS used with different meshes then this example http://doc.babylonjs.com/extensions/sps_tree_generator does that. There is a link to the code within it. jerome 1 Quote Link to comment Share on other sites More sharing options...
jerome Posted June 6, 2017 Share Posted June 6, 2017 @juanmajr93 your scene looks really nice, but, well ... 10K+ of lines of code to check !?!? I strongly recommend you to try to isolate your issue. Making a PG is really a good start, because it forces people to focus only on the very part of the code having the unexpected behavior. How many times was I about to claim on the forum that something was wrong in the framework ..? and then I just realized, when making a PG to submit it to the community, that I had a tiny hidden bug in my own code... something lost in the big mess, something I could have never found without recoding this little problematic part of code, in other terms without isolating it somewhere outside the global code. adam and JohnK 2 Quote Link to comment Share on other sites More sharing options...
juanmajr93 Posted June 6, 2017 Author Share Posted June 6, 2017 @jerome@JohnK I will try to create a playground but I have a some of javascript vector in my localserver.... The error is in the function that I have published. My main goal is reduce to one mesh all of building of my scene. By this way, I will improve the performance of my web application. Maybe, if there are another ways to do this without SPS please tell me... 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.