LYGIA Shader Library

intersect (lygia/geometry/aabb/intersect)

compute the near and far intersections of the cube (stored in the x and y components) using the slab method no intersection means vec.x > vec.y (really tNear > tFar) https://gist.github.com/DomNomNom/46bb1ce47f68d255fd5d

Dependencies:

Use:

<vec2> intersect(<AABB> box, <vec3> rayOrigin, <vec3> rayDir)

Check it on Github



#ifndef FNC_AABB_INTERSECT
#define FNC_AABB_INTERSECT

vec2 intersect(const in AABB box, const in vec3 rayOrigin, const in vec3 rayDir) {
    vec3 tMin = (box.min - rayOrigin) / rayDir;
    vec3 tMax = (box.max - rayOrigin) / rayDir;
    vec3 t1 = min(tMin, tMax);
    vec3 t2 = max(tMin, tMax);
    float tNear = max(max(t1.x, t1.y), t1.z);
    float tFar = min(min(t2.x, t2.y), t2.z);
    return vec2(tNear, tFar);
}

vec2 intersect(const in AABB box, const in Ray ray) {
    vec3 tMin = (box.min - ray.origin) / ray.direction;
    vec3 tMax = (box.max - ray.origin) / ray.direction;
    vec3 t1 = min(tMin, tMax);
    vec3 t2 = max(tMin, tMax);
    float tNear = max(max(t1.x, t1.y), t1.z);
    float tFar = min(min(t2.x, t2.y), t2.z);
    return vec2(tNear, tFar);
}

#endif

Dependencies:

Use:

<float2> intersect(<AABB> box, <float3> rayOrigin, <float3> rayDir)

Check it on Github



#ifndef FNC_AABB_INTERSECT
#define FNC_AABB_INTERSECT

float2 intersect(const in AABB box, const in float3 rayOrigin, const in float3 rayDir) {
    float3 tMin = (box.min - rayOrigin) / rayDir;
    float3 tMax = (box.max - rayOrigin) / rayDir;
    float3 t1 = min(tMin, tMax);
    float3 t2 = max(tMin, tMax);
    float tNear = max(max(t1.x, t1.y), t1.z);
    float tFar = min(min(t2.x, t2.y), t2.z);
    return float2(tNear, tFar);
}

float2 intersect(const in AABB box, const in Ray ray) {
    float3 tMin = (box.min - ray.origin) / ray.direction;
    float3 tMax = (box.max - ray.origin) / ray.direction;
    float3 t1 = min(tMin, tMax);
    float3 t2 = max(tMin, tMax);
    float tNear = max(max(t1.x, t1.y), t1.z);
    float tFar = min(min(t2.x, t2.y), t2.z);
    return float2(tNear, tFar);
}

#endif

Dependencies:

Use:

<float2> intersect(<AABB> box, <float3> rayOrigin, <float3> rayDir)

Check it on Github



#ifndef FNC_AABB_INTERSECT
#define FNC_AABB_INTERSECT

inline __host__ __device__ float2 intersect(const AABB& _box, const float3& _rayOrigin, const float3& _rayDir) {
    float3 tMin = (_box.min - _rayOrigin) / _rayDir;
    float3 tMax = (_box.max - _rayOrigin) / _rayDir;
    float3 t1 = min(tMin, tMax);
    float3 t2 = max(tMin, tMax);
    float tNear = max(max(t1.x, t1.y), t1.z);
    float tFar = min(min(t2.x, t2.y), t2.z);
    return make_float2(tNear, tFar);
}

inline __host__ __device__ float2 intersect(const AABB& _box, const Ray& _ray) { return intersect(_box, _ray.origin, _ray.direction); }

#endif

License

LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.

Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).

It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository