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-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)]],
|
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:05:53 +10:00
|
|
|
auto lightVec = -u.directionalLight;
|
|
|
|
float lambert = metal::dot(normal, lightVec);
|
2024-08-18 01:10:25 +10:00
|
|
|
float diffuse = metal::max(0.0, lambert);
|
|
|
|
|
2024-08-19 00:05:53 +10:00
|
|
|
auto eyeVector = metal::normalize(u.cameraPosition - in.world);
|
|
|
|
auto halfDir = metal::normalize(lightVec + eyeVector);
|
|
|
|
float specularAngle = metal::max(0.0, metal::dot(halfDir, normal));
|
|
|
|
float specularTerm = metal::pow(specularAngle, u.specularIntensity);
|
|
|
|
half4 specularAmount = specularTerm * metal::smoothstep(0, 2, lambert * u.specularIntensity);
|
|
|
|
|
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:05:53 +10:00
|
|
|
return albedo * diffuse + half4(u.specularColor) * specularAmount;
|
2024-08-05 00:08:16 -07:00
|
|
|
}
|