Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Does anyone know how to create an AABB for and SDF (Signed Distance field - procedural)

Discussion in 'Scripting' started by dave_g_69, Apr 3, 2023.

  1. dave_g_69

    dave_g_69

    Joined:
    Jul 24, 2021
    Posts:
    21
    Hi everyone, I am using my own raymarcher, done in compute, with procedurally generated distance fields and was wondering if there was an easy way to create an AABB for groups of SDF's or as individual components. being that I was hoping to also include dynamic as SDF's are so robust.

    so as a simple example if I use:
    Code (CSharp):
    1. //**********************************************************
    2. // SPHERE
    3. //**********************************************************
    4. float iSphere(float3 p, float r)
    5. {
    6.     //------------------------------------------------------
    7.     return length(p) - r;
    8.     //------------------------------------------------------
    9. }
    obviously can become more complex once adding unions of shapes, but how would I create a bounding box, as it really doesn't exist except when it is being tested against with a ray march. doesn't need to be created in compute, so a c# example is fine. Can think of some ways to do it, but I think I am overthinking and not happy with my complex thoughts about it.

    thanks in advance!
     
  2. DouglasPotesta

    DouglasPotesta

    Joined:
    Nov 6, 2014
    Posts:
    108
    You can think of an AABB as an intersection with your map.
    Max(AABB(),Map());
    In the above the AABB and Map will be evaluated (no performance improvement).
    Since you know that the AABB is going to be the more efficient test you can early out from it.
    float aabb = AABB();
    If (aabb < 0.01)
    return Map();
    else
    return aabb;

    A good process for working through the problem is start off with a blank AABB function that simply returns the same thing as your map.
    Then you can start to think of your AABB function as a simplified version of your map function.
    Now for every shape you added to your ABBB function determine the maximum point and minimum point (does not have to be a perfectly tight fit you can start with liberally large spheres). The algorithm for AABB building is simple you calculate the maximum and minimum points of your collection. In the case of a sphere you can do center + (radius, radius, radius) for the max and center - (radius, radius, radius).
    Once you have the simple beginnings it becomes easier to optimize and explore ideas that are better catered for your implementation.
     
    dave_g_69 likes this.
  3. dave_g_69

    dave_g_69

    Joined:
    Jul 24, 2021
    Posts:
    21
    Ah yes, brilliant, thank you so much for taking the time to give me a starting point. So much simpler than what I was originally thinking, glad I asked before diving in).
     
    DouglasPotesta likes this.