-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNC_Shader.metal
52 lines (41 loc) · 1.25 KB
/
CNC_Shader.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
#include <metal_stdlib>
using namespace metal;
struct VertexInput
{
float3 m_position [[attribute(0)]];
float2 m_uv [[attribute(1)]];
float m_angle [[attribute(2)]];
};
struct VertexOutput
{
float4 m_position [[position]];
float2 m_uv;
};
struct UniformData
{
float4x4 m_projection2d;
float2 m_screenSize;
};
constexpr sampler textureSampler(mag_filter::linear, min_filter::linear);
vertex VertexOutput VertexShader( VertexInput in [[stage_in]],
constant UniformData& uniform [[buffer(1)]] )
{
VertexOutput out;
float angle = -in.m_angle;
// rotate the vertices
float2x2 rotationMatrix = {
{ cos(angle), -sin(angle) }, // important to define row major
{ sin(angle), cos(angle) } // important to define row major
};
float4 position = float4( in.m_position, 1.0 );
out.m_position = uniform.m_projection2d * position;
out.m_position.xy = out.m_position.xy * rotationMatrix;
out.m_uv = in.m_uv;
return out;
}
fragment float4 FragmentShader( VertexOutput in [[stage_in]],
texture2d<float> image )
{
float4 color = image.sample( textureSampler, in.m_uv );
return color;
}