Slide 13
Slide 13 text
Metal Shader
Ripple.metalɹίʔυͷશମ
#include
#include
using namespace metal;
[[ stitchable ]]
half4 Ripple(
float2 position,
SwiftUI
:
:
Layer layer,
float2 origin,
float time,
float amplitude,
float frequency,
float decay,
float speed
) {
/
/
The distance of the current pixel position from `origin`.
float distance = length(position - origin);
/
/
The amount of time it takes for the ripple to arrive at the current pixel position.
float delay = distance / speed;
/
/
Adjust for delay, clamp to 0.
time -= delay;
time = max(0.0, time);
/
/
The ripple is a sine wave that Metal scales by an exponential decay
/
/
function.
float rippleAmount = amplitude * sin(frequency * time) * exp(-decay * time);
/
/
A vector of length `amplitude` that points away from position.
float2 n = normalize(position - origin);
/
/
Scale `n` by the ripple amount at the current pixel position and add it
/
/
to the current pixel position.
/
/
/
/
This new position moves toward or away from `origin` based on the
/
/
sign and magnitude of `rippleAmount`.
float2 newPosition = position + rippleAmount * n;
/
/
Sample the layer at the new position.
half4 color = layer.sample(newPosition);
/
/
Lighten or darken the color based on the ripple amount and its alpha
/
/
component.
color.rgb += 0.3 * (rippleAmount / amplitude) * color.a;
return color;
}