SphericalHarmonics: Difference between revisions

From Resonite Wiki
No edit summary
Line 1: Line 1:
Spherical harmonics are a system that blend values using an integer <code>Order</code> using the Coder<T> library.
Spherical harmonics are a Resonite system that blend values (individual harmonics) using an integer <code>Order</code> using the Coder<T> library into a single value. In mathematics, the spherical harmonics are a special set of functions that can be added together in a weighted sum to create any function defined on a sphere's surface. Resonite is able to combine the harmonics up to 4th order (count starts at 0th order) with each increasing order providing higher resolution/definition/information.
 
A simple way of explaining spherical harmonics is that they are areas on a sphere that increase in number (and decrease in size) depending on it's complexity order (L1-L4). These areas can represent any value. When sampling them a direction is used to sample from the areas. When between areas, there is blending between their values. This finally outputs a value.


How this works mathematically is outside the scope of this wiki, and is better explained on it's wikipedia page [https://en.wikipedia.org/wiki/Spherical_harmonics].
How this works mathematically is outside the scope of this wiki, and is better explained on it's wikipedia page [https://en.wikipedia.org/wiki/Spherical_harmonics].


= Usage =
== Usage ==
Spherical harmonics can be used in many different areas, including:
 
Spherical harmonics can be used in many different areas, including:
Spherical harmonics can be used in many different areas, including:
* Audio mapping/direction
* Audio mapping/direction
Line 13: Line 9:
* Light data (used for color, skyboxes, ambient/world lighting)
* Light data (used for color, skyboxes, ambient/world lighting)
* [[Gaussian_Splat|Gaussian Splat]] rendering
* [[Gaussian_Splat|Gaussian Splat]] rendering
In general, they can be used to approximate functions whose domain can be represented as the surface of a sphere: every point on the surface has some value like color, temperature, brightness, transparency, volume, etc.
== Similar Concepts ==
The spherical harmonics form a basis for the functions on a sphere (technically SO(3)). This allows you to mix and match them to create many arbitrary values out of a small set of simple terms. You can also decompose complex structures into their unique constituent parts (often with a bit more effort). You may already be familiar with other bases and their properties:
* The X, Y, and Z unit vectors form a basis in 3D space that you can use to locate any point in the world and all points have a unique (X ,Y, Z) value.
* are a basis that you can use to make any polynomial
* Sine waves with different frequencies are a basis and can be used to create any function (e.g. sound wave). Sine waves are also an example of harmonic functions like the spherical harmonics. Because of this, the spherical harmonics can be thought of as the 'frequencies' or 'modes' of the sphere's surface.
While the fundamental principles may be simple, knowing which individual values to tweak to produce some desired outcome is often incredibly difficult. Therefore, most uses of harmonics use specialized algorithms for decomposing functions into a more digestible form: such as using a (Fast) Fourier Transform for decomposing waves into their individual sinusoidal frequency components.


= Technical Notes =
== Technical Notes ==
Spherical harmonics are blended using the code below
Spherical harmonics are blended using the code below
  <nowiki>public static T Evaluate<T>(this ISphericalHarmonics<T> harmonics, float3 dir) where T : unmanaged
  <nowiki>public static T Evaluate<T>(this ISphericalHarmonics<T> harmonics, float3 dir) where T : unmanaged

Revision as of 18:29, 21 November 2025

Spherical harmonics are a Resonite system that blend values (individual harmonics) using an integer Order using the Coder<T> library into a single value. In mathematics, the spherical harmonics are a special set of functions that can be added together in a weighted sum to create any function defined on a sphere's surface. Resonite is able to combine the harmonics up to 4th order (count starts at 0th order) with each increasing order providing higher resolution/definition/information.

How this works mathematically is outside the scope of this wiki, and is better explained on it's wikipedia page [1].

Usage

Spherical harmonics can be used in many different areas, including:

  • Audio mapping/direction
  • Mesh creation (to visualize the spherical harmonic)
  • Light data (used for color, skyboxes, ambient/world lighting)
  • Gaussian Splat rendering

In general, they can be used to approximate functions whose domain can be represented as the surface of a sphere: every point on the surface has some value like color, temperature, brightness, transparency, volume, etc.

Similar Concepts

The spherical harmonics form a basis for the functions on a sphere (technically SO(3)). This allows you to mix and match them to create many arbitrary values out of a small set of simple terms. You can also decompose complex structures into their unique constituent parts (often with a bit more effort). You may already be familiar with other bases and their properties:

  • The X, Y, and Z unit vectors form a basis in 3D space that you can use to locate any point in the world and all points have a unique (X ,Y, Z) value.
  • are a basis that you can use to make any polynomial
  • Sine waves with different frequencies are a basis and can be used to create any function (e.g. sound wave). Sine waves are also an example of harmonic functions like the spherical harmonics. Because of this, the spherical harmonics can be thought of as the 'frequencies' or 'modes' of the sphere's surface.

While the fundamental principles may be simple, knowing which individual values to tweak to produce some desired outcome is often incredibly difficult. Therefore, most uses of harmonics use specialized algorithms for decomposing functions into a more digestible form: such as using a (Fast) Fourier Transform for decomposing waves into their individual sinusoidal frequency components.

Technical Notes

Spherical harmonics are blended using the code below

public static T Evaluate<T>(this ISphericalHarmonics<T> harmonics, float3 dir) where T : unmanaged
{
    T val = default(T);
    dir = new float3(0f - dir.x, 0f - dir.y, dir.z);
    for (int i = 0; i <= harmonics.Order; i++)
    {
        for (int j = -i; j <= i; j++)
        {
            T t = harmonics[CoefficientIndex(i, j)];
            float s = EvaluateScale(i, j, in dir);
            val = Coder<T>.Add(val, Coder<T>.Scale(t, s));
        }
    }
    return val;
}

The EvaluateScale(i, j, in dir) uses precalculated values that would take much longer to evaluate if done on the fly. How to calculate these values are also explained on the spherical harmonics wikipedia page [2].