Is it possible to access the FRawMesh data of a static mesh?
With some difficulty I have accessed the FRawMeshBUlkData but I can’t find a way to access the FRawMesh structure.
I want to access it in the editor.
Is it possible to access the FRawMesh data of a static mesh?
With some difficulty I have accessed the FRawMeshBUlkData but I can’t find a way to access the FRawMesh structure.
I want to access it in the editor.
I had to do this myself recently and there wasn’t any decent answer online so I’m going to post my findings here for future reference, although you probably moved on and hopefully had a solution.
There is two different set of vertices you can obtain from the UStaticMesh class. One is from the the RenderData class which Rama explains here. The problem with this, is that I found extra vertices on the mesh when compared to the model is loaded in Maya; which I believe is explained here.
The second way is through the FRawMesh struct, which stores the mesh data as it was from Maya or whatever modelling software you use. The FRawMesh and equally important FRawMeshBulkData are both in the RawMesh.h header file and are apart of the RAWMESH api so you will need to include RAWMESH in your module dependencies.
Here’s code of the function I needed the raw mesh data for as an example of it in use. I probably do a few stupid things in here so please correct me if something is not optimal.
#if WITH_EDITORONLY_DATA
void ExtractMeshInfo(UStaticMesh *_mesh, UCurveLinearColor *_curve) // I dump the info into a Linear Color Curve because the Vector curve contains the Minimal_API macro, hopefully I'll find a use for the extra float.
{
// Check that the mesh passed in is a valid mesh, valid meaning contains no junk data that would cause a crash
if (!_mesh->IsValidLowLevel())
return;
if (&_mesh->SourceModels[0] == nullptr)
return;
// Load up and access information from the SourceModel used for LOD0
FStaticMeshSourceModel *sourceModel = &_mesh->SourceModels[0]; // The index corresponds to the LOD group
FRawMesh rawMesh;
sourceModel->RawMeshBulkData->LoadRawMesh(rawMesh); // This function loads bulk data into rawMesh and makes the data nice and accessible
// Will store vertex information in these variables
FVector vertPos;
uint32 vertices = rawMesh.VertexPositions.Num();
if (vertices > 0)
{
// Clear out any previously store data in the curve
_curve->FloatCurves[0].Reset();
_curve->FloatCurves[1].Reset();
_curve->FloatCurves[2].Reset();
_curve->FloatCurves[3].Reset();
for (uint32 ii = 0; ii < vertices; ++ii)
{
vertPos = rawMesh.VertexPositions[ii];
// Add the vertex information to the curve at a time which corresponds to the vertex's index
_curve->FloatCurves[0].AddKey(ii, vertPos.X);
_curve->FloatCurves[1].AddKey(ii, vertPos.Y);
_curve->FloatCurves[2].AddKey(ii, vertPos.Z);
}
}
}
#endif
edit: Just found this, which uses the same concept and helped me optimize this code.
Hello Philip, I try to access the rawMesh in my code the same way you did, but I can’t because the function is mark
#if WITH_EDITORONLY_DATA
How did you call that function in your game ?
sourceModel->RawMeshBulkData->LoadRawMesh(rawMesh);
Any help would be appreciate.
Thanks
gamer08
Hey Gamer08, the quick answer is that you don’t. If I remember right, and also keep in mind that I’m talking about the module as it is in 4.7, the entire module that defines FRawMesh is designed to work only in the editor. If you click the API link in my answer take notice that the the header file path starts in the developer directory. Here’s some reading material about what that means.
See how in my example code I dump vertex information into a curve? Well, that’s so I can store the information when I select a mesh in the editor and then I access the curve in game when I want the data. Again, not sure if a curve is the best storage device for that, it probably isn’t. If you need to select and load information on a mesh that you didn’t store info about in the editor then I would recommend writing a file parser.
First of all, thanks for the answer.
So your class run in the editor not in game ? So I suppose you make you class in the developer directory am I right ??
The main purpose of my question is because I need to force the reconstruction of satic mesh in my game. e.g ( apply custom transformation such shear and reflection on mesh)
see my thread for more infos :
All my attemps fails … so if you have any solution for it, I could take any advice.
Thanks
gamer08
So your class run in the editor not in
game ? So I suppose you make you class
in the developer directory am I right
??
No, the module and class I wrote that contain the example function is in a runtime directory. I used the macro WITH_EDITORONLY_DATA to prevent the function from compiling in a packaged build. I’ll update the code here to prevent future confusion. Thanks!