Allows loadTexture to load encoded data

Currently loadTexture loads data from an url. It could be nice to let it loads data from base64 encoded data (something like data:image/png;base64,XXXXX for eg).

It nearly works with the current implementation, only that we get a Cross-origin image load denied by Cross-Origin Resource Sharing policy. error (in Chrome) because of this: loader.crossOrigin = this.crossOrigin;.

Also, the url is put in texture.sourceFile, something that we would avoid to do when the url is really raw data.

So, in the current implementation, I would replace:

loader.crossOrigin = this.crossOrigin;
loader.load( url, image );
texture.sourceFile = url;


var isRawData = url.substr(0, 5) == 'data:';
if (!isRawData) {
    loader.crossOrigin = this.crossOrigin;
    texture.sourceFile = url;
} else {
    texture.sourceFile = '';
loader.load( url, image );

What do you think ?

Author: Fantashit

1 thought on “Allows loadTexture to load encoded data

  1. And just to be safe…

    var image = document.createElement( 'img' );
    var texture = new THREE.Texture( image );
    image.onload = function()  {
        texture.needsUpdate = true;
    image.src = 'data:image/png;base64,XXXXX';

Comments are closed.