# all files / src/shader/ library.js

76.19% Statements 16/21
75% Branches 3/4
57.14% Functions 4/7
76.19% Lines 16/21

    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

    1×

    1×

    1×
    18×

    18×
    16×

    2×
    2×

    2×
    2×
    2×

    1×

    7×

    1×

    1×

    18×

     import Shader from '../Shader';

    var _library = {};

    function ShaderLibrary () {
        this._pool = {};
    }

    ShaderLibrary.prototype.get = function(name) {
        var key = name;

        if (this._pool[key]) {
            return this._pool[key];
        }
        else {
            var source = _library[name];
            Iif (!source) {
                console.error('Shader "' + name + '"' + ' is not in the library');
                return;
            }
            var shader = new Shader(source.vertex, source.fragment);
            this._pool[key] = shader;
            return shader;
        }
    };

    ShaderLibrary.prototype.clear = function() {
        this._pool = {};
    };

    function template(name, vertex, fragment) {
        _library[name] = {
            vertex: vertex,
            fragment: fragment
        };
    }

    var defaultLibrary = new ShaderLibrary();

    /**
     * ### Builin shaders
     * + clay.standard
     * + clay.basic
     * + clay.lambert
     * + clay.wireframe
     *
     * @namespace clay.shader.library
     */
    export default {
        /**
         * Create a new shader library.
         */
        createLibrary: function () {
            return new ShaderLibrary();
        },
        /**
         * Get shader from default library.
         * @param {string} name
         * @return {clay.Shader}
         * @memberOf clay.shader.library
         * @example
         *     clay.shader.library.get('clay.standard')
         */
        get: function () {
            return defaultLibrary.get.apply(defaultLibrary, arguments);
        },
        /**
         * @memberOf clay.shader.library
         * @param  {string} name
         * @param  {string} vertex - Vertex shader code
         * @param  {string} fragment - Fragment shader code
         */
        template: template,
        clear: function () {
            return defaultLibrary.clear();
        }
    };