2017년 10월 2일 월요일

Spherical harmonics

The purpose of this post

I don't want to explain the mathmatical details of spherical harmonics in this post. Actually I am not that good at math. There are tons of great article about spherical harmonics that has great mathematical explanations. But not many people try to read these article to understand this useful toolset because they cannot even imagine how useful this is. I just want to tell what spherical harmonics could do and emphasize one more time with some useful links. so I will focus on top level explanation and will try to use intuitive words as possible as I can.

What is spherical harmonics(SH)

Complex spherical function can be encoded as SH coefficients vector. In other words, you can compress cube map to several number of float vector although high frequency signal will be lost mostly. The process which transform complex function to sh vector is called Projection. You can find the mathematical details under SH on the internet. Like Robin Green's gritty detail or Sloan's stupid SH tricks papers. These two papers are the best explanation about spherical harmonics lighting. Maybe you might not be able to understand these article at once. But if you try several times you could understand one concept by one.

example usage

  • Lighting signal over sphere can be represented using SH.
  • Visibility(Shadow signal) over hemisphere can be represented using SH.
  • BRDF can be can be represented using SH. For example lambert diffuse BRDF.

spherical harmonics dot product

Integral of product of two functions can be done by simple dot product. Many article will tell you that this is the most important property of spherical harmonics. Usually many integral problem like diffuse lighting convolution(adding all diffuse lighting contribution over hemisphere) cannot be calculated at runtime, but can be done if we use spherical harmonics. In other words the rendering equation can be solved using spherical harmonics at least for diffuse surfaces.
The process is like below(very simplified version. You can find all detail explanation on the internet). This is most basic and simple usage of spherical harmonics in lighting.
  • You need SH coefficients vector which is projected from lighting over sphere(ex : cube map).
  • You need SH coefficients which represents clamped cosine function.
  • Simple Dot product of two coefficients is same as sum of all irradiance over hemisphere.
  • Evaluate Irradiance for certain normal.

Adding spherical harmonics coefficients

Adding two spherical harmonics lighting coefficients vector means that adding two lighting signal.This is also very simple and useful property. I think this is so intuitive property so there aren't much to talk about this.
  • You have sh vector for skylight and sh vector for some bounced lighting from the floor.
  • Adding these two sh vector results to sh vector which has both lighting signals.

Multiplying two spherical harmonics signal

Adding two sh vector makes sh vector which represents added spherical signal. And dot product of two sh vector results integral of product of two signal(ex : diffuse lighting convolution). But what if when we need some new sh vector which is the result of multiplication of two spherical signals. For example, we multiply shadow function to lighting signal. This cannot be done by adding or dot product. Maybe you can imagine multiplying vector component by component would do this magic but it is not that simple. And it is one of most difficult concept relating spherical harmonics. Actually this operation is vector-matrix multiplication. If we have light sh vector and shadow sh vector, we can multiply some special matrix which is made from shadow sh vector to light sh vector. This is called Triple product integral. There is good article explaining how this works(Better than Robin Green's).

Rotation of spherical harmonics

We can rotate spherical harmonics vector. Rotation of spherical harmonics vector is the most difficult concept to understand among the sh toolsets. But important thing is that we can rotate spherical harmonics. But when do we need this? Suppose you encoded ambient occlusion signal for each vertex for skinned mesh(animated character), you will have to rotate the AO SH vector like we rotate normal vector according to movement of vertex. You can find the underground detail in Robin Green's article. And also can find optimized rotation method from below link.

Convenient zonal harmonics

Zonal harmonics is rotationally symmetric basis functions among all sh basis whose m number is zero. These basis is frequently used to approximate functions which is symmetric along z axis. 
  • Simple Diffuse Transfer function).
  • Adding analytic lighting to existing sh vector.
  • Light Propagation Volume uses ZH for light injection to voxel.

Spherical Harmonics code in UE4

You can learn about spherical harmonics usage from UE4. There are several typical use case like light map evaluation(UE4 uses sh light map), and irradiance volume evaluation. In BasePassPixelShader.usf, you can find GetPreComputedLighting function, and you could learn how actual sh lighting shader code looks like. Like I said before, It is simple dot product between lighting sh vector and diffuse transfer function made from normal vector. If you are more curious person you could look into Lightmass source code and could find how lighting and ambient occlusion signal is projected to light map. If you are working on UE3, LightEnvironment gathers static light to one 3rd order spherical harmonics vector analytically for moving character in CPU. This is also very interesting use case of spherical harmonics. Maybe there are more SH use cases in Unrealengine, for example skylighting, you could look for another one for fun.

댓글 없음:

댓글 쓰기