From a6b3237211843efffafc3d3999e8c4afd84bcf19 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 12 Sep 2016 11:07:44 -0500 Subject: [PATCH 1/2] lens distortion post-processing shader --- Data/Sys/Shaders/lens_distortion.glsl | 93 +++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Data/Sys/Shaders/lens_distortion.glsl diff --git a/Data/Sys/Shaders/lens_distortion.glsl b/Data/Sys/Shaders/lens_distortion.glsl new file mode 100644 index 0000000000..35390b4d69 --- /dev/null +++ b/Data/Sys/Shaders/lens_distortion.glsl @@ -0,0 +1,93 @@ +/* +[configuration] + +[OptionRangeFloat] +GUIName = Distortion amount +OptionName = DISTORTION_FACTOR +MinValue = 1.0 +MaxValue = 10.0 +StepAmount = 0.5 +DefaultValue = 4.0 + +[OptionRangeFloat] +GUIName = Eye Distance Offset +OptionName = EYE_OFFSET +MinValue = 0.0 +MaxValue = 10.0 +StepAmount = 0.25 +DefaultValue = 5.0 + +[OptionRangeFloat] +GUIName = Zoom adjustment +OptionName = SIZE_ADJUST +MinValue = 0.0 +MaxValue = 1.0 +StepAmount = 0.025 +DefaultValue = 0.5 + +[OptionRangeFloat] +GUIName = Aspect Ratio adjustment +OptionName = ASPECT_ADJUST +MinValue = 0.0 +MaxValue = 1.0 +StepAmount = 0.025 +DefaultValue = 0.5 + +[/configuration] +*/ + + +void main() +{ + // Base Cardboard distortion parameters + float factor = GetOption(DISTORTION_FACTOR) * 0.01f; + float ka = factor * 3.0f; + float kb = factor * 5.0f; + + // size and aspect adjustment + float sizeAdjust = 1.0f - GetOption(SIZE_ADJUST) + 0.5f; + float aspectAdjustment = 1.25f - GetOption(ASPECT_ADJUST); + + // offset centering per eye + float stereoOffset = GetOption(EYE_OFFSET) * 0.01f; + float offsetAdd; + + // layer0 = left eye, layer1 = right eye + if (layer == 1) + { + offsetAdd = stereoOffset; + } + else + { + offsetAdd = 0 - stereoOffset; + } + + // convert coordinates to NDC space + float2 fragPos = (GetCoordinates() - 0.5f - vec2(offsetAdd, 0.0f)) * 2.0f; + + // Calculate the source location "radius" (distance from the centre of the viewport) + float destR = length(fragPos); + + // find the radius multiplier + float srcR = destR * sizeAdjust + ( ka * pow(destR,2) + kb * pow(destR,4)); + + // Calculate the source vector (radial) + vec2 correctedRadial = normalize(fragPos) * srcR; + + // fix aspect ratio + vec2 widenedRadial = correctedRadial * vec2(aspectAdjustment, 1.0f); + + // Transform the coordinates (from [-1,1]^2 to [0, 1]^2) + vec2 uv = (widenedRadial/2.0f) + vec2(0.5f) + vec2(offsetAdd, 0.0f); + + // Sample the texture at the source location + if (uv[0] > 1.0 || uv[1] > 1.0 || uv[0] < 0.0 || uv[1] < 0.0) + { + // black if beyond bounds + SetOutput(float4(0,0,0,0)); + } + else + { + SetOutput(SampleLocation(uv)); + } +} From 5fc51b45a064766393de7599d10e83403de39939 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 13 Sep 2016 10:52:14 -0500 Subject: [PATCH 2/2] code adjustments in response to review --- Data/Sys/Shaders/lens_distortion.glsl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Data/Sys/Shaders/lens_distortion.glsl b/Data/Sys/Shaders/lens_distortion.glsl index 35390b4d69..6311cd956b 100644 --- a/Data/Sys/Shaders/lens_distortion.glsl +++ b/Data/Sys/Shaders/lens_distortion.glsl @@ -59,7 +59,7 @@ void main() } else { - offsetAdd = 0 - stereoOffset; + offsetAdd = 0.0 - stereoOffset; } // convert coordinates to NDC space @@ -69,7 +69,7 @@ void main() float destR = length(fragPos); // find the radius multiplier - float srcR = destR * sizeAdjust + ( ka * pow(destR,2) + kb * pow(destR,4)); + float srcR = destR * sizeAdjust + ( ka * pow(destR, 2.0) + kb * pow(destR, 4.0)); // Calculate the source vector (radial) vec2 correctedRadial = normalize(fragPos) * srcR; @@ -81,10 +81,10 @@ void main() vec2 uv = (widenedRadial/2.0f) + vec2(0.5f) + vec2(offsetAdd, 0.0f); // Sample the texture at the source location - if (uv[0] > 1.0 || uv[1] > 1.0 || uv[0] < 0.0 || uv[1] < 0.0) + if(clamp(uv, 0.0, 1.0) != uv) { // black if beyond bounds - SetOutput(float4(0,0,0,0)); + SetOutput(float4(0.0, 0.0, 0.0, 0.0)); } else {