#include using namespace metal; struct PaperVertex { float3 position; float3 normal; }; /// `light.xyz` is the view direction, `light.w` the shade of a face seen edge-on. struct Shading { float4 front; float4 back; float4 light; }; struct Fragment { float4 position [[position]]; float3 normal [[flat]]; }; vertex Fragment paperVertex(uint id [[vertex_id]], const device PaperVertex *vertices [[buffer(0)]], constant float4x4 &viewProjection [[buffer(1)]]) { Fragment out; out.position = viewProjection * float4(vertices[id].position, 1); out.normal = vertices[id].normal; return out; } fragment float4 paperFragment(Fragment in [[stage_in]], bool front [[front_facing]], constant Shading &shading [[buffer(0)]]) { float facing = abs(dot(in.normal, shading.light.xyz)); float shade = 1 - (1 - shading.light.w) * (1 - facing); float4 colour = front ? shading.front : shading.back; return float4(colour.rgb * shade, colour.a); }