Jump to content

Extract VerticeData from subMesh


eucly2
 Share

Recommended Posts

Hey everyone!

I'm trying to extract VerticeData from a SubMesh to create an other mesh but i always failed.

I want to extract indices, positions, normals and uvs for the moment:

Indices:

var result = new VertexData();
var indices = subMesh.getMesh().getIndices();
for (var index = subMesh.indexStart; index < subMesh.indexStart + subMesh.indexCount; index++) 
{
    result.indices.push(indices[index]);
}

Positions and normals:

var positions = subMesh.getMesh().getVerticesData(BABYLON.VertexBuffer.PositionKind, copyWhenShared);
var vertex;
for (var index = subMesh.indexStart; index < subMesh.indexStart + subMesh.indexCount; index+=3) {                    
    vertex = new BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(positions[index], positions[index + 1], positions[index + 2]), worldMatrix);
    result.positions.push(vertex.x);
    result.positions.push(vertex.y);
    result.positions.push(vertex.z);
}

 

Anyone to help me?

Link to comment
Share on other sites

Hello!

actually the index you use here is wrong, you have to use the indices array with it (like you did for the indices actually :))

var positions = subMesh.getMesh().getVerticesData(BABYLON.VertexBuffer.PositionKind, copyWhenShared);
var indices = subMesh.getMesh().getIndices();
var vertex;
for (var index = subMesh.indexStart; index < subMesh.indexStart + subMesh.indexCount; index+=3) {                    
    vertex = new BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(positions[indices[index]], positions[indices[index + 1]], positions[indices[index + 2]]), worldMatrix);
    result.positions.push(vertex.x);
    result.positions.push(vertex.y);
    result.positions.push(vertex.z);
}

 

Link to comment
Share on other sites

Using DK's idea as a basis I came up with this PG http://www.babylonjs-playground.com/#1WYYET#1 which I hope helps you to complete your project. You can change which submesh is constructed using line 21. The are ( for reasons I do not understand) 6 submeshes rather than 3. The sphere.subMeshes numbers 0 and 1, 2 and 3, 4 and 5 result in the same submesh.

A suggestion for further questions a PG (which could be a simplification of the problem aspect of your project) helps to elicit faster responses.

Link to comment
Share on other sites

Because of the way the you are constructing the  custom meshes from the sub meshes of the cube the vertices of the sub meshes have coordinates relative to their local origin and so scaling is relative to the local origin. When you scale the cube as in http://www.babylonjs-playground.com/#1WYYET#5 with reference to z it behaves as you would expect. The green plane has the same relative vertex coordinates as the front of the box so scaling the green plane has the same effect as scaling the front of the box. The simplest way is to just adjust positions until they fall where you want them.

The alternate is to create a new local origin for the custom mesh. Once you have determined where you want the new local origin of the custom mesh to be you can either set it by adjusting the coordinates of the vertices when forming the result.positions or you could use setPivotMatrix   Which ever way you create a new local origin you will still need to determine where you want the new local origin to be.

Also when you use meshBuilder for creating a mesh the sizes have to be given inside the options object, http://babylonjsguide.github.io/basics/Shapes in your PG the 10 is ignored and the default size of 1 is used.

I have corrected this in the following PG (using size 1) and also simplified the code by constructing a mesh prototype function that can be called to create a mesh from a submesh. If you change to size:10 you will have to re-adjust the positioning.

Link to comment
Share on other sites

Have added to CreateMeshFromSubMesh - first parameter is now the name for the custom mesh and added a final parameter, true, false or leave out. When true a new local origin is created for the custom mesh which is then re-positioned in the correct place on the original mesh. The new local origin is the barycentre of the custom mesh. Using translation rather than position on the created submesh means if you wish the translation for all created submeshes can be consistent as in http://www.babylonjs-playground.com/#1WYYET#7

Though of course using position will still place the submesh.

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