Harbor

branch main
showing the latest snapshot on main
pbr_fragment.hlsl 1.6 KB · Plain text
shader/tests/golden/pbr_fragment.hlsl 0644 Raw
struct Material {
	float3 albedo;
	float metallic;
	float roughness;
};
cbuffer material_CB : register(b0, space0) {
	Material material;
};
cbuffer light_dir_CB : register(b1, space0) {
	float3 light_dir;
};
cbuffer camera_pos_CB : register(b2, space0) {
	float3 camera_pos;
};
Texture2D albedo_tex_tex : register(t3, space0);
SamplerState albedo_tex_samp : register(s4, space0);

struct FragmentInput {
	float4 _sv_position : SV_Position;
	float2 uv : TEXCOORD0;
	float3 normal : TEXCOORD1;
	float3 world_pos : TEXCOORD2;
};

struct FragmentOutput {
	float4 color : SV_Target0;
};

float3 fresnel_schlick(float cos_theta, float3 f0) {
	return (f0 + ((float3(1.0, 1.0, 1.0) - f0) * pow((1.0 - cos_theta), 5.0)));
}

float3 compute_lighting(float3 normal, float3 view_dir, float3 albedo) {
	float3 n = normalize(normal);
	float3 l = normalize(light_dir);
	float3 h = normalize((l + view_dir));
	float ndotl = max(dot(n, l), 0.0);
	float ndoth = max(dot(n, h), 0.0);
	float3 f0 = lerp(float3(0.04, 0.04, 0.04), albedo, material.metallic);
	float3 f = fresnel_schlick(max(dot(h, view_dir), 0.0), f0);
	float3 specular = (f * pow(ndoth, ((1.0 - material.roughness) * 256.0)));
	float3 diffuse = ((albedo * ndotl) * (float3(1.0, 1.0, 1.0) - f));
	return (diffuse + specular);
}

FragmentOutput fs_main(FragmentInput input) {
	FragmentOutput __luma_output;
	float4 tex_color = albedo_tex_tex.Sample(albedo_tex_samp, input.uv);
	float3 albedo = (tex_color.rgb * material.albedo);
	float3 view_dir = normalize((camera_pos - input.world_pos));
	float3 color = compute_lighting(input.normal, view_dir, albedo);
	__luma_output.color = float4(color, tex_color.a);
	return __luma_output;
}