tizzle / aframe-sprite-component

A Sprite component for A-Frame VR
MIT License
16 stars 6 forks source link

ideas for efficient image loading #5

Open micuat opened 4 years ago

micuat commented 4 years ago

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

tizzle commented 4 years ago

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!

micuat commented 4 years ago

@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