LYGIA Shader Library

signedDistance (lygia/geometry/triangle/signedDistance)

Returns the signed distance from the surface of a triangle to a point

Dependencies:

Use:

<vec3> closestDistance(<Triangle> tri, <vec3> _pos)

Check it on Github



#ifndef FNC_TRIANGLE_SIGNED_DISTANCE
#define FNC_TRIANGLE_SIGNED_DISTANCE

float signedDistance(Triangle _tri, vec3 _triNormal, vec3 _p) {
    vec3 nearest = closestPoint(_tri, _triNormal, _p);
    vec3 delta = _p - nearest;
    float distance = length(delta);
    distance *= sign( dot(delta/distance, _triNormal) );
    return distance;
}

float signedDistance(Triangle _tri, vec3 _p) { return signedDistance(_tri, normal(_tri), _p); }

#endif

Dependencies:

Use:

<float3> closestDistance(<Triangle> tri, <float3> _pos)

Check it on Github



#ifndef FNC_TRIANGLE_SIGNED_DISTANCE
#define FNC_TRIANGLE_SIGNED_DISTANCE

float signedDistance(Triangle _tri, float3 _triNormal, float3 _p) {
    float3 nearest = closestPoint(_tri, _triNormal, _p);
    float3 delta = _p - nearest;
    float distance = length(delta);
    distance *= sign( dot(delta/distance, _triNormal) );
    return distance;
}

float signedDistance(Triangle _tri, float3 _p) { return signedDistance(_tri, normal(_tri), _p); }

#endif

Dependencies:

Use:

<float3> closestDistance(<Triangle> tri, <float3> _pos)

Check it on Github



#ifndef FNC_TRIANGLE_SIGNED_DISTANCE
#define FNC_TRIANGLE_SIGNED_DISTANCE

inline __host__ __device__ float signedDistance(const Triangle& _tri, const float3& _triNormal, const float3& _p) {
    float3 nearest = closestPoint(_tri, _triNormal, _p);
    float3 delta = _p - nearest;
    float distance = length(delta);
    distance *= sign( dot(delta/distance, _triNormal) );
    return distance;
}

inline __host__ __device__ float signedDistance(const Triangle& _tri, const float3& _p) { return signedDistance(_tri, normal(_tri), _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