Advertisement
entrusc

utils.glsllib

Mar 25th, 2015
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // some util functions
  2.  
  3. float decodeFloat(in vec4 color) {
  4.     return color.r * (255.0 * 256.0 * 256.0) + color.g * 255.0 * 256.0 + color.b * 255.0;
  5. }
  6.  
  7. /**
  8.  * Reconstructs the 3D position in world space from given screen uv coordinates and depth from
  9.  * the zBuffer.
  10.  */
  11. vec3 getPosition(in float zBuffer, in vec2 uv, in mat4 viewProjectionMatrixInverse){
  12.     vec4 pos = vec4(uv, zBuffer, 1.0) * 2.0 - 1.0;
  13.     pos = viewProjectionMatrixInverse * pos;
  14.     return pos.xyz / pos.w;
  15. }
  16.  
  17. /**
  18.  * approximates the real distance in [0 - 1] - but is not entirely independent
  19.  * of the camera position - so use with caution!
  20.  **/
  21. float getDepthByZBuffer(in float zBuffer, in mat4 projectionMatrix) {
  22.      float normalized_depth = zBuffer * 2.0 - 1.0;
  23.      return max(0.0, projectionMatrix[2][3] / (normalized_depth - projectionMatrix[1][1]));
  24. }
  25.  
  26. float getDistanceByZBuffer(in float zBuffer, in float near, in float far) {
  27.     float z_n = 2.0 * zBuffer - 1.0;
  28.     return 2.0 * near * far / (far + near - z_n * (far - near));
  29. }
  30.  
  31. /**
  32.  * calculates a linear interpretation of the (non linear) depth buffer in
  33.  * the range of [0 - 1].
  34.  **/
  35. float linearizeDepth(in float z, in float near, in float far) {
  36.   return (2.0 * near) / (far + near - z * (far - near));
  37. }
  38.  
  39. vec4 getAttributeAt(in sampler2D texture, in ivec2 position) {
  40.     return texelFetch(texture, position, 0);
  41. }
  42.  
  43. /**
  44.  * symmetric modulo
  45.  */
  46. float modulo(float a, float x) {
  47.     float result = mod(a, x);
  48.     if (result < 0) {
  49.         result = x + result;
  50.     }
  51.     return abs(result);
  52. }
  53.  
  54. /**
  55.  * returns a signmoid distribution from a given
  56.  * value between 0 and 1 projected to a output
  57.  * value between 0 and 1
  58.  *
  59.  * the multiplicant defines how courvy the projection
  60.  * is: 10 ist normal -> while 20 or above makes it more S shaped
  61.  **/
  62. float sigmoid(float mult, float x) {
  63.     return 1 / (1 + pow(2.718281828459045235,  -((x - 0.5) * mult)));
  64. }
  65.  
  66. /**
  67.  * converts rgb to hsv
  68.  * h is in the range of [0-1]
  69.  **/
  70. vec3 rgb2hsv(vec3 c) {
  71.     vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
  72.     vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
  73.     vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
  74.  
  75.     float d = q.x - min(q.w, q.y);
  76.     float e = 1.0e-10;
  77.     return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
  78. }
  79.  
  80. /**
  81.  * converts hsv to rgb
  82.  **/
  83. vec3 hsv2rgb(vec3 c) {
  84.     vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
  85.     vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
  86.     return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
  87. }
  88.  
  89. /**
  90.  * mixes in a cubic interpolation insted of a linear one
  91.  **/
  92. vec4 mixCubic(in vec4 valA, in vec4 valB, in float i) {
  93.     float pos = pow(i, 1.5);
  94.     return mix(valA, valB, pos);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement