Download

Accessing Vertex Positions of static mesh

Hi guys! Not that I want to hijack the thread, but I’m quite desperate and this seemed at least mildly related to my problem.

Is there any possibility something similar could be done with SkeletalMeshes? What I want to do is freeze a SkeletalMesh in a pose and obtain the vertex positions of the mesh in that pose, and then procced to dump those positions to a file. A more accurate description of the problem can be found here. Unfortunately no one suggested anything :frowning:

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!

Uvs and normals?

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 Rama 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?

I am having the same problem as Kheka. I’m new to UE4 so I hope it’s not something painfully obvious.

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, Rama! 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: https://answers.unrealengine.com/questions/525510/set-allow-cpu-access-on-mesh.html

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

Rama’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 Rama 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 Rama !!!: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 https://youtu.be/w6Y7Z1R_z9w ?