Packages/FoldKit/Sources/Renderer/Shaders.metal
37 lines · 1123 bytes
1#include <metal_stdlib>
2using namespace metal;
3
4struct PaperVertex {
5 float3 position;
6 float3 normal;
7};
8
9/// `light.xyz` is the view direction, `light.w` the shade of a face seen edge-on.
10struct Shading {
11 float4 front;
12 float4 back;
13 float4 light;
14};
15
16struct Fragment {
17 float4 position [[position]];
18 float3 normal [[flat]];
19};
20
21vertex Fragment paperVertex(uint id [[vertex_id]],
22 const device PaperVertex *vertices [[buffer(0)]],
23 constant float4x4 &viewProjection [[buffer(1)]]) {
24 Fragment out;
25 out.position = viewProjection * float4(vertices[id].position, 1);
26 out.normal = vertices[id].normal;
27 return out;
28}
29
30fragment float4 paperFragment(Fragment in [[stage_in]],
31 bool front [[front_facing]],
32 constant Shading &shading [[buffer(0)]]) {
33 float facing = abs(dot(in.normal, shading.light.xyz));
34 float shade = 1 - (1 - shading.light.w) * (1 - facing);
35 float4 colour = front ? shading.front : shading.back;
36 return float4(colour.rgb * shade, colour.a);
37}