voxelotl-engine/Sources/Voxelotl/shader.metal

63 lines
2.2 KiB
Metal
Raw Normal View History

2024-08-05 07:19:49 +00:00
#include "shadertypes.h"
2024-08-05 07:08:16 +00:00
#include <metal_stdlib>
struct FragmentInput {
float4 position [[position]];
2024-08-18 14:05:53 +00:00
float3 world;
2024-08-17 15:10:25 +00:00
float3 normal;
2024-08-05 10:09:33 +00:00
float2 texCoord;
float4 color;
2024-08-05 07:08:16 +00:00
};
vertex FragmentInput vertexMain(
uint vertexID [[vertex_id]],
2024-08-09 11:16:07 +00:00
uint instanceID [[instance_id]],
2024-08-17 15:20:19 +00:00
device const ShaderVertex* vtx [[buffer(VertexShaderInputIdxVertices)]],
device const VertexShaderInstance* i [[buffer(VertexShaderInputIdxInstance)]],
constant VertexShaderUniforms& u [[buffer(VertexShaderInputIdxUniforms)]]
2024-08-05 07:08:16 +00:00
) {
2024-08-06 06:51:29 +00:00
auto position = vtx[vertexID].position;
auto world = i[instanceID].model * float4(position, 1);
2024-08-06 06:51:29 +00:00
2024-08-05 07:08:16 +00:00
FragmentInput out;
2024-08-18 14:05:53 +00:00
out.position = u.projView * world;
out.world = world.xyz;
2024-09-01 13:34:32 +00:00
out.color = vtx[vertexID].color * i[instanceID].color;
out.normal = (i[instanceID].normalModel * float4(vtx[vertexID].normal, 0)).xyz;
2024-08-05 10:09:33 +00:00
out.texCoord = vtx[vertexID].texCoord;
2024-08-05 07:08:16 +00:00
return out;
}
fragment float4 fragmentMain(
2024-08-05 10:09:33 +00:00
FragmentInput in [[stage_in]],
2024-08-17 15:10:25 +00:00
metal::texture2d<half, metal::access::sample> texture [[texture(0)]],
2024-08-17 15:20:19 +00:00
constant FragmentShaderUniforms& u [[buffer(FragmentShaderInputIdxUniforms)]]
2024-08-05 10:09:33 +00:00
) {
2024-08-09 11:16:07 +00:00
constexpr metal::sampler sampler(metal::address::repeat, metal::filter::nearest);
2024-08-17 15:10:25 +00:00
auto normal = metal::normalize(in.normal);
2024-08-18 14:24:01 +00:00
// Components for blinn-phong & fresnel
2024-08-18 14:05:53 +00:00
auto lightVec = -u.directionalLight;
auto eyeVector = metal::normalize(u.cameraPosition - in.world);
auto halfDir = metal::normalize(lightVec + eyeVector);
2024-08-18 14:24:01 +00:00
// Compute diffuse component
float lambert = metal::dot(normal, lightVec);
float diffuseAmount = metal::max(0.0, lambert);
float4 diffuse = u.diffuseColor * diffuseAmount;
2024-08-18 14:24:01 +00:00
// Compute specular component (blinn-phong)
2024-08-18 14:05:53 +00:00
float specularAngle = metal::max(0.0, metal::dot(halfDir, normal));
float specularTerm = metal::pow(specularAngle, u.specularIntensity);
2024-08-18 14:24:01 +00:00
// smoothstep hack to ensure highlight tapers gracefully at grazing angles
float specularAmount = specularTerm * metal::smoothstep(0, 2, lambert * u.specularIntensity);
float4 specular = u.specularColor * specularAmount;
2024-08-18 14:05:53 +00:00
2024-08-18 14:24:01 +00:00
// Sample texture & vertex color to get albedo
float4 albedo = float4(texture.sample(sampler, in.texCoord));
2024-08-17 15:10:25 +00:00
albedo *= in.color;
return albedo * (u.ambientColor + diffuse) + specular;
2024-08-05 07:08:16 +00:00
}