LYGIA Shader Library

intersect (lygia/v1.1.6/geometry/triangle/intersect)

based on https://www.iquilezles.org/www/articles/intersectors/intersectors.htm

Dependencies:

Use:

<float> intersect(<Triangle> tri, <vec3> rayOrigin, <vec3> rayDir, out <vec3> intersectionPoint)

Check it on Github



#ifndef FNC_TRIANGLE_INTERSECT
#define FNC_TRIANGLE_INTERSECT

float intersect(Triangle _tri, vec3 _rayOrigin, vec3 _rayDir, inout vec3 _point) {
    vec3 v1v0 = _tri.b - _tri.a;
    vec3 v2v0 = _tri.c - _tri.a;
    vec3 rov0 = _rayOrigin - _tri.a;
    _point = cross(v1v0, v2v0);
    vec3 q = cross(rov0, _rayDir);
    float d = 1.0f / dot(_rayDir, _point);
    float u = d * -dot(q, v2v0);
    float v = d *  dot(q, v1v0);
    float t = d * -dot(_point, rov0);
    if (u < 0.0f || u > 1.0f || v < 0.0f || (u+v) > 1.0f || t < 0.0f)
        t = 9999999.9f; // No intersection

    return t;
}

float intersect(Triangle _tri, Ray _ray, inout vec3 _point) { return intersect(_tri, _ray.origin, _ray.direction, _point); }
float intersect(Triangle _tri, Ray _ray) { 
    vec3 p;
    return intersect(_tri, _ray, p); 
}

#endif

Dependencies:

Use:

<float> intersect(<Triangle> tri, <float3> rayOrigin, <float3> rayDir, out <float3> intersectionPoint)

Check it on Github



#ifndef FNC_TRIANGLE_INTERSECT
#define FNC_TRIANGLE_INTERSECT

float intersect(Triangle _tri, float3 _rayOrigin, float3 _rayDir, inout float3 _point) {
    float3 v1v0 = _tri.b - _tri.a;
    float3 v2v0 = _tri.c - _tri.a;
    float3 rov0 = _rayOrigin - _tri.a;
    _point = cross(v1v0, v2v0);
    float3 q = cross(rov0, _rayDir);
    float d = 1.0f / dot(_rayDir, _point);
    float u = d * -dot(q, v2v0);
    float v = d *  dot(q, v1v0);
    float t = d * -dot(_point, rov0);
    if (u < 0.0f || u > 1.0f || v < 0.0f || (u+v) > 1.0f || t < 0.0f)
        t = 9999999.9f; // No intersection

    return t;
}

float intersect(Triangle _tri, Ray _ray, inout float3 _point) { return intersect(_tri, _ray.origin, _ray.direction, _point); }
float intersect(Triangle _tri, Ray _ray) { 
    float3 p;
    return intersect(_tri, _ray, p); 
}

#endif

Dependencies:

Use:

<float> intersect(<Triangle> tri, <float3> rayOrigin, <float3> rayDir, out <float3> intersectionPoint)

Check it on Github



#ifndef FNC_TRIANGLE_INTERSECT
#define FNC_TRIANGLE_INTERSECT

inline __host__ __device__ float intersect(const Triangle& _tri, const float3& _rayOrigin, const float3& _rayDir, float3& _point) {
    float3 v1v0 = _tri.b - _tri.a;
    float3 v2v0 = _tri.c - _tri.a;
    float3 rov0 = _rayOrigin - _tri.a;
    _point = cross(v1v0, v2v0);
    float3 q = cross(rov0, _rayDir);
    float d = 1.0f / dot(_rayDir, _point);
    float u = d * -dot(q, v2v0);
    float v = d *  dot(q, v1v0);
    float t = d * -dot(_point, rov0);
    if (u < 0.0f || u > 1.0f || v < 0.0f || (u+v) > 1.0f || t < 0.0f)
        t = MAX_FLOAT_VALUE; // No intersection

    return t;
}

inline __host__ __device__ float intersect(const Triangle& _tri, const Ray& _ray, float3& _point) { return intersect(_tri, _ray.origin, _ray.direction, _point); }
inline __host__ __device__ float intersect(const Triangle& _tri, const Ray& _ray) { 
    float3 p;
    return intersect(_tri, _ray, p); 
}

#endif

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