Search Unity

Question Volumetric Clouds Get Density At Point on CPU

Discussion in 'High Definition Render Pipeline' started by gguttilla, Jul 5, 2023.

  1. gguttilla

    gguttilla

    Joined:
    Apr 1, 2017
    Posts:
    3
    Does anyone know of a way to sample the density of HDRP's built in volumetric clouds at a certain point in space? I need to get this information on the CPU for gameplay-related calculations, as I need to know if a player is sufficiently in a cloud or not.

    The easiest way to do this would probably be to just have a c# method that, given the parameters for the active volumetric clouds volume override can perform the exact same calculation as is performed in a single step of the raymarching shader and return that value. However, I have no idea how I can reverse engineer this as Unity's implementation is scattered across multiple files and I cannot even find the source code.

    Any suggestions? Much thanks!
     
    chap-unity likes this.
  2. chap-unity

    chap-unity

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    764
    Hey, sorry for the delay.
    Currently there's no API for that but it is indeed a very valid request, it's being tracked internally and we'll see what's possible. We'll keep you updated here.

    In the meantime, when using the volumetric clouds in avanced or custom mode (where you need to author a texture). You could find a way by trial and error how large is a texel in World Space and sample your cloud texture acorrdingly.
    I assume that's how they did this for example.
     
  3. adrien-de-tocqueville

    adrien-de-tocqueville

    Unity Technologies

    Joined:
    Mar 16, 2020
    Posts:
    266
    So as remi said there is no existing API to get that information, and unfortunately it seems too game specific so we can't provide a generic system to get that information on CPU.

    The suggested solution currently would be to run a ComputeShader written manually that would evaluate the clouds at a given position and output the value in a graphicsbuffer. The buffer could then be readback on CPU with a few frames delay.

    Warning: this is not straightforward to do and requires some knowledge of HLSL
    To get that working properly, you'll have to bind the clouds texture and constant buffers to your compute shader, so you'll probably have to modify hdrp code. Then you can call the function EvaluateCloud which computes the density.
    You can find example of code doing that in TraceVolumetricRay inside VolumetricCloudsUtilities.hlsl
     
    chap-unity and DevDunk like this.
  4. gms9rc

    gms9rc

    Joined:
    Apr 27, 2019
    Posts:
    4
    @adrien-de-tocqueville Is this feature ever expected to be implemented? It would be useful for me too, and I have seen other people requesting it elsewhere.
     
  5. adrien-de-tocqueville

    adrien-de-tocqueville

    Unity Technologies

    Joined:
    Mar 16, 2020
    Posts:
    266
    Hey, unfortunately i don't think this is coming anytime soon. An alternative solution i can provide that is maybe easier depending on the needs is to copy the content of EvaluateCloudProperties inside VolumetricCloudsUtilities.hlsl in a c# script and evaluate in on CPU. This is not gonna work in modes that sample textures (expect if you replicate those textures on CPU), but in simple modes it shouldn't require reading textures so it should be relatively easy to do
     
    chap-unity likes this.
  6. gms9rc

    gms9rc

    Joined:
    Apr 27, 2019
    Posts:
    4
    thank you for the response