-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbr.hlsl
67 lines (56 loc) · 1.94 KB
/
pbr.hlsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Physically Based Rendering
// Adapted from 2017-2018 Michał Siejak
// Physically Based shading model: Lambetrtian diffuse BRDF + Cook-Torrance microfacet specular BRDF + IBL for ambient.
// This implementation is based on "Real Shading in Unreal Engine 4" SIGGRAPH 2013 course notes by Epic Games.
// See: http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
#ifndef PI
#define PI 3.141592
#endif
#ifndef Epsilon
#define Epsilon 0.00001
#endif
struct PbrVertex
{
float3 Position;
float3 Normal;
float3 Tangent;
float3 Bitangent;
float2 TexCoord;
float Selected;
float __padding;
};
// Constant normal incidence Fresnel factor for all dielectrics.
#define Fdielectric 0.04
// GGX/Towbridge-Reitz normal distribution function.
// Uses Disney's reparametrization of alpha = roughness^2.
float ndfGGX(float cosLh, float roughness)
{
float alpha = roughness * roughness;
float alphaSq = alpha * alpha;
float denom = (cosLh * cosLh) * (alphaSq - 1.0) + 1.0;
return alphaSq / (PI * denom * denom);
}
// Single term for separable Schlick-GGX below.
float gaSchlickG1(float cosTheta, float k)
{
return cosTheta / (cosTheta * (1.0 - k) + k);
}
// Schlick-GGX approximation of geometric attenuation function using Smith's method.
float gaSchlickGGX(float cosLi, float cosLo, float roughness)
{
float r = roughness + 1.0;
float k = (r * r) / 8.0; // Epic suggests using this roughness remapping for analytic lights.
return gaSchlickG1(cosLi, k) * gaSchlickG1(cosLo, k);
}
// Shlick's approximation of the Fresnel factor.
float3 fresnelSchlick(float3 F0, float cosTheta)
{
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
}
// Returns number of mipmap levels for specular IBL environment map.
uint querySpecularTextureLevels(Texture2D specularTexture)
{
uint width, height, levels;
specularTexture.GetDimensions(0, width, height, levels);
return levels;
}