|
| 1 | +#version 320 es |
| 2 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 3 | +// Use of this source code is governed by a BSD-style license that can be |
| 4 | +// found in the LICENSE file. |
| 5 | + |
| 6 | +// This shader was created based on or with reference to the implementation found at: |
| 7 | +// https://cs.android.com/android/platform/superproject/main/+/512046e84bcc51cc241bc6599f83ab345e93ab12:frameworks/base/libs/hwui/effects/StretchEffect.cpp |
| 8 | + |
| 9 | +#include <flutter/runtime_effect.glsl> |
| 10 | + |
| 11 | +uniform vec2 u_size; |
| 12 | +uniform sampler2D u_texture; |
| 13 | + |
| 14 | +// Multiplier to apply to scale effect. |
| 15 | +uniform float u_max_stretch_intensity; |
| 16 | + |
| 17 | +// Normalized overscroll amount in the horizontal direction. |
| 18 | +uniform float u_overscroll_x; |
| 19 | + |
| 20 | +// Normalized overscroll amount in the vertical direction. |
| 21 | +uniform float u_overscroll_y; |
| 22 | + |
| 23 | +// u_interpolation_strength is the intensity of the interpolation. |
| 24 | +uniform float u_interpolation_strength; |
| 25 | + |
| 26 | +float ease_in(float t, float d) { |
| 27 | + return t * d; |
| 28 | +} |
| 29 | + |
| 30 | +float compute_overscroll_start( |
| 31 | + float in_pos, |
| 32 | + float overscroll, |
| 33 | + float u_stretch_affected_dist, |
| 34 | + float u_inverse_stretch_affected_dist, |
| 35 | + float distance_stretched, |
| 36 | + float interpolation_strength |
| 37 | +) { |
| 38 | + float offset_pos = u_stretch_affected_dist - in_pos; |
| 39 | + float pos_based_variation = mix( |
| 40 | + 1.0, |
| 41 | + ease_in(offset_pos, u_inverse_stretch_affected_dist), |
| 42 | + interpolation_strength |
| 43 | + ); |
| 44 | + float stretch_intensity = overscroll * pos_based_variation; |
| 45 | + return distance_stretched - (offset_pos / (1.0 + stretch_intensity)); |
| 46 | +} |
| 47 | + |
| 48 | +float compute_overscroll_end( |
| 49 | + float in_pos, |
| 50 | + float overscroll, |
| 51 | + float reverse_stretch_dist, |
| 52 | + float u_stretch_affected_dist, |
| 53 | + float u_inverse_stretch_affected_dist, |
| 54 | + float distance_stretched, |
| 55 | + float interpolation_strength, |
| 56 | + float viewport_dimension |
| 57 | +) { |
| 58 | + float offset_pos = in_pos - reverse_stretch_dist; |
| 59 | + float pos_based_variation = mix( |
| 60 | + 1.0, |
| 61 | + ease_in(offset_pos, u_inverse_stretch_affected_dist), |
| 62 | + interpolation_strength |
| 63 | + ); |
| 64 | + float stretch_intensity = (-overscroll) * pos_based_variation; |
| 65 | + return viewport_dimension - (distance_stretched - (offset_pos / (1.0 + stretch_intensity))); |
| 66 | +} |
| 67 | + |
| 68 | +float compute_streched_effect( |
| 69 | + float in_pos, |
| 70 | + float overscroll, |
| 71 | + float u_stretch_affected_dist, |
| 72 | + float u_inverse_stretch_affected_dist, |
| 73 | + float distance_stretched, |
| 74 | + float distance_diff, |
| 75 | + float interpolation_strength, |
| 76 | + float viewport_dimension |
| 77 | +) { |
| 78 | + if (overscroll > 0.0) { |
| 79 | + if (in_pos <= u_stretch_affected_dist) { |
| 80 | + return compute_overscroll_start( |
| 81 | + in_pos, overscroll, u_stretch_affected_dist, |
| 82 | + u_inverse_stretch_affected_dist, distance_stretched, |
| 83 | + interpolation_strength |
| 84 | + ); |
| 85 | + } else { |
| 86 | + return distance_diff + in_pos; |
| 87 | + } |
| 88 | + } else if (overscroll < 0.0) { |
| 89 | + float stretch_affected_dist_calc = viewport_dimension - u_stretch_affected_dist; |
| 90 | + if (in_pos >= stretch_affected_dist_calc) { |
| 91 | + return compute_overscroll_end( |
| 92 | + in_pos, |
| 93 | + overscroll, |
| 94 | + stretch_affected_dist_calc, |
| 95 | + u_stretch_affected_dist, |
| 96 | + u_inverse_stretch_affected_dist, |
| 97 | + distance_stretched, |
| 98 | + interpolation_strength, |
| 99 | + viewport_dimension |
| 100 | + ); |
| 101 | + } else { |
| 102 | + return -distance_diff + in_pos; |
| 103 | + } |
| 104 | + } else { |
| 105 | + return in_pos; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +out vec4 frag_color; |
| 110 | + |
| 111 | +void main() { |
| 112 | + vec2 uv = FlutterFragCoord().xy / u_size; |
| 113 | + float in_u_norm = uv.x; |
| 114 | + float in_v_norm = uv.y; |
| 115 | + |
| 116 | + float out_u_norm; |
| 117 | + float out_v_norm; |
| 118 | + |
| 119 | + bool isVertical = u_overscroll_y != 0; |
| 120 | + float overscroll = isVertical ? u_overscroll_y : u_overscroll_x; |
| 121 | + |
| 122 | + float norm_distance_stretched = 1.0 / (1.0 + abs(overscroll)); |
| 123 | + float norm_dist_diff = norm_distance_stretched - 1.0; |
| 124 | + |
| 125 | + const float norm_viewport = 1.0; |
| 126 | + const float norm_stretch_affected_dist = 1.0; |
| 127 | + const float norm_inverse_stretch_affected_dist = 1.0; |
| 128 | + |
| 129 | + out_u_norm = isVertical ? in_u_norm : compute_streched_effect( |
| 130 | + in_u_norm, |
| 131 | + overscroll, |
| 132 | + norm_stretch_affected_dist, |
| 133 | + norm_inverse_stretch_affected_dist, |
| 134 | + norm_distance_stretched, |
| 135 | + norm_dist_diff, |
| 136 | + u_interpolation_strength, |
| 137 | + norm_viewport |
| 138 | + ); |
| 139 | + |
| 140 | + out_v_norm = isVertical ? compute_streched_effect( |
| 141 | + in_v_norm, |
| 142 | + overscroll, |
| 143 | + norm_stretch_affected_dist, |
| 144 | + norm_inverse_stretch_affected_dist, |
| 145 | + norm_distance_stretched, |
| 146 | + norm_dist_diff, |
| 147 | + u_interpolation_strength, |
| 148 | + norm_viewport |
| 149 | + ) : in_v_norm; |
| 150 | + |
| 151 | + uv.x = out_u_norm; |
| 152 | + #ifdef IMPELLER_TARGET_OPENGLES |
| 153 | + uv.y = 1.0 - out_v_norm; |
| 154 | + #else |
| 155 | + uv.y = out_v_norm; |
| 156 | + #endif |
| 157 | + |
| 158 | + frag_color = texture(u_texture, uv); |
| 159 | +} |
0 commit comments