• Hi Guest!

    We are extremely excited to announce the release of our first Beta1.1 and the first release of our Public AddonKit!
    To participate in the Beta, a subscription to the Entertainer or Creator Tier is required. For access to the Public AddonKit you must be a Creator tier member. Once subscribed, download instructions can be found here.

    Click here for information and guides regarding the VaM2 beta. Join our Discord server for more announcements and community discussion about VaM2.
  • Hi Guest!

    VaM2 Resource Categories have now been added to the Hub! For information on posting VaM2 resources and details about VaM2 related changes to our Community Forums, please see our official announcement here.
GPU Raycast

Plugins + Scripts GPU Raycast

Download [<1 MB]
GPU Raycast Header

Hearts Icon
Built something awesome? I'd love to know!
Leave a like, and tell me in your review how this is helpful or could be improved!
Info Sign
API Only Plugin
This is an API for plugin developers, and not meant to be used as a standalone plugin in your scenes.

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).
Info Sign
Why not simply use Unity's physics raycast?
Unity's physics raycast will work fine in most cases. However, it requires a MeshCollider (which person atoms don't have) on the target to provide texture coordinates in the hit info.

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
Warning Sign
Packaging your plugin
VaM resolves paths to other packages in cslist files just fine when loading plugins. However, when you try to build your package with VaM's package builder, there will be broken references and no dependency to GPU Raycast will be added in your package meta file. Either modify the "meta.json" file inside the var afterwards (add dependency to "perfectbloo.GpuRaycast.<version>"; remove broken reference reports), or manually create the meta file to begin with and zip your package by hand (which is quite manageable for script only packages).
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.

Setting up the plugin for the user​

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#:
private ScenePlugin<GpuRaycastApi> GpuRaycast = new ScenePlugin<GpuRaycastApi>(
    GpuRaycastApi.Author,
    GpuRaycastApi.PackageName,
    GpuRaycastApi.PluginName,
    GpuRaycastApi.ScriptName
);

[...]

if (GpuRaycast.Api?.Raycast(...) == true) { [...] }

API​

C#:
bool Raycast(
    Vector3 origin,
    Vector3 direction,
    out GpuRaycastHit hit,
    float maxDistance = 1000f,
    int layerMask = 29);

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 = 29);

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​

GPU Raycast only works with person atoms, and only on skin. Clothing and hair will block raycasts.

React to this content...

Share this resource

More resources from perfectbloo

Credits

Creator Support Link
perfectbloo perfectbloo
Back
Top Bottom