Skip to content

Instantly share code, notes, and snippets.

@nbhasin2
Created March 13, 2025 05:41
Show Gist options
  • Select an option

  • Save nbhasin2/ef4fbfc41133dc8e612dd631391002bb to your computer and use it in GitHub Desktop.

Select an option

Save nbhasin2/ef4fbfc41133dc8e612dd631391002bb to your computer and use it in GitHub Desktop.
metal13.metal
#include <metal_stdlib>
using namespace metal;
struct VertexOut {
float4 position [[position]];
float2 texCoord;
};
vertex VertexOut multi_vertex(uint vertexID [[vertex_id]]) {
float2 positions[4] = {
{-1, -1}, // Bottom-left
{ 1, -1}, // Bottom-right
{-1, 1}, // Top-left
{ 1, 1} // Top-right
};
float2 texCoords[4] = {
{0, 0}, // Bottom-left
{1, 0}, // Bottom-right
{0, 1}, // Top-left
{1, 1} // Top-right
};
VertexOut out;
out.position = float4(positions[vertexID], 0, 1);
out.texCoord = texCoords[vertexID];
return out;
}
fragment half4 multi_fragment(VertexOut in [[stage_in]], constant float &time [[buffer(0)]]) {
// Three animated control points with different movement patterns
float2 p1 = float2(
0.3 + 0.25 * sin(time * 0.8),
0.3 + 0.25 * cos(time * 0.8)
);
float2 p2 = float2(
0.7 + 0.25 * cos(time * 0.7),
0.7 + 0.25 * sin(time * 0.7)
);
float2 p3 = float2(
0.5 + 0.25 * sin(time * 0.6 + 2.0),
0.5 + 0.25 * cos(time * 0.9 + 1.0)
);
// Calculate distance to each point
float d1 = distance(in.texCoord, p1);
float d2 = distance(in.texCoord, p2);
float d3 = distance(in.texCoord, p3);
// Maximum influence distance
float maxDist = 0.3;
// Calculate influence for each circle (0 to 1)
float i1 = 1.0 - min(d1 / maxDist, 1.0);
float i2 = 1.0 - min(d2 / maxDist, 1.0);
float i3 = 1.0 - min(d3 / maxDist, 1.0);
// Apply smooth falloff
i1 = pow(max(i1, 0.0), 2.0);
i2 = pow(max(i2, 0.0), 2.0);
i3 = pow(max(i3, 0.0), 2.0);
// Define colors for each circle
half3 color1 = half3(1.0, 0.3, 0.1); // Orange-red
half3 color2 = half3(0.1, 0.6, 1.0); // Blue
half3 color3 = half3(0.1, 0.9, 0.3); // Green
half3 backgroundColor = half3(0.1, 0.1, 0.1); // Dark gray
// Blend all colors based on their influence
half3 finalColor = backgroundColor;
if (i1 > 0.0) finalColor = mix(finalColor, color1, i1);
if (i2 > 0.0) finalColor = mix(finalColor, color2, i2);
if (i3 > 0.0) finalColor = mix(finalColor, color3, i3);
return half4(finalColor, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment