demotool/test/lib/utils.glsl

115 lines
1.5 KiB
Text
Raw Normal View History

2017-10-04 18:40:15 +02:00
//! type library
2017-10-04 19:06:50 +02:00
2017-10-05 20:29:39 +02:00
const float PI = 3.1415926535;
const float TAU = 2 * PI;
const float PHI = sqrt( 5 ) * .5 + .5;
2017-10-04 19:06:50 +02:00
2017-10-04 18:40:15 +02:00
// -----------------------------------------------------------------------------
2017-10-04 19:06:50 +02:00
float M_Hash(
in vec2 p )
2017-10-04 18:40:15 +02:00
{
p = fract(p * vec2(5.3987, 5.4421));
p += dot(p.yx, p.xy + vec2(21.5351, 14.3137));
return fract(p.x * p.y * 95.4307);
}
2017-10-04 19:06:50 +02:00
// -----------------------------------------------------------------------------
float M_Luminosity(
in vec3 color )
2017-10-04 19:06:50 +02:00
{
2017-10-05 11:47:33 +02:00
return dot( color , vec3( .299 , .587 , .114 ) );
2017-10-04 19:06:50 +02:00
}
vec3 M_NormalizeColor(
in vec3 color )
2017-10-04 19:06:50 +02:00
{
const float l = M_Luminosity( color );
return l > 0 ? ( color / l ) : vec3( 1 );
}
// -----------------------------------------------------------------------------
float M_Saturate(
in float x )
{
2017-10-06 09:36:49 +02:00
return clamp( x , 0. , 1. );
}
float M_LengthSqr(
in vec3 x )
{
return dot( x , x );
}
2017-10-05 20:29:39 +02:00
float M_Sqr(
in float x )
{
return x * x;
}
vec2 M_Sqr(
in vec2 x )
{
return x * x;
}
vec3 M_Sqr(
in vec3 x )
{
return x * x;
}
float M_Sign(
in float x )
{
return ( x < 0 ) ? -1 : 1;
}
vec2 M_Sign(
in vec2 v )
{
return vec2( M_Sign( v.x ) , M_Sign( v.y ) );
}
float M_VecMax(
in vec2 v )
{
return max( v.x , v.y );
}
float M_VecMax(
in vec3 v )
{
return max( M_VecMax( v.xy ) , v.z );
}
float M_VecMax(
in vec4 v )
{
return max( M_VecMax( v.xy ) , M_VecMax( v.zw ) );
}
float M_VecMin(
in vec2 v )
{
return min( v.x , v.y );
}
float M_VecMin(
in vec3 v )
{
return min( M_VecMin( v.xy ) , v.z );
}
float M_VecMin(
in vec4 v )
{
return min( M_VecMin( v.xy ) , M_VecMin( v.zw ) );
}