Lesson 11 – Physically-based rendering Image-based lighting PV227 – GPU Rendering Jiˇrí Chmelík, Jan ˇCejka Fakulta informatiky Masarykovy univerzity 26. 11. 2019 PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 1 / 29 Physically-based rendering (PBR) Physically-based rendering: Theory (cont.) Light & Lights BRDF Sensors (cameras, eyes) Image-based lighting PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 2 / 29 Light – quantities and units Quantities and units Radiant energy Radiant flux Irradiance Intensity Radiance Different equations use different quantities Convertible between each other PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 3 / 29 Light – quantities and units (cont.) Radiant energy (Q) “Energy of one photon” Joule: J Radiant flux, radiant power (Φ) “Energy per second” dQ/dt Watt: W = J/s Great to describe the power of lights like light bulb, area lights, . . . PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 4 / 29 Light – quantities and units (cont.) Irradiance (E) “Flux through area” dΦ/dA Watt per square meter: W/m2 Drops with the square of the distance Great to describe the power of strong distant lights like the sun Intensity (I) “Flux through a cone of directions” dΦ/dω Watt per steradian: W/sr Does not drop with the distance PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 5 / 29 Light – quantities and units (cont.) Radiance (L) “Flux through a cone of directions from an area” or “Flux through an area from a cone of directions” d2 Φ/dAproj dω Watt per square meter: W/m2 sr This is what sensors measure PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 6 / 29 BRDF Bidirectional Reflectance Distribution Function Describes the relation between the incoming and outcoming light f(l, v) = dLo(v) dE(l) Surface is illuminated from direction l with irradiance dE(l) It is reflected in various directions dLo(v) is the outcoming radiance in direction v PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 7 / 29 Properties of BRDF For non-area lights: f(l, v) = Lo(v) EL cos(θi) Energy conservation (for each incoming direction l : Ω f(l, v) cos θodωo < 1 PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 8 / 29 BRDF – Examples BRDF of diffuse light: f(l, v) = Cdiff π Note: this is what we use in shaders: dif = max(0.0, dot(N, L)) * Cdiff; PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 9 / 29 BRDF – Examples BRDF in the Cook-Torrance paper PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 10 / 29 BRDF – Examples BRDF in TriAce (presented at SIGGRAPH 2010 course) PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 11 / 29 BRDF – Examples BRDF in Frostbite (presented at SIGGRAPH 2014 course) PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 12 / 29 Sensors Many small sensors, each measure irradiance (flux through an area) over time System of lences and aperatures, which define the cone Lences in camera or eye, aperature of a camera, pupil in an eye So instead of irradiance, the system measures radiance Remember Depth-of-field techniques The result is the energy Conversion to the output signal (logarithmic etc.) Linear color space (RGB) vs. non-linear spaces (sRGB) Remember HDR, gamma correction PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 13 / 29 Image-based lighting Image-based lighting PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 14 / 29 Image-based lighting Use the light from a texture Environment textures, light probes Usually HDR cubemap textures Evaluate the integral using the BRDF to obtain the final color Sampling the directions Uniform sampling Non-uniform importance sampling Precomputation PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 15 / 29 Task: Implement image-based lighting Based on Real Shading in Unreal Engine 4 (presented at SIGGRAPH 2013 Course) With some changes, we use: Uniform sampling for diffuse light Importance sampling for specular light Cook-Torrance based material Fresnel as at the previous lecture Geometry attenuation as at the previous lecture Microfacet distribution is not important (according to the paper) PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 16 / 29 Legend to the following equations N, T, B are surface normal, tangent, and bitangent L is direction to the light, V is direction to the viewer H is half-vector, vector between the light and the viewer All dot products are non-negative, e.g.: max(0, N · L) For better result, clamp them to be non-zero, e.g. not less than 0.001, to avoid divisions by zero All vectors are normalized Fresnel(V · H) = F0 + (1 − F0)(1 − V · H)5 Geom. atten. G = min(1, 2·(N·H)·(N·V) (V·H) , 2·(N·H)·(N·L) (V·H) ) PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 17 / 29 Uniform sampling for diffuse lighting Output: Random direction r on a hemisphere (in the direction of z) Input: Two random numbers R.x and R.y, uniformly distributed in (0, 1) begin φ ← 2π · R.x cos(θ) ← R.y sin(θ) ← 1 − cos2(θ) r.x ← sin(θ) cos(φ) r.y ← sin(θ) sin(φ) r.z ← cos(θ) return r end PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 18 / 29 Computation of diffuse lighting Output: Diffuse color color begin color ← (0, 0, 0) forall diffuse samples i do R.x, R.y ← i-th pair of random numbers r ← random direction from R.x, R.y L ← r.x · T + r.y · B + r.z · N light ← SampleCubeTexture(L)/#samples color ← color + Cdiff · (N · L) · light end return color end PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 19 / 29 Non-uniform importance sampling for specular lighting Output: Random direction r on a hemisphere (in the direction of z) Input: Two random numbers R.x and R.y, uniformly distributed in (0, 1), roughtness m begin φ ← 2π · R.x cos(θ) ← 1−R.y 1+(m2−1)R.y sin(θ) ← 1 − cos2(θ) r.x ← sin(θ) cos(φ) r.y ← sin(θ) sin(φ) r.z ← cos(θ) return r end PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 20 / 29 Computation of specular lighting Output: Specular color color begin color ← (0, 0, 0) forall specular samples i do R.x, R.y ← another i-th pair of random numbers r ← random direction from R.x, R.y H ← r.x · T + r.y · B + r.z · N L ← reflect(−V, H) light ← SampleCubeTexture(L)/#samples F ← Fresnel(. . .) G ← GeometricAttenuation(. . .) color ← color + F · G · (V · H)/((N · H) · (N · V)) · light end return color end PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 21 / 29 Task: Test scene Test scene Materials: red/green/blue plastics, iron, copper, gold, alluminium, silver Roughness: 0.0, 0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5 PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 22 / 29 Task: IBL with diffuse lighting Task 1: Implement diffuse lighting Fragment shader object_fragment.glsl Try higher number of samples Try sampling higher mipmap-levels of cube map texture PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 23 / 29 Task: IBL with diffuse lighting Result, metals have zero diffuse light PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 24 / 29 Task: IBL with specular lighting Task 2: Implement specular lighting Try higher number of samples Try sampling higher mipmap-levels of cube map texture Try using a mask texture to change the roughness PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 25 / 29 Task: IBL with specular lighting Result, with masked roughness PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 26 / 29 Task: Layered material Task 3: Create a thin shiny layer The layer is completely transparent (except for the perfect reflection) Set its base Fresnel reflectance to 0.04 (it is a dielectric material) Try using a mask texture to create parts of semitransparent white areas. PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 27 / 29 Task: Layered material Result, with masked semitransparent areas PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 28 / 29 Things we used Depth-prepass Some graphic cards reorder evaluation of fragment shaders and evaluation of the depth test (when safe) Depth test is first, skipping FS when the fragment is hidden Sometimes, it is benefical to render the whole scene very simply into depth buffer first, and then into the color buffer Each fragment is evaluated only once Rendering the objects from the closest also helps Early depth tests Fragment shader: layout (early_fragment_tests) in; Forces the above behaviour Stencil test is also performed before running the fragment shader Do not use this when you change the fragment depth or when you discard the fragment PV227 – GPU Rendering (FI MUNI) Lesson 11 – PBR, IBL 26. 11. 2019 29 / 29