Advertisement
Guest User

GetVertexPaintData Implementation

a guest
Oct 8th, 2016
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. FColor UYourBPLibrary::GetVertexPaintData(UStaticMeshComponent* StaticMeshComp, FVector Position, int32 LODIndex)
  2. {
  3.     // Make sure user doesn't pass nullptr
  4.     if (StaticMeshComp == nullptr)
  5.     {
  6.         UE_LOG(LogTemp, Error, TEXT("Passed Static Mesh Component is not valid!"));
  7.     }
  8.     else if (StaticMeshComp->LODData.IsValidIndex(LODIndex)) // And a valid LODIndex
  9.     {
  10.         // Get the painted Vertices from the passed StaticMeshComponent's LODData
  11.         TArray<FPaintedVertex> PaintedVertices = StaticMeshComp->LODData[LODIndex].PaintedVertices;
  12.  
  13.         // Make sure we have PaintedVertices
  14.         if (PaintedVertices.Num() > 0)
  15.         {
  16.             // Find the closest Vertex Position to the passed Position (local space!)
  17.             float ClosestDistance;
  18.             FVector EmptyDir; // No use, just for the Function below
  19.  
  20.             (PaintedVertices[0].Position - Position).ToDirectionAndLength(EmptyDir, ClosestDistance);
  21.  
  22.             FColor ClosestColor = PaintedVertices[0].Color;
  23.  
  24.             // Simple "if smaller, than use smaller one" algorithm
  25.             for (int32 i = 1; i < PaintedVertices.Num(); i++)
  26.             {
  27.                 float TempDistance;
  28.  
  29.                 (PaintedVertices[i].Position - Position).ToDirectionAndLength(EmptyDir, TempDistance);
  30.  
  31.                 if (TempDistance < ClosestDistance)
  32.                 {
  33.                     ClosestDistance = TempDistance;
  34.                     ClosestColor = PaintedVertices[i].Color;
  35.                 }
  36.             }
  37.  
  38.             return ClosestColor;
  39.         }
  40.         else
  41.         {
  42.             UE_LOG(LogTemp, Error, TEXT("No painted vertices found!"));
  43.         }
  44.     }
  45.     else
  46.     {
  47.         UE_LOG(LogTemp, Error, TEXT("Please provide a valid LODIndex. LODIndex: %d is out of bounds."), LODIndex);
  48.     }
  49.  
  50.     // Return generic White Color if any error occurred
  51.     return FColor::White;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement