Jump to content

SPS with different models


juanmajr93
 Share

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!

Link to comment
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 ...

Link to comment
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();
		};

 

Link to comment
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!

Link to comment
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.

Link to comment
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.

 

Link to comment
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:

Link to comment
Share on other sites

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