I create some mesh using RenderData, like this
UStaticMesh* StaticMesh = NewObject<UStaticMesh>();
// Create a new render data object for the static mesh
FStaticMeshRenderData* RenderData = new FStaticMeshRenderData();
// Create a new LOD resources object for the static mesh
FStaticMeshLODResources& LODResources = RenderData->LODResources.AddDefaulted_GetRef();
// Set the number of vertices and triangles in the LOD resources
int32 NumVertices = /* Set the number of vertices in the mesh */;
int32 NumTriangles = /* Set the number of triangles in the mesh */;
LODResources.VertexBuffers.StaticMeshVertexBuffer.Init(NumVertices, true);
LODResources.IndexBuffer.AllocateDefaultIndexBuffer(NumTriangles, false);
// Generate some random texture coordinates for the mesh
for (uint32 i = 0; i < NumVertices; ++i)
{
FVector2D TextureCoordinate(FMath::RandRange(0.0f, 1.0f), FMath::RandRange(0.0f, 1.0f));
LODResources.VertexBuffers.StaticMeshVertexBuffer.SetVertexUV(i, TextureCoordinate);
}
// Generate some random triangle indices for the mesh
for (uint32 i = 0; i < NumTriangles; ++i)
{
uint32 Index0 = FMath::RandRange(0, NumVertices - 1);
uint32 Index1 = FMath::RandRange(0, NumVertices - 1);
uint32 Index2 = FMath::RandRange(0, NumVertices - 1);
LODResources.IndexBuffer.Indices.Add(Index0);
LODResources.IndexBuffer.Indices.Add(Index1);
LODResources.IndexBuffer.Indices.Add(Index2);
}
// Set the material for the static mesh
UMaterial* Material = /* Set the material for the mesh */;
StaticMesh->Materials.Add(Material);
// Set the render data for the static mesh
StaticMesh->SetRenderData(RenderData);
// Set the bounding box for the static mesh
FBoxSphereBounds Bounds(/* Set the bounding box for the mesh */);
StaticMesh->GetBodySetup()->AggGeom.LocalBoundingBox = Bounds.GetBox();
StaticMesh->GetBodySetup()->AggGeom.SphereRadius = Bounds.SphereRadius;
// Set the mobility of the static mesh to "Dynamic" to allow for runtime updates
StaticMesh->SetMobility(EComponentMobility::Movable);
// Create a new static mesh component and set the static mesh as its mesh
UStaticMeshComponent* StaticMeshComponent = NewObject<UStaticMeshComponent>();
StaticMeshComponent->SetStaticMesh(StaticMesh);
// Attach the static mesh component to a scene component in the world
USceneComponent* ParentComponent = /* Set the parent component */;
StaticMeshComponent->AttachToComponent(ParentComponent, FAttachmentTransformRules::KeepRelativeTransform);
Then updating the texture coordinate like this
UStaticMesh* pStaticMesh = pStaticMeshComponet->GetStaticMesh();
const FStaticMeshRenderData* RenderData = pStaticMesh->GetRenderData();
// Get the LOD resources for LOD level 0 (highest detail level)
const FStaticMeshLODResources& LODResources = RenderData->LODResources[0];
// Get a pointer to the texture coordinate data for the mesh
FStaticMeshVertexBuffer& staticMeshVertexBuffer = const_cast<FStaticMeshVertexBuffer&>(LODResources.VertexBuffers.StaticMeshVertexBuffer);
FPositionVertexBuffer& fp = const_cast<FPositionVertexBuffer&>(LODResources.VertexBuffers.PositionVertexBuffer);
// Loop through each vertex in the mesh and modify its texture coordinates
for (int i = 0; i < LODResources.GetNumVertices(); ++i)
{
const FVector2f newValue = FVector2f(1.0, 0.0);
staticMeshVertexBuffer.SetVertexUV(i, 0, newValue, false);
fp.VertexPosition(i).Z = 0;
}
FVector2f uvValue = LODResources.VertexBuffers.StaticMeshVertexBuffer.GetVertexUV(0, 0);
UE_LOG(LogCesium, Warning, TEXT("uv %s"), *(uvValue.ToString()));
pStaticMeshComponet->MarkRenderStateDirty();
pStaticMeshComponet->MarkRenderDynamicDataDirty();
The update operation does not work anyway.
How can I update the mesh info at runtime?