# Code coverage report for src/util/sh.js

# all files / src/util/ sh.js

5.49% Statements 5/91
0% Branches 0/28
0% Functions 0/3
5.49% Lines 5/91

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218

    1×
    1×

    1×

    1×

    1×

     // Spherical Harmonic Helpers
    import Texture from '../Texture';
    import FrameBuffer from '../FrameBuffer';
    import Texture2D from '../Texture2D';
    import Pass from '../compositor/Pass';
    import vendor from '../core/vendor';
    import Skybox from '../plugin/Skybox';
    import Skydome from '../plugin/Skydome';
    import EnvironmentMapPass from '../prePass/EnvironmentMap';
    import Scene from '../Scene';
    import glmatrix from '../dep/glmatrix';
    var vec3 = glmatrix.vec3;
    var sh = {};

    import projectEnvMapShaderCode from './shader/projectEnvMap.glsl.js';

    var targets = ['px', 'nx', 'py', 'ny', 'pz', 'nz'];

    // Project on gpu, but needs browser to support readPixels as Float32Array.
    function projectEnvironmentMapGPU(renderer, envMap) {
        var shTexture = new Texture2D({
            width: 9,
            height: 1,
            type: Texture.FLOAT
        });
        var pass = new Pass({
            fragment: projectEnvMapShaderCode
        });
        pass.material.define('fragment', 'TEXTURE_SIZE', envMap.width);
        pass.setUniform('environmentMap', envMap);

        var framebuffer = new FrameBuffer();
        framebuffer.attach(shTexture);
        pass.render(renderer, framebuffer);

        framebuffer.bind(renderer);
        // TODO Only chrome and firefox support Float32Array
        var pixels = new vendor.Float32Array(9 * 4);
        renderer.gl.readPixels(0, 0, 9, 1, Texture.RGBA, Texture.FLOAT, pixels);

        var coeff = new vendor.Float32Array(9 * 3);
        for (var i = 0; i < 9; i++) {
            coeff[i * 3] = pixels[i * 4];
            coeff[i * 3 + 1] = pixels[i * 4 + 1];
            coeff[i * 3 + 2] = pixels[i * 4 + 2];
        }
        framebuffer.unbind(renderer);

        framebuffer.dispose(renderer);
        pass.dispose(renderer);
        return coeff;
    }

    function harmonics(normal, index){
        var x = normal[0];
        var y = normal[1];
        var z = normal[2];

        if (index === 0) {
            return 1.0;
        }
        else if (index === 1) {
            return x;
        }
        else if (index === 2) {
            return y;
        }
        else if (index === 3) {
            return z;
        }
        else if (index === 4) {
            return x * z;
        }
        else if (index === 5) {
            return y * z;
        }
        else if (index === 6) {
            return x * y;
        }
        else if (index === 7) {
            return 3.0 * z * z - 1.0;
        }
        else {
            return x * x - y * y;
        }
    }

    var normalTransform = {
        px: [2, 1, 0, -1, -1, 1],
        nx: [2, 1, 0, 1, -1, -1],
        py: [0, 2, 1, 1, -1, -1],
        ny: [0, 2, 1, 1, 1, 1],
        pz: [0, 1, 2, -1, -1, -1],
        nz: [0, 1, 2, 1, -1, 1]
    };

    // Project on cpu.
    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 * 51.5;
                        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;
    }

    /**
     * @param  {clay.Renderer} renderer
     * @param  {clay.Texture} envMap
     * @param  {Object} [textureOpts]
     * @param  {Object} [textureOpts.lod]
     * @param  {boolean} [textureOpts.decodeRGBM]
     */
    sh.projectEnvironmentMap = function (renderer, envMap, opts) {

        // TODO sRGB

        opts = opts || {};
        opts.lod = opts.lod || 0;

        var skybox;
        var dummyScene = new Scene();
        var size = 64;
        if (envMap.textureType === 'texture2D') {
            skybox = new Skydome({
                scene: dummyScene,
                environmentMap: envMap
            });
        }
        else {
            size = (envMap.image && envMap.image.px) ? envMap.image.px.width : envMap.width;
            skybox = new Skybox({
                scene: dummyScene,
                environmentMap: envMap
            });
        }
        // Convert to rgbm
        var width = Math.ceil(size / Math.pow(2, opts.lod));
        var height = Math.ceil(size / Math.pow(2, opts.lod));
        var rgbmTexture = new Texture2D({
            width: width,
            height: height
        });
        var framebuffer = new FrameBuffer();
        skybox.material.define('fragment', 'RGBM_ENCODE');
        if (opts.decodeRGBM) {
            skybox.material.define('fragment', 'RGBM_DECODE');
        }
        skybox.material.set('lod', opts.lod);
        var envMapPass = new EnvironmentMapPass({
            texture: rgbmTexture
        });
        var cubePixels = {};
        for (var i = 0; i < targets.length; i++) {
            cubePixels[targets[i]] = new Uint8Array(width * height * 4);
            var camera = envMapPass.getCamera(targets[i]);
            camera.fov = 90;
            framebuffer.attach(rgbmTexture);
            framebuffer.bind(renderer);
            renderer.render(dummyScene, camera);
            renderer.gl.readPixels(
                0, 0, width, height,
                Texture.RGBA, Texture.UNSIGNED_BYTE, cubePixels[targets[i]]
            );
            framebuffer.unbind(renderer);
        }

        skybox.dispose(renderer);
        framebuffer.dispose(renderer);
        rgbmTexture.dispose(renderer);

        return projectEnvironmentMapCPU(renderer, cubePixels, width, height);
    };

    export default sh;