Files
voxelotl-engine/Sources/Voxelotl/shader.metal

47 lines
1.3 KiB
Metal
Raw Normal View History

2024-08-05 00:19:49 -07:00
#include "shadertypes.h"
2024-08-05 00:08:16 -07:00
#include <metal_stdlib>
struct FragmentInput {
float4 position [[position]];
2024-08-18 01:10:25 +10:00
float3 normal;
2024-08-05 20:09:33 +10:00
float2 texCoord;
2024-08-09 21:16:07 +10:00
half4 color;
2024-08-05 00:08:16 -07:00
};
vertex FragmentInput vertexMain(
uint vertexID [[vertex_id]],
2024-08-09 21:16:07 +10:00
uint instanceID [[instance_id]],
2024-08-06 16:51:29 +10:00
device const ShaderVertex* vtx [[buffer(ShaderInputIdxVertices)]],
2024-08-09 21:16:07 +10:00
device const ShaderInstance* i [[buffer(ShaderInputIdxInstance)]],
2024-08-06 16:51:29 +10:00
constant ShaderUniforms& u [[buffer(ShaderInputIdxUniforms)]]
2024-08-05 00:08:16 -07:00
) {
2024-08-06 16:51:29 +10:00
auto position = vtx[vertexID].position;
2024-08-09 21:16:07 +10:00
auto world = i[instanceID].model * position;
auto ndc = u.projView * world;
2024-08-06 16:51:29 +10:00
2024-08-05 00:08:16 -07:00
FragmentInput out;
2024-08-09 21:16:07 +10:00
out.position = ndc;
2024-08-13 08:38:21 +10:00
out.color = half4(i[instanceID].color) / 255.0;
2024-08-18 01:10:25 +10:00
out.normal = (i[instanceID].normalModel * vtx[vertexID].normal).xyz;
2024-08-05 20:09:33 +10:00
out.texCoord = vtx[vertexID].texCoord;
2024-08-05 00:08:16 -07:00
return out;
}
2024-08-05 20:09:33 +10:00
fragment half4 fragmentMain(
FragmentInput in [[stage_in]],
2024-08-18 01:10:25 +10:00
metal::texture2d<half, metal::access::sample> texture [[texture(0)]],
constant ShaderUniforms& u [[buffer(ShaderInputIdxUniforms)]]
2024-08-05 20:09:33 +10:00
) {
2024-08-09 21:16:07 +10:00
constexpr metal::sampler sampler(metal::address::repeat, metal::filter::nearest);
2024-08-18 01:10:25 +10:00
auto normal = metal::normalize(in.normal);
float lambert = metal::dot(normal, -u.directionalLight);
float diffuse = metal::max(0.0, lambert);
2024-08-09 21:16:07 +10:00
half4 albedo = texture.sample(sampler, in.texCoord);
2024-08-18 01:10:25 +10:00
albedo *= in.color;
return albedo * diffuse;
2024-08-05 00:08:16 -07:00
}