Jump to content

Inverting every meshes x direction including normals


KevinBLT
 Share

Recommended Posts

Hello everybody.

 

I think I got myself into a trap.

In our configurator, you start planning in 2D space.

Where X is from left to right and Y from top to bottom.

Then you can switch and see the 3D view of your planning.

 

Now as I have forgotten or just suppressed it, is that babylon handles some directions differently.

 

Now X is mirrored.

I tried to but an invisible box as a parent and then set scaling.x to -1.

Unfortunately then everything is illuminated wrong, therefore I tried to flip the normals too, but that

didn't help neither.

 

What would be the best way?

Please keep in mind that there is no model that could be exported with another parameter for the left or right handed thing.

 

Please look at the screenshot below for more clearity.

 

Thanks!

 

 

 

post-14468-0-77101500-1434972556.jpg

post-14468-0-51249700-1434972557.jpg

Link to comment
Share on other sites

Hi,

 

If I understand correctly, you have a piece of code that translates the projected schematic into a 3D model. Wouldn't that be easier to handle the X-flipping in here, instead of doing it after the model has been imported into BJS?

 

I've tried a few things but I can't really manage to have a mesh mirrored on an axis (i.e. with a scaling parameter < 0) and obtain consistent normals at the same time. Here's what I've tried.

 

Also there's this thread on the same topic, which didn't really offer a solution either: http://www.html5gamedevs.com/topic/13454-flipping-a-mesh/?hl=flipped#entry79488

 

EDIT: actually, I got it to work (somehow) on my sphere test: http://www.babylonjs-playground.com/#1ZBM09#1

The trick is to inverse the order in which each face's vertices are saved, which basIcally means inverting the face orientation. There are still issues at the seams though. I guess a FlipNormals function which does that would be a nice addition to BJS!

Link to comment
Share on other sites

Hello thanks for your helping work.

So this looks promising.

 

But there remains a small dark "blurred line". Sorry I don't know the correct englisch word.

 

So I did the same with the normals too:

http://www.babylonjs-playground.com/#1ZBM09#2

 

Then the result is even a bit better, but not completly perfect.

Maybe there is just one small thing left to figure out?

 

Then it could be added as a function to babylon?

Mesh.mirror(Axis) ?

 

For the other question:

Yes I could do it in 2D space,

but it would be practial for future projects to get it done after 3D creation.

 

Thanks so far.

Link to comment
Share on other sites

The blurred line you are talking about is due to the fact that in my example the normals are recomputed based on the faces orientations with the function BABYLON.VertexData.ComputeNormals(). This does not give good results on the sphere seams because of how vertices are arranged in the mesh.

 

Please note that in your specific case, using the method I've shown you should be enough to solve your problem.

 

Still, we need a way to mirror a mesh without having to recompute its normals! I'll try to think of something.

Link to comment
Share on other sites

True but I think these functions should just be shortcuts for this:

mesh.bakeTransformIntoVertices(BABYLON.Matrix.Scaling(...))

Because, I mean, this function should really allow us to apply inverse scaling.

 

I've dug into the Vector3 code and can't really find a nice way to handle it. Here is a PG with visible normals and a custom bakeCurrentTransform function: http://www.babylonjs-playground.com/#1ZBM09#3

 

I managed to make it work by manually inverting the normals on the X axis, but this shouldn't be necessary. EDIT: actually, it doesn't work... there's a bug in my code :(

Link to comment
Share on other sites

Yes so thanks for your work!

But I have a small question on my mind:

 

So do I need to do this for every single mesh?

Or do I need to merge them into a mesh with sub meshes? (and if yes,  don't know how yet).

Link to comment
Share on other sites

Well it's hard to answer this question without knowing how your system works, and what your meshes are exactly (small parts or the whole object?). What I wrote earlier was intended for the whole object shown in your first post.

 

Also, just a thought: what if there was no error between the schematic and your object in the first post, but the schematic was simply a view from underneath it?

Link to comment
Share on other sites

Yes. Sorry if I told you to few informations.

 

So this is created dynamically from some scaled boxes and/or some loaded meshes (the aluminium bar for example).

Then they get placed and every identical object gets instanced.

 

For the viewing:

In 2D the roof's top left corner is 0/0.

 

I place everything I know from 2D in 3D setting Vec3 (X = x_2D, Y = 0, Z = y_2D).

Therefore I could flip X axis while placing in this case, but I want to be on a safer road with

the other configurations, as they may not only depend on two bars.

 

So what do you think?

Or should I always flip when coming from 2D?

 

I just thought I could avoid that, so that every configuration type can be built without careing about

2D positions etc.

 

post-14468-0-85389900-1435144650_thumb.j

Link to comment
Share on other sites

Happy to help :)

 

@KevinBLT: okay, so first: if you merge all those meshes together (use BABYLON.Mesh.MergeMeshes), you can then mirror them on the X axis and your end result will be fine.

 

Now, concerning the logic of your process: mirroring the object after merging the meshes seems like a not so good way to do things. Merging means you lose some information, specifically the different subparts of your object, so I'd advise you to avoid merging if not necessary. For example, that would prevent you from modifying the object again in BJS.

 

There must be a step in your process where it would be logical to adapt the coordinate system to BJS, and invert the x-coordinate. What about just before setting the different subparts' coordinates?

 

You probably have a piece of code that looks like that:

// fetch position in configurator spacevar position_projected = new BABYLON.Vector2(x_2d, y_2d);// position the subpart on the XY plane// ** This is where the coordinate system switch (configurator -> BJS) is made **var position = new BABYLON.Vector3(-position_projected.x, 0, position_projected.y);// adjust heightposition.y = height/2;// create meshvar mesh_subpart = new BABYLON.Mesh("subpart", scene);mesh_subpart.position = position;

I don't know if that helps...

Link to comment
Share on other sites

Thanks folks for your feedback.

I really appreciate that.  ^_^

When I'll have finshed my project, I will contribute to babylon.

Maybe by working on the CSG things, which I used a lot.

 

So for now I think it's better, as you said, to invert when coming from 2D.

So I ended up with this code for this configuration:

ForAll(gData.bars,function InvertBarX(cBar) {  cBar.x = (cBar.x * -1) + configurator2D.roof.size.width;    }); 

I'll will see what that will bring when working on other configuration types with walls, windows, doors and stuff.

 

Thanks guys and have a nice day!  :)

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