Jump to content

scaling a scene loaded from babylon file


grire974
 Share

Recommended Posts

This is probably quite a simple problem to solve; but I'm pretty new to BabylonJS.  I would like users of my code to be able to arbitrarily scale a 3D model loaded from a .babylon file using code like this:

 


BABYLON.SceneLoader.Load("", "model.babylon", engine, function (newScene) {newScene.executeWhenReady(function () {...
 
After the scene has been rendered using, 
 


engine.runRenderLoop(function() {newScene.render();});
 
I'm then trying to apply the user defined scaling value like this (for example 200% scaling):
 


for (i = 0; i < newScene.meshes.length; i++){        newScene.meshes[i].scaling = new BABYLON.Vector3(2, 2, 2);  } 
 
As you can see from the attached images of normal vs scaled;
post-16528-0-01284000-1442933040.jpg
post-16528-0-67915200-1442933040.jpg
 
the 200 percent scaled version isn't scaled proportionately; e.g. the wheels out grow the chassis of the vehicle.  Is this the correct way to scale a scene of multiple meshes?  Should I instead be adding all my meshes to a parent node and then scaling that? (if so how).
 
Appreciate the help; thanks!

 

Link to comment
Share on other sites

This could be because the scale of the mesh before you set it isn't 1,1,1 (which is sometimes the case when importing meshes).

 

You could try,

for (i = 0; i < newScene.meshes.length; i++){        newScene.meshes[i].scaling.multiply(new BABYLON.Vector3(2, 2, 2));  } 

What happens to the mesh if you set it to (1,1,1) does the mesh change size?

Link to comment
Share on other sites

Good question - I think I'd already tried that; Tried it again just to be sure... All 6 meshes are showing initial scaling of {x: 1, y: 1, z: 1}.. also noticed that you added the .multiply member of the Vector3 class into your code; so I tried some variations of that - however as you have it; I don't think it will have any effect as its a statement rather than an assignment, I did try something like this though:

newScene.meshes[i].scaling = newScene.meshes[i].scaling.multiply(new BABYLON.Vector3(2, 2, 2));

However this still gave me the out of proportion scaling shown in my 200 percent attachment.  Any other thoughts?

Link to comment
Share on other sites

Yeh I figured it would be something like that.. Using cocos3d I used something similar; attaching the model (including its meshes) to a node; & then performing scaling / positioning/ rotation operations on that parent node..

 

Looking at the API documentation for Babylon though; I'm not too sure how that might look; the Node class doesn't have the scaling member; and Scene doesn't seem to extend any other classes (nor does it have the scaling property either).

 

Any hints? 

 

p.s thanks for the quick reply!

Link to comment
Share on other sites

Thanks for that tip.. I tried something like this:

 

var m = BABYLON.Mesh.CreateBox('pmesh',15000, newScene);m.isVisible = false; for (i = 0; i < newScene.meshes.length; i++){         newScene.meshes[i].parent = m;}  m.scaling = new BABYLON.Vector3(2, 2, 2);
 
However, unfortunately it doesn't work; I get a javascript error with babylon.js: Uncaught RangeError: Maximum call stack size exceeded.  Its citing line 6; however the file is minified so I'm not so sure.  Before the error is thrown, there are numerous console logs: 
t.isEnabled @ babylon.js:6

 

Any thoughts?

Link to comment
Share on other sites

aha! Of course.. Thanks for that.  For those of you reading the thread this was my code that solved it for me:

 

var m = BABYLON.Mesh.CreateBox('pmesh',15000, newScene);m.isVisible = false; for (i = 0; i < newScene.meshes.length; i++){                if(newScene.meshes[i].id == 'ground1'){continue}//substitute 'ground1' for the id of any mesh you don't want to scale                if(newScene.meshes[i].id == 'pmesh'){continue}                newScene.meshes[i].parent = m;}  m.scaling = new BABYLON.Vector3(2, 2, 2);//e.g. for 200% scaling
Link to comment
Share on other sites

p.s. deltakosh - on a side note while I've got you here; not sure if you're interested but I made a fix to the FBX exporter visual c++ solution a couple of weeks back.. However couldn't get my pull request to be accepted; are there any permissions that I need to be aware of for the github repo?

Link to comment
Share on other sites

  • 3 years later...

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