# Code coverage report for src/plugin/Skybox.js

# all files / src/plugin/ Skybox.js

87.1% Statements 27/31
50% Branches 5/10
77.78% Functions 7/9
87.1% Lines 27/31

    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
    1×

    1×

    1×

    1×

    1×

    1×
    1×
    1×

    1×

    1×
    1×

    1×

    1×
    1×

    2×
    2×
    2×

    2×

    1×
    1×

    42×

    42×
    42×

    42×
    42×

    42×

    42×

     // TODO Should not derived from mesh?
    import Mesh from '../Mesh';
    import CubeGeometry from '../geometry/Cube';
    import Shader from '../Shader';
    import Material from '../Material';

    import skyboxEssl from '../shader/source/skybox.glsl.js';
    Shader.import(skyboxEssl);
    /**
     * @constructor clay.plugin.Skybox
     *
     * @example
     *     var skyTex = new clay.TextureCube();
     *     skyTex.load({
     *         'px': 'assets/textures/sky/px.jpg',
     *         'nx': 'assets/textures/sky/nx.jpg'
     *         'py': 'assets/textures/sky/py.jpg'
     *         'ny': 'assets/textures/sky/ny.jpg'
     *         'pz': 'assets/textures/sky/pz.jpg'
     *         'nz': 'assets/textures/sky/nz.jpg'
     *     });
     *     var skybox = new clay.plugin.Skybox({
     *         scene: scene
     *     });
     *     skybox.material.set('environmentMap', skyTex);
     */
    var Skybox = Mesh.extend(function () {

        var skyboxShader = new Shader({
            vertex: Shader.source('clay.skybox.vertex'),
            fragment: Shader.source('clay.skybox.fragment')
        });
        var material = new Material({
            shader: skyboxShader,
            depthMask: false
        });

        return {
            /**
             * @type {clay.Scene}
             * @memberOf clay.plugin.Skybox.prototype
             */
            scene: null,

            geometry: new CubeGeometry(),

            material: material,

            environmentMap: null,

            culling: false
        };
    }, function () {
        var scene = this.scene;
        Eif (scene) {
            this.attachScene(scene);
        }
        Iif (this.environmentMap) {
            this.setEnvironmentMap(this.environmentMap);
        }
    }, /** @lends clay.plugin.Skybox# */ {
        /**
         * Attach the skybox to the scene
         * @param  {clay.Scene} scene
         */
        attachScene: function (scene) {
            Eif (this.scene) {
                this.detachScene();
            }
            scene.skybox = this;

            this.scene = scene;
            scene.on('beforerender', this._beforeRenderScene, this);
        },
        /**
         * Detach from scene
         */
        detachScene: function () {
            Eif (this.scene) {
                this.scene.off('beforerender', this._beforeRenderScene);
                this.scene.skybox = null;
            }
            this.scene = null;
        },

        /**
         * Dispose skybox
         * @param  {clay.Renderer} renderer
         */
        dispose: function (renderer) {
            this.detachScene();
            this.geometry.dispose(renderer);
        },
        /**
         * Set environment map
         * @param {clay.TextureCube} envMap
         */
        setEnvironmentMap: function (envMap) {
            this.material.set('environmentMap', envMap);
        },
        /**
         * Get environment map
         * @return {clay.TextureCube}
         */
        getEnvironmentMap: function () {
            return this.material.get('environmentMap');
        },

        _beforeRenderScene: function(renderer, scene, camera) {
            this.renderSkybox(renderer, camera);
        },

        renderSkybox: function (renderer, camera) {
            this.position.copy(camera.getWorldPosition());
            this.update();
            // Don't remember to disable blend
            renderer.gl.disable(renderer.gl.BLEND);
            Iif (this.material.get('lod') > 0) {
                this.material.define('fragment', 'LOD');
            }
            else {
                this.material.undefine('fragment', 'LOD');
            }
            renderer.renderPass([this], camera);
        }
    });

    export default Skybox;