1
0
mirror of https://github.com/troisjs/trois.git synced 2024-11-24 04:12:02 +08:00

update sprite

This commit is contained in:
Kevin Levron 2020-09-19 16:41:26 +02:00
parent 17e334444d
commit f5b1bd1934

View File

@ -7,19 +7,51 @@ export default {
props: { props: {
src: String, src: String,
position: Object, position: Object,
scale: Object,
}, },
mounted() { mounted() {
this.texture = new TextureLoader().load(this.src, () => { this.$emit('loaded'); }); this.texture = new TextureLoader().load(this.src, this.onLoaded);
this.material = new SpriteMaterial({ map: this.texture }); this.material = new SpriteMaterial({ map: this.texture });
this.sprite = new Sprite(this.material); this.sprite = new Sprite(this.material);
this.geometry = this.sprite.geometry;
useBindProp(this, 'position', this.sprite.position); useBindProp(this, 'position', this.sprite.position);
useBindProp(this, 'scale', this.sprite.scale);
this.scene.add(this.sprite); this.scene.add(this.sprite);
this.$emit('ready'); this.$emit('ready');
}, },
unmounted() { unmounted() {
this.texture.dispose(); this.texture.dispose();
this.material.dispose();
},
methods: {
onLoaded() {
this.updateUV();
this.$emit('loaded');
},
updateUV() {
// console.log(this.texture);
this.iWidth = this.texture.image.width;
this.iHeight = this.texture.image.height;
this.iRatio = this.iWidth / this.iHeight;
let x = 0.5, y = 0.5;
if (this.iRatio > 1) {
y = 0.5 / this.iRatio;
} else {
x = 0.5 / this.iRatio;
}
const positions = this.geometry.attributes.position.array;
positions[0] = -x; positions[1] = -y;
positions[5] = x; positions[6] = -y;
positions[10] = x; positions[11] = y;
positions[15] = -x; positions[16] = y;
this.geometry.attributes.position.needsUpdate = true;
},
}, },
render() { render() {
return []; return [];
}, },
__hmrId: 'Sprite',
}; };