Files
vibe5_metal/data/shaders/crt.metal
Sergio Valor 7cccedb5fb Extraer shaders a archivos .metal separados para mejor organización
- Crear directorio data/shaders/ para organizar todos los shaders MSL
- Extraer shaders embebidos a archivos individuales:
  * background.metal - Shader de fondo degradado
  * triangle.metal - Shader de triángulo multicolor
  * sprite.metal - Shader de sprites con vertex color tinting
  * crt.metal - Shader CRT post-processing completo
- Modificar main.cpp para cargar shaders desde archivos:
  * Usar stringWithContentsOfFile para leer código fuente
  * Compilar dinámicamente con newLibraryWithSource
  * Manejo robusto de errores de lectura y compilación
- Eliminar 351 líneas de strings embebidos de main.cpp
- Mantener funcionalidad completa: CRT + sprites + fondo + triángulo

Beneficios:
- Shaders editables sin recompilar ejecutable
- Mejor organización y mantenimiento del código
- Syntax highlighting completo en editores
- Reutilización de shaders en otros proyectos
- Desarrollo más ágil de efectos visuales

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-28 18:40:28 +02:00

158 lines
4.2 KiB
Metal

#include <metal_stdlib>
using namespace metal;
// Parámetros del CRT shader
#define CURVATURE_X 0.05
#define CURVATURE_Y 0.1
#define MASK_BRIGHTNESS 0.80
#define SCANLINE_WEIGHT 6.0
#define SCANLINE_GAP_BRIGHTNESS 0.12
#define BLOOM_FACTOR 3.5
#define INPUT_GAMMA 2.4
#define OUTPUT_GAMMA 2.2
// Features habilitadas
#define SCANLINES
#define MULTISAMPLE
#define GAMMA
// #define FAKE_GAMMA
// #define CURVATURE
// #define SHARPER
#define MASK_TYPE 2
struct CRTVertexOut {
float4 position [[position]];
float2 texCoord;
};
vertex CRTVertexOut crt_vertex_main(uint vertexID [[vertex_id]]) {
CRTVertexOut out;
// Fullscreen quad con coordenadas de textura
float2 positions[6] = {
float2(-1.0, -1.0), // Bottom left
float2( 1.0, -1.0), // Bottom right
float2(-1.0, 1.0), // Top left
float2( 1.0, -1.0), // Bottom right
float2( 1.0, 1.0), // Top right
float2(-1.0, 1.0) // Top left
};
float2 texCoords[6] = {
float2(0.0, 1.0), // Bottom left
float2(1.0, 1.0), // Bottom right
float2(0.0, 0.0), // Top left
float2(1.0, 1.0), // Bottom right
float2(1.0, 0.0), // Top right
float2(0.0, 0.0) // Top left
};
out.position = float4(positions[vertexID], 0.0, 1.0);
out.texCoord = texCoords[vertexID];
return out;
}
float CalcScanLineWeight(float dist) {
return max(1.0 - dist * dist * SCANLINE_WEIGHT, SCANLINE_GAP_BRIGHTNESS);
}
float CalcScanLine(float dy, float filterWidth) {
float scanLineWeight = CalcScanLineWeight(dy);
#ifdef MULTISAMPLE
scanLineWeight += CalcScanLineWeight(dy - filterWidth);
scanLineWeight += CalcScanLineWeight(dy + filterWidth);
scanLineWeight *= 0.3333333;
#endif
return scanLineWeight;
}
fragment float4 crt_fragment_main(CRTVertexOut in [[stage_in]],
texture2d<float> sceneTexture [[texture(0)]],
sampler sceneSampler [[sampler(0)]]) {
float2 TextureSize = float2(320.0, 240.0); // Resolución virtual del CRT
float filterWidth = (768.0 / 240.0) / 3.0;
float2 texcoord = in.texCoord;
// Convertir a píxeles
float2 texcoordInPixels = texcoord * TextureSize;
#ifdef SHARPER
float2 tempCoord = floor(texcoordInPixels) + 0.5;
float2 coord = tempCoord / TextureSize;
float2 deltas = texcoordInPixels - tempCoord;
float scanLineWeight = CalcScanLine(deltas.y, filterWidth);
float2 signs = sign(deltas);
deltas.x *= 2.0;
deltas = deltas * deltas;
deltas.y = deltas.y * deltas.y;
deltas.x *= 0.5;
deltas.y *= 8.0;
deltas /= TextureSize;
deltas *= signs;
float2 tc = coord + deltas;
#else
float tempY = floor(texcoordInPixels.y) + 0.5;
float yCoord = tempY / TextureSize.y;
float dy = texcoordInPixels.y - tempY;
float scanLineWeight = CalcScanLine(dy, filterWidth);
float signY = sign(dy);
dy = dy * dy;
dy = dy * dy;
dy *= 8.0;
dy /= TextureSize.y;
dy *= signY;
float2 tc = float2(texcoord.x, yCoord + dy);
#endif
// Samplear la textura de escena
float3 colour = sceneTexture.sample(sceneSampler, tc).rgb;
#ifdef SCANLINES
#ifdef GAMMA
#ifdef FAKE_GAMMA
colour = colour * colour;
#else
colour = pow(colour, float3(INPUT_GAMMA));
#endif
#endif
scanLineWeight *= BLOOM_FACTOR;
colour *= scanLineWeight;
#ifdef GAMMA
#ifdef FAKE_GAMMA
colour = sqrt(colour);
#else
colour = pow(colour, float3(1.0 / OUTPUT_GAMMA));
#endif
#endif
#endif
// Shadow mask
#if MASK_TYPE == 0
return float4(colour, 1.0);
#else
#if MASK_TYPE == 1
float whichMask = fract((in.position.x * 1.0001) * 0.5);
float3 mask;
if (whichMask < 0.5) {
mask = float3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);
} else {
mask = float3(1.0, MASK_BRIGHTNESS, 1.0);
}
#elif MASK_TYPE == 2
float whichMask = fract((in.position.x * 1.0001) * 0.3333333);
float3 mask = float3(MASK_BRIGHTNESS, MASK_BRIGHTNESS, MASK_BRIGHTNESS);
if (whichMask < 0.3333333) {
mask.x = 1.0;
} else if (whichMask < 0.6666666) {
mask.y = 1.0;
} else {
mask.z = 1.0;
}
#endif
return float4(colour * mask, 1.0);
#endif
}