2016-10-31 2 views
1

Ich möchte das Material des statischen Mesh mit Linien-Trace erhalten, weil das statische Mesh mehr als ein Material halten. Ich setze komplexe Linienverfolgung über Chaanel, aber das FHitResult gibt kein Rendering-Material zurück, also möchte ich FHitResult.FaceIndx verwenden, um das Material des Dreiecks zu erhalten. Gibt es einen Rat?Wie man Material durch Linienverfolgung in Unreal Engine 4

+0

Sie haben viel mehr Chancen, dass Ihre Antwort auf Unreal Engine Forum https://forums.unrealengine.com/ – Treycos

+0

Die Menge an poeple diese Tags folgenden zu bekommen, ist wirklich gering – Treycos

Antwort

0

Zuerst eine Probe Raycast, es traf ein rechtes Objekt, die eine komplexe Raycast tun und erhalten das Ziel Mesh, Int der komplexen Raycast FHitResult, erhalten wir den FaceIndex, bedeutet, welches Dreieck wir treffen. Dann können wir das Netz abfragen mit dem FaceIndex, und erhalten Sie die Section ID (in UE4, der Abschnitt ist Einheit des Mesh, und ein Scetion verwenden ein Material, ein Scetion kann viele Dreiecke enthalten). Zuletzt, erhalten Sie Material-ID von Abschnitt ID.

FHitResult simpleResult = GetRaycastHitResultSimple(pc, startPos, endPos); 
if (simpleResult.bBlockingHit) 
{ 
    UStaticMeshComponent* staticMeshComponent = Cast<UStaticMeshComponent>(simpleResult.GetComponent()); 
    if (staticMeshComponent) 
    { 
     UStaticMesh* mesh = staticMeshComponent->StaticMesh;   
     if (mesh) 
     { 
      FHitResult complexResult = GetRaycastHitResultComplex(pc, startPos, endPos); 

      return GetMaterialByFaceIndex(mesh, complexResult.FaceIndex); 
     } 
     else 
     { 
      UE_LOG(LogTemp, Warning, TEXT("Get mesh from static mesh component faild")); 
     } 
    } 
    else 
    { 
     UE_LOG(LogTemp, Warning, TEXT("Cast FHitResul.GetComponent to static mesh component faild")); 
    } 
} 
else 
{ 
    UE_LOG(LogTemp, Warning, TEXT("Simple raycast faild")); 
} 


int32 UChangeMaterials::GetMaterialByFaceIndex(const UStaticMesh* mesh, const int32& faceIndex) 
{ 
    const FStaticMeshLODResources& lodRes = mesh->GetLODForExport(0); 
    int32 totalSection = lodRes.Sections.Num(); 

    int32 totalFace = lodRes.GetNumTriangles(); 
    if (faceIndex > totalFace) 
    { 
     ///Wrong faceIndex. 
     UE_LOG(LogTemp, Warning, TEXT("GetMaterialbyFaceIndex Faild, Wrong faceIndex!")); 
     return -1; 
    } 

    int32 totalTriangleIndex = 0; 
    for (int32 sectionIndex = 0; sectionIndex < totalSection; ++sectionIndex) 
    { 
     FStaticMeshSection currentSection = lodRes.Sections[sectionIndex]; 
     /*Check for each triangle, so we can know which section our faceIndex in. For Low Performance, do not use it. 
     for (int32 triangleIndex = totalTriangleIndex; triangleIndex < (int32)currentSection.NumTriangles + totalTriangleIndex; ++triangleIndex) 
     { 

      if (faceIndex == triangleIndex) 
      { 
       //return sectionIndex; 
       return currentSection.MaterialIndex; 
      } 
     } 
     totalTriangleIndex += lodRes.Sections[sectionIndex].NumTriangles; 
     */ 

     ///the triangle index is sorted by section, the larger sectionIndex contains larger triangle index, so we can easily calculate which section the faceIndex in.Performance Well. 
     int32 triangleIndex = totalTriangleIndex; 
     if (faceIndex >= triangleIndex && faceIndex < triangleIndex + (int32)lodRes.Sections[sectionIndex].NumTriangles) 
     { 
      //return sectionIndex; 
      return currentSection.MaterialIndex; 
     } 
     totalTriangleIndex += lodRes.Sections[sectionIndex].NumTriangles; 
    } 

    /// did not find the face in the mesh. 
    UE_LOG(LogTemp, Warning, TEXT("GetMaterialByFaceIndex, did not find it!")); 
    return -2; 
} 
Verwandte Themen