Jump to content

How to draw parallel mesh ?


dsman
 Share

Recommended Posts

This is rather programming logic question . But if I get answer to this, I will be able to know more about what information babylon.js provides for mesh and how I can use to do dynamic mesh generation. 

 

Assuming there's one plane mesh (can be positioned anywhere, facing any direction) , how do I draw another plane mesh exactly parallel to it ? 

 

As shown image below, the mesh which is in back will be there in the model. It will be accessible to me by name. While the 6 smaller meshes in the front will the one I will be creating and positioning dynamically which will be parallel to the mesh in the back. The important thing is the dynamic meshes should be perfectly aligned with the mesh in the back . ie. top left corner of 1st mesh in the front (going by top left to right bottom) should match top left corner of mesh in the back. 

 

 

post-13379-0-24039300-1427140228.jpg

Link to comment
Share on other sites

var origMesh = ...;

var x = origMesh.position.x;

var y = origMesh.position.y;

var z = origMesh.position.z - 4;

var parallelMesh = ...;

parallelMesh.rotation = origMesh.rotation.clone();

parallelMesh.position.x = x;

parallelMesh.position.y = y;

parallelMesh.position.z = z;

x += width of parallelMesh and border for to the right of;

y += height of parallelMesh and border for to below of;

parallelMesh = ...;

Link to comment
Share on other sites

right.

var origMesh = ...;var amountRight = 0;var amountUp = 0;var amountForward = - 4; // neg due to being behind the front of meshvar parallelMesh = ...;parallelMesh.rotation = origMesh.rotation.clone();parallelMesh.position = origMesh.position.clone();parallelMesh.position.addInPlace(origMesh.calcMovePOV(amountRight, amountUp, amountForward)); amountRight -= width of parallelMesh and border for to the right of; amountUp -= height of parallelMesh and border for to below of; parallelMesh = ...;
 
Link to comment
Share on other sites

@JCPalmer  What if "origMesh"  is a mesh from preloaded model and not created by babylon dynamically ?  In that scenario code isn't working. 

 

I edited the default playground code to include your code : http://www.babylonjs-playground.com/#FUIQ1 

 

The default playground had Ground mesh. when I tried to draw parallel mesh to ground mesh, it didn't work. Ground was horizontal , while new mesh I added appeared vertical. 

 

Then I changed ground mesh to another plane mesh and it worked . It created parallel plane. 

 

So I doubt  babylon might be treating plane mesh imported from max scene differently and following properties might not work for it . Please help.

parallelMesh.rotation = origMesh.rotation.clone();parallelMesh.position = origMesh.position.clone();    
Link to comment
Share on other sites

The link is empty.  Either way CreateGround probably constructs the plane horizontally without rotation. CreatePlane does so vertically.  What ever you wish to make parallel should have its vertices created in the same orientation.

 

parallel grounds for ground

parallel planes for plan

 

If you want planes horizontal, create your original then rotate it first.

Link to comment
Share on other sites

I think you misunderstood my question.  I don't want horizontal. I was just referring to how in default playground code on http://www.babylonjs-playground.com/  , if we try to draw parallel plane mesh to ground plane doesn't work . But if we replace ground with new plane and then try to draw parallel plane mesh then it works. 

 

Here's same link which should work fine now. I simply replaced CreateGround with CreatePlane in the default example. And implemented your code to draw parallel plane. And it works. But if you change CreatePlane to CreateGround , it wont' work. 

 

http://www.babylonjs-playground.com/#1T9AAU

 

So I am wondering what's wrong with it. This will give  me clue why it is not working in my application. In my application , as I said above, I am choosing a mesh by name from a model loaded from babylon.js and trying to create parallel mesh to it dynamically. 

Link to comment
Share on other sites

Your setup has to the camera too far away, or your meshes are too small.  I made them much bigger, with tile being still smaller.  Gave tile a material so you could see it from the ground, in grey.

 

I did have the sign wrong on amountForward.  http://www.babylonjs-playground.com/#1T9AAU#1

 

I would switch to arc rotate camera for any development.  I am surprised default uses FreeCamera.  I do not use Playground myself. I develop in Typescript, and Javascript just confuses me.

Link to comment
Share on other sites

In my original app , as I said, I am using a model imported from 3dsMax. 

 

Now I am able to generate all parallel planes and I am also able place them correctly in up and right direction in relative to background plane. 

 

But the newly generated parallel plane are appearing far away from original background plane even if I keep amountForward = 0 , in your code. So newly generate parallel plane meshes are appears at 0,0,0 position in the world. While original mesh is at 0,0,-750.

 

What could be the reason ?

 

PS: Does this mean origMesh.position.clone()  is giving original position of mesh (0,0,0) and because the model was imported, babylon engine treated it as locally translated mesh and so when rendered it appears at some other place (0,0,-750) ? And newly generated parallel mesh appears at center because with position.clone returns (0,0,0)  ? 

Link to comment
Share on other sites

I am re-declaring that your code isnt working anymore. 

 

mesh.rotation.clone ()   and mesh.position.clone() aren't working for mesh loaded from 3DSMax model. 

 

So my model has two plane A & B. For which I wanted to create parallel planes dynamically. 

 

It worked for once, as I tried for the plane A which was already parallel to XY plane. So despite the rotation.clone() not working, the newly drawn dynamic planes were parallel to original plane. Because they are drawn  by default on XY plane , hence became parallel to plane A. 

 

But when I tried with  plane B,  which was parallel to YZ plane in the model, the same code , generated parallel meshes  at same position parallel to Plane A  instead of Plane B. 

 

So I believe mesh.rotation.clone() doesn't work on meshes imported from model . As engine don't know whether they are plane or any other random shape. It doesn't know whether they were rotated or not. Am I right ?

 

What are my option to draw parallel mesh to a plane inside a imported model which can be placed anywhere in the model ?

Link to comment
Share on other sites

POV functions only support meshes defined as facing forward or away from the XY Plane.  You could:

  • Plug in an implied rotation instead of cloning it.
  • Define you mesh in the 3DSMax model in the XY plane, then assign it a rotation, if 3DSMax does this. (Blender does)
  • Try to figure out the plane the original mesh is defined in from boundingInfo, originalMesh.getBoundingInfo().
  • Just build everything in 3DSMax.
Link to comment
Share on other sites

@JCPalmer  

 

Out of four options you suggested none except #3 will suite our application need. 

 

I tried to fin four points of original surface using boundingInfo().BoundingBox.Vectors  . I am able to find them. To make sure I have found right corner points, I placed simple box at those points by doing box.position = originalMesh.getBoundingInfo().BoundingBox.Vectors . But instead of those corners box appeared in center of scene. So position weren't set correctly basically. 

 

I don't know if it is due to local space and world space. 

Link to comment
Share on other sites

Instead of getBoundingInfo() method, I am trying to do it using vertices data. In my case, the origMesh is always going to be plane. So using getVerticesData I get six vertices and from that I can get absolute position in world space of four corner points of original plane surface ( using origMesh.getWorldMatrix()  ) . 

 

Now after having absolute position of four corner point , I can position my new dynamically generate planes at correct position (starting from corner) . But I am not able to follow rotation of original plane mesh. newPlane.rotation = origMesh.rotation.clone(); isn't working. 

 

How do I get rotation of plane mesh imported from 3dMax Model ?

Link to comment
Share on other sites

I tried rotationQuaternion . Its not empty but its not giving correct rotation to new plane mesh (that I am generating by code ) such that it perfectly aligns the orientation/rotation of original mesh inside imported mode.  May be this is because how the plane was placed in 3Dmax model. May be the original plane wasn't rotated at all in 3DMax and was originally drawn in its current inclination/orientation. 

So I was trying method to find normal to plane and then form Rotation Matrix somehow . But since I am not good at geometry , I couldn't implement it.  (I read http://bit.ly/1yNm4Nm  but didn't quite understand)

 

Any clue ?  

 

I thought drawing parallel plane mesh would be simple thing. But its turning out to be big deal. :) 

PS : 3DMax model in my case will unpredictable. We can't dictate how will design/rotate objects in it. Basically we want to allow user to add their own models at runtime. 

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