Skip to content

Instantly share code, notes, and snippets.

@ngokevin
Created February 28, 2019 20:37
Show Gist options
  • Save ngokevin/967ac4eec6f1ed72f5e79169811e90b3 to your computer and use it in GitHub Desktop.
Save ngokevin/967ac4eec6f1ed72f5e79169811e90b3 to your computer and use it in GitHub Desktop.
AFRAME.registerSystem('render-order', {
schema: {type: 'array'},
init: function () {
this.el.renderer.sortObjects = true;
},
update: function () {
this.order = {};
for (i = 0; i < this.data.length; i++) {
this.order[this.data[i]] = i;
}
}
});
AFRAME.registerComponent('render-order', {
schema: {type: 'string'},
init: function () {
this.set = this.set.bind(this);
this.el.addEventListener('object3dset', evt => {
evt.detail.object.traverse(this.set);
});
},
update: function () {
this.el.object3D.traverse(this.set);
},
set: function (node) {
// String (named order).
if (isNaN(this.data)) {
node.renderOrder = this.system.order[this.data];
} else {
node.renderOrder = parseFloat(this.data);
}
}
});
AFRAME.registerComponent('render-order-recursive', {
init: function () {
this.el.addEventListener('child-attached', evt => {
evt.detail.el.setAttribute('render-order', this.el.getAttribute('render-order'));
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment