# Spherical Harmonics help?

This topic is 2111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok so I've just about got my head around the theory. So can someone correct me if I'm wrong in thinking that Spherical Harmonics are an efficient means for storing the lighting contributions of the nearby environment for any given point in space.

Assuming I'm roughly correct so far, I need a bit of help in understanding how this is/can be applied in real time.

So I get that for a largely static environment, you can pre-compute a set of low resolution cubemaps and then reduce them to sets of spherical harmonic coefficients for the sake of memory efficiency. The questions I have so far are:

1) Where do you set the light probes? At the locations of each individual model? What about larger models that will need to deal with a possibly changing set of light values? Assuming you'd need more than one set, how do you blend between them?

2) How do you handle moving objects (just a few, obviously). Do you have to constantly render an environment map or is there a direct way of getting the right coefficients? I assume if you're going to be constantly rendering it, you'd only take into account objects within a certain distance?

Edited by gchewood

##### Share on other sites

Thanks,

But man, was that long! I've looked into a few other approaches and given that my scene is relatively simple, I'm considering just pre-baking

it all in 3ds max and then just using SH for the main character.

##### Share on other sites

Spherical harmonics are generally good for storing any low-frequency function that's located on the surface of a sphere. Diffuse lighting at a single point is a good fit for SH, since you can store the diffuse lighting for any direction from that point and then "look up" the lighting based on the surface normal.

If you have large static models, you'll want to place sample points along the surface of the mesh (not inside the mesh, since there will probably be no lighting in there). An easy way to do is to calculate lighting at vertex positions, or you can use a lightmap and store SH coefficients at each texel. With either vertex or lightmap SH samples you can interpolate between the nearby sample points in the pixel shader, and compute lighting for the normal of that surface.

For dynamic objects, you generally place probes throughout the scene and bake lighting without the dynamic objects. Then when you render the dynamics, you sample nearby probes. This lets the dynamic objects receive lighting, but no lighting will bounce off of the objects. Usually this is a pretty good approximation, since dynamic objects are typically small relative to static meshes. If you do need dynamic objects to reflect lighting, then you would need to generate the probes at runtime. There's a good blog post here that describes a few methods of placing probes for dynamic objects, and interpolating between them.

##### Share on other sites

Thanks MJP.

I'm a bit overwhelmed by it. I understand the theory, just not the implementation. That link seems to at least suggest the interpolating between probes

is quite easy.

My main issue at the moment is how do you get the coefficients in the first place? The only method I've seen was based on rendering a set of environment maps. But I don't see that being mentioned very often....?

Thanks

##### Share on other sites

My main issue at the moment is how do you get the coefficients in the first place? The only method I've seen was based on rendering a set of environment maps. But I don't see that being mentioned very often....?

The most common methods are probably projecting a cube map into spherical harmonics or evaluating the light direction and scaling, both methods are explained in this paper (starting at page 9) and there are functions to do it in the D3DX Math.

Edited by TiagoCosta

##### Share on other sites

You compute SH coefficients by integrating the function against the SH basis functions. If you have an environment map already rendered, then you can perform a spherical integral using the method that Peter-Pike Sloan suggests. Or you can use monte carlo integration if you'd like, which works well with a ray-tracer. For each sample point you can cast rays and project using the SH basis functions, which allows you to directly integrate without having to render a cubemap first.

##### Share on other sites

Thanks people. That Stupid SH paper is pretty much what I was looking for!

• 18
• 18
• 11
• 21
• 16