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

63 lines
2.2 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-19 00:05:53 +10:00
float3 world;
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-18 01:20:19 +10:00
device const ShaderVertex* vtx [[buffer(VertexShaderInputIdxVertices)]],
device const VertexShaderInstance* i [[buffer(VertexShaderInputIdxInstance)]],
constant VertexShaderUniforms& u [[buffer(VertexShaderInputIdxUniforms)]]
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;
2024-08-06 16:51:29 +10:00
2024-08-05 00:08:16 -07:00
FragmentInput out;
2024-08-19 00:05:53 +10:00
out.position = u.projView * world;
out.world = world.xyz;
2024-08-19 07:59:36 +10:00
out.color = half4(i[instanceID].color);
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)]],
2024-08-18 01:20:19 +10:00
constant FragmentShaderUniforms& u [[buffer(FragmentShaderInputIdxUniforms)]]
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);
2024-08-19 00:24:01 +10:00
// Components for blinn-phong & fresnel
2024-08-19 00:05:53 +10:00
auto lightVec = -u.directionalLight;
auto eyeVector = metal::normalize(u.cameraPosition - in.world);
auto halfDir = metal::normalize(lightVec + eyeVector);
2024-08-19 00:24:01 +10:00
// Compute diffuse component
float lambert = metal::dot(normal, lightVec);
float diffuseAmount = metal::max(0.0, lambert);
half4 diffuse = half4(u.diffuseColor) * diffuseAmount;
// Compute specular component (blinn-phong)
2024-08-19 00:05:53 +10:00
float specularAngle = metal::max(0.0, metal::dot(halfDir, normal));
float specularTerm = metal::pow(specularAngle, u.specularIntensity);
2024-08-19 00:24:01 +10:00
// smoothstep hack to ensure highlight tapers gracefully at grazing angles
float specularAmount = specularTerm * metal::smoothstep(0, 2, lambert * u.specularIntensity);
half4 specular = half4(u.specularColor) * specularAmount;
2024-08-19 00:05:53 +10:00
2024-08-19 00:24:01 +10:00
// Sample texture & vertex color to get albedo
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;
2024-08-19 00:24:01 +10:00
return albedo * (half4(u.ambientColor) + diffuse) + specular;
2024-08-05 00:08:16 -07:00
}