vr.glsl 801 B

1234567891011121314151617181920212223242526
  1. uniform mat4 U; // Undistortion
  2. uniform float maxRadSq;
  3. // GoogleVR Distortion using Vertex Displacement
  4. float distortionFactor(const float rSquared) {
  5. float ret = 0.0;
  6. ret = rSquared * (ret + U[1][1]);
  7. ret = rSquared * (ret + U[0][1]);
  8. ret = rSquared * (ret + U[3][0]);
  9. ret = rSquared * (ret + U[2][0]);
  10. ret = rSquared * (ret + U[1][0]);
  11. ret = rSquared * (ret + U[0][0]);
  12. return ret + 1.0;
  13. }
  14. // Convert point from world space to undistorted camera space
  15. vec4 undistort(const mat4 WV, vec4 pos) {
  16. // Go to camera space
  17. pos = WV * pos;
  18. const float nearClip = 0.1;
  19. if (pos.z <= -nearClip) { // Reminder: Forward is -Z
  20. // Undistort the point's coordinates in XY
  21. float r2 = clamp(dot(pos.xy, pos.xy) / (pos.z * pos.z), 0.0, maxRadSq);
  22. pos.xy *= distortionFactor(r2);
  23. }
  24. return pos;
  25. }