Skip to content

Instantly share code, notes, and snippets.

@radiofun
Created January 1, 2026 22:14
Show Gist options
  • Select an option

  • Save radiofun/909bea538f35a6f197b836b5a1f6783f to your computer and use it in GitHub Desktop.

Select an option

Save radiofun/909bea538f35a6f197b836b5a1f6783f to your computer and use it in GitHub Desktop.
Distortion Shader that is influenced by dragging position.
#include <metal_stdlib>
#include <SwiftUI/SwiftUI.h>
using namespace metal;
//Use DistortionSampleView.swift to try it out
//2026 Minsang Choi
[[ stitchable ]] half4 distortion(float2 pos, SwiftUI::Layer l, float4 boundingRect, float2 dragp) {
float2 delta = pos - dragp;
float dist = length(delta);
// Define the influence of the "force" from the drag point
float radius = 150.0; // Radius of effect
float strength = 0.6; // How much it pulls
// Smooth falloff using a Gaussian-like curve
float influence = exp(-dist * dist / (radius * radius));
// Displace the sampling position.
float2 samplePos = pos + delta * influence * strength;
// Sample the color
half4 color = l.sample(samplePos);
// Add brightness based on influence, clipped by alpha and boundingRect
float brightnessBoost = influence * 0.64 * color.a;
bool inside = samplePos.x >= boundingRect[0] && samplePos.x <= boundingRect[0] + boundingRect[2] &&
samplePos.y >= boundingRect[1] && samplePos.y <= boundingRect[1] + boundingRect[3];
if (inside) {
color.r += brightnessBoost * 0.5;
color.g += brightnessBoost * 0.3;
color.b += brightnessBoost * 0.6;
}
return color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment