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
#include <metal_stdlib>
using namespace metal;
struct Camera {
float4x4 view_proj;
float3 position;
};
struct Lighting {
float3 direction;
float3 color;
float intensity;
};
struct VertexOutput {
float4 position [[position]];
float2 uv [[user(locn0)]];
};
struct FragmentOutput {
float4 color [[user(locn0)]];
};
fragment FragmentOutput fs_main(VertexOutput input [[stage_in]],
constant Lighting& lighting [[buffer(0)]],
texture2d<float> diffuse_tex_tex [[texture(0)]],
sampler diffuse_tex_samp [[sampler(0)]],
texture2d<float> normal_tex_tex [[texture(1)]],
sampler normal_tex_samp [[sampler(1)]]) {
FragmentOutput __luma_output;
float4 diffuse = diffuse_tex_tex.sample(diffuse_tex_samp, input.uv);
float4 normal_sample = normal_tex_tex.sample(normal_tex_samp, input.uv);
float3 n = normalize(((normal_sample.xyz * 2.0) - float3(1.0, 1.0, 1.0)));
float3 l = normalize(lighting.direction);
float ndotl = max(dot(n, l), 0.0);
float3 lit = (((diffuse.rgb * lighting.color) * ndotl) * lighting.intensity);
__luma_output.color = float4(lit, diffuse.a);
return __luma_output;
}