Dear Community,
I’ve been getting asked for a while how to get UV information back from line trace hits in UE4.
Well I recently was diving into PhysX code for my own personal research, and along the way I learned how I could easily modify the UE4 engine to support returning of UV information from raycasts!
Please note you can only return UV info from PhysX raycasts / UE4 line traces (so not capsule sweeps for example), because you need to use a PxRaycastHit, which is the PhysX structure whose unique addition to PxLocationHit is the UV information.
**Landscapes**
Please note getting UV information back from raycasts can be especially useful for Landscapes, as UE4's landscape system uses PhysX heightfields, and UV information from raycasts is supported!
Quoting the PhysX link above:
**"Barycentric coordinates of hit point, for triangle mesh and height field"**
So this means that you can know the UV coordinates of a character within their current landscape component at all times, and **can use this information to modify the landscape according to player-driven actions.**
UE4 Wiki Link to My C++ Code
I’ve created a wiki that shows all of the engine changes that you need to make to add support for returning UV information to UE4!
How to Get UV Hit Information back from Line Traces / Ray Casts in UE4
**Line Numbers from UE4 4.7.3**
Please note I use line numbers specific to version 4.7.3 of UE4 to avoid posting any .cpp Engine code publicly.
Relevant PhysX Code
The core changes to the PhysX code to add support for UV info are actually quite simple:
First you have to add the additional bit flag telling PhysX that you want the UV info back from the hit query
if(Params.bReturnUV)
{
POutputFlags |= PxSceneQueryFlag::eUV;
}
Then you have to retrieve the info after the hit query has occurred:
if(Params.bReturnUV)
{
OutHit.UV = FVector2D(PHit.u,PHit.v); //u and v are exclusive to PxRaycastHit
}
The rest of my code, which is on the wiki, is to add the support structure that allows you to easily just add the flag to CollisionQuery params to tell UE4 to add the UV info to the FHitResult.
**Sample Code At Project Level**
![PxUV.jpg|1280x960](upload://d3bRr0teMZdhvom6b6CDeDU0Xob.jpeg)
Once you've made the changes at my wiki link to your UE4 Github engine, you can then use code similar to this at the project-level to retrieve UV information from line traces in UE4!
```
void AISMCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
//~~~~~~~~~~~~
FVector Start = GetActorLocation();
FVector End = Start + GetActorRotation().Vector() * 10240;
**FCollisionQueryParams TraceParams**(FName(TEXT("VictoreCore Trace")), true, this);
TraceParams.bTraceComplex = true;
//TraceParams.bTraceAsyncScene = true;
TraceParams.bReturnPhysicalMaterial = true; //if you want landscape phys mat
//~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Rama's Addition to UE4 for returning UV Info
**TraceParams.bReturnUV = true;**
//~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Ignore Actors
TraceParams.AddIgnoredActor(this);
//Hit Info
**FHitResult HitOut;**
//Trace!
GetWorld()->LineTraceSingle(
HitOut, //result
Start, //start
End , //end
ECC_Pawn, //collision channel
TraceParams
);
VSCREENMSG2("UV!", **HitOut.UV.ToString()**);
}
```
Github Offering
I have posted a pull request to add support for UV hit information to line traces in UE4, if you wish to support this addition you can do so here:
https://github.com/EpicGames/UnrealEngine/pull/1022
**Conclusion**
Now you have the code to get UV hit information back from line traces / ray casts in UE4!
Again my full code that you need to add to your Github version of UE4 is here:
**How to Get UV Hit Information back from Line Traces / Ray Casts in UE4**
https://wiki.unrealengine.com/Line_Traces_/_RayCasts_How_To_Get_UV_Info_From_Hits
Enjoy!
Special Thanks
Special thanks to Epic for giving us Github version of UE4 so we can make these sort of additions to the code base!
[FONT=Comic Sans MS]
Rama