Page 1 of 1

Hit location on renderable model

Posted: Thu Dec 05, 2013 12:19 am
by sergiusz308
Norbo, I have a physics object and valid raycastresult - how to "map" hit location from physics object to renderable model (to draw a decal there, for example)? I assume that there are many ways to do so, but what you would recomend? I red that Crytec has physics proxies slightly larger than display models - is that to aid such mapping?

Re: Hit location on renderable model

Posted: Thu Dec 05, 2013 1:18 am
by Norbo
I haven't seen anything from Crytek specifically about how they do decals, but one fairly simple way is to draw a decal volume at the physical intersection. Regular old projective decals would probably do the trick in most cases. Volumetric decals could also be handy depending on the geometry and effects you want to apply.

When rendered onto a deforming mesh, the usual projective approaches can morph around a little bit, since the decal isn't in the deforming texture space. Parenting the projection to all of the relevant animation bones should mostly take care of it.

If you wanted to introduce a bit more preprocessing, you could create an explicit mapping from physical impact volumes to model impact volumes. For example, a voxelized mapping, or a graphical raycasting acceleration structure- you can dive directly into texture space and do fancy stuff there. (This is probably more work than it's worth in comparison to simple projective decals if you aren't already doing some graphical raytracing.)

Edit:
All this does assume that the physics objects are at least a semi-reasonable approximation for the graphics. Trying to use projective decals based on impact points on the CharacterController cylinder probably wouldn't turn out so hot. At that point, if accurate decals are really desired, a graphically-dedicated tight fit would be a good idea. Depending on the number of triangles, you might even be able to get away with just brute force raytesting the graphical model without an acceleration structure to find the graphical hit point.

Re: Hit location on renderable model

Posted: Thu Dec 05, 2013 9:21 am
by sergiusz308
Norbo wrote: When rendered onto a deforming mesh, the usual projective approaches can morph around a little bit, since the decal isn't in the deforming texture space. Parenting the projection to all of the relevant animation bones should mostly take care of it.
By "parenting" you mean transform decal by related bone's transformation?
Norbo wrote: At that point, if accurate decals are really desired, a graphically-dedicated tight fit would be a good idea. Depending on the number of triangles, you might even be able to get away with just brute force raytesting the graphical model without an acceleration structure to find the graphical hit point.
I thought about something like this:
- use bepu objects to detect hit, i.e. CharacterController detect that "arm" cylinder was hit
- based on that use graphical mesh to brute-force find particular triangles where decal should be placed

One question here: knowing location and normal of the hit result and ray direction how can I find an aprox. impact point on graphical mesh that "wraps" physics object? Is there any tricky math I can employ or the only option is just to iterate through graphical mesh triangles and to find ray intersection? You mentioned about various options for acceleration structures but I'd like to avoid using them at this moment - just how far I can go using just bepu objects and graphical mesh?

Re: Hit location on renderable model

Posted: Thu Dec 05, 2013 6:56 pm
by Norbo
By "parenting" you mean transform decal by related bone's transformation?
Yes, make the projection follow the bone(s).
One question here: knowing location and normal of the hit result and ray direction how can I find an aprox. impact point on graphical mesh that "wraps" physics object? Is there any tricky math I can employ or the only option is just to iterate through graphical mesh triangles and to find ray intersection?
When using projective decals, a reasonably tight ray proxy (at the level of having a forearm shape, for example) would avoid the need for finding the impact point on the graphical model.

If you truly want impact points on the graphical model based on physical hit points, some type of mapping is required. Marching over triangles and finding a graphical intersection from the physical intersection is a brute force runtime implementation of such a mapping, but there are lots of others. Off the top of my head, here's another example:

The model has a decal-dedicated UV map. It is subdivided into different regions, one for each bone. Each region represents the output space of a cylindrical projection. Vertices are placed in the UV map such that they sample from the bone specific decal areas appropriately (e.g. a forearm vertex will sample from the appropriate spot in the forearm decal region). This mapping could be mostly automated in the content pipeline.

Assume a hit is found on a bone proxy. Apply a cylindrical projection to the impact point on the bone proxy. Look up the appropriate region in the decal UV texture based on the bone. Draw the decal onto the character's decal texture.

When rendering, sample the decal texture the same way you'd sample any other texture.

The above could be improved a little bit depending on the specific use- there's no fundamental requirement that there exist a dedicated texture and uv map, but it could make content development easier.