Skip to content

Instantly share code, notes, and snippets.

@coderespawn
Created February 8, 2018 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderespawn/d20ede2c6a33b2e198a9df556506cc39 to your computer and use it in GitHub Desktop.
Save coderespawn/d20ede2c6a33b2e198a9df556506cc39 to your computer and use it in GitHub Desktop.
#include "MyMeshExporter.h"
#include "RawIndexBuffer.h"
#include "StaticMeshVertexBuffer.h"
#include "PositionVertexBuffer.h"
void UMyMeshExporter::ExportStaticMesh(UStaticMesh* StaticMesh)
{
const FStaticMeshVertexBuffer& VertexBuffer = StaticMesh->RenderData->LODResources[0].VertexBuffer;
const FPositionVertexBuffer& PositionVertexBuffer = StaticMesh->RenderData->LODResources[0].PositionVertexBuffer;
// Build the vertex list
TArray<FVector> Vertices;
const int32 VertexCount = PositionVertexBuffer.GetNumVertices();
for (int32 i = 0; i < VertexCount; i++)
{
FVector Vertex = PositionVertexBuffer.VertexPosition(i);
FVector Normal = VertexBuffer.VertexTangentZ(i);
// save in your vertex array
Vertices.Add(Vertex);
}
// Build the index list
TArray<uint32> Indices; // << Result saved here
const FRawStaticIndexBuffer& IndexBuffer = StaticMesh->RenderData->LODResources[0].IndexBuffer;
IndexBuffer.GetCopy(Indices);
// Optional: If you want the triangle vertices
{
int NumIndices = Indices.Num();
for (int i = 0; i < NumIndices; i += 3) {
uint32 i0 = Indices[i + 0];
uint32 i1 = Indices[i + 1];
uint32 i2 = Indices[i + 2];
const FVector& v0 = Vertices[i0];
const FVector& v1 = Vertices[i1];
const FVector& v2 = Vertices[i2];
/// ...
}
}
}
#pragma once
#include "CoreMinimal.h"
#include "MyMeshExporter.generated.h"
class UStaticMesh;
UCLASS(Blueprintable)
class UMyMeshExporter : public UObject {
GENERATED_BODY()
UFUNCTION(BlueprintCallable, Category="Exporter")
static void ExportStaticMesh(UStaticMesh* StaticMesh);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment