• 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]
I am working on creating a scene where an empty atom can cast a ray and print out "hit" if hit a person but had no luck at all and i am not sure what is going on. I have loaded the gpuraycast.cslist as scene plugin and set up the path to GpuRaycastHit.cs and GpuRaycastApi.g.cs, but all i am getting when casting the ray (thru button trigger) is

GPU Raycast Exception: Object reference not set to an instance of an object
Stack: at perfectbloo.GpuRaycast.GpuRaycastApi.Raycast (Vector3 origin, Vector3 direction, perfectbloo.GpuRaycast.GpuRaycastHit& hit, Single maxDistance, Int32 layerMask) [0x00000] in <filename unknown>:0
at GunRaycastPlugin.Shoot () [0x00000] in <filename unknown>:0

below is my code, appreciate if anyone with experience can share some light...thx
 
The problem is this line:

C#:
_api = new GpuRaycastApi(this);

You must pass the GpuRaycast plugin here, not your own plugin. Below is how you can do using my Core plugin, otherwise you need to implement your own solution to get the GpuRaycast instance from the scene plugins.

Add these to your cslist:
Code:
perfectbloo.Core.1:/Custom/Scripts/perfectbloo/Core/Source/Core/Extensions/AtomEx.cs
perfectbloo.Core.1:/Custom/Scripts/perfectbloo/Core/Source/Core/Extensions/MVRScriptEx/Ui/UiElement.cs
perfectbloo.Core.1:/Custom/Scripts/perfectbloo/Core/Source/Core/Extensions/SuperControllerEx.cs
perfectbloo.Core.1:/Custom/Scripts/perfectbloo/Core/Source/Core/Plugins/UserConfirmPanel.cs
perfectbloo.Core.1:/Custom/Scripts/perfectbloo/Core/Source/Core/ScenePlugin.cs

Then you can raycast like:
C#:
using perfectbloo.Core;
using perfectbloo.GpuRaycast;

[...]

private ScenePlugin<GpuRaycastApi> gpuRaycast = new ScenePlugin<GpuRaycastApi>(
    GpuRaycastApi.Author,
    GpuRaycastApi.PackageName,
    GpuRaycastApi.PluginName,
    GpuRaycastApi.ScriptName
);

[...]

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

When you access gpuRaycast.Api, this will also automatically add GpuRaycast as a scene plugin if it's missing. You can change that behavior by setting gpuRaycast.AutoCreate = false (it is enabled by default).
 
Last edited:
@PiggyCat FYI the suggestion from earlier will fix the exception, but there might still be issues.

First off, I just released an update to fix some bugs. The shader I use to perform raycasts did not render non-person atoms correctly, which meant they wouldn't occlude persons. Also, I used the wrong layer mask as a default, and I'm seeing you're trying to raycast on layer 29, too. But actually all atoms are rendered on the default layer (so layer mask = 1). I just got lucky because I forgot to bit shift, so I did not notice anything wrong 😅 The default of the Raycast function is now set correctly, and you shouldn't even need to pass a layer mask yourself.

Secondly, I forgot to mention an important detail in the resource description:
VaM uses custom vertex and normal buffers to render person atoms. If you try to raycast before these are updated, your raycast will fail since the persons are not rendered correctly. The safest place to raycast is in a Coroutine after yielding WaitForEndOfFrame.

So you'll need to do something like this:
C#:
private void YourFunction()
{
    StartCoroutine(YourRaycast());
}

private IEnumerator YourRaycast()
{
    yield return new WaitForEndOfFrame();
    [...]
    if (gpuRaycast.Api?.Raycast(...) == true) { [...] }
}
 
@PiggyCat FYI the suggestion from earlier will fix the exception, but there might still be issues.

First off, I just released an update to fix some bugs. The shader I use to perform raycasts did not render non-person atoms correctly, which meant they wouldn't occlude persons. Also, I used the wrong layer mask as a default, and I'm seeing you're trying to raycast on layer 29, too. But actually all atoms are rendered on the default layer (so layer mask = 1). I just got lucky because I forgot to bit shift, so I did not notice anything wrong 😅 The default of the Raycast function is now set correctly, and you shouldn't even need to pass a layer mask yourself.

Secondly, I forgot to mention an important detail in the resource description:


So you'll need to do something like this:
C#:
private void YourFunction()
{
    StartCoroutine(YourRaycast());
}

private IEnumerator YourRaycast()
{
    yield return new WaitForEndOfFrame();
    [...]
    if (gpuRaycast.Api?.Raycast(...) == true) { [...] }
}

Hey perfectbloo, thanks for the update.

I did notice you used a code routine so I did something similar, updated the path referencing version 2 of the raycast plugins, also used
layerMask 1 as well.
However I am still trying to fix something here because the ray cast seems unable to hit person atom.
I rendered a yellow beam to visualize the ray cast and I saw the beam clearly penetrated the person, so the aiming should be correct, but still the raycast return false. I also tried removing clothing/hair as well. Right now still trying to figure out what is going on.
I have attach my code below in case anyone is interested.
 
Hmm weird, your code works fine for me. You can see in the video below, the raycast hits on skin, and misses on clothing as expected.

My guess is, there's either something in your scene blocking the raycast, or there's some issue with your person atom appearance. So here's two things you can try:
  1. Try to raycast the default person atom (no custom appearance) in your scene.
  2. Try to raycast your appearance preset in an empty scene.
Like that we can maybe narrow down the issue.

 
Hmm weird, your code works fine for me. You can see in the video below, the raycast hits on skin, and misses on clothing as expected.

My guess is, there's either something in your scene blocking the raycast, or there's some issue with your person atom appearance. So here's two things you can try:
  1. Try to raycast the default person atom (no custom appearance) in your scene.
  2. Try to raycast your appearance preset in an empty scene.
Like that we can maybe narrow down the issue.

View attachment 570102

Hi I have figured out this issue

1. You must have textures on your person ( Duh! yeah I didn't. trying to save some memory lol)
2. Raycast conflicts with SubsurfaceScattering plugin from Hunting Succubus, if I have Subsurface Scattering active then the ray cast would fly through the person
 
Last edited:
Working on a

GPU Raycast–based shooting plugin demonstrated alongside the third-person WASD plugin. The ray cast applies physical force when a Person atom is hit and optionally triggers a hit-reaction animation.
 
Working on a GPU Raycast–based shooting plugin demonstrated alongside the third-person WASD plugin. The ray cast applies physical force when a Person atom is hit and optionally triggers a hit-reaction animation.
Oh, cool stuff! Glad to see you've sorted out all the issues. Are you planning to make this basically into a game?

Just make sure you're not violating any hub content policies with shooting sexy zombie girls haha 😅
 
Back
Top Bottom