tizzle/aframe-sprite-component

ideas for efficient image loading

Opened this issue · 2 comments

This is not an issue but rather a feature request - currently the component loads the texture every time when <a-sprite> is processed, right? I made a workaround to reuse same textures:

var textureSingleton = {};

AFRAME.registerComponent('sprite', {

  schema: {
    src: {
      default: ''
    },
    resize: {
      default: '1 1 1'
    }
  },


  init: function () {
    this.textureLoader = new THREE.TextureLoader();
  },


  play: function () {
    if (textureSingleton[this.data.src] == undefined) {
      textureSingleton[this.data.src] = this.textureLoader.load(this.data.src);
    }
    this.map = textureSingleton[this.data.src];

    this.material = new THREE.SpriteMaterial({
      map: this.map
    });

    this.sprite = new THREE.Sprite(this.material);

    resizeData = this.data.resize.split(' ');
    this.sprite.scale.set(resizeData[0], resizeData[1], resizeData[2]);

    this.el.setObject3D('mesh', this.sprite);
  },


  remove: function () {
    console.log('remove sprite');
    if (this.mesh) this.el.removeObject3D('mesh');
  }

});

AFRAME.registerPrimitive('a-sprite', {
  defaultComponents: {
    sprite: {}
  },
  mappings: {
    src: 'sprite.src',
    resize: 'sprite.resize'
  }
});

And perhaps a better way is to use <a-assets><img> as a source. A friend of mine showed me an example to load <img> into three.js texture (this uses data URL but you can think of reading from static <img> - this example might suppress multiple http requests but still creates multiple textures, I guess) :
https://codepen.io/fand/pen/LYpJMqW

Hey @micuat,

It's great to see you are using this package. I moved away from doing VR devlopment quite some time ago so this repository is quite neglected, i fear.

Would you probably like to collaborate on this project? It's not big, not much code at all, and i would really like if somebody who is actually using it is involved. I'd basically give you free decision on where to go with this, but also would support if it's helpful.

What do you think?

Cheers!

@tizzle sure! I don't know when I leave VR like you, but for now I can give some input to the repo. I think it's a good idea rather than making a fork