Differential D16990 Diff 60050 source/blender/compositor/realtime_compositor/shaders/compositor_screen_lens_distortion.glsl
Changeset View
Changeset View
Standalone View
Standalone View
source/blender/compositor/realtime_compositor/shaders/compositor_screen_lens_distortion.glsl
| Show All 16 Lines | vec3 compute_chromatic_distortion_scale(float distance_squared) | ||||
| return 1.0 / (1.0 + sqrt(max(vec3(0.0), 1.0 - chromatic_distortion * distance_squared))); | return 1.0 / (1.0 + sqrt(max(vec3(0.0), 1.0 - chromatic_distortion * distance_squared))); | ||||
| } | } | ||||
| /* Compute the image coordinates after distortion by the given distortion scale computed by the | /* Compute the image coordinates after distortion by the given distortion scale computed by the | ||||
| * compute_distortion_scale function. Note that the function expects centered normalized UV | * compute_distortion_scale function. Note that the function expects centered normalized UV | ||||
| * coordinates but outputs non-centered image coordinates. */ | * coordinates but outputs non-centered image coordinates. */ | ||||
| vec2 compute_distorted_uv(vec2 uv, float uv_scale) | vec2 compute_distorted_uv(vec2 uv, float uv_scale) | ||||
| { | { | ||||
| return (uv * uv_scale + 0.5) * texture_size(input_tx) - 0.5; | return (uv * uv_scale + 0.5) * vec2(texture_size(input_tx)) - 0.5; | ||||
| } | } | ||||
| /* Compute the number of integration steps that should be used to approximate the distorted pixel | /* Compute the number of integration steps that should be used to approximate the distorted pixel | ||||
| * using a heuristic, see the compute_number_of_steps function for more details. The numbers of | * using a heuristic, see the compute_number_of_steps function for more details. The numbers of | ||||
| * steps is proportional to the number of pixels spanned by the distortion amount. For jitter | * steps is proportional to the number of pixels spanned by the distortion amount. For jitter | ||||
| * distortion, the square root of the distortion amount plus 1 is used with a minimum of 2 steps. | * distortion, the square root of the distortion amount plus 1 is used with a minimum of 2 steps. | ||||
| * For non-jitter distortion, the distortion amount plus 1 is used as the number of steps */ | * For non-jitter distortion, the distortion amount plus 1 is used as the number of steps */ | ||||
| int compute_number_of_integration_steps_heuristic(float distortion) | int compute_number_of_integration_steps_heuristic(float distortion) | ||||
| ▲ Show 20 Lines • Show All 63 Lines • ▼ Show 20 Lines | for (int i = 0; i < steps; i++) { | ||||
| /* The increment will be in the [0, 1) range across iterations. */ | /* The increment will be in the [0, 1) range across iterations. */ | ||||
| float increment = (i + get_jitter(i)) / steps; | float increment = (i + get_jitter(i)) / steps; | ||||
| float distortion = chromatic_distortion[start] + increment * distortion_amount; | float distortion = chromatic_distortion[start] + increment * distortion_amount; | ||||
| float distortion_scale = compute_distortion_scale(distortion, distance_squared); | float distortion_scale = compute_distortion_scale(distortion, distance_squared); | ||||
| /* Sample the color at the distorted coordinates and accumulate it weighted by the increment | /* Sample the color at the distorted coordinates and accumulate it weighted by the increment | ||||
| * value for both the start and end channels. */ | * value for both the start and end channels. */ | ||||
| vec2 distorted_uv = compute_distorted_uv(uv, distortion_scale); | vec2 distorted_uv = compute_distorted_uv(uv, distortion_scale); | ||||
| vec4 color = texture(input_tx, distorted_uv / texture_size(input_tx)); | vec4 color = texture(input_tx, distorted_uv / vec2(texture_size(input_tx))); | ||||
| accumulated_color[start] += (1.0 - increment) * color[start]; | accumulated_color[start] += (1.0 - increment) * color[start]; | ||||
| accumulated_color[end] += increment * color[end]; | accumulated_color[end] += increment * color[end]; | ||||
| } | } | ||||
| return accumulated_color; | return accumulated_color; | ||||
| } | } | ||||
| void main() | void main() | ||||
| { | { | ||||
| ivec2 texel = ivec2(gl_GlobalInvocationID.xy); | ivec2 texel = ivec2(gl_GlobalInvocationID.xy); | ||||
| /* Compute the UV image coordinates in the range [-1, 1] as well as the squared distance to the | /* Compute the UV image coordinates in the range [-1, 1] as well as the squared distance to the | ||||
| * center of the image, which is at (0, 0) in the UV coordinates. */ | * center of the image, which is at (0, 0) in the UV coordinates. */ | ||||
| vec2 center = texture_size(input_tx) / 2.0; | vec2 center = vec2(texture_size(input_tx)) / 2.0; | ||||
| vec2 uv = scale * (texel + 0.5 - center) / center; | vec2 uv = scale * (vec2(texel) + vec2(0.5) - center) / center; | ||||
| float distance_squared = dot(uv, uv); | float distance_squared = dot(uv, uv); | ||||
| /* If any of the color channels will get distorted outside of the screen beyond what is possible, | /* If any of the color channels will get distorted outside of the screen beyond what is possible, | ||||
| * write a zero transparent color and return. */ | * write a zero transparent color and return. */ | ||||
| if (any(greaterThan(chromatic_distortion * distance_squared, vec3(1.0)))) { | if (any(greaterThan(chromatic_distortion * distance_squared, vec3(1.0)))) { | ||||
| imageStore(output_img, texel, vec4(0.0)); | imageStore(output_img, texel, vec4(0.0)); | ||||
| return; | return; | ||||
| } | } | ||||
| Show All 24 Lines | |||||