Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ideas for efficient image loading #5

Open
micuat opened this issue May 15, 2020 · 2 comments
Open

ideas for efficient image loading #5

micuat opened this issue May 15, 2020 · 2 comments
Assignees

Comments

@micuat
Copy link
Collaborator

micuat commented May 15, 2020

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
Copy link
Owner

tizzle commented May 15, 2020

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
Copy link
Collaborator Author

micuat commented May 15, 2020

@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

@micuat micuat self-assigned this May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants