# all files / src/compositor/ Pass.js

68.85% Statements 42/61
38.24% Branches 13/34
72.73% Functions 8/11
68.85% Lines 42/61

    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

    1×

    1×
    1×

    1×

    1×
    13×

    13×
    13×

    13×

    13×

    18×

    1×
    1×

    1×
    1×

    1×
    1×
    1×
    1×
    1×

    1×
    1×

    1×

    1×

    1×
    1×

    1×
    1×

    1×

    1×
    1×
    1×

    1×

    1×

    1×
    1×

    1×

    1×

    1×
    1×

    1×
    1×

     import Base from '../core/Base';
    import OrthoCamera from '../camera/Orthographic';
    import Plane from '../geometry/Plane';
    import Shader from '../Shader';
    import Material from '../Material';
    import Mesh from '../Mesh';
    import glenum from '../core/glenum';
    import vertexEssl from '../shader/source/compositor/vertex.glsl.js';

    Shader['import'](vertexEssl);

    var planeGeo = new Plane();
    var mesh = new Mesh({
        geometry: planeGeo,
        frustumCulling: false
    });
    var camera = new OrthoCamera();

    /**
     * @constructor clay.compositor.Pass
     * @extends clay.core.Base
     */
    var Pass = Base.extend(function () {
        return /** @lends clay.compositor.Pass# */ {
            /**
             * Fragment shader string
             * @type {string}
             */
            // PENDING shader or fragment ?
            fragment : '',

            /**
             * @type {Object}
             */
            outputs : null,

            /**
             * @type {clay.Material}
             */
            material : null,

            /**
             * @type {Boolean}
             */
            blendWithPrevious: false,

            /**
             * @type {Boolean}
             */
            clearColor: false,

            /**
             * @type {Boolean}
             */
            clearDepth: true
        };
    }, function() {

        var shader = new Shader(Shader.source('clay.compositor.vertex'), this.fragment);
        var material = new Material({
            shader: shader
        });
        material.enableTexturesAll();

        this.material = material;

    },
    /** @lends clay.compositor.Pass.prototype */
    {
        /**
         * @param {string} name
         * @param {} value
         */
        setUniform : function(name, value) {
            this.material.setUniform(name, value);
        },
        /**
         * @param  {string} name
         * @return {}
         */
        getUniform : function(name) {
            var uniform = this.material.uniforms[name];
            if (uniform) {
                return uniform.value;
            }
        },
        /**
         * @param  {clay.Texture} texture
         * @param  {number} attachment
         */
        attachOutput : function(texture, attachment) {
            Eif (!this.outputs) {
                this.outputs = {};
            }
            attachment = attachment || glenum.COLOR_ATTACHMENT0;
            this.outputs[attachment] = texture;
        },
        /**
         * @param  {clay.Texture} texture
         */
        detachOutput : function(texture) {
            for (var attachment in this.outputs) {
                if (this.outputs[attachment] === texture) {
                    this.outputs[attachment] = null;
                }
            }
        },

        bind : function(renderer, frameBuffer) {

            Eif (this.outputs) {
                for (var attachment in this.outputs) {
                    var texture = this.outputs[attachment];
                    Eif (texture) {
                        frameBuffer.attach(texture, attachment);
                    }
                }
            }

            Eif (frameBuffer) {
                frameBuffer.bind(renderer);
            }
        },

        unbind : function(renderer, frameBuffer) {
            frameBuffer.unbind(renderer);
        },
        /**
         * @param  {clay.Renderer} renderer
         * @param  {clay.FrameBuffer} [frameBuffer]
         */
        render : function(renderer, frameBuffer) {

            var _gl = renderer.gl;

            Eif (frameBuffer) {
                this.bind(renderer, frameBuffer);
                // MRT Support in chrome
                // https://www.khronos.org/registry/webgl/sdk/tests/conformance/extensions/ext-draw-buffers.html
                var ext = renderer.getGLExtension('EXT_draw_buffers');
                Iif (ext && this.outputs) {
                    var bufs = [];
                    for (var attachment in this.outputs) {
                        attachment = +attachment;
                        if (attachment >= _gl.COLOR_ATTACHMENT0 && attachment <= _gl.COLOR_ATTACHMENT0 + 8) {
                            bufs.push(attachment);
                        }
                    }
                    ext.drawBuffersEXT(bufs);
                }
            }

            this.trigger('beforerender', this, renderer);

            // FIXME Don't clear in each pass in default, let the color overwrite the buffer
            // FIXME pixels may be discard
            var clearBit = this.clearDepth ? _gl.DEPTH_BUFFER_BIT : 0;
            _gl.depthMask(true);
            Iif (this.clearColor) {
                clearBit = clearBit | _gl.COLOR_BUFFER_BIT;
                _gl.colorMask(true, true, true, true);
                var cc = this.clearColor;
                if (Array.isArray(cc)) {
                    _gl.clearColor(cc[0], cc[1], cc[2], cc[3]);
                }
            }
            _gl.clear(clearBit);

            Iif (this.blendWithPrevious) {
                // Blend with previous rendered scene in the final output
                // FIXME Configure blend.
                // FIXME It will cause screen blink？
                _gl.enable(_gl.BLEND);
                this.material.transparent = true;
            }
            else {
                _gl.disable(_gl.BLEND);
                this.material.transparent = false;
            }

            this.renderQuad(renderer);

            this.trigger('afterrender', this, renderer);

            Eif (frameBuffer) {
                this.unbind(renderer, frameBuffer);
            }
        },

        /**
         * Simply do quad rendering
         */
        renderQuad: function (renderer) {
            mesh.material = this.material;
            renderer.renderPass([mesh], camera);
        },

        /**
         * @param  {clay.Renderer} renderer
         */
        dispose: function (renderer) {}
    });

    export default Pass;