|
#include <metal_stdlib> |
|
#include <SwiftUI/SwiftUI.h> |
|
using namespace metal; |
|
|
|
[[ stitchable ]] half4 Sticker(float2 position, SwiftUI::Layer layer, float2 size, float2 offset) { |
|
offset.x *= 3; |
|
float angle = offset.y * (360 / size.x) * 0.02; // adjust multiplier to control sensitivity |
|
|
|
half4 original_color = layer.sample(position); |
|
|
|
float2 centered = float2(offset.x / 2 - position.x, position.y); |
|
float2 sample_position = float2(centered.x * cos(angle) - centered.y * sin(angle), |
|
centered.x * sin(angle) + centered.y * cos(angle)); |
|
|
|
sample_position.x += offset.x / 2; |
|
|
|
half4 flip_side_color = layer.sample(sample_position); |
|
half4 result_color = flip_side_color; |
|
float clip_position = centered.x * cos(angle / 2) - centered.y * sin(angle / 2); |
|
|
|
if (clip_position < 0) { |
|
original_color = max(original_color - result_color, 0.0); |
|
result_color += original_color * (1 - result_color.a); |
|
float flip_start_point = sample_position.x - position.x; |
|
|
|
float inner_shadow_pow = offset.x + 1; |
|
half3 shadow_rgb = 0.5; |
|
float alpha = (1 - smoothstep(inner_shadow_pow, 1.0, flip_start_point + inner_shadow_pow)) * original_color.a * 0.25; |
|
result_color.rgb = mix(result_color.rgb, shadow_rgb, alpha); |
|
|
|
float lighting_length = 30; |
|
float shadow_length = 80; |
|
|
|
float flip_side_lighting_alpha = smoothstep(1.0, lighting_length, flip_start_point + lighting_length) * 0.2; |
|
result_color.rgb += flip_side_lighting_alpha * flip_side_color.a; |
|
|
|
float flip_side_shadow_alpha = smoothstep(1.0, shadow_length, flip_start_point + shadow_length) * 0.1; |
|
result_color.rgb = mix(result_color.rgb, shadow_rgb, flip_side_shadow_alpha * flip_side_color.a); |
|
|
|
return result_color; |
|
} |
|
|
|
if (original_color.a != 0) { |
|
float flip_start_point = sample_position.x - position.x; |
|
float shadow_alpha = smoothstep(1.0, 40, flip_start_point); |
|
shadow_alpha = 1.0 - shadow_alpha; |
|
half4 shadow_color = half4(0.0, 0.0, 0.0, shadow_alpha * 0.2); |
|
return shadow_color; |
|
} |
|
return half4(0); |
|
} |