Sign in to follow this  
juanmajr93

SPS with different models

Recommended Posts

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!

Share this post


Link to post
Share on other sites

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 ...

Share this post


Link to post
Share on other sites

@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!

Share this post


Link to post
Share on other sites

@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();
		};

 

Share this post


Link to post
Share on other sites

@jerome Well, it is a good news. How can I show you my problem??:huh: 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!

Share this post


Link to post
Share on other sites

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...:mellow:

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

@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.

 

Share this post


Link to post
Share on other sites

@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...:wacko:

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.