Three.js: Allows loadTexture to load encoded data

Created on 3 May 2013  路  6Comments  路  Source: mrdoob/three.js

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;

by

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 ?

Suggestion

Most helpful comment

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';

All 6 comments

What about...

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

Or...

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

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';

I should have been more precise: I'm using the SceneLoader class, so I don't have the possibility to write my own loadTexture => SceneLoader is calling the THREE.ImageUtils.LoadTexture function internally to load textures.

An alternative to this would be to let SceneLoader take a function pointer to be used when loading textures, that would default to THREE.ImageUtils.LoadTexture.

https://github.com/mrdoob/three.js/issues/3412

I suggest the same solution to some problems I have this issue.

Maybe this issue #3430 should be closed as #3442 encompasses it ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clawconduce picture clawconduce  路  3Comments

seep picture seep  路  3Comments

ghost picture ghost  路  3Comments

alexprut picture alexprut  路  3Comments

boyravikumar picture boyravikumar  路  3Comments