Skip to content

Instantly share code, notes, and snippets.

@omarojo
Last active June 23, 2022 18:24
Show Gist options
  • Save omarojo/6c9eaee96b76b15fb532f76bef713c72 to your computer and use it in GitHub Desktop.
Save omarojo/6c9eaee96b76b15fb532f76bef713c72 to your computer and use it in GitHub Desktop.
//
// G8Shaders.metal
// GenerateMetal-iOS
//
// Created by Omar Juarez Ortiz on 2019-02-25.
// Copyright © 2019 Generate Software Inc. All rights reserved.
//
#include <metal_stdlib>
#include "MTIShaderLib.h"
#include "G8ShaderFunctions.h"
using namespace metal;
namespace metalpetal {
namespace g8shaders {
fragment float4 glitch1(VertexOut vertexIn [[stage_in]],
texture2d<float, access::sample> sourceTexture [[texture(0)]],
sampler sourceSampler [[sampler(0)]],
constant float &time [[buffer(0)]]){
float2 resolution = float2(sourceTexture.get_width(), sourceTexture.get_height());
float2 vUv = vertexIn.textureCoordinate;
float strength = smoothstep(interval * 0.5, interval, interval - mod(time, interval));
float2 shake = float2(strength * 8.0 + 0.5) * float2(
random(float2(time)) * 2.0 - 1.0,
random(float2(time * 2.0)) * 2.0 - 1.0
) / resolution;
float y = vUv.y * resolution.y;
float rgbWave = (
noise3D(float3(0.0, y * 0.01, time * 400.0)) * (2.0 + strength * 32.0)
* noise3D(float3(0.0, y * 0.02, time * 200.0)) * (1.0 + strength * 4.0)
+ step(0.9995, sin(y * 0.005 + time * 1.6)) * 12.0
+ step(0.9999, sin(y * 0.005 + time * 2.0)) * -18.0
) / resolution.x;
float rgbDiff = (6.0 + sin(time * 500.0 + vUv.y * 40.0) * (20.0 * strength + 1.0)) / resolution.x;
float rgbUvX = vUv.x + rgbWave;
float r = sourceTexture.sample(sourceSampler, float2(rgbUvX + rgbDiff, vUv.y) + shake).r;
float g = sourceTexture.sample(sourceSampler, float2(rgbUvX, vUv.y) + shake).g;
float b = sourceTexture.sample(sourceSampler, float2(rgbUvX - rgbDiff, vUv.y) + shake).b;
float whiteNoise = (random(vUv + fmod(time, 10.0)) * 2.0 - 1.0) * (0.15 + strength * 0.15);
float bnTime = floor(time * 20.0) * 200.0;
float noiseX = step((snoise3D(float3(0.0, vUv.x * 3.0, bnTime)) + 1.0) / 2.0, 0.12 + strength * 0.3);
float noiseY = step((snoise3D(float3(0.0, vUv.y * 3.0, bnTime)) + 1.0) / 2.0, 0.12 + strength * 0.3);
float bnMask = noiseX * noiseY;
float bnUvX = vUv.x + sin(bnTime) * 0.2 + rgbWave;
float bnR = sourceTexture.sample(sourceSampler, float2(bnUvX + rgbDiff, vUv.y)).r * bnMask;
float bnG = sourceTexture.sample(sourceSampler, float2(bnUvX, vUv.y)).g * bnMask;
float bnB = sourceTexture.sample(sourceSampler, float2(bnUvX - rgbDiff, vUv.y)).b * bnMask;
float4 blockNoise = float4(bnR, bnG, bnB, 1.0);
float bnTime2 = floor(time * 25.0) * 300.0;
float noiseX2 = step((snoise3D(float3(0.0, vUv.x * 2.0, bnTime2)) + 1.0) / 2.0, 0.12 + strength * 0.5);
float noiseY2 = step((snoise3D(float3(0.0, vUv.y * 8.0, bnTime2)) + 1.0) / 2.0, 0.12 + strength * 0.3);
float bnMask2 = noiseX2 * noiseY2;
float bnR2 = sourceTexture.sample(sourceSampler, float2(bnUvX + rgbDiff, vUv.y)).r * bnMask2;
float bnG2 = sourceTexture.sample(sourceSampler, float2(bnUvX, vUv.y)).g * bnMask2;
float bnB2 = sourceTexture.sample(sourceSampler, float2(bnUvX - rgbDiff, vUv.y)).b * bnMask2;
float4 blockNoise2 = float4(bnR2, bnG2, bnB2, 1.0);
float waveNoise = (sin(vUv.y * 1200.0) + 1.0) / 2.0 * (0.15 + strength * 0.2);
return float4(r, g, b, 1.0) * (1.0 - bnMask - bnMask2) + (whiteNoise + blockNoise + blockNoise2 - waveNoise);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment