1
\$\begingroup\$

I'm newbie with Unreal Engine 4. I need to create a function which calculates the volume of a mesh, based on the following example:

https://n-e-r-v-o-u-s.com/blog/?p=4415

To achieve that I created an actor blueprint containing a cone. I then created a function to calculate the volume of a tetrahedron composed by a polygon plus the center point of the scene, like that: enter image description here

Now I want to iterate through each of the polygons composing the cone actor, one by one, and use the above function to calculate the total volume of the cone.

I found a possible beginning for a such iteration: enter image description here

However I'm unable to find a way to create a loop which iterate through the Triangles array and extract the 3 vertices of each triangles composing the mesh.

I would be grateful if someone explained to me how to implement a such loop.

Or, if so, how to access correctly to the actor mesh vertex buffer object, and to iterate through each of the polygons composing it.

\$\endgroup\$
2
\$\begingroup\$

The triangles array is an array of integers indices that refer to vertices in the vertices array. Every three integers describes one of the triangles of the mesh.

Since the built-in "for each" Blueprint node doesn't support arbitrary step sizes, you can implement it yourself: initialize a local integer variable Step to 0, and use a "while loop" node attached to a condition for "step < length of the Triangles array." For the body node of the loop, access the triangles array elements at Step, Step + 1, and Step + 2. Each of those will give you an integer you can then use to index into the Vertices array to get the triangle positions. Then increment step by three before moving to the next iteration of the loop.

This will, honestly, be a little clunky in Blueprint. It's probably easier to do in C++, not because it will be that much faster (reading the mesh data is probably the bottleneck here), but because it will be easier to express.


Note that GetSectionFromStaticMesh will not work on all meshes; it requires the mesh to be CPU-accessible, among other things. You will get warnings and empty result arrays in the cases where the operation isn't supported.

An arbitrary mesh may have more than one section in any given LOD level as well, which you'll ultimately want to decide if you care about or not.

\$\endgroup\$
  • \$\begingroup\$ Thanks for your answer, and for the clarifications. I thought it was something like this, but I didn't thought to use a while loop. I'm myself a c++ developer and for me it would be easier to do that in c++ :-D however it's a constraint to not use it on the project I'm working. About the GetSectionFromStaticMesh, is there a better way to get access to the VBO from blueprints, and grarantee the access in all situations? And if yes, how to do that? \$\endgroup\$ – Jean-Milost Reymond Nov 6 '18 at 20:22
  • 1
    \$\begingroup\$ No, there isn't really a better way. Some vertex buffers are simply created without being CPU accessible (in the D3D layer of the engine). Honestly, I'd strongly suggest doing this kind of thing at content import/generation time and not at runtime in Blueprint, and just storing it with the mesh in some sidecar asset or using the user data fields for the asset. But otherwise, I'd just verify that all your meshes you're going to use with this are procedural and can be accessed on the CPU, and handle the failure care so you know when you make a mistake. \$\endgroup\$ – user1430 Nov 6 '18 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.