src/util/sh.js
function projectEnvironmentMapCPU(renderer, cubePixels, width, height) {
    var coeff = new vendor.Float32Array(9 * 3);
    var normal = vec3.create();
    var texel = vec3.create();
    var fetchNormal = vec3.create();
    for (var m = 0; m < 9; m++) {
        var result = vec3.create();
        for (var k = 0; k < targets.length; k++) {
            var pixels = cubePixels[targets[k]];

            var sideResult = vec3.create();
            var divider = 0;
            var i = 0;
            var transform = normalTransform[targets[k]];
            for (var y = 0; y < height; y++) {
                for (var x = 0; x < width; x++) {

                    normal[0] = x / (width - 1.0) * 2.0 - 1.0;
                    // TODO Flip y?
                    normal[1] = y / (height - 1.0) * 2.0 - 1.0;
                    normal[2] = -1.0;
                    vec3.normalize(normal, normal);

                    fetchNormal[0] = normal[transform[0]] * transform[3];
                    fetchNormal[1] = normal[transform[1]] * transform[4];
                    fetchNormal[2] = normal[transform[2]] * transform[5];

                    texel[0] = pixels[i++] / 255;
                    texel[1] = pixels[i++] / 255;
                    texel[2] = pixels[i++] / 255;
                    // RGBM Decode
                    var scale = pixels[i++] / 255 * 8.12;
                    texel[0] *= scale;
                    texel[1] *= scale;
                    texel[2] *= scale;

                    vec3.scaleAndAdd(sideResult, sideResult, texel, harmonics(fetchNormal, m) * -normal[2]);
                    // -normal.z equals cos(theta) of Lambertian
                    divider += -normal[2];
                }
            }
            vec3.scaleAndAdd(result, result, sideResult, 1 / divider);
        }

        coeff[m * 3] = result[0] / 6.0;
        coeff[m * 3 + 1] = result[1] / 6.0;
        coeff[m * 3 + 2] = result[2] / 6.0;
    }
    return coeff;
}
function projectEnvironmentMapCPU(renderer, cubePixels, width, height) {
    var coeff = new vendor.Float32Array(9 * 3);
    var normal = vec3.create();
    var texel = vec3.create();
    var fetchNormal = vec3.create();
    for (var m = 0; m < 9; m++) {
        var result = vec3.create();
        for (var k = 0; k < targets.length; k++) {
            var pixels = cubePixels[targets[k]];

            var sideResult = vec3.create();
            var divider = 0;
            var i = 0;
            var transform = normalTransform[targets[k]];
            for (var y = 0; y < height; y++) {
                for (var x = 0; x < width; x++) {

                    normal[0] = x / (width - 1.0) * 2.0 - 1.0;
                    // TODO Flip y?
                    normal[1] = y / (height - 1.0) * 2.0 - 1.0;
                    normal[2] = -1.0;
                    vec3.normalize(normal, normal);

                    fetchNormal[0] = normal[transform[0]] * transform[3];
                    fetchNormal[1] = normal[transform[1]] * transform[4];
                    fetchNormal[2] = normal[transform[2]] * transform[5];

                    texel[0] = pixels[i++] / 255;
                    texel[1] = pixels[i++] / 255;
                    texel[2] = pixels[i++] / 255;
                    // RGBM Decode
                    var scale = pixels[i++] / 255 * 8.12;
                    texel[0] *= scale;
                    texel[1] *= scale;
                    texel[2] *= scale;

                    vec3.scaleAndAdd(sideResult, sideResult, texel, harmonics(fetchNormal, m) * -normal[2]);
                    // -normal.z equals cos(theta) of Lambertian
                    divider += -normal[2];
                }
            }
            vec3.scaleAndAdd(result, result, sideResult, 1 / divider);
        }

        coeff[m * 3] = result[0] / 6.0;
        coeff[m * 3 + 1] = result[1] / 6.0;
        coeff[m * 3 + 2] = result[2] / 6.0;
    }
    return coeff;
}
function projectEnvironmentMapCPU ( renderer , cubePixels , width , height ) { var coeff = new vendor . Float32Array ( 9 * 3 ) ; var normal = vec3 . create ( ) ; var texel = vec3 . create ( ) ; var fetchNormal = vec3 . create ( ) ; for ( var m = 0 ; m < 9 ; m ++ ) { var result = vec3 . create ( ) ; for ( var k = 0 ; k < targets . length ; k ++ ) { var pixels = cubePixels [ targets [ k ] ] ; var sideResult = vec3 . create ( ) ; var divider = 0 ; var i = 0 ; var transform = normalTransform [ targets [ k ] ] ; for ( var y = 0 ; y < height ; y ++ ) { for ( var x = 0 ; x < width ; x ++ ) { normal [ 0 ] = x / ( width - 1.0 ) * 2.0 - 1.0 ; // TODO Flip y? normal [ 1 ] = y / ( height - 1.0 ) * 2.0 - 1.0 ; normal [ 2 ] = - 1.0 ; vec3 . normalize ( normal , normal ) ; fetchNormal [ 0 ] = normal [ transform [ 0 ] ] * transform [ 3 ] ; fetchNormal [ 1 ] = normal [ transform [ 1 ] ] * transform [ 4 ] ; fetchNormal [ 2 ] = normal [ transform [ 2 ] ] * transform [ 5 ] ; texel [ 0 ] = pixels [ i ++ ] / 255 ; texel [ 1 ] = pixels [ i ++ ] / 255 ; texel [ 2 ] = pixels [ i ++ ] / 255 ; // RGBM Decode var scale = pixels [ i ++ ] / 255 * 8.12 ; texel [ 0 ] *= scale ; texel [ 1 ] *= scale ; texel [ 2 ] *= scale ; vec3 . scaleAndAdd ( sideResult , sideResult , texel , harmonics ( fetchNormal , m ) * - normal [ 2 ] ) ; // -normal.z equals cos(theta) of Lambertian divider += - normal [ 2 ] ; } } vec3 . scaleAndAdd ( result , result , sideResult , 1 / divider ) ; } coeff [ m * 3 ] = result [ 0 ] / 6.0 ; coeff [ m * 3 + 1 ] = result [ 1 ] / 6.0 ; coeff [ m * 3 + 2 ] = result [ 2 ] / 6.0 ; } return coeff ; }
Project on cpu.
Project on cpu .