Getting RGBA value of a particular voxel "colliding" with a Game Object

Hi everyone,

I am trying to get the RGBA value of a particular voxel that my probe touches or crosses in the air.

Since it is being generated by a shader, I am failing at using this:

void OnCollisionEnter(Collision other)
    {
        Debug.Log(other.gameObject.GetComponent<Renderer>().material.color); // this line will tell you the object's color which collide to the gameObject you attached this script to
    }

Here is a video of my attempt:

I have attached the shader that is generating the volume rendering I would like to probe.

Thank you for your help!

8007173–1030082–DirectVolumeRenderingShader.shader (12.5 KB)

random ideas:
could pass probe position into shader, and then sample that position from 3d texture,
and get values back with appendbuffer or something similar.

or if can somehow calculate local position for probe (in that “3d chunk”) and do getpixel (x,y,z) there…maybe from bounds can interpolate it.

1 Like

Thank you! Do you think that I should do it with CollisionEnter or CollisionStay? I would like to probe it as often as possible as it’s for haptics, which should run at 1kHz, but I guess FixedUpdate wouldn’t have the position info, right?

could add box collider on that cube area, to get overall collision on the bounds,
but it won’t give per voxel hit position, need to calculate the position and get value manually,
so at that point could just do it inside FixedUpdate().

i’m thinking something like this,
if you know that volumetric gameobject bounds, and if its aligned bounding box then easier, then interpolate to get probe position in X,Y,Z 3d texture coordinates

1 Like

Thank

Thank you again

I managed to pass in my probe position by adding a script to the same GameObject that has the shader

material.SetVector("_probePosition", probe.transform.position);

Inside the shader, I declared this Property:

_probePosition("Probe Position", Vector) = (0, 0, 0, 1)

I thought about declaring another property to hold the voxel color, but I don’t understand the shader lifecycle and how to make it update the property’s value so that I can access it with my script, so these didn’t work:

_voxelDensity("Voxel Density", Float) = 0.0
...
_voxelDensity = getDensity(_probePosition);

Do you know how to invoke an update on this method call to update _voxelDensity when _probePosition changes? Thank you!

this is whats happening:

so cannot directly read values from shader.

some options

also need to check if that passed position is correct, local space vs worldspace. (you could debug it by setting some vertex or pixel colors based on distance to that passed position)

i’d also explore the other way around, reading directly from 3d texture, by mapping the probe position into local coordinates inside bounds. (no need shader work there)

Thank you again, I will try those options!