lens_distortion.glsl 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. [configuration]
  3. [OptionRangeFloat]
  4. GUIName = Distortion amount
  5. OptionName = DISTORTION_FACTOR
  6. MinValue = 1.0
  7. MaxValue = 10.0
  8. StepAmount = 0.5
  9. DefaultValue = 4.0
  10. [OptionRangeFloat]
  11. GUIName = Eye Distance Offset
  12. OptionName = EYE_OFFSET
  13. MinValue = 0.0
  14. MaxValue = 10.0
  15. StepAmount = 0.25
  16. DefaultValue = 5.0
  17. [OptionRangeFloat]
  18. GUIName = Zoom adjustment
  19. OptionName = SIZE_ADJUST
  20. MinValue = 0.0
  21. MaxValue = 1.0
  22. StepAmount = 0.025
  23. DefaultValue = 0.5
  24. [OptionRangeFloat]
  25. GUIName = Aspect Ratio adjustment
  26. OptionName = ASPECT_ADJUST
  27. MinValue = 0.0
  28. MaxValue = 1.0
  29. StepAmount = 0.025
  30. DefaultValue = 0.5
  31. [/configuration]
  32. */
  33. void main()
  34. {
  35. // Base Cardboard distortion parameters
  36. float factor = GetOption(DISTORTION_FACTOR) * 0.01f;
  37. float ka = factor * 3.0f;
  38. float kb = factor * 5.0f;
  39. // size and aspect adjustment
  40. float sizeAdjust = 1.0f - GetOption(SIZE_ADJUST) + 0.5f;
  41. float aspectAdjustment = 1.25f - GetOption(ASPECT_ADJUST);
  42. // offset centering per eye
  43. float stereoOffset = GetOption(EYE_OFFSET) * 0.01f;
  44. float offsetAdd;
  45. // layer0 = left eye, layer1 = right eye
  46. if (src_layer == 1)
  47. {
  48. offsetAdd = stereoOffset;
  49. }
  50. else
  51. {
  52. offsetAdd = 0.0 - stereoOffset;
  53. }
  54. // convert coordinates to NDC space
  55. float2 fragPos = (GetCoordinates() - 0.5f - float2(offsetAdd, 0.0f)) * 2.0f;
  56. // Calculate the source location "radius" (distance from the centre of the viewport)
  57. float destR = length(fragPos);
  58. // find the radius multiplier
  59. float srcR = destR * sizeAdjust + ( ka * pow(destR, 2.0) + kb * pow(destR, 4.0));
  60. // Calculate the source vector (radial)
  61. float2 correctedRadial = normalize(fragPos) * srcR;
  62. // fix aspect ratio
  63. float2 widenedRadial = correctedRadial * float2(aspectAdjustment, 1.0f);
  64. // Transform the coordinates (from [-1,1]^2 to [0, 1]^2)
  65. float2 uv = (widenedRadial/2.0f) + float2(0.5f, 0.5f) + float2(offsetAdd, 0.0f);
  66. // Sample the texture at the source location
  67. if (clamp(uv, 0.0, 1.0) != uv)
  68. {
  69. // black if beyond bounds
  70. SetOutput(float4(0.0, 0.0, 0.0, 0.0));
  71. }
  72. else
  73. {
  74. SetOutput(SampleLocation(uv));
  75. }
  76. }