math.glsl 1.2 KB

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