Shader.metal (2000B)
1 // 2 // Shaders.metal 3 // 4 5 #include <metal_stdlib> 6 using namespace metal; 7 8 // https://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner 9 float hashVal(float2 p) { 10 return fract(sin(dot(p, float2(12.9898, 78.233))) * 43758.5453); 11 } 12 13 float noiseFrom(float2 p) { 14 float2 intVal = floor(p); 15 float2 fraction = fract(p); 16 fraction = fraction * fraction * (3.0 - 2.0 * fraction); 17 float a = hashVal(intVal); 18 float b = hashVal(intVal + float2(1.0, 0.0)); 19 float c = hashVal(intVal + float2(0.0, 1.0)); 20 float d = hashVal(intVal + float2(1.0, 1.0)); 21 return mix(mix(a, b, fraction.x), mix(c, d, fraction.x), fraction.y); 22 } 23 24 // MARK: - 25 /// The first two parameters are required by SwiftUI: it will automatically pass the position of the view, and its current color. 26 /// The remaining parameters are created by us, and need to be passed by the caller. 27 [[ stitchable ]] half4 liquidBack(float2 position, half4 color, float2 size, float time) { 28 /// we don't use the original color 29 float t = time * 0.3; 30 float2 uv2 = (position * 2.0 - size) * 2.0 / min(size.x, size.y); 31 float noisA = noiseFrom(uv2 + float2(t, t * 0.6)); 32 float noisB = noiseFrom(uv2 + noisA * 1.5 + float2(-t * 0.4, t * 0.3)); 33 float noise = noiseFrom(uv2 * 1.5 + noisB * 1.2 + float2(t * 0.2, -t * 0.5)); 34 35 float3 silver = float3(0.2, 0.2, 0.25); 36 float3 highlight = float3(0.5, 0.5, 0.6); 37 float3 shadow = float3(0.02, 0.01, 0.05); 38 float3 tint = float3(0.706, 0.773, 1.0); // Taler dark:(0.0, 0.259, 0.702) is too blue, but light (0.706, 0.773, 1.0) is fine 39 40 float chrome = pow(noise * 0.5 + 0.5, 0.6); 41 float3 color3 = mix(shadow, silver, chrome); 42 color3 = mix(color3, highlight, smoothstep(0.8, 0.98, chrome)); 43 color3 += tint * smoothstep(0.3, 0.6, noisA) * 0.15; 44 45 // Add some highlights 46 float spec = pow(max(chrome, 0.0), 12.0); 47 color3 += float3(0.6, 0.6, 0.8) * spec * 0.3; 48 49 return half4(half3(color3), 1.0); 50 }