2017년 10월 19일 목요일

Understanding diffuse transfer function.

Diffuse transfer function

In PRT, transfer function includes much information(visibilityintereflected lighting) and projected to sh coefficients in offline using monte carlo integration. But many game engine uses sh lighting without these pre-computed transfer function. For example, characters usually don’t contain visibility data for each vertex or texture. So we have to do lighting with only surface normal and lighting function. Because we don’t have pre-computed transfer function, Lambert(clamped cosine) function like below image will be used. 

Maybe you might have wondered how this Lambert transfer function could be generated in runtime. Ok, Cosine function could be projected to sh coefficients using monte carlo integration. But transfer function should be different for specific surface normal. How could this transfer function be rotated along surface normal vector? The famous R.Ramanmoorthi’s IrradianceEnvironmentMap paper lacks detail explanation for this process.

Irradiance Convolution

The convenient Irradiance convolution is like this.
Llm is lighting function,
L(θ,ϕ)=l,mLlmYlm(θ,ϕ)
Tlm is transfer function,
T(θ,ϕ)=l,mTlmYlm(θ,ϕ)
Then irradiance E is,E=l,mLlmTlm

Analytic diffuse transfer function

In this paper, You can learn how cosine function is projected to sh coefficients analytically. Note that this function is in local coordinate.A(θi)=cosθi=n=0AnYn,0(θi)
An=2π0π2Yn,0(θi)cosθisinθidθi
An=2π(2n+14π)01Pn(u)P1(u)du
-11Pa(u)Pb(u)=22a+1δa,b
n=1:An=π3
n>1,odd:An=0
n=even:2π2n+14π-1n/2-1(n+2)(n-1)×[n!2n(n!/2)2]

Local? World? coordinate

The analytic cosine function above is in local coordinate. It means that you can use it with surfaces whose normal vector is z axis. So you have to rotate this along surface normal vector.

Rotation of clamped cosine function(Zonal harmonics rotation)

When (θ,ϕ) is direction to which we want to rotate,
Arlm is rotated cosine function along (θ,ϕ),
Aolm is original cosine function along z axis,
Arlm=4π2l+1AolmYlm(θ,ϕ)
When n is 1, A1=π3 and 4π2l+1=2π3Ar1=2π3 and other coefficients match as well.
So in conclusion you just need to use this Arn coefficients to rotate diffuse transfer function. And you can use this resulting coefficient to do dot product with lighting coefficients as R.Ramanmoorthi’s IrradianceEnvironmentMap paper says.

Other BRDF

Most game engine uses same diffuse transfer function like above even the diffuse BRDF of surface is not Lambert, therefore indirect lighting for BRDF other than lambert is not realistic. But with knowledge like this we could do better. The Order 1886 generated skin diffuse transfer function which is projected by monte carlo integration and used it as skin diffuse lighting. If we have other isotropic diffuse BRDF function we could do exactly same thing like this.

2017년 10월 12일 목요일

Apps for writing

Writing app

Recently, I tried to establish optimal writing environment. I usually Microsoft OneNote for Memo and blogging. It is actually very good application for memo and clipping useful web urls. But when you want to do some format (ex : Bold or underline or others) it would be really painful because you have to click mouse many times during typing. And finally I came to know MarkDown and editors which support MarkDown like iAWriter and Scrivener. And found that many people uses these apps for blogging and writing their books. So I tried these app.

Scrivener

This app is really powerful and professional. Lots of people uses this app for writing books and paper. In my opinion, most powerful feature of this app is the Organizing. You can collect datas and small part of text(with MarkDown support) and compile all these material to one big book or paper. I barely understands very small parts of features of this app. But I felt like this is too much for my purpose.

iAWriter

I am writing this post using this app. IAWriter is simple app app compared to Scrivener. But this app has Focus Modewhich help you to focus only on writing and simple organizing feature which make you write small parts of whole text and make one big text. And it supports MarkDown flawlessly. I bought both iOS version and Mac version. It seems that it is perfect fit for blogging and small writing. The only bad thing of this app is the fact that it only support iOS and Mac.

Theme for iAWriter

If you want to use iAWriter to write blog post about programming or math, you may have to install below theme. Because the default themes doesnt support code syntax hightliging and MathJax. After installing this theme you can copy content in Preview mode and paste to blog pages. You could also export as Html or PDF or Microsoft Word format but copy & paste would be best choice for blogging.

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.