I would like to use my own precomputed spherical harmonics of the environment to compute the diffuse part of my materials.
However, it seems like I can only set the spherical polynomials, so I’m using SphericalPolynomial.FromHarmonics, and somehow the polynomials are converted back to harmonics before they are sent to the shader uniforms, but the values are different.
You can see an example here: https://playground.babylonjs.com/#U7YDJW#4
The values in the uniforms are different than what I set in the code, here is the value of L00 for instance:
By the way, I think there is an issue with spherical harmonics computation, that’s why I’m using mine.
If you look into CubeMapToSphericalPolynomialTools.ConvertCubeMapToSphericalPolynomial, where they are computed, the total solid angle is very different than 4 * pi, have a look at the debugging values:
From my experience with SH computation, the correction factor should be close to 1.
I suspect the problem is in the solid angle computation in the loop: var deltaSolidAngle = Math.pow(1.0 + u * u + v * v, -3.0 / 2.0);
It does not make sense to me. The cubemap texel contribution is scaled depending on u and v? I’m not sure where the formula is coming from, but maybe the developer wanted to use du and dv?
I have not verified the math behind this formula though.
I usually work with 2D equirectangular projection instead of cube maps, for which the solid angle computation is straightforward.