LYGIA Shader Library

opUnion (lygia/sdf/opUnion)

Union operation of two SDFs

Dependencies:

Use:

<float> opUnion( in <float|vec4> d1, in <float|vec4> d2 [, <float> smooth_factor] )

Check it on Github



#ifndef FNC_OPUNION
#define FNC_OPUNION

float opUnion( float d1, float d2 ) { return min(d1, d2); }
vec4  opUnion( vec4 d1, vec4 d2 ) { return (d1.a < d2.a) ? d1 : d2; }

// Soft union
float opUnion( float d1, float d2, float k ) {
    float h = saturate( 0.5 + 0.5*(d2-d1)/k );
    return mix( d2, d1, h ) - k*h*(1.0-h); 
}

vec4 opUnion( vec4 d1, vec4 d2, float k ) {
    float h = saturate( 0.5 + 0.5*(d2.a - d1.a)/k );
    return mix( d2, d1, h ) - k*h*(1.0-h); 
}

#endif

Use:

<float> opUnion( in <float|float4> d1, in <float|float4> d2 [, <float> smooth_factor] )

Check it on Github


#ifndef FNC_OPUNION
#define FNC_OPUNION

float opUnion( float d1, float d2 ) { return min(d1, d2); }
float4  opUnion( float4 d1, float4 d2 ) { return (d1.a < d2.a) ? d1 : d2; }

// Soft union
float opUnion( float d1, float d2, float k ) {
    float h = saturate( 0.5 + 0.5*(d2-d1)/k );
    return lerp( d2, d1, h ) - k*h*(1.0-h); 
}

float4 opUnion( float4 d1, float4 d2, float k ) {
    float h = saturate( 0.5 + 0.5*(d2.a - d1.a)/k );
    return lerp( d2, d1, h ) - k*h*(1.0-h); 
}

#endif

Check it on Github

fn opUnion( d1: f32, d2:f32 ) -> f32 { return min(d1, d2); }

Dependencies:

Use:

<float> opUnion( in <float|float4> d1, in <float|float4> d2 [, <float> smooth_factor] )

Check it on Github

// #include "../math/saturate.cuh"


#ifndef FNC_OPUNION
#define FNC_OPUNION

inline __host__ __device__ float opUnion( float d1, float d2 ) { return min(d1, d2); }
inline __host__ __device__ float4 opUnion( float4 d1, float4 d2 ) { return (d1.w < d2.w) ? d1 : d2; }

// Soft union
inline __host__ __device__ float opUnion( float d1, float d2, float k ) {
    float h = clamp( 0.5f + 0.5f * (d2-d1)/k, 0.0f, 1.0f );
    return mix( d2, d1, h ) - k * h * (1.0f-h); 
}

inline __host__ __device__ float4 opUnion( float4 d1, float4 d2, float k ) {
    float h = clamp( 0.5f + 0.5f * (d2.w - d1.w)/k, 0.0f, 1.0f );
    return mix( d2, d1, h ) - k * h * (1.0f-h); 
}

#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