math.glsl 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef _MATH_GLSL_
  2. #define _MATH_GLSL_
  3. const float PI = 3.1415926535;
  4. const float PI2 = PI * 2.0;
  5. float hash(const vec2 p) {
  6. float h = dot(p, vec2(127.1, 311.7));
  7. return fract(sin(h) * 43758.5453123);
  8. }
  9. vec2 envMapEquirect(const vec3 normal) {
  10. const float PI = 3.1415926535;
  11. const float PI2 = PI * 2.0;
  12. float phi = acos(normal.z);
  13. float theta = atan(-normal.y, normal.x) + PI;
  14. return vec2(theta / PI2, phi / PI);
  15. }
  16. float rand(const vec2 co) { // Unreliable
  17. return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
  18. }
  19. vec2 rand2(const vec2 coord) {
  20. const float width = 1100;
  21. const float height = 500;
  22. float noiseX = ((fract(1.0 - coord.s * (width / 2.0)) * 0.25) + (fract(coord.t * (height / 2.0)) * 0.75)) * 2.0 - 1.0;
  23. float noiseY = ((fract(1.0 - coord.s * (width / 2.0)) * 0.75) + (fract(coord.t * (height / 2.0)) * 0.25)) * 2.0 - 1.0;
  24. return vec2(noiseX, noiseY);
  25. }
  26. float linearize(const float depth, vec2 cameraProj) {
  27. // to viewz
  28. return cameraProj.y / (depth - cameraProj.x);
  29. }
  30. float attenuate(const float dist) {
  31. // float attenuate(float dist, float constant, float linear, float quadratic) {
  32. return 1.0 / (dist * dist);
  33. // 1.0 / (constant * 1.0)
  34. // 1.0 / (linear * dist)
  35. // 1.0 / (quadratic * dist * dist);
  36. }
  37. #endif