Accessing Vertex Positions of static mesh

splinemesh?

could this be modified to work with splinemesh?

HI guys,
I’ve obtained the vertex position of the mesh as you suggested and it worked! Thanks!

Now I’ve to get the UV coordinate and the vertex normal for each vertex, how can I obtain such information?

Thanks in advance!

I’m unable to get this work in the current version of the engine. I’ve created a c++ function library


#pragma once

#include "Engine.h"
#include "Components/StaticMeshComponent.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "StaticMeshResources.h"
#include "kohLibrary.generated.h"




/**
 * 
 */
UCLASS()
class TEST_API UkohLibrary : public UBlueprintFunctionLibrary
{
    GENERATED_UCLASS_BODY()


    UFUNCTION(BlueprintPure, Category = "KohFunctions", meta = (Keywords = "vertex mesh get", NativeBreakFunc))
    TArray<FVector> MeshData(const UStaticMeshComponent* StaticMeshComponent);
    
};


#include "Test.h"#include "kohLibrary.h"
#include "Components/StaticMeshComponent.h"
#include "StaticMeshResources.h"


UkohLibrary::UkohLibrary(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{


}


TArray<FVector> UkohLibrary::MeshData(const UStaticMeshComponent* StaticMeshComponent)
{
    TArray<FVector> vertices = TArray<FVector>();


    //~~~~~~~~~~~~~~~~~~~~
    // Many thanks to  for this solution! :)
    //
    // Vertex Buffer
    if (!IsValidLowLevel()) return vertices;
    if (!StaticMeshComponent) return vertices;
    if (!StaticMeshComponent->StaticMesh) return vertices;
    if (!StaticMeshComponent->StaticMesh->RenderData) return vertices;


    if (StaticMeshComponent->StaticMesh->RenderData->LODResources.Num() > 0)
    {
        FPositionVertexBuffer* VertexBuffer = &StaticMeshComponent->StaticMesh->RenderData->LODResources[0].PositionVertexBuffer;
        if (VertexBuffer)
        {
            const int32 VertexCount = VertexBuffer->GetNumVertices();
            for (int32 Index = 0; Index < VertexCount; Index++)
            {
                //This is in the Static Mesh Actor Class, so it is location and tranform of the SMActor
                const FVector WorldSpaceVertexLocation = GetActorLocation() + GetTransform().TransformVector(VertexBuffer->VertexPosition(Index));
                //add to output FVector array
                vertices.Add(WorldSpaceVertexLocation);
            }
        }
    }    
    
    return vertices;
}


The errors are located in the red line. What is wrong?

Simple. You have no AActor’s functionality in UBlueprintFunctionLibrary, but the original code does (since it’s inherited from AActor).

But how can the position vertex buffer be retrieved in a packaged game. It’s possible to retrieve the index buffer using ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER but it seems the position vertex buffer cannot be acquired the same way. It doesn’t work even when the thread is held up using FlushRenderingCommands. I’m trying to use it only during initialization so this wouldn’t cause gameplay delay but it seems difficult to grab the data. I don’t want to just edit the engine to set bNeedsCPUAccess to true for packaged builds. It would be a waste of memory. Anyone have experience with this?

Hi!

Sorry for reviving this long since dead thread :frowning:
First of all, Thank you for all your help, ! Your tutorials are fun to watch and makes you think a lot about what you can do.
The same with this thread full of interesting information.
I see that there is a way to get all the vertices of the mesh. This is amazing, and close to what I need. but, still, not there…

My question is… is there a way to get the Edges of the mesh, as there is with the vertices?
I need only to know what vertices are adjacent, for a pattern detection AI.

I’ve tried doing it by myself, thinking that the order I read the vertices, is given by the connection, but after some tries, I don’t know what to think… “Am I doing it wrong, or the order has nothing to do with the connections?”

Please, could you help me?

Thank you very much!
Stefan

Shown how here:

where are you putting this part of the code?
i need to figure out GetTransform() of whose.

Hey, that post was from 2014 :slight_smile: if I were to do this now, I would extract ComponentToWorld from your mesh - i.e. GetComponentTransform() and then use TransformPosition(vert.Position) on it.

Or you can use TransformVector like the original code did and add it to GetComponentLocation(), should have the same effect in theory.

GetTransform - i.e. the actor’s transform will only work if your static mesh component is also the root component (true for a Static Mesh Actor, but not necessarily for others), so the component’s transform should be what you’re looking for.

Edit: If you specifically meant for a Brush/Volume actor, then GetTransform() (the brush/volume actor’s transform) is indeed what you’re looking for because in that case no primitive components are involved.

I know this is an old thread, but it helped me nonetheless to get the vertex positions of a mesh. I also ran unto the problem that it only works in the editor and not in builds (except when using OpenGL, apparently Unreal chooses to keep them CPU accessible there), and struggled with fixing it.

For anyone else who ever runs into this exact problem in the future: there’s a “Allow CPUAccess” flag you can set on your meshes’ properties: Set "Allow CPU Access" on mesh - Programming & Scripting - Epic Developer Community Forums

May I ask have you found ways to get the Edges of the mesh?

's C++ codes are incredible! But it seems they are no longer working in updated versions, need fixed.

You have to add those headers, it’s critical:



#include "Components/StaticMeshComponent.h"
#include "Rendering/PositionVertexBuffer.h"
#include "Engine/StaticMesh.h"
#include "StaticMeshResources.h"


header:



#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "GetStaticMeshVertexes.generated.h"

UCLASS()
class EXPERIMENT_2_24_VER_API AGetStaticMeshVertexes : public AActor
{
        GENERATED_BODY()

        UFUNCTION(BlueprintPure, Category = "Corridor", meta = (Keywords = "corridor vertex mesh meshdata", NativeBreakFunc))
        TArray<FVector> MeshData(const UStaticMeshComponent* StaticMeshComponent);

public:
        // Sets default values for this actor's properties
        AGetStaticMeshVertexes();

protected:
        // Called when the game starts or when spawned
        virtual void BeginPlay() override;

public:
        // Called every frame
        virtual void Tick(float DeltaTime) override;

};

cpp:



#include "GetStaticMeshVertexes.h"
#include "Components/StaticMeshComponent.h"
#include "Rendering/PositionVertexBuffer.h"
#include "Engine/StaticMesh.h"
#include "StaticMeshResources.h"
#include "UObject/ConstructorHelpers.h"
#include "DrawDebugHelpers.h"


TArray<FVector> AGetStaticMeshVertexes::MeshData(const UStaticMeshComponent* StaticMeshComponent)
{
        TArray<FVector> vertices = TArray<FVector>();

//~~~~~~~~~~~~~~~~~~~~
// Many thanks to  for this solution! :)
//
// Vertex Buffer
        if (!IsValidLowLevel()) return vertices;
        if (!StaticMeshComponent) return vertices;
        if (!StaticMeshComponent->GetStaticMesh()) return vertices;
        if (!StaticMeshComponent->GetStaticMesh()->RenderData) return vertices;
        if (StaticMeshComponent->GetStaticMesh()->RenderData->LODResources.Num() > 0)
        {
        FPositionVertexBuffer* VertexBuffer = &StaticMeshComponent->GetStaticMesh()->RenderData->LODResources[0].VertexBuffers.PositionVertexBuffer;
        if (VertexBuffer)
                {
                        const int32 VertexCount = VertexBuffer->GetNumVertices();
                        for (int32 Index = 0; Index < VertexCount; Index++)
                        {
//This is in the Static Mesh Actor Class, so it is location and tranform of the SMActor
                                const FVector WorldSpaceVertexLocation = GetActorLocation() + GetTransform().TransformVector(VertexBuffer->VertexPosition(Index));
//add to output FVector array
                                vertices.Add(WorldSpaceVertexLocation);
                         }
                }
         }

         return vertices;
}



Thanks for everyone’s idea under this topic! Especially !!!:slight_smile:

Hey y’all!

Using the vertex locations of an object, how can one linetrace to each and every vertex, similar to this video Superliminal Perspective Scaling (Unreal Engine 4) - YouTube ?

How to Get Animated Vertex Positions(skeleton mesh) through blueprint are C++.

bool UVictoryBPFunctionLibrary::AnimatedVertex__GetAnimatedVertexLocations(
USkeletalMeshComponent* Mesh,
TArray& Locations,
bool PerformPawnVelocityCorrection
){
if(!Mesh || !Mesh->SkeletalMesh)
{
return false;
}

//~~~~~~~~~~~~~
Locations.Empty(); 
//~~~~~~~~~~~~~
 
Mesh->ComputeSkinnedPositions(Locations);

FTransform ToWorld = Mesh->GetComponentTransform();
FVector WorldLocation = ToWorld.GetLocation();

//Pawn Velocity Correction
UPawnMovementComponent* MovementComp = nullptr;
if(PerformPawnVelocityCorrection)
{
	APawn* Pawn = Cast<APawn>(Mesh->GetOwner());
	MovementComp = (Pawn) ? Pawn->GetMovementComponent() : NULL;
}
bool DoVelocityCorrection = PerformPawnVelocityCorrection && MovementComp;
//Pawn Velocity Correction
 
for(FVector& EachVertex : Locations)
{
	EachVertex = WorldLocation + ToWorld.TransformVector(EachVertex);
	if(DoVelocityCorrection)
	{
		EachVertex += MovementComp->Velocity * FApp::GetDeltaTime();
	} 
} 

return true;

}

With This, I am unable to create node giving some issues and Which part go to where like .H Are .Cpp

Any one can help on this if you have idea.

It was a good solution from Rama for UE4. But in UE5, the type of the Vertex Position() argument changed and everything broke. Was FVector, became FVector3f. Here you probably need to use the new Geometry Mode plugin and in it FGeomVertex::GetActualVertex. But accessing it in the actor class via #include “EditorGeometry.h” doesn’t work for some reason.