How do I get a list of vertices of a mesh?

I thought I could get it using the following code, but I get an error in the console: Uncaught TypeError: Cannot set properties of undefined (setting ‘length’). Can anyone help?

 var meshInstance = bbox_entity.render.meshInstances[0]; // Assuming there's only one mesh instance
  var vertexPositions = meshInstance.mesh.getPositions();

Many Thanks

Hi @Grimmy,

You need to pass an array to the method, and the method will fill it up with the positions:

const positions = [];
meshInstance.mesh.getPositions(positions);

console.log(positions);

Okay Thanks. So getPositions just returns a list of numbers? 9 numbers for each vertex? If thats correct how do I get each vertex position into a usable vec3?

Maybe I should clarify that I want the positions of each vertex. getPositions sounded like it was the right function to use for that.

Thanks

It’s returning numbers, not Vec3s

Aargh, I think I edited my last post instead of hitting reply!!

Here is my code again:

var meshInstance = bbox_entity.render.meshInstances[0]; // Assuming there's only one mesh instance
var vertexPositions =[];
 meshInstance.mesh.getPositions(vertexPositions);

var screenPoints = [];//screen points is the array of vertices of the bounding box
console.log(bbox_entity.name+" . How many verts:: "+vertexPositions.length);//Prints 72..should be 8
for (var i = 0; i < vertexPositions.length; i++) 
    {
        var vertexPosition = vertexPositions[i];
        var screenPoint = this.camera.camera.worldToScreen(vertexPosition);
        console.log(bbox_entity.name+" : "+screenPoint); //screenPoint prints Nan
        screenPoints.push(screenPoint);
    }

My current issue is that I want the vertex positions but I need vertexPositions to be returned as vec3 positions of each vertex…but I think they are 72 numbers of Nan (?)

The create a Vec3 of each 3 numbers.

Note that 72 numbers means 24 verticies, as each face has to have unique verticies due to different UVs.

Hi @Grimmy,

Check out this post for when I needed to get a list of vertices and their positions. I looped through the array and passed the values to an additional function that would generate the positions in Vec3 format.

I hope this is helpful.

1 Like

How do I create a vec of every other 3 numbers? Out of interest, why doesnt getPosition return Vec3s?

Hi, Thanks for that but I’m having quite a bit of trouble understanding how it works :slight_smile:

It’s just a data in the vertex buffer. In most cases a vertex is 3 numbers, but you can have anything between 1 and 4 numbers per vertex, same as other attributes.

Also, working with an array of numbers in JS is a lot faster than an array of Vec3 as you need to allocate each Vec3 object.

But if it’s the getPosition function that is returning the data shouldn’t we presume its required in Vec3 format?

If there is a fourth number in some vector data as you say, what is that data and doesnt that mean I can’t really create a Vec3 from the data by just iterated over every 3 numbers (because it might have 4) ?

I’m even more confused now.

For typical 3d meshes it’s always xyz for position. But the mesh class itself is more general, and you can store other data in position stream. So don’t worry about it, for you unless you create a custom mesh with custom data in positions, it’s always 3.

Thanks so much! My vertices are reading numbers like this though (see attached text below). Seems to be that the numbers are based on local space or something? Any ideas what this could be? Apologies for all the questions…

Vert Array:: [-0.5, -0.5, 0.5],[-0.5, 0.5, -0.5],[0.5, -0.5, 0.5],[-0.5, 0.5, 0.5],[0.5, 0.5, 0.5],[0.5, 0.5, -0.5],[0.5, -0.5, 0.5],[-0.5, 0.5, 0.5],[0.5, 0.5, 0.5],[0.5, 0.5, 0.5],[0.5, 0.5, 0.5],[0.5, 0.5, -0.5],[0.5, -0.5, -0.5],[-0.5, -0.5, 0.5],[-0.5, 0.5, 0.5],[0.5, 0.5, -0.5],[0.5, -0.5, -0.5],[-0.5, -0.5, -0.5],[-0.5, -0.5, -0.5],[-0.5, -0.5, -0.5],[-0.5, -0.5, 0.5],[-0.5, 0.5, -0.5],[0.5, -0.5, -0.5],[-0.5, -0.5, 0.5]

of course, they are always in local space, and a matrix on the entity transforms them to the world space … so that the mesh can be moved around, without vertices having to be adjusted in the mesh every frame.

Do you know if there is an example of how I can get the world positions of each vertex?

I tried also the example here but it just gives local positions too.

@eproasim is doing a nice trick here, using dummy entities and set local position. From there you could later call getPosition on each entity and you can get the world position.

There are more code friendly ways to do this using math, not sure if there is a forum post on the topic though.

Okay, I understand what he’s doing now. Let me see if I can replicate with something similar. Cheers

the batching code transforms positions to world space, see in batch-manager.js. Something like this

const positions = [];     // data from the mesh
const transform = meshInstance.node.getWorldTransform();
const vec = new Vec3();

for (let j = 0; j < positions.length; j += 3) {
  vec.set(positions[j], positions[j + 1], positions[j + 2]);
  transform.transformPoint(vec, vec);

  // now vec has world position of the vertex
}
1 Like