forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.metal
79 lines (59 loc) · 1.43 KB
/
Example.metal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Metal shader
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct Settings
{
float4x4 wMatrix;
float4x4 vpMatrix;
float4 lightDir;
float4 diffuse;
};
// VERTEX SHADER STENCIL-BUFFER
struct VStencilIn
{
float3 position [[attribute(0)]];
};
struct VStencilOut
{
float4 position [[position]];
};
vertex VStencilOut VStencil(
VStencilIn inp [[stage_in]],
constant Settings& settings [[buffer(1)]])
{
VStencilOut outp;
outp.position = settings.vpMatrix * (settings.wMatrix * float4(inp.position, 1));
return outp;
}
// VERTEX SHADER SCENE
struct VSceneIn
{
float3 position [[attribute(0)]];
float3 normal [[attribute(1)]];
};
struct VSceneOut
{
float4 position [[position]];
float4 normal;
};
vertex VSceneOut VScene(
VSceneIn inp [[stage_in]],
constant Settings& settings [[buffer(1)]])
{
VSceneOut outp;
outp.position = settings.vpMatrix * (settings.wMatrix * float4(inp.position, 1));
outp.normal = settings.wMatrix * float4(inp.normal, 0);
return outp;
}
// PIXEL SHADER SCENE
fragment float4 PScene(
VSceneOut inp [[stage_in]],
constant Settings& settings [[buffer(1)]])
{
// Compute lighting
float3 normal = normalize(inp.normal.xyz);
float NdotL = max(0.2, dot(normal, -settings.lightDir.xyz));
// Set final output color
return float4(settings.diffuse.rgb * NdotL, 1.0);
}