About
GPU Raycast for person atoms gives you advanced hit info like texture coordinates, person texture regions, and normals. It exposes an API to get the information either as a HitInfo struct like Unity's physics raycast, or raycast an area to a texture (e.g. for use in a compute shader).How To
The easiest way to raycast is through the GpuRaycastApi class. Add these lines to your cslist to use it in your plugin (replace the version accordingly):
Code:
perfectbloo.GpuRaycast.<version>:/Custom/Scripts/perfectbloo/GpuRaycast/Source/GpuRaycast/GpuRaycastApi.g.cs
perfectbloo.GpuRaycast.<version>:/Custom/Scripts/perfectbloo/GpuRaycast/Source/GpuRaycast/GpuRaycastHit.cs
To raycast and get a hit info struct simply use the Raycast function. For advanced usage checkout the "Painter" class from my Skin Paint plugin, which raycasts using RaycastToTexture, asynchronously reads the data on the CPU, and uses the raycast result in a compute shader.
Using the plugin
It's best to add the GPU Raycast scene plugin programmatically, so users of your plugin don't need to worry about it. Either implement your own solution, or use the ScenePlugin class from my Core code library like so:
C#:
using perfectbloo.Core;
using perfectbloo.GpuRaycast;
public class YourScript : MVRScript
{
private ScenePlugin<GpuRaycastApi> gpuRaycast = new ScenePlugin<GpuRaycastApi>(
GpuRaycastApi.Author,
GpuRaycastApi.PackageName,
GpuRaycastApi.PluginName,
GpuRaycastApi.ScriptName
);
private void YourFunction()
{
StartCoroutine(YourRaycast());
}
private IEnumerator YourRaycast()
{
yield return new WaitForEndOfFrame();
if (gpuRaycast.Api?.Raycast(...) == true) { [...] }
}
}
If you're using the ScenePlugin class, don't forget to add the required files from Core to your cslist (replace the version accordingly):
Code:
perfectbloo.Core.<version>:/Custom/Scripts/perfectbloo/Core/Source/Core/Extensions/AtomEx.cs
perfectbloo.Core.<version>:/Custom/Scripts/perfectbloo/Core/Source/Core/Extensions/MVRScriptEx/Ui/UiElement.cs
perfectbloo.Core.<version>:/Custom/Scripts/perfectbloo/Core/Source/Core/Extensions/SuperControllerEx.cs
perfectbloo.Core.<version>:/Custom/Scripts/perfectbloo/Core/Source/Core/Plugins/UserConfirmPanel.cs
perfectbloo.Core.<version>:/Custom/Scripts/perfectbloo/Core/Source/Core/ScenePlugin.cs
API
C#:
bool Raycast(
Vector3 origin,
Vector3 direction,
out GpuRaycastHit hit,
float maxDistance = 1000f,
int layerMask = 1,
float alphaThreshold = 0.01f);
bool RaycastToTexture(
Vector3 origin,
Vector3 direction,
RenderTexture rtNormalDepth, // rgb: Normal | a: Depth (1: origin, 0: maxDistance)
RenderTexture rtTextureCoordId, // rg: TextureCoords | b: Material ID | a: unused
float orthographicSize = 0.01f,
float maxDistance = 1000f,
int layerMask = 1,
float alphaThreshold = 0.01f);
RenderTexture CreateNormalDepthTexture(int size = 1); // Use as input for RaycastToTexture
RenderTexture CreateTextureCoordIdTexture(int size = 1); // Use as input for RaycastToTexture
Texture2D CreateReadBackTexture(int size = 1); // Use to read RaycastToTexture results
// Use to get atom and region from RaycastToTexture results
bool TryGetAtomAndRegion(
int materialId,
out Atom atom,
out DAZCharacterTextureControl.Region region);
float EncodeMaterialId(int id);
int DecodeMaterialId(float id);
struct GpuRaycastHit
{
Vector3 Point { get; set; }
Vector3 Normal { get; set; }
Vector2 TextureCoord { get; set; }
float Distance { get; set; }
Atom Atom { get; set; }
DAZCharacterTextureControl.Region Region { get; set; }
}
Limitations
- raycasts only work for person atoms, and only on bare skin
- clothing blocks raycasts, but alpha masks are respected (adjust via alpha threshold parameter)
- hair scalps blocks raycasts
- hair is ignored, meaning raycasts will hit skin behind it
- Slut show - Your personal Exhibition Hall by supperGR (CC BY-NC-SA)
- Sabrina by willmason (CC BY)
- Skin 6 by Riddler (CC BY)
- [Hairstyle] Ear hook bob by VaM_LO (CC BY)
- Enhanced Eyes by Hunting-Succubus (CC BY-NC)
- Pet Furry Kitty Accessory Set #neko-girl by VRDollz (CC BY-NC-ND)
- Blouse & Skirt Set004P by YameteOuji (CC BY)
- Blouse & Skirt Set006 by YameteOuji (CC BY)
- BootsA by YameteOuji (CC BY)
- Shimapan Technology by YameteOuji (CC BY)
- asco's Facial Expression Morph Pack by ascorad (CC BY)
- VAM Lightsabers by hazmhox (CC BY-SA)