eucly2 Posted February 22, 2017 Share Posted February 22, 2017 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? Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted February 22, 2017 Share Posted February 22, 2017 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); } Quote Link to comment Share on other sites More sharing options...
eucly2 Posted February 23, 2017 Author Share Posted February 23, 2017 Sorry but it doesn't work. With your code, for example, if the size of my indices array is 132 the for loop put 132 values in the positions array instead of 396 (132 x 3) Quote Link to comment Share on other sites More sharing options...
JohnK Posted February 23, 2017 Share Posted February 23, 2017 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. GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted February 23, 2017 Share Posted February 23, 2017 I did not test my code and did it on my phone but @JohnK saved my day JohnK 1 Quote Link to comment Share on other sites More sharing options...
eucly2 Posted February 23, 2017 Author Share Posted February 23, 2017 Hi! Thanks for your answer. That is exactly what i want to do: http://www.babylonjs-playground.com/#1WYYET#2 It's work on the PG so I hope it will work on my project, I try tomorrow on it and I will tell you the result. Thanks DK and JohnK. JohnK 1 Quote Link to comment Share on other sites More sharing options...
eucly2 Posted February 24, 2017 Author Share Posted February 24, 2017 Ok it's work, positions, normals and uvs are good, but there is one curious problem. When i change the scale of a new mesh, the scale is good but it move too. You can see the problem on my joins file or on this PG : http://www.babylonjs-playground.com/#1WYYET#4 Quote Link to comment Share on other sites More sharing options...
JohnK Posted February 24, 2017 Share Posted February 24, 2017 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. Quote Link to comment Share on other sites More sharing options...
JohnK Posted February 24, 2017 Share Posted February 24, 2017 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. eucly2 1 Quote Link to comment Share on other sites More sharing options...
eucly2 Posted February 24, 2017 Author Share Posted February 24, 2017 You are amazing!!! All work perfectly (I can die in peace now ) Thank you so much for this GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.