Download

[New Wiki] How to get UV hit information back from Line Traces in UE4!

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
https://wiki.unrealengine.com/Line_Traces_/_RayCasts_How_To_Get_UV_Info_From_Hits


**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]:heart:

Rama

Adding UV Info to FHitResult

Code for you to add support for returning UV information from line traces in UE4!

See above!

Rama

Thank you, Rama!

This could be very useful for modular destruction systems like this one:

But with a little difference for dynamic destruction: to project ray, same as velocity vector, to get a hit point and then generate destruction volume etc.

It’s mine Houdini realtime destruction, and I would repeat this in UE (if I will find a time :frowning: )

Thank you once again, Rama!

Thank you, Epics!

This is awesome! This would be exceptionally useful for things like blood masks or hit masks.

Great to hear from you the Jamsh! Hee hee!

You’re welcome! Let me know if you get around to implementing that destruction system!

:slight_smile:

Rama

Great work, after much searching, it looks like this is the only solution for collecting UV hit locations from traces. Please forgive my ignorance, I see that the PhysX technology is being used, does this work on all client graphics hardware or is it limited to Nvidia only?

Thank you.

It will work on everything :slight_smile: PhysX is mostly multi-platform supported, but some nVidia cards have dedicated hardware that allow it to be processed faster or more efficiently. Either way though, you can safely use most of the PhysX stuff for any end hardware.

[FONT=Comic Sans MS]Welcome to the forums watdge!
( it was his first post :))

:slight_smile:

Rama

Thank you both! :slight_smile:

Hello Rama!

thank you really much for this solution. I will probably implement it soon.
I have a character with a cape and I was wondering if this solution would work on cloth?
I guess it should since cloth are simulated with Physx but never sure ^^

Wonder of procedural mesh would work, given that UProceduralMeshComponent::GetPhysicsTriMeshData() doesn’t seem to pass in any UV information…

We were about to work out a way of doing something similar, by way of interrogating closest primitive to HitLocation… (yikes)

Huh, that’s really neat. I’m trying to get my head around what this could be used for, I’m sure there are a lot of cool things but I’m having trouble connecting the dots :smiley:

Above changes giving build error in UE 4.10.1…can you give more information on were to add the above code in UE 4.10.1…

sorry for the semi-necro thread

Rama: any news on fixing the issues that prevent the pull request from Epic?

Hey Rama,

I’ve made the changes to a 4.11 source build following the guide on your Wiki. Everything builds correctly, and I’m getting UV results returned on my line traces. However, they don’t seem to tally up with what I’d expect. Specifically, the values I’m getting don’t make a lot of sense (its like the entire mesh is divided into a grid, and the values being returned go up and down as you move across the surface). The video below shows the results I’m getting (ignore the music, if there is anything, I was listening to the Dead Space soundtrack at the time…)

This has been tested with both Unreal engine assets and confirmed correctly UV’d planes straight out of Maya. Any ideas what’d be causing this? I’m guessing its something to do with *where *I’ve put the extra code lines in BodyInstance.cpp - the line numbers supplied didn’t quite match up with the actual .cpp so I had to use my best (limited) judgement.

https://youtube.com/watch?v=l8NUAJMTtT8

Have you taken into account the tiling? Just a random guess.

If I wanted to create say, an ink splotch at the location of my trace, but my trace hit a UV seam, it’d be wrong, right? I’d have to make sure my meshes have no seams on my “splotchable” areas?

That would make sense. However I was under the impression that the PxHit was taking its UVs from the geometry, as opposed to the shader. But now that you say it, those values definitely look like they’re tiling…

EDIT: I’ve tested this on meshes with non-tiling textures, and the behavior is still the same.

Hi

I am also looking into a solution on how to get the UV coordinates out of the ray cast result.

According to the PhysX documentation:
http://docs.nvidia.com/gameworks/content/gameworkslibrary/physx/apireference/files/structPxRaycastHit.html

the UV coordinates stored in PxRaycastHit are the barycentric coordinates of the hit location, not the UV of the mesh. You can use these barycentric coordinates to interpolate the UV coordinates of the triangle vertices.

As far as I know if you cook your game the UV coordinates of static meshes are only available inside the GPU Buffers, not in the static mesh object anymore.
Therefore if you want to get the UV of the mesh at the hit location you need to get the mesh data and use the barycentric coordinates of the HIT location to compute the UV coords from the vertices of triangle you have hit.
There is a boolean in UStaticMesh that can be set to keep the UV coordinates in the cooked build, but I haven’t tried this yet and I think this boolean is not exposed as public.
An alternative attempt to keep the UV without rebuilding the engine could be to create a data asset that just copies the content of the static mesh into a dedicated data structure in its OnPropertyChange (exact naming?) callback.

Well that sucks.

Is there no way to sample a pixel value from the UV buffer?