# all files / src/ Texture2D.js

77.11% Statements 64/83
77.55% Branches 38/49
80% Functions 12/15
77.11% Lines 64/83

    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
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231

    1×

    1×
    11×

    17×
    17×

    17×

    17×
    17×

    17×
    17×

    17×
    17×

    17×
    17×
    1×

    17×
    1×
    1×

    17×

    17×

    17×
    13×

    17×

    17×
    5×

    12×

    12×

    17×
    5×
    5×

    12×
    12×

    17×

    57×
    5×

    52×

    57×

    5×
    5×

    5×
    5×
    5×
    5×
    5×

    5×

    5×
    5×

    5×

    1×

    269×
    5×

    264×

    25×

    25×
    24×

    25×

    1×

    219×
    5×

    214×

    25×

    25×
    25×

    25×

     import Texture from './Texture';
    import glenum from './core/glenum';
    import mathUtil from './math/util';
    var isPowerOfTwo = mathUtil.isPowerOfTwo;

    /**
     * @constructor clay.Texture2D
     * @extends clay.Texture
     *
     * @example
     *     ...
     *     var mat = new clay.Material({
     *         shader: clay.shader.library.get('clay.phong', 'diffuseMap')
     *     });
     *     var diffuseMap = new clay.Texture2D();
     *     diffuseMap.load('assets/textures/diffuse.jpg');
     *     mat.set('diffuseMap', diffuseMap);
     *     ...
     *     diffuseMap.success(function () {
     *         // Wait for the diffuse texture loaded
     *         animation.on('frame', function (frameTime) {
     *             renderer.render(scene, camera);
     *         });
     *     });
     */
    var Texture2D = Texture.extend(function () {
        return /** @lends clay.Texture2D# */ {
            /**
             * @type {?HTMLImageElement|HTMLCanvasElemnet}
             */
            image: null,
            /**
             * Pixels data. Will be ignored if image is set.
             * @type {?Uint8Array|Float32Array}
             */
            pixels: null,
            /**
             * @type {Array.<Object>}
             * @example
             *     [{
             *         image: mipmap0,
             *         pixels: null
             *     }, {
             *         image: mipmap1,
             *         pixels: null
             *     }, ....]
             */
            mipmaps: []
        };
    }, {

        textureType: 'texture2D',

        update: function (renderer) {

            var _gl = renderer.gl;
            _gl.bindTexture(_gl.TEXTURE_2D, this._cache.get('webgl_texture'));

            this.updateCommon(renderer);

            var glFormat = this.format;
            var glType = this.type;

            _gl.texParameteri(_gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, this.getAvailableWrapS());
            _gl.texParameteri(_gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, this.getAvailableWrapT());

            _gl.texParameteri(_gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, this.getAvailableMagFilter());
            _gl.texParameteri(_gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, this.getAvailableMinFilter());

            var anisotropicExt = renderer.getGLExtension('EXT_texture_filter_anisotropic');
            if (anisotropicExt && this.anisotropic > 1) {
                _gl.texParameterf(_gl.TEXTURE_2D, anisotropicExt.TEXTURE_MAX_ANISOTROPY_EXT, this.anisotropic);
            }

            // Fallback to float type if browser don't have half float extension
            if (glType === 36193) {
                var halfFloatExt = renderer.getGLExtension('OES_texture_half_float');
                Iif (!halfFloatExt) {
                    glType = glenum.FLOAT;
                }
            }

            Iif (this.mipmaps.length) {
                var width = this.width;
                var height = this.height;
                for (var i = 0; i < this.mipmaps.length; i++) {
                    var mipmap = this.mipmaps[i];
                    this._updateTextureData(_gl, mipmap, i, width, height, glFormat, glType);
                    width /= 2;
                    height /= 2;
                }
            }
            else {
                this._updateTextureData(_gl, this, 0, this.width, this.height, glFormat, glType);

                if (this.useMipmap && !this.NPOT) {
                    _gl.generateMipmap(_gl.TEXTURE_2D);
                }
            }

            _gl.bindTexture(_gl.TEXTURE_2D, null);
        },

        _updateTextureData: function (_gl, data, level, width, height, glFormat, glType) {
            if (data.image) {
                _gl.texImage2D(_gl.TEXTURE_2D, level, glFormat, glFormat, glType, data.image);
            }
            else {
                // Can be used as a blank texture when writing render to texture(RTT)
                Iif (
                    glFormat <= Texture.COMPRESSED_RGBA_S3TC_DXT5_EXT
                    && glFormat >= Texture.COMPRESSED_RGB_S3TC_DXT1_EXT
                ) {
                    _gl.compressedTexImage2D(_gl.TEXTURE_2D, level, glFormat, width, height, 0, data.pixels);
                }
                else {
                    // Is a render target if pixels is null
                    _gl.texImage2D(_gl.TEXTURE_2D, level, glFormat, width, height, 0, glFormat, glType, data.pixels);
                }
            }
        },

        /**
         * @param  {clay.Renderer} renderer
         * @memberOf clay.Texture2D.prototype
         */
        generateMipmap: function (renderer) {
            var _gl = renderer.gl;
            if (this.useMipmap && !this.NPOT) {
                _gl.bindTexture(_gl.TEXTURE_2D, this._cache.get('webgl_texture'));
                _gl.generateMipmap(_gl.TEXTURE_2D);
            }
        },

        isPowerOfTwo: function () {
            var width;
            var height;
            if (this.image) {
                width = this.image.width;
                height = this.image.height;
            }
            else {
                width = this.width;
                height = this.height;
            }
            return isPowerOfTwo(width) && isPowerOfTwo(height);
        },

        isRenderable: function () {
            if (this.image) {
                return this.image.nodeName === 'CANVAS'
                    || this.image.nodeName === 'VIDEO'
                    || this.image.complete;
            }
            else {
                return !!(this.width && this.height);
            }
        },

        bind: function (renderer) {
            renderer.gl.bindTexture(renderer.gl.TEXTURE_2D, this.getWebGLTexture(renderer));
        },

        unbind: function (renderer) {
            renderer.gl.bindTexture(renderer.gl.TEXTURE_2D, null);
        },

        load: function (src, crossOrigin) {
            var image = new Image();
            Iif (crossOrigin) {
                image.crossOrigin = crossOrigin;
            }
            var self = this;
            image.onload = function () {
                self.dirty();
                self.trigger('success', self);
                image.onload = null;
            };
            image.onerror = function () {
                self.trigger('error', self);
                image.onerror = null;
            };

            image.src = src;
            this.image = image;

            return this;
        }
    });

    Object.defineProperty(Texture2D.prototype, 'width', {
        get: function () {
            if (this.image) {
                return this.image.width;
            }
            return this._width;
        },
        set: function (value) {
            Iif (this.image) {
                console.warn('Texture from image can\'t set width');
            }
            else {
                if (this._width !== value) {
                    this.dirty();
                }
                this._width = value;
            }
        }
    });
    Object.defineProperty(Texture2D.prototype, 'height', {
        get: function () {
            if (this.image) {
                return this.image.height;
            }
            return this._height;
        },
        set: function (value) {
            Iif (this.image) {
                console.warn('Texture from image can\'t set height');
            }
            else {
                Eif (this._height !== value) {
                    this.dirty();
                }
                this._height = value;
            }
        }
    });

    export default Texture2D;