medfall

A super great game engine
Log | Files | Refs

common.glsl (1583B)


      1 const float PI = 3.14159265359;
      2 
      3 vec3 alu_noise( vec2 uv ) {
      4 	vec3 dither = vec3( dot( vec2( 171.0, 231.0 ), uv ) );
      5 	dither = fract( dither / vec3( 103.0, 71.0, 97.0 ) ) - vec3( 0.5, 0.5, 0.5 );
      6 	return ( dither / 256.0 ) * 0.5;
      7 }
      8 
      9 float saturate( float x ) {
     10 	return clamp( x, 0.0, 1.0 );
     11 }
     12 
     13 vec3 saturate( vec3 x ) {
     14 	return clamp( x, 0.0, 1.0 );
     15 }
     16 
     17 float unlerp( float x, float lo, float hi ) {
     18 	return ( x - lo ) / ( hi - lo );
     19 }
     20 
     21 float unlerp_sat( float x, float lo, float hi ) {
     22 	return saturate( unlerp( x, lo, hi ) );
     23 }
     24 
     25 float dot_sat( vec3 a, vec3 b ) {
     26 	return saturate( dot( a, b ) );
     27 }
     28 
     29 vec3 reinhard_tonemap( vec3 c ) {
     30 	return c / ( c + vec3( 1.0 ) );
     31 }
     32 
     33 float linear_to_srgb( float linear ) {
     34 	linear = saturate( linear );
     35 
     36 	if( linear <= 0.0031308 )
     37 		return 12.92 * linear;
     38 	return 1.055 * pow( linear, 1.0 / 2.4 ) - 0.055;
     39 }
     40 
     41 vec3 linear_to_srgb( vec3 linear ) {
     42 	return vec3( linear_to_srgb( linear.r ), linear_to_srgb( linear.g ), linear_to_srgb( linear.b ) );
     43 }
     44 
     45 vec4 linear_to_srgb( vec4 linear ) {
     46 	return vec4( linear_to_srgb( linear.rgb ), linear.a );
     47 }
     48 
     49 vec3 apply_fog( vec3 c, float dist ) {
     50 	vec3 fog_colour = vec3( 0.5, 0.6, 0.7 );
     51 	float fog_strength = 0.0003;
     52 	float fog_amount = 1.0 - exp( -fog_strength * max( dist, 0.0 ) );
     53 	return mix( c, fog_colour, fog_amount );
     54 }
     55 
     56 #define get_dither_noise( tex ) ( ( texture( tex, gl_FragCoord.xy / textureSize( tex, 0 ) ).xxx - vec3( 0.5 ) ) / 128.0 )
     57 
     58 vec3 normal_to_01( vec3 normal ) {
     59 	return ( normal + 1.0 ) * 0.5;
     60 }
     61 
     62 vec4 normal_to_01( vec4 normal ) {
     63 	return vec4( ( normal.xyz + 1.0 ) * 0.5, 1.0 );
     64 }