forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.metal
More file actions
93 lines (77 loc) · 2.24 KB
/
Example.metal
File metadata and controls
93 lines (77 loc) · 2.24 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
typedef struct
{
float4x4 vpMatrix;
float4 viewPos;
float4 fogColorAndDensity;
float2 animVec;
}
Settings;
typedef struct
{
float3 position [[attribute(0)]];
float2 texCoord [[attribute(1)]];
float3 color [[attribute(2)]];
float arrayLayer [[attribute(3)]];
float4 wMatrix1 [[attribute(4)]];
float4 wMatrix2 [[attribute(5)]];
float4 wMatrix3 [[attribute(6)]];
float4 wMatrix4 [[attribute(7)]];
}
VertexIn;
typedef struct
{
float4 position [[position]];
float4 worldPos;
float3 color;
float2 texCoord;
uint arrayLayer;
}
VertexOut;
vertex VertexOut VS(
VertexIn vert [[stage_in]],
constant Settings& settings [[buffer(2)]],
uint instID [[instance_id]])
{
VertexOut outp;
float2 offset = settings.animVec * vert.position.y;
float4 coord = float4(
vert.position.x + offset.x,
vert.position.y,
vert.position.z + offset.y,
1.0
);
float4x4 wMatrix = float4x4(
vert.wMatrix1,
vert.wMatrix2,
vert.wMatrix3,
vert.wMatrix4
);
outp.worldPos = wMatrix * coord;
outp.position = settings.vpMatrix * outp.worldPos;
outp.texCoord = vert.texCoord;
outp.arrayLayer = static_cast<uint>(vert.arrayLayer);
outp.color = vert.color;
return outp;
}
fragment float4 PS(
VertexOut inp [[stage_in]],
constant Settings& settings [[buffer(2)]],
texture2d_array<float> tex [[texture(3)]],
sampler smpl [[sampler(4)]])
{
// Sample albedo texture
float4 color = tex.sample(smpl, inp.texCoord, inp.arrayLayer);
// Apply alpha clipping
if (color.a < 0.5)
discard_fragment();
// Compute fog density
float viewDist = distance(settings.viewPos, inp.worldPos);
float fog = viewDist*settings.fogColorAndDensity.a;
fog = 1.0 - 1.0/exp(fog*fog);
// Interpolate between albedo and fog color
color.rgb = mix(color.rgb * inp.color, settings.fogColorAndDensity.rgb, fog);
return color;
}