common.glsl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #define PI 3.14159265359
  2. float saturate(float x) {
  3. return clamp(x, 0.0, 1.0);
  4. }
  5. vec3 saturate3(vec3 x) {
  6. return clamp(x, vec3(0.0), vec3(1.0));
  7. }
  8. float linearstep(float minValue, float maxValue, float v) {
  9. return clamp((v - minValue) / (maxValue - minValue), 0.0, 1.0);
  10. }
  11. float inverseLerp(float minValue, float maxValue, float v) {
  12. return (v - minValue) / (maxValue - minValue);
  13. }
  14. float inverseLerpSat(float minValue, float maxValue, float v) {
  15. return saturate((v - minValue) / (maxValue - minValue));
  16. }
  17. float remap(float v, float inMin, float inMax, float outMin, float outMax) {
  18. float t = inverseLerp(inMin, inMax, v);
  19. return mix(outMin, outMax, t);
  20. }
  21. float smootherstep(float edge0, float edge1, float x) {
  22. x = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
  23. return x * x * x * (x * (x * 6.0 - 15.0) + 10.0);
  24. }
  25. vec2 smootherstep2(vec2 edge0, vec2 edge1, vec2 x) {
  26. x = clamp((x - edge0) / (edge1 - edge0), vec2(0.0), vec2(1.0));
  27. return x * x * x * (x * (x * 6.0 - 15.0) + 10.0);
  28. }
  29. vec3 smootherstep3(vec3 edge0, vec3 edge1, vec3 x) {
  30. x = clamp((x - edge0) / (edge1 - edge0), vec3(0.0), vec3(1.0));
  31. return x * x * x * (x * (x * 6.0 - 15.0) + 10.0);
  32. }
  33. float circularOut(float t) {
  34. return sqrt((2.0 - t) * t);
  35. }
  36. /////////////////////////////////////////////////////////////////////////
  37. //
  38. // 3D SDF's
  39. //
  40. /////////////////////////////////////////////////////////////////////////
  41. // https://iquilezles.org/articles/distfunctions/
  42. float sdfSphere(vec3 p, float r) {
  43. return length(p) - r;
  44. }
  45. float sdfBox( vec3 p, vec3 b ) {
  46. vec3 q = abs(p) - b;
  47. return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
  48. }
  49. float sdCutSphere( vec3 p, float r, float h )
  50. {
  51. // sampling independent computations (only depend on shape)
  52. float w = sqrt(r*r-h*h);
  53. // sampling dependant computations
  54. vec2 q = vec2( length(p.xz), p.y );
  55. float s = max( (h-r)*q.x*q.x+w*w*(h+r-2.0*q.y), h*q.x-w*q.y );
  56. return (s<0.0) ? length(q)-r :
  57. (q.x<w) ? h - q.y :
  58. length(q-vec2(w,h));
  59. }
  60. /////////////////////////////////////////////////////////////////////////
  61. //
  62. // Misc
  63. //
  64. /////////////////////////////////////////////////////////////////////////
  65. vec3 vignette(vec2 uvs) {
  66. float v1 = smoothstep(0.5, 0.3, abs(uvs.x - 0.5));
  67. float v2 = smoothstep(0.5, 0.3, abs(uvs.y - 0.5));
  68. float v = v1 * v2;
  69. v = pow(v, 0.25);
  70. v = remap(v, 0.0, 1.0, 0.4, 1.0);
  71. return vec3(v);
  72. }
  73. /////////////////////////////////////////////////////////////////////////
  74. //
  75. // ToneMapping Operators
  76. //
  77. /////////////////////////////////////////////////////////////////////////
  78. vec3 ACESToneMap(vec3 color){
  79. mat3 m1 = mat3(
  80. 0.59719, 0.07600, 0.02840,
  81. 0.35458, 0.90834, 0.13383,
  82. 0.04823, 0.01566, 0.83777
  83. );
  84. mat3 m2 = mat3(
  85. 1.60475, -0.10208, -0.00327,
  86. -0.53108, 1.10813, -0.07276,
  87. -0.07367, -0.00605, 1.07602
  88. );
  89. vec3 v = m1 * color;
  90. vec3 a = v * (v + 0.0245786) - 0.000090537;
  91. vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;
  92. return saturate3(m2 * (a / b));
  93. }
  94. /////////////////////////////////////////////////////////////////////////
  95. //
  96. // Intersections
  97. //
  98. /////////////////////////////////////////////////////////////////////////
  99. // Return the near and far intersections of an infinite ray and a sphere.
  100. // Assumes sphere at origin. No intersection if result.x > result.y
  101. vec2 sphereIntersections(vec3 start, vec3 dir, float radius){
  102. float a = dot(dir, dir);
  103. float b = 2.0 * dot(dir, start);
  104. float c = dot(start, start) - (radius * radius);
  105. float d = (b*b) - 4.0*a*c;
  106. if (d < 0.0){
  107. return vec2(1e5, -1e5);
  108. }
  109. return vec2((-b - sqrt(d))/(2.0*a), (-b + sqrt(d))/(2.0*a));
  110. }
  111. struct AABB {
  112. vec3 min;
  113. vec3 max;
  114. };
  115. struct AABBIntersectResult {
  116. float near;
  117. float far;
  118. };
  119. bool insideAABB(vec3 rayOrigin, AABB box) {
  120. return all(lessThanEqual(rayOrigin, box.max)) && all(lessThan(box.min, rayOrigin));
  121. }
  122. // https://gist.github.com/DomNomNom/46bb1ce47f68d255fd5d
  123. // Compute the near and far intersections using the slab method.
  124. // No intersection if tNear > tFar.
  125. AABBIntersectResult intersectAABB(vec3 rayOrigin, vec3 rayDir, AABB box) {
  126. vec3 tMin = (box.min - rayOrigin) / rayDir;
  127. vec3 tMax = (box.max - rayOrigin) / rayDir;
  128. vec3 t1 = min(tMin, tMax);
  129. vec3 t2 = max(tMin, tMax);
  130. float tNear = max(max(t1.x, t1.y), t1.z);
  131. float tFar = min(min(t2.x, t2.y), t2.z);
  132. return AABBIntersectResult(tNear, tFar);
  133. }
  134. struct AABB2D {
  135. vec2 min;
  136. vec2 max;
  137. };
  138. struct AABB2DIntersectResult {
  139. float near;
  140. float far;
  141. };
  142. bool insideAABB(vec2 rayOrigin, AABB2D box) {
  143. return all(lessThanEqual(rayOrigin, box.max)) && all(lessThan(box.min, rayOrigin));
  144. }
  145. // https://gist.github.com/DomNomNom/46bb1ce47f68d255fd5d
  146. // Compute the near and far intersections using the slab method.
  147. // No intersection if tNear > tFar.
  148. AABB2DIntersectResult intersectAABB2D(vec2 rayOrigin, vec2 rayDir, AABB2D box) {
  149. vec2 tMin = (box.min - rayOrigin) / rayDir;
  150. vec2 tMax = (box.max - rayOrigin) / rayDir;
  151. vec2 t1 = min(tMin, tMax);
  152. vec2 t2 = max(tMin, tMax);
  153. float tNear = max(t1.x, t1.y);
  154. float tFar = min(t2.x, t2.y);
  155. return AABB2DIntersectResult(tNear, tFar);
  156. }