LYGIA Shader Library

opSubtraction (lygia/sdf/opSubtraction)

Substraction operation of two SDFs

Dependencies:

Use:

<float> opSubstraction( in <float> d1, in <float> d2 [, <float> smooth_factor])

Check it on Github



#ifndef FNC_OPSUBSTRACTION
#define FNC_OPSUBSTRACTION

float opSubtraction( float d1, float d2 ) { return max(-d1, d2); }
vec4  opSubtraction( vec4 d1, vec4 d2 ) { return (-d1.a > d2.a) ? -d1 : d2; }

float opSubtraction( float d1, float d2, float k ) {
    float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 );
    return mix( d2, -d1, h ) + k*h*(1.0-h);
}


vec4 opSubtraction( vec4 d1, vec4 d2, float k ) {
    float h = clamp( 0.5 - 0.5*(d2.a+d1.a)/k, 0.0, 1.0 );
    vec4 result = mix( d2, -d1, h );
    result.a += k*h*(1.0-h);
    return result;
}

#endif

Use:

<float> opSubstraction( in <float> d1, in <float> d2 [, <float> smooth_factor])

Check it on Github


#ifndef FNC_OPSUBSTRACTION
#define FNC_OPSUBSTRACTION

float opSubtraction( float d1, float d2 ) { return max(-d1,d2); }

float opSubtraction( 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);
}

#endif

Dependencies:

Use:

<float> opSubstraction( <float> d1, <float> d2 [, <float> smooth_factor])

Check it on Github



#ifndef FNC_OPSUBSTRACTION
#define FNC_OPSUBSTRACTION

float opSubtraction( float d1, float d2 ) { return max(-d1, d2); }
float4  opSubtraction( float4 d1, float4 d2 ) { return (-d1.a > d2.a) ? -d1 : d2; }

float opSubtraction( float d1, float d2, float k ) {
    float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 );
    return mix( d2, -d1, h ) + k*h*(1.0-h);
}


float4 opSubtraction( float4 d1, float4 d2, float k ) {
    float h = clamp( 0.5 - 0.5*(d2.a+d1.a)/k, 0.0, 1.0 );
    return mix( d2, -d1, h ) + k*h*(1.0-h);
}

#endif

Check it on Github


fn opSubtraction( d1: f32, d2: f32 )-> f32 { return max(-d1, d2); }

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