Move (drag) vertices to transform mesh

Hello, everyone. Babylon forum.

I’m inquiring about something that’s blocked during my work.

I’ve made each vertex (8) of the cube, and I’m trying to convert the cube mesh by dragging the vertex.

However, only one of the six sides moves as shown in the example below.

Why does it work like this?
Attached is the link.

Please point out if there is anything I am misunderstanding about Mesh.

I thought if only one of the eight vertices moved, the rest of the sides would also be modified.

I’ll be waiting for the reply.

Thank you!

Hello !

First of all you don’t need this in the Playground :

const canvas = document.getElementById("renderCanvas");
const engine = new BABYLON.Engine(canvas, true);
const scene = createScene();

engine.runRenderLoop(() => {
    scene.render();
});

window.addEventListener("resize", () => {
    engine.resize();
});

Just define the createScene function, the rest is handled automatically :wink:

Also, you can detach and reattach camera on pick, in order to keep camera action with the possiblity of stoping in on vertex drag

Fixed PG with the above considerations :


Coming back to your separated faces issue : even if a cube has 8 vertices in theory, here there are 24 :

const positions = cube.getVerticesData(BABYLON.VertexBuffer.PositionKind);
console.log("positions", positions.length/3)// --> Shows 24

The reason is that the default Cube is flat shaded, with 4 vertices per face (6*4 = 24) in order to define different normals on each.

It would not be the same problem with a custom smooth shaded cube :


So at the end, if you want to keep flat shaded (24 vertices), you need to work with a trick to drag all 4 vertices when you drag.


Also, best way to drag is to set an invisible plane perpendicular to the camera at the picked point position. That way you would make sure to drag in right plane. Here you are dragging on a surface your are actually modifying, which is problematic (and the reason why if you move too fast, you leave the picked surface within one frame, and it stops)


Edit : @deet_dev since you already had this function :
setupVertexDrag(marker, cube, i);
Is was quite easy to add the above “trick”. Basically you add markers only if no marker already at this positoin. And the setupVertexDrag function can be called multiple times on the same marker, to move several vertices :

Screencast from 05-12-2024 13:50:29

1 Like

Thank you very much for your reply.

This is what I want.

May I ask you additional questions?

If you move the vertices of each side at the same time, it doesn’t seem to be a problem.

Then I think it’s going to do the same with a polygon mesh that’s made of hexagonal hexagons, not squares, or irregular vertices.

However, for that to happen, it seems important to find the order or indications of the vertices of each figure.

How do I save the order of vertex?

And what exactly is indications…?

How can I calculate it and get it?

Thank you for teaching me who is lacking.

I would say the current code is ok for any topology.
Just replaced cube by flat shaded sphere :

Screencast from 06-12-2024 09:17:12

3 Likes

thanks you very much!

Your answer gave me strength.

I hope you have a great day.